在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

AragornK發表於2018-09-27

搭建方法Discuz官方有提供,一路下來遇到不少坑和報錯,在網上也都是有跡可循的。中秋放假之前,已經全部搞定所有流程,由於自己懶惰沒有及時將全部流程進行完整的梳理和記錄,今天參考有道雲筆記中的零散資料整理,腦子裡根本沒有一點思路,都是無意識的文字堆砌。筆記寫了一大半的時候,我覺得裡面肯定有很多遺漏的關鍵點。所以,決定再把這個過程走一遍。


準備

  • 免備案伺服器。系統是 CentOS 6 ,之後的防火牆配置,CentOS 7 與此有差異,需要注意。
  • 域名。購買阿里雲的域名,還能方便申請免費的證照。

LNMP 環境搭建

LAMP 是 Linux、Apache、MySQL 和 PHP 的縮寫,是 Discuz 論壇系統依賴的基礎執行環境。

官方提供的是 LAMP 環境搭建教程,我覺得 Apache 配置太麻煩,所以就把 Apache 換成了 Nginx。

這裡安利一個SSH客戶端:X-Shell

安裝 MySQL

// 安裝
yum install mysql-server -y

// 啟動 MySQL 服務
service mysqld restart

// 設定 mysql 的賬戶名:root和密碼:pwdmysql
/usr/bin/mysqladmin -u root password 'pwdmysql'

// 設定 MySQL 開機啟動
chkconfig mysqld on
複製程式碼

安裝 PHP

// 安裝
yum install php php-fpm php-mysql -y

//啟動 php-fpm 程式
service php-fpm start

// 檢視該程式監聽的埠
netstat -nlpt | grep php-fpm

// 自啟動
chkconfig php-fpm on
複製程式碼

安裝 Nginx

// 安裝必須環境
yum install gcc-c++  // 編譯 nginx 需要
yum -y install pcre*  // 重定向需要 prce 支援
yum -y install openssl*  // https 支援

// 安裝 Nginx
cd /usr/local/
wget http://nginx.org/download/nginx-1.15.3.tar.gz
tar -zxvf nginx-1.15.3.tar.gz
cd nginx-1.15.3  // 進入 nginx 目錄
./configure --prefix=/usr/local/nginx  設定即將安裝 nginx 的目錄
make  //開始編譯安裝
make install

// 啟動 nginx 服務
cd /usr/local/nginx
./nginx

// 檢視 nginx 的 master 和 worker 程式
ps -ef | grep nginx

// nginx 啟動、停止、重啟
./nginx -s reload  // 過載配置檔案
./nginx -s stop  // 停止nginx服務
./nginx -s restart  // 重啟nginx服務
複製程式碼

將 nginx 新增到系統服務

  • 將這段指令碼程式碼儲存至本地,命名為 nginx,無字尾名。
#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 

esac
複製程式碼
  • 上傳指令碼至伺服器 /etc/init.d/ 目錄
scp nginx root@IPAddress:/etc/init.d/
// 修改檔案許可權
chmod 755 /etc/init.d/nginx
chkconfig --add nginx

// nginx 啟動、停止、無間隔重啟
service niginx configtest
service nginx start
service nginx stop
service nginx reload
複製程式碼

最後4個命令中的任何一個都會報錯! 這是因為我們是在 windows 建立的 nginx 指令碼檔案,檔案格式是 dos 格式,需要改成 unix 格式。

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

// 用vim開啟該sh檔案,輸入:
:set ff
// 回車,顯示fileformat=dos,重新設定下檔案格式
:set ff=unix  
:wq  // 儲存退出
複製程式碼

配置 nginx

  • 建立 nginx 執行使用的使用者 www
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
複製程式碼
  • 配置 nginx.conf,替換 /usr/local/nginx/nginx.conf 為一下內容
user www www;
worker_processes 1; #設定值和CPU核心數一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日誌位置和日誌級別
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虛擬主機的配置
 server
  {
    listen 80;#監聽埠
    server_name localhost;#域名
    index index.html index.htm index.php;
    root /usr/local/nginx/html;#站點目錄
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }
}
複製程式碼
  • 校驗配置檔案的正確性
service nginx configtest
複製程式碼
  • 校驗沒有問題的話,啟動 nginx
service nginx start
複製程式碼

配置防火牆

因為我們還沒有將80埠新增到防火牆的白名單中,所以此時在瀏覽器中用伺服器的IP地址無法訪問。 以下的配置都是基於 CentOS 6 的!

  • 檢視防火牆列表
iptables -L -n  // 80 埠並不在其中
複製程式碼

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

  • 將80、3306埠新增到防火牆白名單中。 3306是mysql預設埠,開放 3306 埠,以便你在遠端端用資料庫管理軟體連線資料庫。443是 https 協議預設埠
