How to Install PHP 8.3 on an EC2 Ubuntu 22.04 LTS Instance

Deploying PHP 8.3 on your Amazon EC2 instance with Ubuntu 22.04 LTS is a straightforward process. Here we’ll cover both setups for Apache and Nginx.

What to do

  1. Launch an EC2 instance with Ubuntu 22.04 LTS.
  2. Connect to your EC2 instance using SSH.
  3. Update package lists and upgrade existing packages.
  4. Add the ondrej/php repository to your package manager.
  5. Install PHP 8.3 and necessary extensions.
  6. Verify the PHP installation.
  7. Optionally, configure PHP settings according to your requirements.
  8. Restart the Apache or Nginx web server.
  9. Test PHP functionality to ensure everything is working correctly.

Step 1: Connect to Your EC2 Instance

First, securely connect to your EC2 instance via SSH:

ssh -i /path/to/your-key.pem ubuntu@your-ec2-ip

Replace /path/to/your-key.pem with the actual path to your PEM file and your-ec2-ip with the public IP of your EC2 instance.

Step 2: Add the PHP Repository

Add the ppa:ondrej/php repository to get the latest PHP versions:

sudo add-apt-repository ppa:ondrej/php -y

Step 3: Install PHP 8.3 and Common Extensions

Install PHP 8.3 along with popularly used extensions:

sudo apt install php8.3-cli php8.3-common php8.3-mbstring php8.3-gd php8.3-intl php8.3-xml php8.3-mysql php8.3-zip php8.3-curl php8.3-tidy php8.3-imagick -y

Step 4: Configure PHP 8.3 for Apache

If you’re using Apache as your web server, install the following package:

sudo apt install libapache2-mod-php8.3

Once installed, restart Apache to apply changes:

sudo systemctl restart apache2

To enable PHP 8.3, you may need to disable the previous version (if any) and enable the new one:

sudo a2dismod php7.x # Replace x with your specific version
sudo a2enmod php8.3
sudo systemctl restart apache2

Step 5: Install PHP 8.3 FPM for Nginx

For those who prefer Nginx, you should install PHP-FPM (FastCGI Process Manager):

sudo apt install php8.3-fpm -y

After installing, you need to configure Nginx to use PHP-FPM. Edit your site’s configuration file in /etc/nginx/sites-available:

server {
    # ... other code ...

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    # ... other code ...
}

Make sure to replace /run/php/php8.3-fpm.sock with the correct path if it differs.

Reload Nginx to implement your changes:

sudo systemctl reload nginx

Step 6: Verifying PHP 8.3 Installation

Check if PHP 8.3 is correctly installed and configured:

php -v

Expect output like this:

PHP 8.3.4 (cli) (built: Mar 16 2024 08:40:08) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.4, Copyright (c) Zend Technologies
with Zend OPcache v8.3.4, Copyright (c), by Zend Technologies

And that’s it! You have successfully installed PHP 8.3 on your AWS EC2 Ubuntu 22.04 LTS instance and configured it to work with either Apache or Nginx.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.