在 Linode 上建立免費且疾速的 WordPress 網站並使用 Ubuntu 18.04, LAMP, Nginx, Varnish and Redis 加速你的網站 | WordPress

SSL Wordpress on Linode with Ubuntu 18.04, LAMP, Nginx, Varnish and Redis

架設網站的難度

現在架設網站的技術門檻其實越來越低了,有了類似 WordPress 等平台工具,不需要會寫程式就可以架設網站,讓架設自己的個人網站變得很輕鬆,加上這幾年 VPS 變得非常普遍,每個月只要花大約 5 美金就能夠租到。所以其實也不要太擔心自己架不架得起來,跟著本篇的教學,我相信你也可以成功架設網站的。

另外提到網站架設的基礎架構,先不論用哪一種程式,大多都不會脫離「伺服器軟體、資料庫、網站頁面這幾項」,而常用的組合通常是 LAMP 和 LEMP,而我的網站也是這樣架設起來的,只是為了優化網站載入速度的關係,我又對 VPS 內的系統的資源使用的方式進行了一些優化,同時用了 Varnish 和 Redis 在我的 VPS 的系統裡面。而目前你所瀏覽的網站,就是在這樣的基礎上運作中。

話說如果你是搜尋「哪些不錯的加速 WordPress 外掛」之後都會介紹,但要在很後面的章節才會出現,不過光靠外掛是無法加速多少的,重點還是機器的效能和是否能把機器的效能運用得淋漓盡致(榨乾機器的意思)。

架設網站的過程

我在架設這個網站的時候,一開始其實只有使用 Ubuntu 18.04 系統搭配 apache 2.4 來架設網站而已,不過在網站性能方面並沒有很好,也就是說,同時太多人用我的網站就可能掛點。

如果大家搜尋網站加速的方法,可能會找到幾個加速網站的外掛如 wp super cache、Autoptimize、W3 Total Cache、WP Rocket 等外掛,或是透過 Apache2 的 cache module 來做快取。不過以上的方式,大多都僅針對網站程式碼進行優化以及增加快取功能而已。

其實我們還忘記了一件事情,我們可以透過加速伺服器的反應速度來加速網站的載入速度,會根本影響網站速度。如果網頁程式碼優化再好,但是主機很慢,那網站一樣是跑得很慢;相反的,雖然沒有對網站程式碼做優化,但是因為主機本身速度很快的關係,網站基本上也不會太慢,而要怎麼讓主機速度變快呢?這將會在這系列的主題後幾篇當中介紹到技術的操作部分。

此企劃的目標

從零開始,從擁有一個空白的 VPS 開始,要製作出包含 apache 2.4、nginx 1.14、varnish 6、redis 4、php 7.2、mariaDB 10.2 和 Ubuntu 18.04 系統的主機環境。
並且,在這個基礎之上,我們要製作出一個 https 連線的 WordPress 網站,並且可以在短時間內載入。

會用到的東西有:

主機商:Linode、amazon、google cloud platform
網域:namecheap
系統:Ubuntu 18.04
軟體:apache 2.4、nginx 1.14、varnish 6、redis 4、php 7.2、mariaDB 10.2

有人問過我,幹嘛把自己搞得那麼麻煩呢?我覺得,除了這個方式可以加速自己的網站之外,也給自己一個磨練技術的機會,何樂而不為呢?

有推薦哪些 VPS

使用 VPS 的話,意思就是說你擁有系統最高的管理員權限,通常我們叫做 root,意思就是說你可以對系統進行各樣修改,當然不要亂改就是了。
通常都會給每個用戶你所使用的 VPS 的機器規格,如 CPU 大小、記憶體大小、硬碟大小等等,不太會有和別人一起共用主機時資源被佔用的問題。

我個人推薦 Linode、Google、Amazon、Vultr、Digital Ocean 這幾家的 VPS 服務,網路上的評價也都蠻不錯的,有興趣的可以參考一下。
而前三篇的文章,將會個別教大家怎麼設定 Linode、Google、Amazon 這三家 VPS 的設定方式。下面就直接從 Linode 開始介紹起。

