初次安裝 WordPress 到 VPS 上並設定 MariaDB | WordPress

初次安裝 Wordpress 到 VPS 上並設定 MariaDB | Wordpress

我們安裝完 MariaDB、Apache、Nginx 以及 Varnish 之後,接著要先設定資料庫 MariaDB 的帳號密碼,才有辦法開始安裝 WordPress。

設定 MariaDB 的帳戶密碼

這個是指 MaraiDB 資料庫軟體所專屬的帳戶和帳戶密碼,這個和系統的帳戶無關。要設定帳號和密碼的方式其實很簡單,請按照下面的步驟來操作即可。

STEP 1

請輸入以下指令。

sudo mysql -u root -p

會顯示要求輸入密碼。

Enter password: 

這裡 Ubuntu 會要求你輸入 MariaDB 的 root 帳戶的密碼,這個東西在你上一章節安裝 MariaDB 就已經設定過,請在這裡輸入。

進去之後應該會看到這個模樣。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 37
Server version: 10.2.15-MariaDB-1:10.2.15+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

STEP 2

接著我們要開始建立資料庫,請記住資料庫名稱,會用到。「MariaDB [(none)]> 」不用跟著輸入,這裡只是方便跟終端機分別辨識才保留的。

MariaDB [(none)]>  CREATE DATABASE [資料庫名稱];

會顯示。

Query OK, 1 row affected (0.13 sec)

STEP 3

然後為資料庫建立一個專用的帳戶,以及這個帳戶的密碼。

MariaDB [(none)]>  GRANT ALL PRIVILEGES ON [資料庫新建帳戶名稱].* TO '[帳戶名稱]'@'localhost' IDENTIFIED BY '[新建帳戶的密碼]';

會顯示。

Query OK, 0 rows affected (0.16 sec)

STEP 4

然後刷洗設定。

MariaDB [(none)]>  FLUSH PRIVILEGES;

會顯示。

Query OK, 0 rows affected (0.00 sec)

STEP 5

最後離開,完成。

MariaDB [(none)]>  EXIT;

以上設定的資料庫帳號密碼都要記下來,在下一篇文章安裝 WordPress 時會使用到。

另外,如果你想要保護你的資料庫帳戶密碼不會隨便被其他人更改的話,可以安裝「simple_password_check」套件。不過進階版的「cracklib_password_check」在 Ubuntu 和 Debian 當中因為有 BUG 的緣故所以已經沒有提供。而「validate_password」這個套件是只有在 MySQL 才存在的。

如果怕太麻煩的話,可以跳過這個步驟,直接閱讀下面的步驟開始安裝 WordPress。

STEP 1

首先請輸入以下指令,重新登入 MariaDB。

sudo mysql -u root -p

請輸入密碼。

Enter password:

接著應該會看到這個畫面。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12574
Server version: 10.2.15-MariaDB-1:10.2.15+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

STEP 2

輸入以下指令,安裝「simple_password_check」套件。

MariaDB [(none)]>  INSTALL SONAME 'simple_password_check';

應該會顯示。

Query OK, 0 rows affected (0.15 sec)

STEP 3

安裝完成。我們下指令離開。

MariaDB [(none)]>  exit;

安裝完此套件之後,設定密碼預設會有這幾項限制。

1. 至少 8 個字元。
2. 至少一個數字字元。
3. 至少要有一個大寫字母和至少一個小寫的英文字母。
4. 至少要一個特殊字元。

STEP 4

接著我們用這個來重新設定密碼。

sudo mysql_secure_installation

STEP 5

接著會有一些問題要問你。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n]

請輸入「Y」,然後輸入密碼。

New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

STEP 6

接著移除訪客用的帳戶。

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]

請輸入「Y」。

STEP 7

讓遠端無法登入 root 帳戶,一定要到本機上面當入。(就是一定要登入 VPS 操作 MariaDB 才行)

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]

請輸入「Y」。

STEP 8

移除測試用資料庫。

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]

請輸入「Y」。

STEP 9

接著要重新載入設定。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]

請輸入「Y」。

然後就完成了。

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

安裝 WordPress

接著我們要下載本系列要介紹你安裝的 WordPress 主角了,請在終端機當中輸入。

cd /var/www/html/

