首頁  >  文章  >  運維  >  CentOS7如何安裝Nginx並設定自動啟動

CentOS7如何安裝Nginx並設定自動啟動

WBOY
WBOY轉載
2023-05-14 15:01:143079瀏覽

1、官網下載安裝包

選擇適合linux的版本,這裡選擇最新的版本,下載到本地後上傳到伺服器或centos下直接wget指令下載。

CentOS7如何安裝Nginx並設定自動啟動

切換到/usr/local目錄,下載軟體包

# cd /usr/local
# wget http://nginx.org/download/nginx-1.11.5.tar.gz

2、安裝nginx

先執行以下指令,安裝nginx依賴函式庫,如果缺少依賴函式庫,可能會安裝失敗,具體可以參考文章後面的錯誤提示資訊。

# yum install gcc-c++
# yum install pcre
# yum install pcre-devel
# yum install zlib 
# yum install zlib-devel
# yum install openssl
# yum install openssl-devel

解壓縮安裝套件
##

# tar -zxvf nginx-1.11.5.tar.gz

nginx被解壓縮到了/usr/local/nginx-1.11.5 目錄下(不要把壓縮包解壓縮到/usr/local/nginx目錄下,或將解壓縮後的目錄重新命名為nginx,因為nginx會預設安裝到/usr/local/nginx目錄下),切換到nginx-1.11.5/目錄

# cd /usr/local/nginx-1.11.5/

執行# ./configure

# ./configure

此操作會偵測目前系統環境,以確保能成功安裝nginx,執行此操作後可能會出現下列幾種提示:


    checking for os

      linux 3.10.0-123.el7.x86_64 x86_64

    checking for c compiler ... not found

#cc#  notr.

##    如果出現以上錯誤提示信息,執行yum install gcc-c 安裝gcc,

    ./configure: error: the http rewrite module requires the pcre library.    you can either disable the module by using --without-http_rewrite_module

    option, or install the pcre library into the 是--with-pcre= option.

    如果出現上方提示,表示缺少pcre庫

    ./configure: error: the http gzip module requires the zlib library.

    you can either disable the module by using --without-http_gzip_module#    option, or bull the zlib library into the system,   option, or bull the zlib library into the system#or #    statically from the source with nginx by using --with-zlib= option.

    如果出現以上提示,表示缺少zlib庫

 如果沒有出現./configure: error提示,表示目前環境可以安裝nginx,執行make和make install編譯nginx

# make
# make install

沒有出錯的話,表示nginx已經成功安裝完成,預設安裝位置為/usr/local/nginx ,之前的/usr/local/nginx-1.11.5/可以刪除掉了。

如果出現cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file,可能是你把安裝套件解壓到了/usr /local/nginx目錄,解決方法是將目錄重新命名為其他名稱後再執行make,make install.

3、配置nginx開機啟動


#切換到/lib/systemd/system/目錄,建立nginx.service檔案vim nginx.service

# cd /lib/systemd/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

退出並儲存文件,執行systemctl enable nginx.service使nginx開機啟動

# systemctl enable nginx.service

systemctl start nginx.service    啟動nginx

systemctl stop nginx.service    結束nginx

systemcservice restart nginx.    結束nginx


systemcservice restart nginx.   

#4、驗證是否安裝成功

輸入http://伺服器ip/ 如果能看到nginx的介面,就表示安裝成功了

以上是CentOS7如何安裝Nginx並設定自動啟動的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除