1.場景描述
項目要部署到新的服務器上,需要安裝nginx,剛好安全部門通知了nginx存在安全漏洞(Nginx整數溢出漏洞,nginx1.13.2之後的版本無問題),就下載最新的nginx進行了安裝,介紹下在線與離線安裝nginx吧,其他關於nginx兩篇博客:nginx實戰操作(常用命令及配置)與一臺服務器通過nginx配置多個域名(80端口)
2. 問題解決
其實所謂的在線安裝與離線安裝,主要是安裝nginx的依賴包,因爲nginx是c語言編寫的,需要安裝些特殊的依賴,常用的cenos中一般都沒帶。
2.1 在線安裝,使用yum安裝
yum install gcc-c++ (選擇y回車)
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
2.2 離線安裝,下載對應包
2.2.1 下載
wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz 或者https://search.grilime.com/?14&n=101&q=pcre-8.40.tar.gz
2.2.2 解壓及安裝
tar zxvf openssl-fips-2.0.10.tar.gz
cd openssl-fips-2.0.10
./config && make && make install
tar zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure && make && make install
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure && make && make install
2.3 安裝nginx
2.3.1 下載最新nginx
http://nginx.org/en/download.html, 目前最新版本是:1.17.2
2.3.2 安裝nginx
(1)上傳tar包到:/usr/local
(2)安裝命令:
cd /usr/local
tar zxvf nginx-1.17.2.tar.gz
cd nginx-1.17.2/
./configure && make && make install
(3)驗證安裝是否成功
whereis nginx
1
2.3.3 啓動nginx
(1)啓動命令
/usr/local/nginx/sbin
./nginx
(2)驗證是否啓動成功
ps -ef|grep nginx
說明: 會有兩個進程,一個master進程,一個worker進程。
啓動服務,檢查運行情況:
[[email protected] nginx-1.21.6] cd /usr/local/nginx/sbin/
[[email protected] sbin] ll
total 4.5M
-rwx------ 1 root root 4.5M Mar 2 14:08 nginx
[[email protected] sbin] ./nginx
[[email protected] sbin]
[[email protected] sbin] ps -ef | grep nginx
root 5729 1 0 14:12 ? 00:00:00 nginx: master process ./nginx
nobody 5730 5729 0 14:12 ? 00:00:00 nginx: worker process
root 5746 2938 0 14:13 pts/0 00:00:00 grep --color=auto nginx
. 修改運行用戶nobody:
[[email protected] sbin] cd /usr/local/nginx/conf/
[[email protected] conf] ll
total 68K
-rw------- 1 root root 1.1K Mar 2 14:08 fastcgi.conf
-rw------- 1 root root 1.1K Mar 2 14:08 fastcgi.conf.default
-rw------- 1 root root 1007 Mar 2 14:08 fastcgi_params
-rw------- 1 root root 1007 Mar 2 14:08 fastcgi_params.default
-rw------- 1 root root 2.8K Mar 2 14:08 koi-utf
-rw------- 1 root root 2.2K Mar 2 14:08 koi-win
-rw------- 1 root root 5.3K Mar 2 14:08 mime.types
-rw------- 1 root root 5.3K Mar 2 14:08 mime.types.default
-rw------- 1 root root 2.6K Mar 2 14:08 nginx.conf
-rw------- 1 root root 2.6K Mar 2 14:08 nginx.conf.default
-rw------- 1 root root 636 Mar 2 14:08 scgi_params
-rw------- 1 root root 636 Mar 2 14:08 scgi_params.default
-rw------- 1 root root 664 Mar 2 14:08 uwsgi_params
-rw------- 1 root root 664 Mar 2 14:08 uwsgi_params.default
-rw------- 1 root root 3.6K Mar 2 14:08 win-utf
[[email protected] conf] vi nginx.conf
按【i】鍵進入輸入模式,將nobody改爲root,之後按Esc進入底欄模式,輸入 :wq 保存修改並退出vi編輯器,配置修改完成。此時可以通過重新啓動nginx服務達到對外訪問的效果
參考:https://www.cnblogs.com/zhangruifeng/p/16335780.html
、啓動Nginx(直接用默認配置啓動測試即可)
cd /usr/local/nginx/sbin
./nginx
3.2、開放端口
開放nginx默認使用的80端口,並重啓防火牆
# 開放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 立即生效
firewall-cmd --reload
設置nginx開機自啓
在/etc/systemd/system
目錄創建一個啓動腳本
vi /etc/systemd/system/nginx.service
輸入以下內容:
[Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
然後執行
# 查找nginx端口
ps -ef|grep nginx
# 先停止nginx
kill -9 xxx
# 必須先執行這個
systemctl daemon-reload
# 開機自啓
systemctl enable nginx
# 啓動nginx
systemctl start nginx
#設置開機自啓動
systemctl enable nginx
#停止開機自啓動
systemctl disable nginx
#查看服務當前狀態
systemctl status nginx
#重新啓動服務
systemctl restart nginx
#查看所有已啓動的服務
systemctl list-units --type=service