軟件及版本選擇
Ubuntu 14.04
Ubuntu 是目前用戶數量數一數二的發行版,背后有大土豪維護,可以說是輕量級用戶的最佳選擇。而 14.04 是目前最新的 LTS 版本,目前已經發布了半年了,基本是目前支持最好的版本。
Nginx
Nginx 是一個輕量級的,配置靈活,擅長并發的 Web 服務器。
PHP-FPM
PHP-FPM 是目前官方推薦的最佳的運行模式。
MariaDB
MySQL 的替代品,畢竟目前 MySQL 的創始人已經不建議我們使用 MySQL 了。
基本配置
通常當你創建了一臺 VPS, 你會得到一個 IP 和一個 root 密碼,所以,先用 ssh 登上你的服務器:
1
|
# 如果有警告輸入 yes 來確認,然后輸入你的 root 密碼
配置一下公鑰登錄,省著每次登錄都要輸入密碼,非常建議像我一樣把公鑰上傳到一個公開的地址,這樣只要一條命令就可以設置好:
1
|
mkdir ~/. ssh ; curl 'https://raw.githubusercontent.com/jysperm/meta/master/Key/JyAir.pub' >> ~/. ssh /authorized_keys ; chmod -R 700 ~/. ssh ; |
然后更新一下軟件包列表,升級現有軟件包:
1
2
|
apt-get update apt-get upgrade |
修改一下主機名,最好改成一個確實可以訪問到這臺服務器的域名:
1
2
|
vi /etc/hostname vi /etc/hosts |
安裝軟件包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
apt-get install nginx postfix php5-fpm mariadb-server memcached apt-get install php-pear php5-mysql php5-curl php5-gd php5-mcrypt php5-memcache apt-get install python make screen git wget zip unzip iftop vim curl htop iptraf nethogs nginx: Web 服務器 postfix: SMTP 服務器,用來支持從本地發送郵件 php5-fpm: PHP 進程管理器,及 PHP 解釋器 mariadb-server: 類 MySQL 數據庫 memcached: 基于內存的緩存,很多程序會用到 php-pear: PHP 的包管理器 php5-mysql: PHP MySQL 數據庫驅動 php5-curl: 一個 HTTP 協議庫 php5-gd: 一個圖像處理庫 php5-mcrypt: 一個加密算法庫 php5-memcache: Memcached 驅動 python: 一個常用的腳本語言解釋器 make: 一個常用的構建工具 screen: 一個常用的 Shell 會話管理工具 git: 一個常用的版本控制工具 wget, curl: 常用的文件下載工具 zip, unzip: ZIP 壓縮和解壓工具 iftop, iptraf, nethogs: 常用的流量監控工具 vim: 一個常用的編輯器 htop: 一個常用的進程監控工具 |
安裝 WordPress
新建一個普通用戶,并切換到該用戶
1
2
3
|
adduser wordpress su wordpress cd ~ |
下載 WordPress, 請自行到官網查看最新版本的下載地址:
1
|
wget http: //cn .wordpress.org /wordpress-3 .9-zh_CN.zip |
解壓文件:
1
|
unzip wordpress-*.zip |
設置文件權限:
1
|
chmod -R 750 wordpress |
刪除安裝包:
1
|
rm wordpress-*.zip |
回到 root:
1
|
exit |
配置 PHP-FPM
為 WordPress 創建一個進程池:
1
|
vi /etc/php5/fpm/pool .d /wordpress .conf |
這是一份很典型的配置文件,通過監聽 Unix Socket 來提供服務,動態調節進程數,最高 10 個進程,最低 3 個進程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[wordpress] user = wordpress group = wordpress listen = /home/wordpress/phpfpm.sock listen.owner = wordpress listen.group = wordpress listen.mode = 0660 pm = dynamic pm.max_children = 10 pm.min_spare_servers = 3 pm.max_spare_servers = 5 slowlog = /home/wordpress/phpfpm.slowlog request_slowlog_timeout = 5s request_terminate_timeout = 15s php_admin_value[error_log] = /home/wordpress/phpfpm_error.log php_admin_flag[log_errors] = On |
配置 Nginx
刪掉 Nginx 的默認站點:
1
|
rm /etc/nginx/sites-enabled/default |
新建一個站點:
1
|
vi /etc/nginx/sites-enabled/wordpress |
這份配置文件已將請求重寫到 index.php, 可以直接在 WordPress 中使用「固定鏈接」功能:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
server { listen 80; server_name jysperm.me; root /home/wordpress/wordpress; index index.html index.php; autoindex off; location / { try_files $uri $uri / /index.php; } location ~ \.php$ { fastcgi_pass unix: ///home/wordpress/phpfpm.sock; include fastcgi_params; fastcgi_index index.php; } } |
如果你希望把其他所有域名都跳轉到你的站點,可以添加這么一段:
1
2
3
4
5
|
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; rewrite ^/(.*)$ http://jysperm.me permanent; } |
然后我們需要修正 Nginx 和 PHP-FPM 配合的一個 Bug:
1
|
vi /etc/nginx/fastcgi_params |
將 fastcgi_param SCRIPT_FILENAME 開頭的行改為:
1
|
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
為 Nginx 添加讀取 WordPress 文件的權限:
1
|
usermod -G wordpress -a www-data |
配置 MySQL
進入 MySQL 控制臺:
1
|
mysql -p |
# 需要輸入你的 MySQL root 密碼
# 創建數據庫
1
|
CREATE DATABASE `wordpress`; |
# 為 WordPress 新建用戶
1
|
CREATE USER 'wordpress' @ 'localhost' IDENTIFIED BY 'password' ; |
# 授予權限
1
|
GRANT ALL PRIVILEGES ON `wordpress` . * TO 'wordpress' @ 'localhost' ; |
# 退出
QUIT
重啟
好了,已經配置完成了,我們直接重啟服務器即可,這樣所有服務都會重啟并使用新的配置:
reboot