因為 Apache 和 Nginx 預設都已經把網站目錄指定在「/var/www/html/」這個位置了,所以我們必須在這裡安裝 WordPress 才讀取得到。當然也可以自己透過修改 Aapche 和 Nginx 的設定檔案「000-default.conf」和「default」檔案就可以更改網站目錄位置。

接著我們要下載和解壓縮 WordPress 了。

話說我自己不習慣把 WordPress 的檔案直接整個釋放到「/var/www/html/」資料夾當中,因為會把「.htaccess」、「robots.txt」等檔案混雜在一起,造成管理上的問題。我建議大家要把 WordPress 檔案和用資料夾獨立出來管理,假設這個獨立出來的資料夾叫做「a」,但我們希望網址上首頁不要變成「https://www.xxx234567.tw/a」,這樣就太不直覺了。而這個是可以透過調整設定達成的。

另外官方的檔案當中,也是把 WordPress 檔案包在叫做「wordpress」的資料夾當中。

sudo wget https://wordpress.org/latest.zip -O wordpress.zip && sudo unzip -o wordpress.zip -d /var/www/html/
sudo mv -f /var/www/html/wordpress /var/www/html/[你想要的資料夾名稱,務必使用英文]

另外,在 Apache 和 Nginx 剛安裝好時,會在「/var/www/html」放這兩個檔案,告訴你有安裝成功。我們這裡用不到他們,就直接刪除。而 WordPress 已經解壓縮好,剛剛下載的「wordpress.zip」也可以刪除了。

sudo rm -R /var/www/html/index.html /var/www/html/index.nginx-debian.html /var/www/html/wordpress.zip

另外為了安全起見,我們把 WordPress 說明書和設定範例檔案給刪除。

sudo rm -R /var/www/html/[你想要的資料夾名稱]/readme.html /var/www/html/[你想要的資料夾名稱]/license.txt /var/www/html/[你想要的資料夾名稱]/wp-config-sample.php

接著,輸入以下指令新建 index.php,自動引導到 WordPress 登入的畫面。

sudo tee /var/www/html/index.php <<EOF
<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/[你想要的資料夾名稱]/wp-blog-header.php' );
EOF

處理 .htaccess 檔案

建立「.htaccess」檔案,這個是 Apache 的設定檔,和 Nginx 沒有關係。

輸入以下指令,讓 Apache 可以自動轉址到 WordPress 正確的位址。