註冊 Linode

STEP 1

首先透過此網址到 Linode 註冊

請參考:
1. https://www.linode.com/?r=0034e9aaeba7bf9ba62f8cdadb9ec00755454580

透過此網址註冊 Linode,雖然沒有回饋到您的 Linode 的帳戶裡頭(不過下方會教你其他獲得購買 Linode VPS 優惠的方式),但是你可以不花錢幫助我維持營運這個網站,希望你也能幫我一把。

STEP 2

輸入您的信箱、帳號名稱、帳號密碼

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

STEP 3

接著登入你的信箱,找到驗證信件並點選「Confirm my email and create my account! »」。

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

STEP 4

點選之後會到一個完成註冊畫面,點選「OK! Let’s complete my signup »」

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

STEP 5

接著是輸入相關的個人資訊部分。

1. Billing 的話請以英文輸入完整的相關資訊,地址的部分可以到

2. Optional Information 的部分,在「Referral Code」輸入:0034e9aaeba7bf9ba62f8cdadb9ec00755454580
輸入此號碼,雖然沒有回饋到 Linode 的帳戶裡頭,但是可以幫助我維持營運這個網站,希望你也能幫我一把。

3. 「Promotion Code」可上網搜尋「linode Promotion Code」可以找到優惠代碼,讓你有免費的 20 美元額度可以使用幾個月看看。
4. Payment Detail 的部分,因為 Linode 會是先從你的信用卡(金融卡)提出 5 美元作為購買主機的費用,所以需要輸入您的卡片相關資訊。
5. Preload your Account 的部分,在「Preload Amount」的部分選擇 5 美元。

最後記得點選「I agree to the Terms of Service and understand my services will automatically renew.」

以上點選完成後,點選「Add Credit」,完成註冊和付款。

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

STEP 6

之後會自動跳到「Linode」標籤頁,這時我們選擇「Linode 1024」,對於新架站的人來說是非常夠用了,這樣的規格可以在裡面架 10 個部落格吧。
主機的位置會建議新增在離台灣近一點,這樣網站載入速度也可以快一點。在「Location」選擇「Tokyo 2, JP」,再點選「Add this Linode!」

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

STEP 7

購買完成之後,我們接著進到你剛剛建立的虛擬主機,點選「linode(一串數字)」,進到控制台後,點選「Rebuild」準備來安裝系統和分割硬碟。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-1
作者ヤンヤン編輯於 2018 / 1 / 22
wp-ssl-ubuntu-lamp-nginx-varnish-redis-1
作者ヤンヤン編輯於 2018 / 1 / 22
另外想提供一個小資訊,這裡你可以看到下方有一個「This Month’s Network Transfer Pool」,意思就是這個系統對外網路流量輸出和輸入多少,也就是上傳下載的量,而 Linode 提供使用者 1TB(1000GB)的免費流量,我想要用也用不完吧…。

STEP 7

接著選擇你想要使用的系統,裡面有各式各樣的 Linux 可以選擇。而系統我習慣使用 Ubuntu 18.04,當然你也可以選擇其他的,不過下面我會以 Ubuntu 18.04 來說明。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-1
作者ヤンヤン編輯於 2018 / 1 / 22
而 Root 的密碼很重要(要記得,之後會用到)後,按下「Rebuild」按鈕之後,就會自動幫你灌好系統(大約不到一分鐘裝好)。

STEP 8

灌好之後,回到 Dashboard,等待系統安裝好之後(大約 1 分鐘的時間),點選「Boot」來開機。

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

讓你的 Mac 能夠用自己的終端機和 Filezilla 進入你的 VPS 的系統

