官方源碼包下載地址: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