ubuntu18-nginx-二进制安装

1、依赖安装

复制代码
sudo apt install gcc make libpcre3-dev zlib1g-dev openssl libssl-dev

2、创建用户及用户组

复制代码
groupadd www
useradd -g www -s /sbin/nologin -M www

3、创建文件夹

复制代码
mkdir -p /data/{wwwroot,nginx,file}
mkdir -p /data/wwwroot/error

4、上传文件

复制代码
nginx-1.21.4.tar.gz #上传到/data/file目录下面,同样也可以采用在线下载 wget http://nginx.org/download/nginx-1.21.4.tar.gz
index.html #上传到/data/wwwroot目录下面
401.html 403.html、404.html、50x.html #上传到/data/wwwroot/error

5、解压并进入编译安装

复制代码
tar -zxf nginx-1.21.4.tar.gz #解压
cd nginx-1.21.4/ #进入
./configure --prefix=/usr/local/nginx #不带参的编译
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #带参的编译
make && make install #编译安装及安装

6、创建vhost目录

复制代码
mkdir -p /usr/local/nginx/conf/vhost

7、修改nginx配置文件

复制代码
vi /usr/local/nginx/conf/nginx.conf
user www;
worker_processes 4;
worker_cpu_affinity auto;
error_log /data/nginx/error.log;
worker_rlimit_nofile 65535;
pid  /data/nginx/nginx.pid;
events {
        worker_connections  10240;
        use epoll;
        multi_accept on;
}
http {
        include       mime.types;
        default_type  application/octet-stream;
        map $time_iso8601 $logdate {
                '~^(?<ymd>\\d{4}-\\d{2}-\\d{2})' $ymd;
                default                       'date-not-found';
        }
        map $time_iso8601 $request_times {
                '~^(?<ymdhms>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})' $ymdhms;
                default                       'date-not-found';
        }
        log_format access-upstream '{"host":"$server_addr",'
                '"clientip":"$remote_addr",'
                '"size":$body_bytes_sent,'
                '"responsetime":$request_times,'
                '"http_host":"$host",'
                '"url":"$uri",'
                '"agent":"$http_user_agent",'
                '"status":"$status"}';
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        server_names_hash_bucket_size 128;
        server_names_hash_max_size 512;
        keepalive_timeout  1000s;
        client_header_timeout 15s;
        client_body_timeout 15s;
        send_timeout 1000s;
        proxy_cache_path /data/nginx levels=1:2 keys_zone=nginx-cache:20m max_size=50g inactive=168h;
        client_body_buffer_size 512k;
        client_header_buffer_size 256k;
        client_max_body_size 1024m;
        large_client_header_buffers 2 8k;
        proxy_connect_timeout 1000s;
        proxy_send_timeout 1000s;
        proxy_read_timeout 1000s;
        proxy_buffer_size 128k;
        proxy_buffers 8 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;
        fastcgi_intercept_errors on;
        fastcgi_ignore_client_abort on;
        fastcgi_connect_timeout 1000s;
        fastcgi_send_timeout 1000s;
        fastcgi_read_timeout 1000s;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 8 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 32k;
        gzip_http_version 1.1;
        gzip_comp_level 6;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss                                   text/javascript;
        gzip_vary on;
        gzip_disable "MSIE [1-6].";
        include vhost/*.conf;
}

8、创建nginx默认配置

复制代码
vi /usr/local/nginx/conf/vhost/localhost_80.conf
server {
        listen        80 default_server;
        server_name _;
        error_log /data/nginx/localhost_error.log crit;
        access_log /data/nginx/localhost_acess_$logdate.log access-upstream;
        error_page   401 /401.html;
        error_page   403 /403.html;
        error_page   404 /404.html;
        error_page      500 502 503 504 /50x.html;
        location / {
                return 401;
        }
        location = /401.html {
                root "/data/wwwroot/error";
        }
        location = /403.html {
                root "/data/wwwroot/error";
        }
        location = /404.html {
                root "/data/wwwroot/error";
        }
        location = /50x.html {
                root "/data/wwwroot/error";
        }
}

9、授权

复制代码
chown -R www:www /data/wwwroot
chown -R www:www /data/nginx
chown -R www:www /usr/local/nginx

10、启用nginx

复制代码
/usr/local/nginx/sbin/nginx

11、设置开机启动

复制代码
vi /etc/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target 
[Service] 	
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true 
[Install] 
WantedBy=multi-user.target

12、加入/移除开机启动

复制代码
systemctl enable nginx.service #加入
systemctl disable nginx.service #移除

13、开发防火墙端口

复制代码
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

14、常用/快捷命令总结

复制代码
systemctl start nginx.service #启动命令
systemctl status nginx.service #状态命令
systemctl reload nginx.service #重新加载命令
systemctl stop nginx.service #停止命令
相关推荐
天才奇男子6 小时前
《深度解析HAProxy七层代理:原理、配置与最佳实践》
linux·运维·微服务·云原生
学嵌入式的小杨同学6 小时前
【Linux 封神之路】文件操作 + 时间编程实战:从缓冲区到时间格式化全解析
linux·c语言·开发语言·前端·数据库·算法·ux
wifi chicken6 小时前
Linux wlan 之sniffer log 解密详解
linux·wlan·sniffer log·空口包·空口解密
济6176 小时前
ARM Linux 驱动开发篇----字符设备驱动开发(1)--字符设备驱动简介---- Ubuntu20.04
linux·嵌入式硬件
浪客灿心6 小时前
Linux的Ext系列文件系统
linux·运维·服务器·c语言
Dontla6 小时前
Kubernetes流量管理双雄:Ingress与Gateway API解析(Nginx与Ingress与Gateway API的关系)
nginx·kubernetes·gateway
速易达网络6 小时前
linux命令大全
linux·运维·excel
落笔映浮华丶6 小时前
linux项目自动构建工具 -make/makefile
linux·运维·服务器
爱装代码的小瓶子7 小时前
【C++与Linux基础】文件篇(3)-fd的本质和minishell的重定向功能
linux·c++
s_daqing7 小时前
arm的ubuntu启动node
linux·arm开发·ubuntu