CentOS/Nginx 安装 Dokuwiki 支持 Https 访问

更新工具包

注:此更新步骤仅为建议,非必须

1
2
sudo yum -y update
sudo yum -y install vim bash-completion wget tar

更新后重启系统

1
sudo reboot

安装工具包

1
2
3
4
5
sudo yum install epel-release yum-utils
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum makecache fast
sudo yum-config-manager --disable remi-php54
sudo yum-config-manager --enable remi-php72

安装 php 和 Nginx

注:若二者已安装,此步可跳过

1
2
3
sudo yum -y install php-cli php-fpm php-mysql php-zip php-ldap 
sudo yum -y install php-devel php-gd php-mcrypt php-mbstring
sudo yum -y install php-curl php-xml php-pear php-bcmath

安装好了后,检查一下 php 版本

1
php -v

若正常,会显示如下信息:

1
2
3
PHP 7.2.10 (cli) (built: Sep 11 2018 11:22:20) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

注:此处略去安装 Nginx 过程,如有需要,请参考其他教程

安装 Dokuwiki

下载前,先到 Github 检查一下它的最新稳定版本,此处假设为”2018-04-22b”

1
2
export RELEASE="2018-04-22b"
wget https://github.com/splitbrain/dokuwiki/archive/release_stable_${RELEASE}.tar.gz

解压下载的安装包,并转移到新建的文件夹 /var/www/html/ 中

1
2
3
tar xvf release_stable_${RELEASE}.tar.gz
sudo mkdir -p /var/www/html/
sudo mv dokuwiki-release_stable_${RELEASE} /var/www/html/dokuwiki

将文件夹 /var/www/html/dokuwiki 所有者权限修改为 nginx_user:nginx_group

注1:更改文件夹的所有者权限,方便 Nginx 有权访问该文件夹中的内容;此处假设 Nginx 进程运行在 nginx_user:nginx_group 下面,如果不是,则相应修改
注2:查看 nginx 所属用户 username 的办法为 ps aux | grep nginx
注3:查看某个用户 username 所属组的方法为 groups username

1
sudo chown -R nginx_user:nginx_group /var/www/html/dokuwiki

安装 SSL 证书

目的:支持使用 https 访问

安装 certbot-auto 到本地的 /usr/local/bin 下

目的:方便从 Letsencrypt 机构申请免费证书并简化后续的证书到期更新工作

注:若已安装过 certbot-auto 此步骤可略过

1
2
sudo wget https://dl.eff.org/certbot-auto -P /usr/local/bin
sudo chmod a+x /usr/local/bin/certbot-auto
配置 pip 国内源

注:若之前已配置,请跳过此步骤;此步骤的目的是加速 CertBot 下载 python 模块的速度

1
2
3
4
5
6
7
8
9
10
# 新建 .pip 文件夹并进入
mkdir .pip && cd .pip
# 创建 pip.conf 文件
vi pip.conf
# 在 pip.conf 文件中输入以下内容
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
# 保存退出
运行脚本,安装依赖
1
/usr/local/bin/certbot-auto --help
配置 nginx

目的:获取域名证书过程中, Let’s Encrypt 会对域名发起访问,以确认申请者对域名的所有权;故需要配置 nginx,以便能够对 Let’s Encrypt 的访问返回正确的响应;

