http://nginx.org/en/download.html,選擇適合Linux的版本,這裏選擇最新的版本,下載到本地後上傳到服務器或者centos下直接wget命令下載。html
切換到/usr/local目錄,下載軟件包nginx
1
2
|
# cd /usr/local
# wget http://nginx.org/download/nginx-1.11.5.tar.gz
|
先執行如下命令,安裝nginx依賴庫,若是缺乏依賴庫,可能會安裝失敗,具體能夠參考文章後面的錯誤提示信息。c++
1
2
3
4
5
6
7
|
# 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
|
解壓安裝包vim
1
|
# 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/目錄centos
1
|
# cd /usr/local/nginx-1.11.5/
|
執行# ./configurebash
1
|
# ./configure
|
該操做會檢測當前系統環境,以確保能成功安裝nginx,執行該操做後可能會出現如下幾種提示:服務器
checking for OSui
+ Linux 3.10.0-123.el7.x86_64 x86_64spa
checking for C compiler ... not foundrest
./configure: error: C compiler cc is not found
若是出現以上錯誤提示信息,執行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 system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> 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 install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
若是出現以上提示,表示缺乏zlib庫
若是沒有出現./configure: error提示,表示當前環境能夠安裝nginx,執行make和make install編譯nginx
1
2
|
# 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.
切換到/lib/systemd/system/目錄,建立nginx.service文件vim nginx.service
1
2
|
# cd /lib/systemd/system/
# vim nginx.service
|
文件內容以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[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開機啓動
1
|
# systemctl enable nginx.service
|
systemctl start nginx.service 啓動nginx
systemctl stop nginx.service 結束nginx
systemctl restart nginx.service 重啓nginx
輸入http://服務器IP/ 若是能看到nginx的界面,就表示安裝成功了