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 能用的權限: 400 只限指定讀取,不能改動。 繼續下一步,Network Settings。選擇 Create security group 建立新的 rule。 同時剔選 這個 EC2 暫時不用 Load Balancer,所以用 Web Server 的概念,需要打開 HTTP & HTTPS,剔選: 背後會是以下這些 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…

Amazon EC2 升級為 IMDSv2

從2024年中起,AWS將對其新推出的Amazon EC2實例類型標準化使用最新版本的元數據服務(IMDSv2)。這項轉變旨在進一步加強安全措施,因為相比於先前版本的IMDSv1,IMDSv2引入了加強的安全功能來抵抗潛在的惡意攻擊,封堵可能的安全漏洞。 IMDSv1 安全問題 舊有 v1 中,可以直接問 IMDS 獲取 EC2 的相關資訊 若 EC2 instance 有綁定 IAM Role 的話,更可以獲取到機密級的 AWS Credential!! 然後就可以用這組 Credential 來登入 AWS CLI!! IMDSv2 新要求 EC2實例提供了一個從固定IP地址訪問IMDS的途徑,允許用戶檢索包括啟動實例所需的AMI ID、網絡設置以及臨時IAM憑據等一系列靜態和動態信息。IMDSv2與其前身的一大差異在於其使用了基於會話的溝通方式,這要求客戶端和服務端在交換數據前建立一個安全會話,這一過程中生成的令牌將用於後續的數據請求和回應。 AWS指出,雖然IMDSv1本身安全性已經很高,IMDSv2進一步增強了對四種主要安全風險的防護——這包括不當配置的開放式網站應用防火牆、反向代理漏洞、伺服器端請求偽造以及網絡層的配置錯誤——從而大大減少了未授權訪問EC2元數據的可能性。 自2019年推出IMDSv2以來,AWS於2023年3月發佈的Amazon Linux 2023已預設啟用IMDSv2。目前,通過控制台啟動的Quick Start以及所有Amazon及其合作夥伴的Quick Start AMI均已支持IMDSv2。 為了促進用戶轉向使用更安全的IMDSv2,AWS計畫於2024年2月推出新的API功能,使得用戶可以在賬戶級別控制IMDSv1的默認使用情況。當IMDSv2成為AWS管理控制台和其他接口的默認選項時,這些新API將使用戶能夠輕鬆地在賬戶級別禁用IMDSv1,進而輕鬆切換至IMDSv2,以提高安全性。 隨著2024年中期新EC2實例類型的推出,IMDSv2將成為預設的元數據服務版本,顯示AWS對於加強其雲平台安全性的持續承諾。即使如此,AWS仍提供靈活性給予用戶選擇,即在不需要重新啟動實例的情況下,於實例啟動期間或之後選擇啟用IMDSv1的選項,這確保了向後兼容性與過渡期的順暢性。 IMDSv2 實際用法 v2 與 v1 最大的不同之處,就是 v2 需要先生成一個 token,然後在後續的請求中傳送這個值作為認證,防止未經授權的訪問。 這項策略旨在鼓勵並促進用戶轉向更安全的IMDSv2,同時也為那些需要時間來適應新系統的用戶提供了足夠的彈性。通過這樣的漸進式轉變,AWS期望其用戶能在保證安全的前提下,充分利用雲計算資源。

AWS 的 IPv4 開始收費,要把 EC2 instance 改成 IPv6

AWS 較早前宣佈使用 IPv4 需要額外付款,目標鼓勵更多人轉用 IPv6。最近終於騰出時間,把 EC2 instance 指向 IPv6。 注意:AWS Free Tier 會有 750 小時免費使用 Public IPv4 當我把 EC2 instance 改成 IPv6,以及相關的 Apache/Nginx 也設定為 IPv6 後,發現 CloudFront 還未完全支援 IPv6 Origins,但又不能拿掉這個 Load Balancer⋯⋯看來這個動作還是沒辦法繼續,目前還是繼續付費用 IPv4(其實不是很貴),希望不久後 AWS 服務都對 IPv6 支援友善一點。 後記:CloudFlare 是可以支援 IPv6,有需要可以轉移過去。

2024 雲端流量費用解析