vim /etc/sysconfig/iptables

// 新增 80 、3306、 443埠
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT

// 重啟防火牆
/etc/init.d/iptables restart
// 檢視開放埠
/etc/init.d/iptables status
複製程式碼

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

// 檢視埠占用程式,80 埠被 nginx 監聽,3306 埠被 mysql 監聽
netstat -nap
複製程式碼

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

  • 接下來,在瀏覽器位址列輸入你的伺服器ip地址,就可以訪問到 nginx 的歡迎頁。

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖


配置 Discuz

Discuz 程式,官方提供了gitee地址

  • 下載 Discuz 建站程式
// 從本地上傳至伺服器
scp ComsenzDiscuz-DiscuzX-master.zip root@IPAddress:/usr/local/
// 需要unzip解壓,安裝 unzip
yum install unzip
複製程式碼

這裡我嘗試用 wget 直接在伺服器上拉取 gitee 上的zip是不行的,貌似是 gitee 下載zip的時候需要輸入驗證碼造成的,所以只能在windows上下載好以後上傳至伺服器。

  • 解壓程式包,拷貝建站程式到 nginx 相應目錄
cd /usr/local/
unzip ComsenzDiscuz-DiscuzX-master.zip
cd DiscuzX
cp -r upload/* /usr/local/nginx/html
// 給許可權
chmod -R 777 /usr/local/nginx/html
// 重啟 nginx
service nginx restart
複製程式碼
  • 記得刪除 /usr/local/nginx/html/ 目錄下的 index.html
rm /usr/local/nginx/html/index.html
複製程式碼
  • 瀏覽器訪問伺服器IP地址,進入 Discuz 配置頁面

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

  • 根據之前的配置,設定Discuz中配置項

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖

  • OK!論壇已經可以通過IP地址來訪問了

開啟 https

購買阿里雲域名新增解析

  • 解析域名,新增記錄
    在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖
    這個時候,瀏覽器地址輸入www.52zsz.xyz訪問成功,瀏覽器提示網站不安全。(誰能告訴我,為什麼點選連結從掘金跳轉不過去:無法建立安全連結。而直接在位址列可以開啟?)

購買證照

  • 進入雲盾控制檯“證照服務”,購買證照。阿里雲是有免費證照的,隱藏在 Symantec->增強型OV SSL中。手動點一下,免費型的就顯示出來了。
  • 進入證照管理介面,“補全”域名和身份資訊。等待稽核。

下載證照

  • 將證照上傳至伺服器 /usr/local/nginx/conf/cert阿里雲給的教程是 /usr/local/nginx/cert,是錯的!
scp 215034878070291.zip root@45.77.43.65:/usr/local/nginx/conf/cert
// 解壓
unzip 215*.zip
複製程式碼

修改 nginx 配置檔案

vim /usr/local/nginx/conf/nginx.conf
複製程式碼
user www www;
worker_processes 1; #設定值和CPU核心數一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日誌位置和日誌級別
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
 #下面是server虛擬主機的配置
 server
  {
    listen 80;#監聽埠
    server_name localhost;
    index index.html index.htm index.php;
    root /usr/local/nginx/html;#站點目錄
    return 301 https://www.52zsz.xyz;  #將http請求重定向到https
    location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }
  server {
    listen 443 ssl;
    server_name localhost;
    root /usr/local/nginx/html;
    index index.html index.htm index.php;
    ssl_certificate   cert/215034878070291.pem;
    ssl_certificate_key  cert/215034878070291.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    #location / {
    #    root html;
    #    index index.html index.htm;
    #}
    location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
}

}
複製程式碼
// 校驗配置檔案
service nginx configtest
複製程式碼
  • 校驗報錯: nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37
  • 報錯原因:nginx 缺少 http_ssl_module 模組
  • 解決辦法:重新編譯,參考這篇文章
  • 以上步驟操作完成以後,校驗配置檔案service nginx configtest,校驗成功。service nginx restart

至此網站,通過www.52zsz.xyz http://www.52zsz.xyz https://www.52zsz.xyz 中的任何一個域名訪問都會以 https 協議進行資源的請求和載入,不過瀏覽器仍然提示存在安全風險。

在LNMP環境下搭建Discuz論壇,開啟https,全站綠鎖
因為我們論壇中的圖片連結的還是http協議。


優化 Discuz 全站的 http 協議為 https

這塊不寫了。也就是把整站的 http 連結都替換成 https 連結就好了,包括站點目錄 /usr/local/nginx/html/ 中的 .php 中的協議也都換過來。

相關文章