在LNMP环境下搭建Discuz论坛,开启https,全站绿锁

本文详细介绍了在CentOS6环境下,使用Nginx、MySQL和PHP搭建Discuz论坛系统的过程,包括服务器配置、LNMP环境搭建、防火墙设置、HTTPS证书申请及配置,最终实现论坛的HTTPS安全访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搭建方法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 格式。

// 用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 端口并不在其中
复制代码

  • 将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
复制代码

// 查看端口占用程序,80 端口被 nginx 监听,3306 端口被 mysql 监听
netstat -nap
复制代码

  • 接下来,在浏览器地址栏输入你的服务器ip地址,就可以访问到 nginx 的欢迎页。


配置 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 配置页面

  • 根据之前的配置,设置Discuz中配置项

  • OK!论坛已经可以通过IP地址来访问了

开启 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 协议进行资源的请求和加载,不过浏览器仍然提示存在安全风险。

因为我们论坛中的图片链接的还是http协议。


优化 Discuz 全站的 http 协议为 https

这块不写了。也就是把整站的 http 链接都替换成 https 链接就好了,包括站点目录 /usr/local/nginx/html/ 中的 .php 中的协议也都换过来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值