CentOS7.6安裝Nginx並配置自動啟動
CentOS7.6安裝Nginx並配置自動啟動
1、官網下載安裝包
http://nginx.org/en/download.html,選擇適合Linux的版本,這裡選擇最新的版本,下載到本地後上傳到伺服器或者centos下直接wget命令下載。
[root@localhost /]# cd /usr/
[root@localhost /]# wgethttp://nginx.org/download/nginx-1.18.0.tar.gz
2、安裝nginx
先執行以下命令,安裝nginx依賴庫,如果缺少依賴庫,可能會安裝失敗,具體可以參考文章後面的錯誤提示資訊。
[root@localhost /]# yum -y install gcc-c++
[root@localhost /]# yum -y install pcre
[root@localhost /]# yum -y install pcre-devel
[root@localhost /]# yum -y install zlib
[root@localhost /]# yum -y install zlib-devel
[root@localhost /]# yum -y install openssl
[root@localhost /]# yum -y install openssl-devel
#也可以直接一條命令代替
[root@localhost /]# yum -y install gcc-c++ pcre-devel zlib zlib-devel openssl openssl-devel
3、解壓nginx安裝包,並安裝
[root@localhost usr]# tar -zxvf nginx-1.18.0.tar.gz
[root@localhost usr]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]./configure
#不報錯誤的話繼續下面命令
[root@localhost nginx-1.18.0]make
[root@localhost nginx-1.18.0]make install
#沒有出錯的話,表示nginx已經成功安裝完成,預設安裝位置為/usr/local/nginx,之前的/usr/nginx-1.18.0/可以刪除掉了。
如果出現cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file,可能是你把安裝包解壓到了/usr/local/nginx目錄,解決辦法是將該目錄重新命名為其他名稱後再執行make,make install.
4、配置nginx開機啟動
切換到/lib/systemd/system/目錄,建立nginx.service檔案vim nginx.service
[root@localhost /]# cd /lib/systemd/system/
[root@localhost system]# vim nginx.service
檔案內容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
退出並儲存檔案
[root@localhost system]# systemctl enable nginx.service
執行systemctl enable nginx.service使nginx開機啟動
systemctl start nginx.service 啟動nginx
systemctl stop nginx.service 結束nginx
systemctl restart nginx.service 重啟nginx
systemctl status nginx.service 檢視nginx狀態
輸入http://伺服器IP/ 如果能看到nginx的介面,就表示安裝成功了。記得先關防火牆哦。