Skip to content

2024 年最新 AWS EC2 + Web Server 建立【逐步圖文教學】

這篇文章記錄了我在 AWS 建立一個新的 EC2 instance + Web Server,提供最簡單直接的步驟流程,避開中伏位,分享經驗小 tips,讓大家可以有個參考。

目標:Amazon Linux 2023 + Apache + PHP

若是想找 Amazon Linux 2 (AL2) 的教學,可以瀏覽這篇 How To Install PHP 8.0 on Amazon Linux 2

建立 AWS EC2

名字隨便填一個就可以,主要方便與其他 instance 辨別。

AMI 選擇 Amazon Linux 2023 AMI,已經是目前最新,沒有 2024 這回事。

今天 2024-03-22 用的是 Amazon Linux 2023 AMI 2023.4.20240319.1 arm64 HVM kernel-6.1

Architecture 選擇 64-bit (Arm),架構比較省電,而下一步可以選擇 t4g 系統。

要想了解t4g 這些是甚麼意思,可以瀏覽這篇 Amazon EC2 names explained

Instance type 選擇 t4g.small,如果測試用可以考慮 t4g.nano 或者 t4g.micro,可以注意一下 vCPU、Memory、價錢分別。後續需要安裝 Web Server,所以我選擇 2 GiB Memory 的 t4g.small

然後,建立一個新 Key pair (best practice 不要重用其他的 key),用於之後 SSH 登入。

Key pair name 輸入一個 file name。

Key pair type 按你個人喜好。一般來說,ED25519 的 algorithm 會比較安全,key size 比較少。

Private key file format 我用 .pem,如果是 windows 用 Putty 的話,用 .ppk 會比較方便。而 .pem.ppk 也是有方法去轉換的。

ED25519 is based on elliptic curve cryptography, which is considered more secure than RSA. The security of RSA is based on the difficulty of factoring large prime numbers, while the security of ED25519 is based on the difficulty of solving the elliptic curve discrete logarithm problem. The latter is generally believed to be harder than factoring large prime numbers, making ED25519 a more secure choice.

先把 key 保存到自己電腦,安全的保管好。同時也可以先把 key 設置 SSH 能用的權限:

chmod 0400 key.pem
Bash

400 只限指定讀取,不能改動。

繼續下一步,Network Settings。選擇 Create security group 建立新的 rule。

同時剔選

  • Allow SSH traffic from: My IP(那只有這個 IP 才能進去 SSH。無論是選 My IP 還是Anywhere,還是預設需要用上面建立的 key 才能成功登入 SSH)

這個 EC2 暫時不用 Load Balancer,所以用 Web Server 的概念,需要打開 HTTP & HTTPS,剔選:

  • Allow HTTPS traffic from the internet:
  • Allow HTTP traffic from the internet

背後會是以下這些 Rules:

然後下一歩,

Storage 預設 8 GB 太少了,我會改為 30 GB,方便之後存放 files。當然之後不夠也是可以向上 resize。

File System 用預設的 EFS 就 ok。其他都用預設值。

IP 可以留意一下選擇 IPv4 或者 IPv6。 可以參考這篇關於 AWS 對 IPv4 收費的文章

檢查一下 configuration 沒有錯的話,點擊「Launch instance」。

以前要等幾十秒,現在幾秒鐘就已經成功建立 instance 了⋯

可以見到 Instance 已經上線了,同時也可以登入 SSH。這個時候,AWS 也會正式開始對你計算收費。

經驗小提示!!

我會建議這個時候先 associate 一個 Elastic IP 給這個 Ec2。因為這 EC2 一旦重新開機,關機後 AWS 就會回收這個沒有在用的 IP,到開機時,AWS會再分配另一個 IP 給 EC2。這樣可能對你 DNS 設置 / SSH 登入不太方便。

在左邊選單選擇 Network & Security > Elastic IPs 頁面。

Allocate Elastic IP address 用預設值就足夠。

然後選剛剛新建立的 IP,右鍵打開選擇 Assoicate

Resource type 選擇 Instance,並揀選 ec2,其他可以留空。

這時候跳回 EC2 列表,你會看到剛剛建的 EC2 的 Elastic IP 變成新的 IP,這樣無論怎麼重新開機,才會保持同一個 IP。

建立 Web Server

使用任何一個方便你的 SSH 連線工作,我是用 macOS 的 Terminal zsh。

ssh ec2-user@IP_ADDRESS -i key.pem
Bash
  • 預設 EC2 的登入名稱是 ec2-user
  • IP_ADDRESS改為正確的
  • key.pem 可以用 absolute path

出現這個畫面代表正常進入了 EC2 SSH 了!

目前 instance 只有 2GB,已經可以預視到是不足夠之後使用。可以透過 Swapfile 的方法,以空間換取速度。或者用 partition 方式其實也一樣。

Decide swap space size

It’s a best practice that swap space is equal to two times the physical RAM, for up to 2 GB of physical RAM. For any amount greater than 2 GB, add an additional 1x physical RAM. It’s also a best practice that swap space is never less than 32 MB.

