一些主機間遷移 WordPress 的操作流程和注意事項。

從 FTP 遍歷下載程式碼
遞迴下載根目錄所有內容

Shell

wget -rm -nH ftp://user:password@example.com/
引數 -r 表示遞迴, -m 開啟映象 (複製檔案的後設資料), -nH 表示不把檔案儲存到主機名對應的路徑下。 (不加的話,會建立一個名為 example.com 的資料夾,然後把下載的檔案放到裡面。)

下載某個子目錄的內容

Shell

wget -rm -nH --cut-dirs=3 ftp://example.com/path/to/dir/
引數 --cut-dirs 表示從路徑中切除前三段 (本例中為 path/to/dir ) 。 (不加的話,會嚴格按照伺服器使用者相對路徑在本機建立對應資料夾。)

下載時使用絕對路徑

Shell

wget -rm -nH ftp://user:password@example.com//usr/share/nginx/path/to/file/
多加一個 / 就可以使用絕對路徑。

匯出 MySQL 資料庫
只需要匯出在 wp-config.php 中設定的資料庫即可。

命令列方式

Shell

mysqldump --databases wp_somedb --result-file=backup.sql
GUI 方式
可以使用 Sequel Pro 等工具完成操作。

Nginx 基礎配置
WordPress(無任何快取外掛)+ Nginx + PHP5-FPM

Default

server {
listen 80;
listen

[::]:80;

root /usr/share/nginx/example.com;
index index.php index.html index.htm;

server_name www.example.com;

# WordPress single blog rules.
# Designed to be included in any server {} block.

# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/wordpress;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
非 www 到 www 域名的 301 跳轉

Default

server   {
server_name example.com;
rewrite  ^/(.*)$  http://www.example.com/$1 permanent;
}
修改檔案許可權

Shell

chown -R www-data:www-data /usr/share/nginx/example.com
find /usr/share/nginx/example.com -type d -exec chmod 755 {} \;  # 把資料夾許可權改為 rwxr-xr-x
find /usr/share/nginx/example.com -type f -exec chmod 644 {} \;  # 把檔案許可權改為 rw-r--r--
匯入 MySQL 資料庫
開啟 MySQL 控制檯

Shell

mysql -u root -p
建立新資料庫

MySQL

CREATE DATABASE wp_somedb;
匯入備份資料

MySQL

use wp_somedb;
source path/to/backup.sql;
建立使用者並授予許可權

MySQL

CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wp_somedb.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
修改 WordPress 配置
編輯 wp-config.php ,修改資料庫配置:

PHP

define('DB_NAME', 'wp_somedb');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');

儲存即生效。

驗證 WordPress 狀態
編輯 wp-config.php ,修改 debug 配置:

PHP

define('WP_DEBUG', true);
然後訪問各頁面,看是否輸出錯誤資訊。如有,檢查對應的程式碼。最後別忘了把 debug 選項關掉。

其他步驟
Hardening WordPress – wordpress.org
Nginx – wordpress.org
遷移到 uWSGI – blog.phoenixlzx.com