Nginx

Nginx 常用版本

安装 Nginx

CentOS

1
2
3
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
./configure --prefix=/usr/local/nginx/v1.24.0 --with-http_stub_status_module --with-http_ssl_module
make && make install

Debian

1
sudo apt install -y libpcre3 libpcre3-dev libssl-dev libxml2-dev libxslt1-dev libgd-dev libextutils-embed-perl libgoogle-perftools-dev

解压

1
2
3
4
5
6
7
8
[root@nginx01 ~]# ll
total 1056
-rw-------. 1 root root 1417 Mar 31 19:35 anaconda-ks.cfg
-rw-r--r--. 1 root root 1073364 Mar 31 20:13 nginx-1.24.0.tar.gz
[root@nginx01 ~]# tar zxvf nginx-1.24.0.tar.gz
nginx-1.24.0/
nginx-1.24.0/auto/
...

configure

1
[root@nginx01 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx/v1.24.0

make

1
[root@nginx01 nginx-1.24.0]# make

make install

1
[root@nginx01 nginx-1.24.0]# make install

Nginx 命令

1
2
3
4
5
6
7
8
9
10
11
# 启动 Nginx
[root@nginx01 sbin]# ./nginx

# 快速停止
[root@nginx01 sbin]# ./nginx -s stop

# 优雅关闭,在推出前完成已经接受的连接请求
[root@nginx01 sbin]# ./nginx -s quit

# 重新加载配置
[root@nginx01 sbin]# ./nginx -s reload

关闭防火墙

1
2
3
4
[root@nginx01 sbin]# systemctl stop firewalld.service
[root@nginx01 sbin]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

将 Nginx 安装成系统服务

先停止 nginx 服务

1
[root@nginx01 sbin]# ./nginx -s stop

创建服务脚本 nginx.service 文件

1
[root@nginx01 v1.24.0]# vi /usr/lib/systemd/system/nginx.service

脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=Nginx_v1.24.0
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/v1.24.0/logs/nginx.pid
ExecStartPre=/usr/local/nginx/v1.24.0/sbin/nginx -t -c /usr/local/nginx/v1.24.0/conf/nginx.conf
ExecStart=/usr/local/nginx/v1.24.0/sbin/nginx -c /usr/local/nginx/v1.24.0/conf/nginx.conf
ExecReload=/usr/local/nginx/v1.24.0/sbin/nginx -s reload
ExecStop=/usr/local/nginx/v1.24.0/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/v1.24.0/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新加载系统服务

1
[root@nginx01 v1.24.0]# systemctl daemon-reload

启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@nginx01 sbin]# systemctl start nginx
[root@nginx01 sbin]# systemctl status nginx
● nginx.service - nginx - web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; static; vendor preset: disabled)
Active: active (running) since Thu 2022-03-31 21:44:28 CST; 21s ago
Process: 12084 ExecStart=/usr/local/nginx/v1.24.0/sbin/nginx -c /usr/local/nginx/v1.24.0/conf/nginx.conf (code=exited, status=0/SUCCESS)
Process: 12081 ExecStartPre=/usr/local/nginx/v1.24.0/sbin/nginx -t -c /usr/local/nginx/v1.24.0/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 12085 (nginx)
CGroup: /system.slice/nginx.service
├─12085 nginx: master process /usr/local/nginx/v1.24.0/sbin/nginx -c /usr/local/nginx/v1.24.0/conf/nginx.conf
└─12086 nginx: worker process

Mar 31 21:44:28 nginx01.centos.gg.com systemd[1]: Starting nginx - web server...
Mar 31 21:44:28 nginx01.centos.gg.com nginx[12081]: nginx: the configuration file /usr/local/nginx/v1.24.0/conf/nginx.conf syntax is ok
Mar 31 21:44:28 nginx01.centos.gg.com nginx[12081]: nginx: configuration file /usr/local/nginx/v1.24.0/conf/nginx.conf test is successful
Mar 31 21:44:28 nginx01.centos.gg.com systemd[1]: Started nginx - web server.

设置为开机启动

1
2
[root@nginx01 ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

配置 SSL

Nginx 目录结构

刚安装完成时

1
2
3
4
5
6
[root@nginx01 v1.24.0]# ll
total 4
drwxr-xr-x. 2 root root 4096 Mar 31 21:03 conf
drwxr-xr-x. 2 root root 40 Mar 31 21:03 html
drwxr-xr-x. 2 root root 6 Mar 31 21:03 logs
drwxr-xr-x. 2 root root 19 Mar 31 21:03 sbin

服务启动后

1
2
3
4
5
6
7
8
9
10
11
[root@nginx01 v1.24.0]# ll
total 4
drwx------. 2 nobody root 6 Mar 31 21:10 client_body_temp
drwxr-xr-x. 2 root root 4096 Mar 31 21:03 conf
drwx------. 2 nobody root 6 Mar 31 21:10 fastcgi_temp
drwxr-xr-x. 2 root root 40 Mar 31 21:03 html
drwxr-xr-x. 2 root root 58 Mar 31 21:19 logs
drwx------. 2 nobody root 6 Mar 31 21:10 proxy_temp
drwxr-xr-x. 2 root root 19 Mar 31 21:03 sbin
drwx------. 2 nobody root 6 Mar 31 21:10 scgi_temp
drwx------. 2 nobody root 6 Mar 31 21:10 uwsgi_temp