Amount of system RAMRecommended swap space
2 GiB of RAM or less2 times the amount of RAM but never less than 32 MB
More than 2 GiB of RAM but less than 64 GiB0.5 times the amount of RAM
More than 64 GiBDepends on the workload or use case

所以來個 4GB Swapfile 吧!

sudo dd if=/dev/zero of=/swapfile bs=512M count=8
Bash

設定好正確權限

sudo chmod 600 /swapfile
Bash

建立 Linux swap area

sudo mkswap /swapfile
Bash

啟用這個 swap

sudo swapon /swapfile
Bash

檢查是否正常運作

sudo swapon -s
Bash

設定就算重新開機後,swapfile也會自動運作,打開這個檔案:

sudo vi /etc/fstab
Bash

在結尾新一行加入這句:

/swapfile swap swap defaults 0 0
Bash

可以試試重新開機,然後用 free -h 去檢查是否有 swapfile 這行,就代表正常。

開始安裝 web server 必要的 modules

sudo dnf update -y
sudo dnf install -y httpd wget php8.2 php8.2-fpm php8.2-gd php8.2-pdo php8.2-zip php8.2-mbstring
Bash

靜心等待一切都跑完後,可以確認 PHP 版本是否正確:

php -v
Bash

然後設定 apache user

sudo usermod -a -G apache ec2-user
Bash

可以執行 groups 確認成功新增。

然後設定檔案的權限:

sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
Bash

然後為了讓 server 內的所 .htaccess 生效,會進行以下設定:

sudo nano /etc/httpd/conf/httpd.conf
Bash
將 <Directory "/var/www/html"> 內的 “AllowOverride None”
改為這句:
AllowOverride All

同時如果已經有 Domain 之類,可以在這刻設定好。

<VirtualHost *:80>
  ServerName YOUR_DOMAIN
  ServerAlias www.YOUR_DOMAIN
  DocumentRoot /var/www/html
  <Directory /var/www/html>
    AllowOverride All
  </Directory>
</VirtualHost>

<VirtualHost *:443>
  ServerName YOUR_DOMAIN
  ServerAlias www.YOUR_DOMAIN
  DocumentRoot /var/www/html
  SSLEngine on
  <Directory /var/www/html>
    AllowOverride All
  </Directory>
</VirtualHost>
Bash

最後還欠一步,就是啟動 HTTPD 了!

sudo systemctl start httpd
sudo systemctl enable httpd
Bash

大功告成!可以直接用 IP 作為網去瀏覽頁面!

技術疑難

  1. 如果無法成功啟動 HTTPD,可以輸入 systemctl status httpd.service 查看記錄。通常都是 configure file 格式錯誤。
  2. 如果遇到是 Syntax error,而指明行數是 SSLEngine on,則表示主機不支援 SSL。
    • 需要安裝 mod_ssl
      • sudo yum install mod_ssl
    • 如果你是用 CloudFlare 內的 Full (strict) 模式,可以到 SSL/TLS -> Origin Server 下載 Origin Certificate 並安裝。
      • 建議位置是
        1. /etc/pki/tls/certs/cloudflare/my.key
        2. /etc/pki/tls/certs/cloudflare/my.pem
      • 並在 conf 加上
        • SSLEngine on
          SSLCertificateFile "/etc/pki/tls/certs/cloudflare/my.pem"
          SSLCertificateKeyFile "/etc/pki/tls/certs/cloudflare/my.key"
      • 然後重啟 sudo systemctl restart httpd

經驗小提示!!

新版的 Amazon Linux 2023 及後沒有預設 cronie 這個套裝提供 crontab,取而代之是 systemd timers 方式,能夠更仔細地去控制。

The cronie package was installed by default on the AL2 AMI, providing support for the traditional crontab way of scheduling periodic tasks. In AL2023, cronie is not included by default. Therefore, support for crontab is no longer provided by default.

In AL2023, you can optionally install the cronie package to use classic cron jobs. We recommend that you migrate to systemd timers due to the added functionality provided by systemd.

It is possible that a future version of Amazon Linux, possibly the next major version, will no longer include support for classic cron jobs and complete the transition to systemd timers. We recommend that you migrate away from using cron.

Deprecated in AL2023 – Amazon Linux 2023 (https://docs.aws.amazon.com/linux/al2023/ug/deprecated-al2023.html)

不過若然你比較喜歡舊時 cronie 的操作方式,你仍是可以自行安裝的:

sudo yum install cronie -y
sudo systemctl enable crond.service
sudo systemctl start crond.service
Bash

結語

這篇文章只是一個記錄過程,只是很多設定方式的其中之一,提供最直接、最簡單的步驟流程,讓大家建議 EC2 instance + Web Server 時可以參考,希望能足以為讓你避開中伏位,如果對系統或者網站有任何疑問,歡迎隨時與我聯絡交流!

Leave a Reply

Your email address will not be published. Required fields are marked *