更新工具包
注:此更新步驟僅為建議,非必須
sudo yum -y update
sudo yum -y install vim bash-completion wget tar
更新后重啟系統
sudo reboot
安裝工具包
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
注:若二者已安裝,此步可跳過
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 版本
php -v
若正常,會顯示如下信息:
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"
export RELEASE="2018-04-22b"
wget https://github.com/splitbrain/dokuwiki/archive/release_stable_${RELEASE}.tar.gz
解壓下載的安裝包,并轉移到新建的文件夾 /var/www/html/ 中
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
sudo chown -R nginx_user:nginx_group /var/www/html/dokuwiki
安裝 SSL 證書
目的:支持使用 https 訪問
安裝 certbot-auto 到本地的 /usr/local/bin 下
目的:方便從 Letsencrypt 機構申請免費證書并簡化后續的證書到期更新工作
注:若已安裝過 certbot-auto 此步驟可略過
sudo wget https://dl.eff.org/certbot-auto -P /usr/local/bin
sudo chmod a+x /usr/local/bin/certbot-auto
配置 pip 國內源
注:若之前已配置,請跳過此步驟;此步驟的目的是加速 CertBot 下載 python 模塊的速度
# 新建 .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
# 保存退出
運行腳本,安裝依賴
/usr/local/bin/certbot-auto --help
配置 nginx
目的:獲取域名證書過程中, Let's Encrypt 會對域名發起訪問,以確認申請者對域名的所有權;故需要配置 nginx,以便能夠對 Let's Encrypt 的訪問返回正確的響應;
# 創建文件夾,用于 Let's Encrypt 訪問時返回響應內容
mkdir /home/letsencrypt
# 打開 nginx 配置文件進行編輯,此處假設 nginx 的配置文件在以下路徑:/usr/local/nginx/conf/nginx.conf,如不是,則相應修改路徑
vi /usr/local/nginx/conf/nginx.conf
// 在 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
# 此處假設 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
運行腳本,申請證書
# 此處為域名 domain.example.com 申請一張證書,其中的 youremail.com 請替換為你自己的郵箱地址
/usr/local/bin/certbot-auto certonly --email youremail.com --webroot -w /home/letsencrypt -d domain.example.com
申請成功后,界面下會有如下的成功提示:
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 配置文件
# 配置文件的路徑請根據實際情況修改
vi /usr/local/nginx/conf/nginx.conf
在nginx 配置文件中,新增兩個 server 條目,內容如下:
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 中的文件
sudo vim /etc/php-fpm.d/www.conf
將文件中以下幾個鍵的值設置為如下:
# 注意替換此處的 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
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
重啟 Nginx
# 先使用 -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 請相應替換為實際域名
其他
Letsenctrypt 的證書有效期為三個月,當剩余一個月時,Letsenctrypt 會發通知郵件到預留的郵箱;收到通知后,只需要登錄服務器,運行相關命令,即可自動更新證書
# 先使用 --dry-run 選項進行測試,非真正執行更新
/usr/local/bin/certbot-auto renew --dry-run
若顯示如下字樣,則表示自動更新功能測試成功
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 服務器,以便啟用新的證書
/usr/local/bin/certbot-auto renew -v