sudo tee /var/www/html/.htaccess <<EOF
<IfModule mod_rewrite.c>
# add www and Force http to https
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ [NC]
RewriteCond %{SERVER_PORT} !^443 [NC]
RewriteCond %{HTTPS} off [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/[你想要的資料夾名稱]/wp-admin/includes/ - [F,L]
RewriteRule !^/[你想要的資料夾名稱]/wp-includes/ - [S=3]
RewriteRule ^/[你想要的資料夾名稱]/wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^/[你想要的資料夾名稱]/wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^/[你想要的資料夾名稱]/wp-includes/theme-compat/ - [F,L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
Options -Indexes
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

<Files .htaccess>
order allow,deny
deny from all
</Files>

<files /[你想要的資料夾名稱]/wp-config.php>
order allow,deny
deny from all
</files>
EOF

新增 robots.txt

為了讓瀏覽器不要亂爬蟲一些重要的檔案,我們必須把這些東西給封鎖起來。
另外也關係到 SEO 的問題,有些東西需要適度開放,讓爬蟲時可以顯示正確的版型(css、js),讓之後你挑選美美的佈景主題可以正確仔入,而被判別成適合手機瀏覽的網站。

sudo tee /var/www/html/robots.txt <<EOF
User-agent: *
Disallow: /[你想要的資料夾名稱]/wp-admin/
Disallow: /[你想要的資料夾名稱]/wp-includes/
Disallow: /[你想要的資料夾名稱]/wp-content/plugins/
Disallow: /[你想要的資料夾名稱]/wp-content/themes/

User-agent: Googlebot
Allow: /[你想要的資料夾名稱]/*.js*
Allow: /[你想要的資料夾名稱]/*.css*
Allow: /[你想要的資料夾名稱]/wp-content/*.js*
Allow: /[你想要的資料夾名稱]/wp-content/*.css*
Allow: /[你想要的資料夾名稱]/wp-includes/*.js*
Allow: /[你想要的資料夾名稱]/wp-includes/*.css*
Allow: /[你想要的資料夾名稱]/wp-content/plugins/*.css*
Allow: /[你想要的資料夾名稱]/wp-content/plugins/*.js*
Allow: /[你想要的資料夾名稱]/wp-content/plugins/*ajax*
Allow: /[你想要的資料夾名稱]/wp-content/themes/*.css*
Allow: /[你想要的資料夾名稱]/wp-content/themes/*.js*
Allow: /[你想要的資料夾名稱]/wp-content/themes/*ajax*
Allow: /[你想要的資料夾名稱]/wp-content/plugins/*/images/
Allow: /[你想要的資料夾名稱]/wp-content/uploads/
Disallow: /[你想要的資料夾名稱]/xmlrpc.php
Disallow: /[你想要的資料夾名稱]/wp-[你想要的資料夾名稱]/
Disallow: /[你想要的資料夾名稱]/wp-includes/
Disallow: /[你想要的資料夾名稱]/wp-content/plugins/
Disallow: /[你想要的資料夾名稱]/wp-content/themes/
Disallow: /[你想要的資料夾名稱]/wp-content/
EOF

設定系統帳戶和系統權限

要把自己的系統帳戶名稱加入「www-data」。

sudo usermod -g www-data [系統帳戶名稱]

接著要設定資料夾的權限,務必要設成「www-data」群組裡面,否則 WordPress 會無法安裝佈景主題或外掛。

sudo chmod -R g+w /var/www
sudo chmod -R 755 /var/www/html/
sudo chown -R www-data:www-data /var/www/html/
sudo chown root:root /var/www/html/
sudo chown -R www-data:www-data /var/www/html/[你想要的資料夾名稱]

設定完之後,設定 .htaccess 檔案的權限就告一個段落了。

sudo chown www-data:www-data /var/www/html/.htaccess
sudo chmod 664 /var/www/html/.htaccess

這裡請先不要關掉終端機。

安裝 WordPress

終於要進入我們的主題了!前面鋪那麼多的路,就只是希望可以讓 WordPress 可以運作得更順暢而已。
接著請打開你的網路瀏覽器,例如 FireFox、Chrome、Safari 之類的,進行 WordPress 安裝步驟。

這裡請先用「HTTP://」進行連線到你的網站,如果先用「HTTPS」連線來進行設定的話,可能會有一點問題,可能圖片會無法正確載入。也就是說,請在瀏覽器裡面輸入『http://www.[你的網域]』

STEP 1

選擇「繁體中文」,然後按「繼續」。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 2

按「衝吧」。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 3

接著請輸入以下資訊,這些是剛剛文章上面所設定的,不要忘記了喔。

資料庫名稱:上面輸入『CREATE DATABASE [資料庫名稱];』時候的名稱。
使用者名稱、密碼:在輸入『GRANT ALL PRIVILEGES ON [資料庫新建帳戶名稱].* TO '[帳戶名稱]'@'localhost' IDENTIFIED BY '[新建帳戶的密碼]';』的時候所輸入的帳號密碼。
資料庫主機位置:這個不用動。

至於資料表前綴,這個你可以不用動他,或是改成其他名稱也可以的。但是這個資料務必不可以告訴任何人,因為知道資料表前綴的話,別人要修改你的資料庫資料就很容易了,因此我也會建議改一下,讓別人無法輕易地動你的資料庫。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 4

按下「執行安裝」。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 5

接著請輸入一些登入用的資訊。

網站標題:會顯示在網站上面的標題。
使用者名稱、密碼:這個就不用我講了。
你的電子郵件:Wordpress系統出狀況時也比較好找你。
wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 6

大功告成,接著請按下「登入」。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 7

大功告成,接著請按下「登入」。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 8

請輸入你的帳密之後,按下「登入」。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

STEP 9

太好了!到此為止基礎的 WordPress 設置算是告一段落了。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 6 / 10

設定 WordPress

接著,我因為我們要徹底啟用 HTTPS 連線的關係,我們這裡要先編輯「wp-config.php」檔案,這就是剛剛安裝 WordPress 時所產生的檔案。

STEP 1

回到終端機,請輸入以下指令,編輯「wp-config.php」檔案。

sudo nano /var/www/html/[你的wordpress檔案存放處]/wp-config.php

接著請在「require_once(ABSPATH . ‘wp-settings.php’);」之前輸入以下內容。

//修復換成ssl後的錯誤
if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
     (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {
    $_SERVER['HTTPS'] = 'on';
}
/* 強制開啟後台https */
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
/* 設定垃圾桶清理週期 */
define ('EMPTY_TRASH_DAYS', 7);
/* 設定文章版本次數 */
define( 'WP_POST_REVISIONS', 4 );
/* 設定佈景主題不可編輯 */
define(‘DISALLOW_FILE_EDIT’, true);

注意,請勿將此段放在『require_once(ABSPATH . ‘wp-settings.php’);』之後,否則會沒有效果。

編輯完成後請按「Control」+「X」,按「Y」,再按「Enter」完成編輯。

STEP 2

接著我們要安裝一款外掛,叫做「Really Simple SSL」,幫助我們移轉到 HTTPS 連線。而 2018 年 6 月有人把它的台灣的繁體中文翻譯修正了,儘管安心地用吧。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 7 / 4
wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 7 / 4

安裝並啟用完成之後,會跳到外掛頁面,請點選他的視窗訊息中的「繼續啟動 SSL」,之後後台應該都會強制由 HTTPS 連線瀏覽。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 7 / 4

這時會登出,請再登入一次即可。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 7 / 4

STEP 3

另外,在登入之後,我建議使用我寫的這篇的操作,就是下面這個連結,先把你的網站設為「單一登入」,可以為你減少許多安全性上的問題。

1. 啟動「單一登入」使 WordPress 自架站更安全 | WordPress 技巧

還有,我們剛剛把 WordPress 安裝到『/var/www/html/[wordpress存放目錄]』之下的關係,我們要把網址修正一下。

STEP 4

滑鼠移到左側選單的「設定(Settings)」之後,點選「一般」,然後找到『WordPress 位址(URL)』欄位,請把網址改成:

https://www.[你的網域]/[你的 WordPress 存放位置]

而網站網址就維持成這個模樣。

https://www.[你的網域]/
wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 7 / 4

改完之後記得按下方的「儲存」按鈕。

STEP 5

最後有一些小設定,建議大家可以去更改掉,減少自己後續的許多麻煩。

滑鼠移到左側選單的「設定(Settings)」之後,點選「媒體」。進去之後,把「縮圖大小」、「中等大小」以及「大圖大小」的寬度高度、最寬最高全部改成「0」,目的是不要產生一大堆有的沒有的縮小圖片,既站空間管理上又不方便。

另外下方的『將我所上傳的檔案安排為以「月」及「年」為基準的目錄』請取消勾選,這個會造成圖片被按照年月分在許多的資料夾當中,會造成管理上的困難。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-7
作者ヤンヤン編輯於 2018 / 7 / 4

下一章節將會教你安裝 Redis,是來幫助你讓你的網站可以再提升速度的一個工具,專門在快取資料庫而使用的。

轉換到 HTTPS
1. 『教學』WordPress 設定HTTPS,使用SSL安全加密協定,SEO提升 | Techmarks劃重點
2. 在 WordPress 設定 HTTPS,強制使用 SSL 安全加密協定教學
3. php - How to force WordPress admin/login pages with SSL? - Stack Overflow
4. Correctly force SSL on wordpress via wp-config.php - Stack Overflow

每年 WordPress 製作的網站被攻擊方式的統計
5. WordPress WordPress : CVE security vulnerabilities, versions and detailed reports

從沒有 www 導向到有 www
6. .htaccess Remove WWW from URL + Directories - Stack Overflow
7. 〔筆記〕將www網址定向到無www,Redirect301導向程式碼 | Techmarks劃重點

MariaDB 的設定
8. Setup WordPress on Ubuntu 16.04 / 17.10 / 18.04 with Nginx, MariaDB, PHP 7.2 and Let’s Encrypt SSL/TLS Certificates | Website for Students
9. Password Strength Checking - MariaDB Knowledge Base

關於 MariaDB 的 plugin 的說明和設定方式
10. How to Secure your MariaDB Database Server - ClusterEngine.me
11. Password validation plugins in MariaDB | the informal blog
12. Password Validation - MariaDB Knowledge Base
13. Maria 엔진 업그레이드 및 계정 패스워드 복잡도 설정 방법
14. [MariaDB 10.1.20] 패스워드 복잡도 설정 > 기술자료 | 해피정닷컴

一些安裝好 Wodrpess 後的基礎設置
15. 殺手級 WordPress 體檢項目:101 個網站必須遵照的原則!