nginx 源碼包下載地址:
http://nginx.org/en/download.html
1)下載依賴庫(kù):
[root@localhost ~]# yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
2) 創(chuàng)建nginx用戶
[root@localhost sbin]# useradd -s /sbin/nologin -M nginx #否則會(huì)出現(xiàn)問題1
3)解壓nginx:
[root@localhost ~]# mkdir /downloads
[root@localhost ~]# cd /downloads/
[root@localhost download]# tar -xzf nginx-1.16.1.tar.gz -C /usr/local/
4) 編譯安裝
[root@localhost local]# cd /usr/local/
[root@localhost local]# mkdir -p /usr/local/nginx /var/tmp/nginx/client/
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@localhost nginx-1.16.1]# make
[root@localhost nginx-1.16.1]# make install
5)啟動(dòng)nginx:
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -t #檢查配置文件語(yǔ)法問題
顯示結(jié)果如下為成功:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost sbin]# /usr/local/nginx/sbin/nginx #啟動(dòng)nginx,可能會(huì)出現(xiàn)問題2绎速,/var/run/nginx/nginx.pid找不到的問題
6) 添加環(huán)境變量
[root@localhost init.d]# vim /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost init.d]# source /etc/profile
7)查看端口80:
[root@localhost sbin]# ss -lntp
8)nginx的相關(guān)動(dòng)作:
nginx -s reload 重新載入配置文件
nginx -s reopen 重啟 Nginx
nginx -s stop 快速關(guān)閉 Nginx
nginx -s quit 關(guān)閉Nginx
擴(kuò)展1:若想用systemctl啟動(dòng)方式啟動(dòng)nginx服務(wù)痛侍,可以進(jìn)行如下修改:
[root@localhost sbin]# vim /usr/lib/systemd/system/nginx.service
添加如下內(nèi)容:
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
KillSignal=SIGQUIT
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost sbin]# systemctl daemon-reload #重新加載配置文件主届,便可進(jìn)行systemctl命令使用
問題1:
由于沒有nginx用戶待德,會(huì)出現(xiàn)如下問題:
解決方法:
[root@localhost sbin]# useradd -s /sbin/nologin -M nginx
問題2:
/var/run/nginx/nginx.pid文件打不開
解決方法:
[root@localhost nginx]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
將原來的#pid logs/nginx.pid; 去掉注釋
并改為:pid /usr/local/nginx/nginx.pid;
再次啟動(dòng)就可以了
實(shí)驗(yàn)結(jié)果顯示:
訪問ip可得: