官方源码包下载地址:https://nginx.org/en/download.html
[root@centos8-1 ~]$yum -y install gcc pcre-devel openssl-devel zlib-devel make ##下载编译时需要用到的包
[root@centos8-1 ~]$useradd nginx -s /sbin/nologin ##添加nginx用户,后面编译会用到
[root@centos8-1 ~]$cd /usr/local/src/
[root@centos8-1 src]$ls
nginx-1.18.0.tar.gz
[root@centos8-1 src]$tar xf nginx-1.18.0.tar.gz ##解压nginx源码包
[root@centos8-1 nginx-1.18.0]$
./configure
--prefix=/apps/nginx
--user=nginx --group=nginx
--with-http_ssl_module
--with-http_realip_module
--with-http_v2_module
--with-http_stub_status_module
--with-http_gzip_static_module
--with-pcre
--with-stream
--with-stream_ssl_module
--with-stream_realip_module
[root@centos8-1 nginx-1.18.0]$make && make install ##编译并安装
[root@centos8-1 nginx-1.18.0]$chown -R nginx.nginx /apps/nginx ##修改nginx安装路径权限
[root@centos8-1 ~]$ln -s /apps/nginx/sbin/nginx /usr/sbin/ ##创建软连接添加到sbin环境环境里
[root@centos8-1 ~]$nginx -v ##查看ngixn版本
nginx version: nginx/1.18.0
[root@centos8-1 ~]$nginx -V ##-V选项查看编译时指定的参数
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1c FIPS 28 May 2019
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos8-1 ~]$nginx ##启动nginx
[root@centos8-1 ~]$ss -ntlop ##查看端口号,看到nginx的80已经启动
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=31950,fd=9),("nginx",pid=31949,fd=9))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1041,fd=4))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1041,fd=6))
##打开浏览器,页面打开
##复制同一版本的nginx的yum安装生成的service文件
##Nginx官方的yum源 参考配置:http://nginx.org/en/linux_packages.html#RHEL-CentOS
例:
[root@centos8-2 ~]$yum info nginx ##查看nginx版本信息
Last metadata expiration check: 1:57:27 ago on Fri 25 Sep 2020 10:08:11 AM CST.
Installed Packages
Name : nginx
Epoch : 1
Version : 1.18.0
Release : 1.el8.ngx
Architecture : x86_64
Size : 3.6 M
Source : nginx-1.18.0-1.el8.ngx.src.rpm
Repository : @System
From repo : nginx-stable
Summary : High performance web server
URL : http://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
[root@centos8-2 ~]$yum -y install nginx ##yum安装nginx
[root@centos8-2 ~]$nginx -v ##安装完成,查看nginx版本与我们编译的nginx版本一致
nginx version: nginx/1.18.0
[root@centos8-2 ~]$scp /usr/lib/systemd/system/nginx.service 10.0.0.8:/usr/lib/systemd/system/nginx.service ##把service文件copy到编译安装的机器上
[root@centos8-1 ~]$vim /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=/apps/nginx/run/nginx.pid ##修改pid文件路径
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf ##修改主配置文件路径
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
[root@centos8-1 ~]$mkdir /apps/nginx/run ##创建目录
[root@centos8-1 ~]$vim /apps/nginx/conf/nginx.conf ##修改配置文件
pid /apps/nginx/run/nginx.pid; ##修改这一行
验证Ningx自启动文件
[root@centos8-1 ~]$systemctl daemon-reload
[root@centos8-1 ~]$systemctl enable --now nginx ##启动并设置开机自启
注:Nginx官方帮助文档 http://nginx.org/en/docs