1. 程式人生 > >Centos 7.5安裝 Nginx

Centos 7.5安裝 Nginx

1. 準備工作

 1.1 檢視系統版本

  1.1.1 輸入命令

cat /etc/redhat-release

  1.1.2 我的Centos版本

CentOS Linux release 7.5.1804 (Core)

 1.2 安裝nginx所需的依賴

  1.2.1 gcc安裝

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

yum install -y gcc-c++

  1.2.2 PCRE pcre-devel 安裝

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

yum install -y pcre pcre-devel

  1.2.3 zlib 安裝

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

yum install -y zlib zlib-devel

  1.2.4 OpenSSL 安裝

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

yum install -y openssl openssl-devel

2. 下載nginx

 2.1 下載地址:

https://nginx.org/en/download.html

 2.2 選擇穩定版本

3. 上傳

 3.1 使用xshell連線linux系統 再使用xftp上傳檔案到指定目錄

 3.2 我的目錄

/app/tool/

4. 解壓

 4.1 解壓到當前目錄

tar zxf nginx-1.14.1.tar.gz

 4.2 目錄結構

5. 初始化nginx(配置)

 5.1 解壓完成後進入解壓目錄


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

 5.2 使用預設配置(推薦)

  5.2.1 編譯

./configure

  5.2.2 完成輸出的結果

 5.3 自定義配置

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

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

6. 編譯安裝

 6.1 執行

make && make install

  6.1.1 輸出結果最後一行為

make[1]: Leaving directory `/app/tool/nginx-1.14.1'

 6.2 查詢安裝路徑

  6.2.1 命令

whereis nginx

  6.2.2 輸出結果

nginx: /usr/local/nginx

7. 啟動與重啟

 7.1 啟動

  7.1.1 進入sbin目錄

cd /usr/local/nginx/sbin

  7.1.2 啟動

./nginx

  7.1.3 停止:先查出nginx程序id再使用kill命令強制殺掉程序

./nginx -s stop

  7.1.4 停止:等待nginx程序處理任務完畢進行停止

./nginx -s quit

 7.2 重啟

  7.2.1 先停止再啟動

./nginx -s quit
./nginx

 7.3 過載配置檔案

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

./nginx -s reload

8. 反向代理

 8.1 修改/usr/local/nginx/conf下的 nginx.conf檔案

vim /usr/local/nginx/conf/nginx.conf

 8.2 在nginx.conf的http{}裡面新增

server {
        listen       80;
        server_name  wbsxch.com;
        location / {
                proxy_pass  http://127.0.0.1:8080;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

9. 配置https

 9.1 檢視nginx版本與編譯安裝了哪些模組 大寫V

./usr/local/nginx/sbin/nginx -V

 9.2 配置nginx 新增http_ssl_module模組

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 9.3 執行make ,千萬不要make install 否則會覆蓋現有的nginx

make

 9.4 關閉nginx

./usr/local/nginx/sbin/nginx -s quit

 9.5 複製配置目錄/objs/nginx 替換/usr/local/nginx/sbin/nginx

 9.6 檢視編譯安裝的模組

./usr/local/nginx/sbin/nginx -V

9.7 上傳證書到nginx的conf目錄

 9.8 修改nginx.conf

server {
    listen  80;
    server_name wbsxch.com;
    send_timeout    1800;
    
    rewrite ^(.*) https://wbsxch.com$1 permanent;
    }

    server {
        listen 443;
        server_name wbsxch.com; #填寫繫結證書的域名
        index index.html index.htm index.php default.html default.htm default.php;          
        ssl on;
        ssl_certificate /usr/local/nginx/conf/1_wbsxch.com_bundle.crt;
        ssl_certificate_key /usr/local/nginx/conf/2_wbsxch.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照這個套件配置
        ssl_prefer_server_ciphers on;
        location / {
            proxy_pass http://127.0.0.1:8080;
        }
    }

 9.9 驗證配置是否正確

./usr/local/nginx/sbin/nginx -t