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…

What’s new in the new PHP 8.3 released?

PHP 8.3 is released on 23rd November 2023. As usual, it has been gone through the 6 months pre-release phase and eventually RFC is pushed. The PHP team has released PHP 8.3 today with typed class constants, a json_validate() function, dynamically fetching a class constant, the #[Override] attribute, and more: Typed class constants rfc You can now typehint class constants: In PHP 8.2, it was still not possible to declare constant (const) types, which can lead to confusion or implications about the type you’re working with: Here’s an example of how typed constants look in PHP 8.3: Readonly amendments rfc This RFC proposed two changes, only one was accepted: being able to reinitialize readonly properties while cloning. That might sound like a very big deal, but this RFC only addresses a very specific (but important) edge case: overwriting property values within __clone(), in order to allow deep cloning readonly properties. #[Override] attribute rfc The new #[Override] attribute is used to show a programmer’s intent. It basically says “I know this method is overriding a parent method. If that would ever change, please let me know”. Here’s an example: Now, let’s imagine at one point the parent method changes its method name: Thanks to the #[Override] attribute, PHP will be able to detect that Child::methodWithDefaultImplementation() doesn’t override anything anymore, and it will throw an error. By adding the #[\Override] attribute to a method, PHP will ensure that a method with the same name exists in a parent class or in an implemented interface. Adding the attribute makes it clear that overriding a parent method is intentional and simplifies refactoring, because the removal of an overridden parent method will be detected. Deep-cloning of readonly properties rfc Before PHP 8.3 New in PHP 8.3 readonly properties may now be modified once within the magic __clone method to enable deep-cloning of readonly properties. New json_validate() function rfc doc Before PHP 8.3 To validate JSON in PHP, you…

預先看看還未推出的 Laravel 11 新功能

Laravel 現時的最新版本是 10,於 2023 年 2 月 14 日推出,Bug Fixes 支援到 2024 年的 8 月。 Laravel 11 預定要到 2024 年第一季才會發佈,根據過往版本發佈時間,估計將於 2 月初。儘管 Laravel 11 還未發佈,一些新功能已經公開,而且 Taylor 在他的 Laracon 主題演講中也介紹了一些重大的新改進: 以下會介紹並分析這些重點更新: 精簡的目錄結構 到目前為止,這些只是 beta 預覽版。這些功能可能會再變動,但就目前來看,這是我們可預計的⋯ Controller 預設不再用 extends 任何東西 當然你還是可以自行選擇加上自己的 Base Controller。 在此之前的版本,所有新加的 Controller 都需要 extends App\Http\Controllers\Controller,當中會: 沒有載入這兩個 traits 就等於沒有 $this->can() $this->authorize() $this->validate(),估計新版本會建議按需要自行加入,或者使用 facade、dependency injection 方式達到同相效果。 不再有 app/Http/Middleware 目錄 Laravel 包含九個 middlewares,但有很多你可能永遠不會去自訂。 根據我的經驗確實幾乎都不會去修改這些檔案,只有曾經有特定的登入流程而修改 RedirectIfAuthenticated 和特定的 Cookies 需求而修改 EncryptCookies。 然而,在新版本如果你確實想要自訂它們,那就移到 App/ServiceProvider,例如: 不再有 Http/Kernel 大部分之前能在 Kernel 裡做的事,現在可以在 Bootstrap/App 裏做。 目前 Http/Kernel 的主要作是,就是定義使用哪些 middleware 和次序,估計相關的 code 只是 copy & paste 轉移到 Http/Kernel 內。 或者你也可以在 route 內設定 middleware。 Model casts 改變 Model casts 改為 method 方式定義,不再是用屬性。這個改動方便了我們做其他事情,例如直接從 casts 調用其他方法,甚麼加入簡單的邏輯判斷。以下是一個使用新的 Laravel 11 AsEnumCollection 的範例: 在先前的版本我就確實遇過這個情況,受 PHP 限制而未能從其他 class 調用 method。 Config 改變 Laravel 有很多 config 檔案,而 Laravel 11 會移除這些,所有的 config 選項都會繼承下來。.env 會延伸包括所有你想設定的選項。 新增了一個新的 config:publish 命令,可以放入任何你想要的 config。另外亦有一個新的 cascade feature,允許你移除不想自訂的選項。 在以往版本,如果我需要新增一個自定義的 config 值,我會先把 config 放入一行新的 .env,然後再在 config/services.php 新增相關 array element。新改動似乎採用自動載入功能,可以減少第二步,方便開發流程。 精簡的預設 Migrations 當你開始一個新的 Laravel 應用程式,它會帶有一些 2014 年和 2019 年的預設 migrations。這些現在將會移除日期並整合到只有兩個檔案。 這個命名是根據 YYYY_MM_DD_HHMMSS_action_table_table_description 格式,方便 migration 時判斷次序。 提到檔名「將會移除日期」,但我相信只適用於預設的檔案。因為沒有日期就沒有辦法去判斷 migration 次序了,所以除 create 以外(如沒有 key dependency 不須要理會次序),自行建立的 migration 檔案也會建議加入時間格式在檔名開頭。 Routes 改變 預設情況下,只會有兩個路由檔案,console.php 和 web.php。新版本可以自行透過 php artisan install:api 建立 API route 和 Laravel Sanctum。 Websocket broadcast 的情況也是一樣,可以自行透過 php artisan install:broadcasting 建立。 畢竟這兩個 route 並非每個系統也必須要用到的,尤其是 API route,每次設定 Laravel 我也會手動把內容清空,以免產生 backdoor。 移除 Console Kernel Console Kernel 將被移除,你可以直接在 routes/console.php 中定義你的 console commands。 相信只是轉移了地方,統一放在 routes/ 目錄內,而寫法應該維持相同。這個版本似是針對減輕開發者對所謂 Kernel 的依賴,或許能夠令 Laravel 新手更易理解 framework 運作。 只支援 PHP 8.2+ Laravel 11 需要最少 PHP 8.2 才能運行,這仍然是一個早期的決定。如果你正在運行舊版的 PHP,現在是一個好時機進行升級。 我亦曾有因為 Laravel 對 PHP 版本的最低要求,迫使我大陣仗地升級 Server 的 PHP 版本,對安全性來絕對是件好事。 總結 到目前為止,所有這些功能都被視為 Laravel 11 beta 版本,有機會出現在最終版本,也可能會改變。根據作者所指,新設計的目的都是改善開發者的工作流程。當新的功能公佈時,我會再緊貼更新。

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…