1
2
3
4
# 创建文件夹,用于  Let's Encrypt 访问时返回响应内容
mkdir /home/letsencrypt
# 打开 nginx 配置文件进行编辑,此处假设 nginx 的配置文件在以下路径:/usr/local/nginx/conf/nginx.conf,如不是,则相应修改路径
vi /usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 在 nginx 配置文件中,找到 http,添加一条监听 80 端口的新 server
http {
//...(略)...
// 添加如下内容,此处假设申请域名为 domain.example.com,请修改为实际申请的域名
server {
listen 80;
server_name domain.example.com;
location ~ /.well-known/acme-challenge/ {
defaulf_type "text/plain";
root /home/letsencrypt/;
}
}

// ......以下略......
重启 nginx
1
2
3
4
5
6
7
# 此处假设 nginx 可执行文件在路径 /usr/local/nginx/sbin 下面,如不是则相应修改路径
# 先使用 -t 参数测试配置文件格式是否正确
/usr/local/nginx/sbin/nginx -t
# 若正确,屏幕上将显示以下字样
> nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# 重启 Nginx
/usr/local/nginx/sbin/nginx -s reload
运行脚本,申请证书
1
2
# 此处为域名 domain.example.com 申请一张证书,其中的 youremail.com 请替换为你自己的邮箱地址
/usr/local/bin/certbot-auto certonly --email youremail.com --webroot -w /home/letsencrypt -d domain.example.com

申请成功后,界面下会有如下的成功提示:

1
2
3
4
5
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/helloworld.com/fullchain.pem. Your cert
   will expire on 2019-08-26. To obtain a new version of the
   certificate in the future, simply run Let's Encrypt again......

注:记下以上提示信息中的 fullchain.pem 和 privkey.pem 两个文件路径,后续配置 nginx 会用到

配置 Nginx

打开 nginx 配置文件

1
2
# 配置文件的路径请根据实际情况修改
vi /usr/local/nginx/conf/nginx.conf

在nginx 配置文件中,新增两个 server 条目,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

server {
listen 443 ssl;
# 注意替换此处的域名
server_name domain.example.com;
root /var/www/html/dokuwiki;

access_log /var/log/dokuwiki.access.log;
error_log /var/log/dokuwiki.error.log;

ssl on;
# 注意替换此处的 fullchain.pem 和 privkey.pem 的路径为正确的实际路径
ssl_certificate /etc/letsencrypt/live/domain.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.example.com/privkey.pem;
ssl_session_timeout 5m;
ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

index index.html index.php doku.php;

location / {
try_files $uri $uri/ @dokuwiki;
}

location @dokuwiki {
rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
rewrite ^/(.*) /doku.php?id=$1 last;
}

location ~ /(data|conf|bin|inc)/ {
deny all;
}

location ~* \.(css|js|gif|jpe?g|png)$ {
expires 1M;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}

location ~ /\.ht {
deny all;
}
}
# 若想支持 80 端口访问,则可以在原监听 80 端口的 server 条目中添加一些信息
server {
listen 80;
server_name domain.example.com;
location ~ /.well-known/acme-challenge/ {
defaulf_type "text/plain";
root /home/letsencrypt/;
}
# 以上是 location 原申请证书时已写好的信息,下面的新 location 是需要新添加的信息
location / {
add_header Strict-Transport-Security max-age=2592000;
rewrite ^ https://$host$request_uri? permanent;
}
}

配置 php-fpm

打开以下 php-fpm 中的文件

1
sudo vim /etc/php-fpm.d/www.conf

将文件中以下几个键的值设置为如下:

1
2
3
4
5
6
7
# 注意替换此处的 nginx_user 和 nginx_group 为实际的用户名和用户组
user = nginx_user
group = nginx_group
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx_user
listen.group = nginx_group
listen.mode = 0660

启动 nginx 和 php-fpm

1
2
sudo systemctl start php-fpm
sudo systemctl enable php-fpm

重启 Nginx

1
2
3
4
5
6
# 先使用 -t 参数测试配置文件格式是否正确
/usr/local/nginx/sbin/nginx -t
# 若正确,屏幕上将显示以下字样
> nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# 重启 Nginx
/usr/local/nginx/sbin/nginx -s reload

配置 DokuWiki

使用浏览器打开网址:https://domain.example.com/install.php,打开后页面如下,配置方法请参考其他教程

注:domain.example.com 请相应替换为实际域名

image

其他

Letsenctrypt 的证书有效期为三个月,当剩余一个月时,Letsenctrypt 会发通知邮件到预留的邮箱;收到通知后,只需要登录服务器,运行相关命令,即可自动更新证书

1
2
# 先使用 --dry-run 选项进行测试,非真正执行更新
/usr/local/bin/certbot-auto renew --dry-run

若显示如下字样,则表示自动更新功能测试成功

1
2
3
4
Congratulations, all renewals succeeded. The following certs have been renewed:  
   /etc/letsencrypt/live/www.helloworld.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)

运行以下实际的更新命令,更新完了后,记得重启 nginx 服务器,以便启用新的证书

1
/usr/local/bin/certbot-auto renew -v

CentOS/Nginx 安装 Dokuwiki 支持 Https 访问
https://ccw1078.github.io/2019/10/20/CentOS Nginx 安装 Dokuwiki 支持 Https 访问/
作者
ccw
发布于
2019年10月20日
许可协议