1. 安装源码编译的工具:
yum install -y gcc gcc-c++ make
2. 安装 Nginx 所需的 pere 库:
yum install -y pcre-devel
3. 安装openssl-devel:
yum install -y openssl-devel
4. 下载nginx源码包并解压:
wget -c https://repo.huaweicloud.com/nginx/nginx-1.20.0.tar.gz
tar xf nginx-1.20.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.20.0/
5. 创建nginx用户和组:
useradd -r -s /sbin/nologin -M nginx
6. 安装perl-ExtUtils-Embed:
yum install perl-ExtUtils-Embed -y
7. 执行configure文件
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
--with-threads --with-http_ssl_module --with-http_sub_module
--with-http_gzip_static_module --with-http_auth_request_module
--with-http_stub_status_module --with-http_perl_module --with-stream --with-pcre
8. 编译并安装nginx:
make && make install
9. 查看nginx的目录结构:
tree /usr/local/nginx/
/usr/local/nginx/
├── client_body_temp
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ ├── index.html
├── logs
├── sbin
└── nginx
10. 为nginx提供启动服务的脚本:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
11. 启动nginx服务:
systemctl start nginx.service