Install Supervisor on Centos 7 for Laravel

Supervisor is a client/server system that allows its users to monitor and control several processes on UNIX-like operating systems. It is used to control processes related to a project or a customer and is meant to start like any other program at boot time. Features of Supervisor includes simple, centralized, efficient, extensible, compatible and proven. The main components of the Supervisor are: supervisord: This is the server piece of the supervisor is named supervisord. It is responsible for starting child programs at its invocation, responding to commands from clients, restarting crashed or exited subprocesses, logging its subprocess stdout and stderr output, and generating and handling “events” corresponding to points in subprocess lifetimes. supervisorctl: The command-line client piece of the supervisor is named supervisorctl. It provides a shell-like interface to the features offered by supervisord. In this guide, we will install the latest version of supervisor on CentOS 7 Deploying your cloud serverIf you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can quickly deploy your cloud servers. Once you have signed up, log into your Cloudwafer Client Area with the password provided in your mail and deploy your Cloudwafer cloud server. Updating System PackagesIt is recommended that you update the system to the latest packages before beginning any significant installations. Issue the command below: Step 1: Install EPEL RepositoryWe are going to install the EPEL Repository as Supervisor is not available in the default CentOS 7 Repository. Issue the command below to install: Step 2: Update and Install SupervisorAfter adding the EPEL Repository, we need to update our system packages before proceeding to install the Supervisor package using the command below : Then start and enable the supervisord daemon to start on boot using the commands below: Issue the command below to check the status of the…

Set Alias of php in Directadmin SSH

nano .bash_aliases alias php /usr/local/php71/bin/phpalias php=/usr/local/php80/bin/phpand add composer dont use: alias php=’/usr/local/php72/bin/php72′ . ~/.profile https://www.raspberrypi.com/documentation/computers/using_linux.html#the-bash_aliases-file The .bash_aliases File .bashrc also contains a reference to a .bash_aliases file, which does not exist by default. You can add it to provide a handy way of keeping all your aliases in a separate file. if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi The if statement here checks the file exists before including it. Install if not work due to php proc_open php -d "disable_functions=" /usr/local/bin/composer create-project --prefer-dist laravel/laravel v1 and for Composer with different PHP version: composer -vvv about This might help some… composer is likely to be using /usr/bin/php, consider the following:- As you can see our hosting has an alias to ensure the configured version of php (for webserver) is used on command line. But composer is configured to use /usr/bin/php. The following is a workaround for the above circumstance. Update .bash_aliases file Once logged out of terminal and logged back in… composer is now using the correct php version.