Linux安裝nginx含ssl(https)配置(親測)


安裝nginx相關依賴軟件

1.選定源碼目錄
選定目錄 /usr/local/ 
cd /usr/local/

2.安裝PCRE庫

yum -y install pcre-devel

3.安裝zlib庫

cd /usr/local/
wget http://www.zlib.net/zlib-1.2.11.tar.gz    
tar -xvf zlib-1.2.11.tar.gz 
cd zlib-1.2.11
./configure
make
make install

4.安裝ssl

cd /usr/local/
wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
tar -xvf openssl-1.0.2t.tar.gz 
cd openssl-1.0.2t
./config
make
make install

5.安裝nginx
Nginx 一般有兩個版本,分別是穩定版和開發版,您可以根據您的目的來選擇這兩個版本的其中一個,下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細步驟: 
cd /usr/local/
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -xvf nginx-1.14.0.tar.gz 
 cd nginx-1.14.0/
./configure --prefix=/usr/local/nginx
make
make install

  使用 /usr/local/nginx/sbin/nginx -t可查看應用nginx.conf的位置

6.the HTTP rewrite module requires the PCRE library報錯

./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-devel解決問題

 yum -y install pcre-devel
7.啟動
確保系統的 80 端口沒被其他程序占用
/usr/local/nginx/sbin/nginx

  檢查是否啟動成功:

netstat -ano|grep 80 有結果輸入說明啟動成功

打開瀏覽器訪問此機器的 IP,如果瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。

  8.重啟

/usr/local/nginx/sbin/nginx –s reload

9.修改配置文件

cd /usr/local/nginx/conf
vi nginx.conf 

 nginx.conf內容

user  root; #這步是防止靜態資源權限提示503
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    underscores_in_headers on; 
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size  80m;

    include  vhost/*.conf; 

}

我在conf目錄創建vhost文件夾,專門放各個項目或者域名的配置,所以這里用到include  vhost/*.conf; 

cd vhost
vi test.conf
server {
        listen       80;
        server_name  www.test.com;

        server_name_in_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        fastcgi_connect_timeout 1200;

        location /test-admin {
           proxy_pass http://127.0.0.1:6001/test-admin;                                             
        }

        location /test-file/ {
           alias  /home/test/test-file/;
        }
}

#如無需配置SSL證書,請忽略以下配置 server { listen
443; server_name www.test.com; #填寫綁定證書的域名 ssl on; ssl_certificate ssl/5334682_www.test.com.pem; # hps文件夾下的.crt文件 ssl_certificate_key ssl/5334682_www.test.com.key; #hps文件夾下的.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 /test-admin { proxy_pass http://127.0.0.1:6001/test-admin; } location /test-file/ { alias /home/test/test-file/; } }

這里我配置SSL證書,在conf目錄下創建ssl文件夾,專門儲存申請的SSL證書

 

如無需配置SSL證書,請忽略以下步驟

由於我們未裝SSL模塊,啟動時,會提示nginx:[emerg]unknown directive ssl錯誤

因為我們配置這個SSL證書需要引用到nginx的中SSL這模塊,然而我們一開始編譯的Nginx的時候並沒有把SSL模塊一起編譯進去,所以導致這個錯誤的出現。

解決方案:

步驟一:我們先來到當初下載nginx的包壓縮的解壓目錄,如果你是看小編寫的教程安裝的,解壓目錄應該在“/usr/loacl/nginx-1.14.0/”,絕大多數應該都是在這個目錄下的。

步驟二:來到解壓目錄下后,按順序執行一下命令: 

 命令1、./configure --with-http_ssl_module  //重新添加這個ssl模塊

  注意如果沒有出現錯誤,則直接看命令2即可 
   執行以上一條命令出現這個錯誤(./configure:錯誤:SSL模塊需要OpenSSL庫。),原因是因為缺少了OpenSSL,那我們再來安裝一個即可執行:yum -y install openssl openssl-devel 
  等待OpenSSL的安裝完成后,再執行./configure --with-http_ssl_module ,最后在執行” 命令1" 即可
 命令2、執行make命令,但是不要執行make install,因為make是用來編譯的,而make install是安裝,不然你整個nginx會重新覆蓋的。
 命令3、在我們執行完做命令后,我們可以查看到在nginx解壓目錄下,objs文件夾中多了一個nginx的文件,這個就是新版本的程序了。首先我們把之前的nginx先備份一下,然后把新的程序復制過去覆蓋之前的即可。
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp objs/nginx /usr/local/nginx/sbin/nginx

 

 命令4,最后我們來到Nginx安裝目錄下,來查看是否有安裝ssl模塊成功。執行./sbin/nginx -V即可 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM