nginx的作用
在 CentOS 上安装
其他版本可见:http://nginx.org/en/linux_packages.html
# yum 方式
# 安装
安装必备软件包
sudo yum install yum-utils
1
增加 nginx 的 yum 仓库
vim /etc/yum.repos.d/nginx.repo
-----------------------
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
如果想切换为 mainline 版本的可以使用
sudo yum-config-manager --enable nginx-mainline
1
然后就是一键安装了
sudo yum -y install nginx
1
# 使用
systemctl cat nginx
----------------
# /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
可以看到一些默认配置
- 配置文件默认在
/etc/nginx/nginx.conf
- 可执行文件在
/usr/sbin/nginx
当然后续使用也可以直接命令,已经加入了 PATH
nginx -h
--------------
nginx version: nginx/1.22.0
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 编译安装
# 下载源码包
- 在 nginx 官网 (opens new window) 下载源码包
- 在 pcre2 github (opens new window)下载用于正则匹配的 pcre 包
- 在 zlib 官网 (opens new window) 下载用于 gzip 压缩的 zlib 包
wget http://nginx.org/download/nginx-1.21.6.tar.gz
# pcre2 用于正则
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.tar.gz
# zlib 用于开启 gzip
wget http://zlib.net/zlib-1.2.12.tar.gz
1
2
3
4
5
6
7
2
3
4
5
6
7
# 解压源码包
tar -zxvf nginx-1.21.6.tar.gz
tar -zxvf pcre2-10.40.tar.gz
# 查看目录结构
ll
-------------
drwxr-xr-x. 8 1001 1001 158 Jan 25 15:04 nginx-1.21.6
drwxr-xr-x. 7 1169 1169 4.0K Apr 15 15:51 pcre2-10.40
drwxr-xr-x. 14 501 games 4.0K Mar 27 23:39 zlib-1.2.12
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 配置
- --prefix 最终的安装位置,默认
/usr/local/nginx
- --sbin-path 可执行文件位置
- --conf-path 默认配置文件位置
- --pid-path pid 文件位置
- --with-http_ssl_module 开启 SSL
- --with-pcre 正则包 pcre 位置,可选
- --with-zlib 压缩工具 zlib 包位置,可选
cd nginx-1.21.6
# 开始配置
./configure \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=../pcre2-10.40 \
--with-zlib=../zlib-1.2.12
-------------------------------------------
Configuration summary
+ using PCRE2 library: ../pcre2-10.40
+ using system OpenSSL library
+ using zlib library: ../zlib-1.2.12
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx"
nginx configuration file: "/usr/local/nginx/nginx.conf"
nginx pid file: "/usr/local/nginx/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 安装
make
# 不会自动添加 PATH
/usr/local/nginx/nginx -h
# 创建链接方便直接 nginx 调用
ln -s /usr/local/nginx/nginx /usr/sbin/nginx
1
2
3
4
5
6
7
2
3
4
5
6
7
# 配置服务
# 开启守护进程
vim /usr/local/nginx/nginx.conf
-------
# 增加
daemon on;
pid /var/run/nginx.pid;
1
2
3
4
5
2
3
4
5
# 配置服务
vim /etc/systemd/system/nginx.service
---------------------
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
然后直接可以通过下面命令启动了
systemctl start nginx
1
开机自启
systemctl enable nginx
1
在 GitHub 编辑此页 (opens new window)
上次更新: 2024/02/25, 12:11:11