1. 程式人生 > >centos環境安裝Nginx

centos環境安裝Nginx

轉自——點選跳轉

安裝所需環境

nginx 是 C語言 開發,建議在 Linux 上執行,當然,也可以安裝 Windows 版本,本篇則使用 Centos 7 作為安裝環境。

一. gcc 安裝
安裝 nginx 需要先將官網下載的原始碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

  1. yum install gcc-c++

二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 相容的正則表示式庫。nginx 的 http 模組使用 pcre 來解析正則表示式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

  1. yum install -y pcre pcre-devel

三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。

  1. yum install -y zlib zlib-devel

四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程式供測試或其它目的使用。
nginx 不僅支援 http 協議,還支援 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。

  1. yum install -y openssl openssl-devel

官網下載

1.直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html

nginx.png

2.使用wget命令下載(推薦)。

 
  1. wget -c https://nginx.org/download/nginx-1.10.1.tar.gz

nginx-wget.png

我下載的是1.10.1版本,這個是目前的穩定版。

解壓

依然是直接命令:

  1. tar -zxvf nginx-1.10.1.tar.gz
  2. cd nginx-1.10.1

配置

其實在 nginx-1.10.1 版本中你就不需要去配置相關東西,預設就可以了。當然,如果你要自己配置目錄也是可以的。
1.使用預設配置

  1. ./configure

2.自定義配置(不推薦)

  1. ./configure \
  2. --prefix=/usr/local/nginx \
  3. --conf-path=/usr/local/nginx/conf/nginx.conf \
  4. --pid-path=/usr/local/nginx/conf/nginx.pid \
  5. --lock-path=/var/lock/nginx.lock \
  6. --error-log-path=/var/log/nginx/error.log \
  7. --http-log-path=/var/log/nginx/access.log \
  8. --with-http_gzip_static_module \
  9. --http-client-body-temp-path=/var/temp/nginx/client \
  10. --http-proxy-temp-path=/var/temp/nginx/proxy \
  11. --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
  12. --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
  13. --http-scgi-temp-path=/var/temp/nginx/scgi

注:將臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立temp及nginx目錄

編譯安裝

  1. make
  2. make install

查詢安裝路徑:

  1. whereis nginx

nginx-whereis.png

啟動、停止nginx

  1. cd /usr/local/nginx/sbin/
  2. ./nginx
  3. ./nginx -s stop
  4. ./nginx -s quit
  5. ./nginx -s reload

./nginx -s quit:此方式停止步驟是待nginx程序處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。

查詢nginx程序:

  1. ps aux|grep nginx

重啟 nginx

1.先停止再啟動(推薦):
對 nginx 進行重啟相當於先停止再啟動,即先執行停止命令再執行啟動命令。如下:

  1. ./nginx -s quit
  2. ./nginx

2.重新載入配置檔案:
當 ngin x的配置檔案 nginx.conf 修改後,要想讓配置生效需要重啟 nginx,使用-s reload不用先停止 ngin x再啟動 nginx 即可將配置資訊在 nginx 中生效,如下:
./nginx -s reload

啟動成功後,在瀏覽器可以看到這樣的頁面:

nginx-welcome.png

開機自啟動

即在rc.local增加啟動程式碼就可以了。

  1. vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
設定執行許可權:

  1. chmod 755 rc.local

nginx-rclocal.png

到這裡,nginx就安裝完畢了,啟動、停止、重啟操作也都完成了,當然,你也可以新增為系統服務,我這裡就不在演示了。