這是一次搬站紀錄
之前蹭 Github Student Pack 的 DigitalOcean VPS 試用金用完了,趁著 Oracle 的虛擬機刷好了把部落格搬過去,順便做一下紀錄,畢竟每次裝環境總是有翻車的不確定性…
事前準備
- Oracle VPS 一台 : Arm64v8、作業系統採用新手友善的 Ubuntu 20.04
( 懶人刷 Oracle VPS 教學 : ttps://github.com/lemoex/oci-help ) - 架站用的域名 ( Gandi、CloudFlare 都不錯用,個人現在用 CloudFlare,便宜 )
- DNS 服務商 ( 個人用 CloudFlare,沒什麼原因,他最好用又不用錢 )
- SSH 連線軟體 ( MobaXterm、TabbyAlpha 都好用 )
- 可以收信的電子郵件
作業環境
- Windows 11 Professional
- MobaXterm v21.5
- ubuntu 20.04
實際操作
安裝順序
系統準備 > Apache > MariaDB > PHP > WordPress > Let’s Encrypt SSL
系統準備
//切換權限至 root user
$ sudo su
//更新套件
$ sudo apt-get update && sudo apt-get upgrade
//清理套件安裝包
$ sudo apt-get clean
//清除舊版本檔案
$ sudo apt-get autoremove
安裝與啟動 Apache2
//安裝 Apache2
$ sudo apt install apache2
//啟動、啟用 Apache2 服務
$ sudo systemctl start apache2.service
$ sudo systemctl enable apache2.service
//如果要停止 Apache2 服務
$ sudo systemctl stop apache2.service
到這邊為止,Apache2 應該已經安裝完成,這時候可以連到 https://your-vps-public-ip 來檢查阿帕契運作狀態,應該如下 :
看的到這個頁面就代表你應該沒有翻車
安裝 MariaDB
為什麼用 MariaDB 而不是 MySQL
MariaDB 更新比較頻繁,而且相容性也很強 ( 它是 MySQL 的 fork ),所以用它
//安裝 MariaDB
$ sudo apt install mariadb-server
$ sudo apt install mariadb-client
//啟動、啟動 MariaDB 服務
$ sudo systemctl start mariadb.service
$ sudo systemctl enable mariadb.service
//如果要停止 MariaDB 服務
$ sudo systemctl stop mariadb.service
如果安裝過程中沒有顯示權限設定的輸入畫面則進行以下操作 :
$ sudo mysql_secure_installation
//接下來會跑出下面的文字,照著操作
If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none): PRESS ENTER
Switch to unix_socket authentication [Y/n] n
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
All done!
//檢查是否設定完成
$ sudo mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 52
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
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)]> EXIT;
Bye
到這邊 MariaDB 就安裝好了
安裝 PHP
//由於 ubuntu 中 PHP 版本只到 7.4 因此需要手動加入第三方 repo
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
# 安裝 PHP 8.1
$ sudo apt install php8.1 php8.1-common php8.1-mysql php8.1-gmp php8.1-curl php8.1-intl php8.1-mbstring php8.1-xmlrpc php8.1-gd php8.1-xml php8.1-cli php8.1-zip php8.1-fpm
//更改 PHP 的配置文件
$ sudo nano /etc/php/8.1/apache2/php.ini
//Ctrl+W, 輸入 file_uploads 找到目標區塊
//並且將內容替換如下 ( 開頭是 ";" 的部分是註解,預設應該只有 file_uploads = on 有開著
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = Asia/Taipei
//清除舊版本 PHP ( 以 PHP 8.0 為例)
$ sudo apt purge '^php8.0.*'
//指定 Apache2 啟動特定版本 PHP
$ sudo a2enmod php8.1
$ systemctl restart apache2
建立 WordPress 用的數據庫
# 進入 MariaDB
$ sudo mysql -u root -p
# 創建名為 "wpdb" 的資料庫
CREATE DATABASE wpdb;
# 設定資料庫的使用者並設定密碼 ( 僅更改 new_password_here )
CREATE USER 'wpdbuser'@'localhost' IDENTIFIED BY 'new_password_here';
# 為資料庫使用者設定權限
GRANT ALL ON wpdb.* TO 'wpdbuser'@'localhost' WITH GRANT OPTION;
# 重新整理資料庫並退出
FLUSH PRIVILEGES;
EXIT;
下載 WordPress
//移動至 /tmp
$ cd /tmp
//從 WordPress 官網下載最新版本與解壓縮
$ wget https://wordpress.org/latest.tar.gz
$ tar -xvzf latest.tar.gz
//將 WordPress 檔案移動到目標資料匣
$ sudo mv wordpress /var/www/wordpress
//設定權限
$ sudo chown -R www-data:www-data /var/www/wordpress/
$ sudo chmod -R 755 /var/www/wordpress/
設定 Apache2 與 WordPress
//開啟設定文件
$ sudo nano /etc/apache2/sites-available/wordpress.conf
把以下內容貼入、更改後保存
//將 example.com、www.example.come 改成自己的域名
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/wordpress
<Directory /var/www/wordpress/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory "/var/www/html/wordpress">
AllowOverride All
</Directory>
重啟 Apache2 :
$ sudo a2ensite wordpress.conf
$ sudo systemctl restart apache2.service
設定 Oracle iptable
為什麼有這個不常見的步驟?
因為翻車之神 Hiraku 已經開釋了 [1]
想了解詳細的請移駕過去看,總之一切都是 Oracle 的鍋
//切換至 root
$ sudo su
//更改密碼避免防火牆設定更改後進不去
$ passwd
//打開需要的 port
$ ufw allow 22 # SSH port
$ ufw allow 80 # http port
$ ufw allow 443 # https port
//啟動防火牆
$ ufw enable
//將 iptable 解除安裝並重新安裝 ufw
$ apt-get remove iptables
$ apt-get install ufw
//重啟 VPS 後檢查狀態,如果顯示 "active" 下面步驟不用執行
$ ufw status
//如果 ufw 沒有在 "active" 狀態下的話手動啟用
$ ufw enable
//再度重啟 VPS
//在自己電腦上檢查 VPS 的 port 是否正確開啟了
$ nmap -v -p 80 你VPS的IP
設定 Let’s Encrypt SSL 加密
詳細作法請參考 “How to Setup Let’s Encrypt on Ubuntu Linux with Apache [2] “
How to Setup Let’s Encrypt on Ubuntu Linux with Apache
//安裝 certbot
$ sudo apt-get install certbot python3-certbot-apache -y
//開始進行申請
$ certbot --apache -d example.com
完成後重新啟動 Apache2 :
$ sudo systemctl reload apache2
最後步驟
到這邊環境就已經安裝完了,之後去 https://example.com 把 WordPress 內的設定跑完就可以了
參考資料
- Oracle Cloud Ubuntu 不用預設 iptables 的方法 By Hiraku : https://hiraku.tw/2020/04/6171/
- How to Setup Let’s Encrypt on Ubuntu Linux with Apache : https://websiteforstudents.com/how-to-setup-lets-encrypt-on-ubuntu-linux-with-apache/