Amazon Linux 2023 (AL2023) is the latest version of Amazon Linux, providing a modern and secure environment for running applications. If you’re looking to set up PHP 8.4 on AL2023, follow this step-by-step guide to get started.
Table of Contents
Prerequisites
- Amazon Linux 2023 Instance: Ensure you have an AL2023 instance running on AWS EC2.
- SSH Access: Have SSH access to the instance with the necessary permissions.
- Root or Sudo Privileges: Ensure you have root access or are a sudo user.
Steps to Install PHP 8.4
Step 1: Update the System
Before installing PHP, ensure your system is up-to-date.
sudo dnf update -y
Step 2: Enable EPEL Repository
The Extra Packages for Enterprise Linux (EPEL) repository is essential for accessing additional packages.
sudo amazon-linux-extras enable epel
sudo dnf install epel-release -y
Step 3: Install Remi Repository
The Remi repository is the preferred source for the latest PHP versions.
- Install the Remi repository:
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm -y
2. Enable the Remi repository:
sudo dnf module enable php:remi-8.4 -y
Step 4: Install PHP 8.4
Now install PHP 8.4 along with commonly used extensions.
sudo dnf install php php-cli php-fpm php-mysqlnd php-xml php-common php-json php-mbstring php-opcache php-gd -y
Step 5: Verify the PHP Installation
Check the installed PHP version to ensure PHP 8.4 is properly set up.
php -v
You should see an output similar to:
PHP 8.4.x (cli) (built: YYYY-MM-DD) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.4.x, Copyright (c) Zend Technologies
Step 6: Configure PHP
Edit the PHP configuration file (php.ini
) if necessary. The file is usually located at:
sudo nano /etc/php.ini
Make changes such as increasing the memory_limit
, setting the upload_max_filesize
, or configuring timezone
.
Example:
memory_limit = 256M
upload_max_filesize = 50M
date.timezone = UTC
Step 7: Configure PHP-FPM (Optional)
If you plan to use PHP-FPM for serving PHP applications, configure its settings:
Edit the PHP-FPM configuration file:
sudo nano /etc/php-fpm.d/www.conf
Look for the user and group settings and adjust as needed:
user = nginx
group = nginx
Start and enable the PHP-FPM service:
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
Step 8: Test PHP with a Web Server
Set up a web server like Apache or Nginx to serve PHP files.
Install Nginx:
sudo dnf install nginx -y
Configure Nginx to Use PHP:
Edit the default Nginx server block:
sudo nano /etc/nginx/conf.d/default.conf
Add the following lines to the server
block:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Restart Nginx:
Step 9: Test Your Setup
Create a sample info.php
file to test PHP processing:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/info.php
Visit the URL http://<your-instance-ip>/info.php
in your browser. You should see the PHP info page.
Conclusion
You’ve successfully set up PHP 8.4 on Amazon Linux 2023! You can now build and deploy your PHP applications. Make sure to secure your instance by removing the info.php
file and following AWS best practices for security.
For questions or further customization, drop a comment below!