這裡實作的環境是:
VMWare的虛擬機, 給予1 CPU與1 GB RAM, 20GB磁碟空間, 並安裝了Debian Linux 9.5為作業系統
同樣採用了Debian開發者Ondřej Surý的PHP 7 repository: https://packages.sury.org/php/
apt-get install apt-transport-https lsb-release ca-certificates zlib1g-dev git
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
更新Ondřej Surý的PHP 7 repository
apt-get update
安裝PHP 7.3, 這裡還是RC5
apt-get install php7.3 php7.3-cli php7.3-common php7.3-curl php7.3-mbstring php7.3-mysql php7.3-xml php7.3-fpm php7.3-dev php7.3-zip
檢視PHP版號
php -v
會有類似輸出訊息
PHP 7.3.0RC5 (cli) (built: Nov 12 2018 13:37:51) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.0RC5, Copyright (c) 1999-2018, by Zend Technologies
安裝Percona Server for MySQL 8.0測試版repository
wget https://repo.percona.com/apt/percona-release_0.1-8.$(lsb_release -sc)_all.deb
dpkg -i percona-release_0.1-8.$(lsb_release -sc)_all.deb
percona-release enable ps-80 testing
更新Percona Server for MySQL 8.0 repository
apt-get update
安裝Percona Server for MySQL 8.0, 同樣還在RC階段
apt-get install percona-server-server
過程中將會詢問MySQL Server的root密碼, 請輸入密碼並確實記住
安裝Web Server, 採用Nginx
apt-get install nginx
軟體安裝完成, 接著個別設定它們的組態檔案, 使其能一同工作
首先是修改Nginx Web Server的PHP-FPM的設定
nano /etc/nginx/sites-available/default
將"location ~ \.php$ {...}"這段啟用, 移除注解"#"井字號讓設定生效, 我這裡採用Unix Socket, 所以"#fastcgi_pass 127.0.0.1:9000;"這裡請保留注解"#"井字號
/etc/nginx/sites-available/default完整設定
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
location ~ /\.ht {
deny all;
}
}
我有把大部份的注解文字刪除, 較容易閱讀, 而Nginx預設的文件根目錄在/var/www/html之下
重新啟動Nginx
service nginx restart
這時你可以寫個PHP簡單程式看PHP-FPM與Nginx是否運作正常, 通常我會寫個phpinfo()來看安裝資訊確認與額外安裝的PECL模組, 例如:
cd /var/www/html
nano test.php
test.php要顯示phpinfo, 貼入程式碼
<?php phpinfo(); ?>
然後用瀏覽器看是否運作正常, 例如: http://192.168.1.74/test.php
測試VM用了IP位址192.168.1.74, 所以使用此URL
設定Percona Server for MySQL 8.0
nano /etc/mysql/mysql.conf.d/mysqld.cnf
my.cnf已改放到這裡, 貼入你想要最佳化的MySQL設定值, 這裡我先只放測試會用到的設定而以
加上
character_set_server = utf8mb4
collation-server = utf8mb4_0900_ai_ci
init_connect = 'SET collation_connection = utf8mb4_0900_ai_ci,NAMES utf8mb4'
雖然預設已是character_set_server = utf8mb4及collation-server = utf8mb4_0900_ai_ci, 還是補上, 並且增加了init_connect的設定
重啟Percona Server for MySQL 8.0
service mysql restart
使用mysql client進入console, 建立WordPress的資料庫與可存取它的mysql用戶
mysql -u root -p
輸入密碼後進入mysql console, 先建立WordPress的資料庫, 就叫"wordpress"吧
CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
建立可以存取它的用戶, 帳號名稱"wordpress", 密碼"wordpresspassword", 請依照自己的需求修改
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'wordpresspassword';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost';
更新權限後離開
FLUSH PRIVILEGES;
exit
回到Linux下的CLI
接著要安裝WordPress, 前往https://wordpress.org/download/取得最新版的WordPress程式碼
cd /var/www/html
wget https://wordpress.org/latest.tar.gz
tar zxvf https://wordpress.org/latest.tar.gz
rm https://wordpress.org/latest.tar.gz
WordPress程式碼解壓縮到wordpress目錄, 這裡我只要用WordPress做為網站, 所以再改一下Nginx的rewrite設定
nano /etc/nginx/sites-available/default
改為
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/wordpress;
index index.php index.html index.htm;
server_name _;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
location ~ /\.ht {
deny all;
}
}
重新啟動Nginx
service nginx restart
這樣WordPress的美化網址才能正確生效, 當然初次進入WordPress需要資料庫的設定, 請依照前面給予WordPress的資料庫名稱與程式存取帳號, 這樣大致完成安裝, 並且進入WordPress後台設定想要的美化網址, 以下是完成範例:
WordPress有幾個提升效能的工具, 其實就是利用Memcached
替換WordPress Memcached Plugin: Memcached Object Cache
產生靜態快取檔案: WP Super Cache
當然會需要PHP memcache擴展, 安裝方式請參考: https://www.mobile01.com/topicdetail.php?f=506&t=5631425
以及Memcached Server
apt-get install memcached
預設將會監聽在127.0.0.1:11211, 使用64MBytes RAM為快取
MySQL 8.0 utf8mb4有更好的繪文字支援, 加上PHP 7.3的速度提升, 是日後網站應用程式的好組合!