獲取 WordPress
訪問個人中文鏡像站點 cn.wp101.net,下載最新的安裝包到服務器
mkdir /home/downloads
cd /home/downloads
wget http://cn.wp101.net/latest-zh_CN.tar.gz
# 解壓獲得一個 wordpress 目錄
tar -xzvf latest-zh_CN.tar.gz
處理 WordPress
wordpress 目錄就是整個項目的根目錄,這個目錄名字是可以更改的,本例中將其命名為 website-wp 即修改目錄名為 website-wp 並將整個目錄移動(復制)到打算放置 web 項目的路徑下
# 假設現在在 downloads 目錄下
mv wordpress website-wp
# 假設存在 /var/website 並將整個目錄移動到 website 目錄下
mv website-wp /var/website
按照官方教程,執行一些其它必要的配置,比如配置項目根目錄下 wp-config.php 中需要的數據庫連接信息
配置 Nginx
沒想到 Nginx 訪問 WordPress 會有坑,默認設置了 location 之后會導致各種 404,百度一番給出的答案是 WordPress 是依靠 .htaccess 偽靜態的,所以移植到 Nginx 訪問的時候路徑訪問就會有問題,需要做一點修改,首先在 /etc/nginx/conf.d 目錄下新建一個配置文件,專門用來配置 website-wp 項目,配置內容如下
# website-wp 項目配置
# 更新: 2020/04/29
#
server {
#使用 one 這個緩存
#proxy_cache one;
#proxy_cache_valid any 10m;
listen 80;
server_name 139.139.139.139 www.website-wp.com;
location / {
root /var/website/wwwsite-wp;
index index.php;
# 關鍵句 !!
try_files $uri $uri/ /index.php?$args;
}
# 關鍵句 !!
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
error_page 401 402 403 404 500 502 503 504 /oops.html;
location = /oops.html {
root /var/website/wwwsite-wp;
}
location ~\.php$ {
root /var/website/wwwsite-wp;
fastcgi_pass unix:/run/php-fpm/www.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存好后,在 /etc/nginx/nginx.conf 配置中包含進這個配置文件即可,值得一提的是 location 中對 PHP 的配置一段,fastcgi_pass 使用的是 socket 方式,與其對應 php-fpm 的配置文件也需要如此配置才能對應,具體如下
# 在/etc/nginx/conf.d 中默認有一個 php-fpm.conf,配置內容如
upstream php-fpm {
server unix:/run/php-fpm/www.sock;
}
配置 PHP
找到本機的 PHP-FPM 自己的配置文件,如果是 yum 安裝通常會在 /etc/php-fpm.conf 和 /etc/php-fpm.d/.conf 兩個地方(whereis php-fpm* 查看一下),它們的關系如 Nginx 的配置文件關系一樣,第一個文件是主配置,使用 include 語句說明包含某個目錄下哪些相關聯的其它配置文件,因此可以在 /etc/php-fpm.d 下新建一個名為 for-wwwsite-wp.conf 的文件
# 部分配置內容
; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]
; Per pool prefix
; It only applies on the following directives:
; - 'access.log'
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'
; When not set, the global prefix (or @php_fpm_prefix@) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php-fpm/www.sock
[www]用來標識 pid,可以改成具體項目名以區別於其它項目(多項目時使用挺好),user=nginx 和 group=nginx 是要改成和 Nginx 配置使用相同的 user 和 group 便於訪問,關鍵是 listen=/run/php-fpm/www.sock 說明了 www 這個(這個什么呢)池監聽的不是 127.0.0.1:9000 這樣的本地ip,而是通過 sock ,這個地方與 Nginx 一致即可,其實應該說 Nginx 要和 PHP 看齊,否則不在一條路就通訊不上了
chown 使 Nginx 擁有所有訪問權限
到此處時 wwwsite-wp 目錄所有操作都是在 root 用戶下進行過的,Nginx 也就是 WordPress 開始工作了也沒有權限訪問(主要是沒有權限寫入),使用一句命令將目錄過戶給 Nginx 用戶(yum 安裝的 Nginx 默認就向系統添加了 nginx 用戶和 nginx 組)
# -R 表示包含目錄下所有子目錄和子文件
chown -R nginx:nginx /var/website/wwwsite-wp
准備啟動
所有准備工作完畢,先啟動 PHP,再啟動 Nginx,然后用 IP 或域名訪問 /readme.html 或直接訪問 /wp-admin/install.php
總結
- Nginx 搭配 WordPress 是有坑的,要注意 location 部分的配置
- Nginx + PHP 的通訊可以是 sock 也可以是 ip+port,保持一致能通上話就行
- 雲服務器 + 雲數據庫沒准兒會遇到安全策略方面的阻礙,也要注意防火牆、端口等的設置,保證互相通訊正確