Linode 會直接給你一個靜態的 IP 位置,所以不需要另外設定靜態網址的部分。灌完系統後,可以點選「Remote Access」來查看你的靜態 IP 位置是多少。
靜態 IP 位置就在 SSH Access 那一欄,「ssh root@」後面就是你的 IP 位置。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-1
作者ヤンヤン編輯於 2018 / 1 / 22
而 Linode 並沒有強制使用金要登入,所以請按照以下的方式登入即可。

如果要用 Mac 內建的終端機連線到 Linode VPS 裡的系統,只要打開終端機後輸入:

sudo ssh root@[你的ip位置]

或是

sudo ssh ubuntu@[你的ip位置]

這原因是在於 ubuntu 系統安裝時,就會自動建立一個名為「ubuntu」的帳戶,他的權限跟一般的使用者是一樣的。
輸入之後按鍵盤的「Enter」,系統會要求你輸入密碼,然後就可以連線了。

wp-ssl-ubuntu-lamp-nginx-varnish-redis-1
作者ヤンヤン編輯於 2018 / 1 / 22
而如果要用 Filezilla 連線到 Linode VPS 裡的系統以方便上傳或下載檔案的話,首先到這裡下載 Mac 版的 FileZilla 安裝。

請參考:
1. https://filezilla-project.org/

wp-ssl-ubuntu-lamp-nginx-varnish-redis-1
作者ヤンヤン編輯於 2018 / 1 / 22
直接新建站台,輸入你的執行個體的靜態 IP 位置、該執行個體上你剛剛新建的帳戶的帳號(該名稱)和密碼,記得選擇 SFTP 連線,port 則可以不甜,預設都是22。然後按下連線,就可以成功連線了。

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

我對 Linode 的評價

Linode 我認為是一家提供穩定 VPS 服務的公司,不僅僅只是公司是老牌子,有口碑,他提供的硬碟是 SSD,VPS 連線到外面的網路也是可以到每秒幾十 MB 的速度。
在剛剛操作的部分,其實我認為是非常簡單易懂的一個流程,操作上並不困難,我認為也很適合新手使用。
另外,Linode 本身不只是提供一個免費的系統,他連系統的套件庫都幫你整理好了,有興趣的話可以登入 VPS 內的系統後,假設以 Ubuntu 為例,輸入這個指令:

sudo cat /etc/apt/sources.list

你可以看到,

## Note, this file is written by cloud-init on first boot of an instance
## modifications made here will not survive a re-bundle.
## if you wish to make changes you can:
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg
##     or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d
## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic main restricted
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-updates main restricted
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic universe
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic universe
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-updates universe
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic multiverse
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic multiverse
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-updates multiverse
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu bionic-security main restricted
deb-src http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
deb-src http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse
deb-src http://security.ubuntu.com/ubuntu bionic-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu bionic partner
# deb-src http://archive.canonical.com/ubuntu bionic partner

你可以看到 Linode 已經幫你建立好你需要用到套件庫,這樣在接下來下面幾個章節要設定 Apache、Varnish 等等東西時不怕找不到東西安裝(用 Ubuntu 官方的套件庫的有時候會出問題)。以我們這次會介紹的 VPS 商來講,有 Lindoe 和 Google Cloud 會提供這樣的套件庫。

最後我最喜歡 Linode 的一點是在他的網路轉換流量有 1000GB,可以不用擔心被收流量費用,因為根本就用不完這個額度啊。如果 Amazon ec2 或是 Google Compute Engine 則要收流量費用,雖然 VPS 可能更便宜,但是流量費用一不小心一個月可能就被扣超過 1000 元台幣,在 Amazon ec2 和 Google Compute Engine 免費試用期間結束之後,我推薦會回來繼續使用 Linode。

下一篇將會介紹關於以 Google Cloud 作為主機的申請與設定過程,會變得比較複雜一點點,但我盡量會說的很簡單。

引用資料區
1. 【VPS】什麼是VPS? 使用VPS有什麼好處嘛?,n.d.,碼人日誌
2. 網站架設(Web Building),n.d.,G. T. Wang
3. 「站長路可」提供給我的建議。