導言 在雲端服務的世界中,資料輸出(Data Egress)的費用是許多人容易忽略的一環。這篇文章提供了對2024年雲端流量費用的概覽,幫助讀者理解雲資料輸出的成本結構,並介紹了降低這些成本的方法。 雲端資料輸出是什麼? 資料輸出指的是將資料從雲端服務商的網絡發送到公共互聯網的過程。根據不同的雲服務提供商,超過免費額度的1TB資料輸出費用各不相同。 為什麼雲提供商會收取輸出費? 雲服務提供商收取資料輸出費是因為發送資料出網絡會產生成本。這些成本包括基礎設施和帶寬等費用。 如何降低資料輸出成本? 比較 Here’s what 1 TB of egress beyond the free allowance would cost you with each provider: Cloud Provider Free allowance 1 TB of egress overage Cloudflare — Free for most services Heroku 2 TB / mo per app Not publicly listed OVHcloud — Free and unlimited Scaleway — Free for most services Hetzner 20-60 TB / mo per instance $1.08 Linode 1-20 TB / mo per instance $5.00 Oracle Cloud 10 TB / mo $8.50 Backblaze 3x the amount of data stored $10.00 Bunny CDN — $10.00 Civo 1 – 12 TB / mo per instance $10.00 DigitalOcean 100 GB – 10 TB / mo per instance $10.00 Entrywan 1 – 16 TB / mo $10.00 UpCloud 500 GB – 24 TB / mo per instance $10.85 Vultr 2 TB / mo for most services $10.00 Fly.io 100 GB / mo $20.00 Koyeb 100 GB / mo $40.00 Alibaba Cloud 10 GB / mo $74.00 Microsoft Azure 100 GB / mo $78.30 Amazon Web Services 100 GB / mo $92.16 Railway — $100.00 Zeabur 10-100 GB, depends on plan $100.00 Google Cloud Depends on service $111.60 Render 100 GB – 1 TB, depends on plan $300.00 Vercel 100 GB – 1 TB, depends on plan $400.00 Netlify 100 GB – 1 TB, depends on plan $550.00 Source: Data Egress: What is it and how much does it cost? (https://getdeploying.com/reference/data-egress) 小工具:您可以使用這工具來比較各雲端的流量費用。 結論 了解雲端資料輸出的成本及其計算方式對於有效管理雲計算成本至關重要。選擇適合自己使用需求和預算的雲服務提供商,並採取策略降低額外費用,是每位開發人員和企業必須考慮的。

Amazon EC2 names explained

Amazon Elastic Compute Cloud (EC2) has over 100 unique instance types with more than 600 size combinations. Picking the right instance type can be a difficult task for even the most seasoned engineer. Running an application on the largest, fastest instance you can afford does not guarantee the best performance. If you are going to setup AWS EC2 instances, you can take look at this friendly guideline – 2024 年最新 AWS EC2 + Web Server 建立【逐步圖文教學】 Or if you are looking at setting up Amazon Linux 2, the another article “How To Install PHP 8.0 on Amazon Linux 2” could help you a lot. If you’ve ever wanted to understand instance type names or need help picking the right instance for your application you’ve found the right article. Here’s a moving picture version of this blog post. And here’s a static picture version of the same information. A quick disclaimer, I work at Amazon but this is my personal blog reflecting my personal opinion. All of the information was gathered from public sources and from my experience. I wrote this because I wished I had it as a guide when I was first learning EC2. Instance names are made up of four sections. So how do you decipher an instance with the name r7iz.8xlarge? From the info above we know r is from the memory family which has more memory per vCPU than general purpose. Specifically it has 8 GiB of memory for every 1 vCPU. 7 means it’s a 7th generation instancy type. The iz capabilities has a Intel processor (i) running at turbo frequencies (z), and 8xlarge is 8 times bigger than a r7iz.xlarge. Many capabilities aren’t listed in the name because some families include capabilities by default. For example, the m family are all EBS and network optimized by default which is usually denoted with the b and n capabilities. Some of the larger m instances…

PHP Create File Permission & umask()

在開發 PHP 程式時,習慣將 Apache 或 PHP-FPM 的群組,與使用者共用 但二者預設新增檔案的權限,經常是 755 也就是說,群組沒有權限異動檔案,在開發時較不方便 以下就是修改預設檔案權限方式。 umask() 用來做檔案權限的遮罩 (限制權限) PHP 預設 umask 是 022,當建立檔案或資料夾權限給 777 時,最終的權限會是 777 – 022 = 755 如下 PHP 語句 若要調整權限,以下有 3 個方式 1. Apache 可以更改預設 umask,修改後重啟 Restart Apache 才會生效,如下 Linux 指令 2.PHP 寫入檔案前,執行 umask(0) 3.PHP 寫入檔案後,使用 chmod() PHP-FPM (NGINX) 修改檔案路徑:`/lib/systemd/system/php-fpm.service` 加上 UMASK 設定後,如下 重啟 php-fpm 服務

How To Install PHP 8.0 on Amazon Linux 2

In this article, we will go through the setup process of Amazon Linux 2. Amazon Linux 2023 (AL2023) is the latest available version. If you are going to setup it, you can take look at this friendly guideline – 2024 年最新 AWS EC2 + Web Server 建立【逐步圖文教學】. And another article “Amazon EC2 names explained” can help you to understand the instance types easily. I spent some time experimenting around, and I got the following to work for me so that I could install PHP 8.0, Apache and MariaDB on an Amazon Linux 2 AMI EC2 instance: PHP 8.0 is a major release of the PHP programming Language mostly used in the Development of Web Applications. It is the stable release and you can plan to migrate your applications but being aware that some could break especially where old features are deprecated. In this article we will cover the installation of PHP 8 on Amazon Linux 2 server running in the Cloud or On-prem infrastructure. Update Amazon Linux 2 system Update the system before starting the installation of PHP 8 on Amazon Linux 2 system. Linux updates often comes with newer kernel patches and reboot is required for the system to run in this new release. Add EPEL and Remi Repositories The packages and dependencies that will be required in the installation of PHP 8 on Amazon Linux 2 server are available in the EPEL and Remi repositories. The two RPM repositories need to be added manually by running the commands below in the terminal. Update Yum index to confirm the two repositories are working. Install PHP 8.0 on Amazon Linux 2 Install the yum-utils package which provides yum-config-manager command line tool. Method 1: Install PHP 8.0 from OS repositories Let’s confirm that PHP 8.0 topic is available in our Amazon Linux 2 machine: We can…