Posted in

How to Install PHP 8.3 on Ubuntu 24.04 (for Nginx and Apache)

How to Install PHP 8.3 on Ubuntu 24.04 (for Nginx and Apache)
How to Install PHP 8.3 on Ubuntu 24.04 (for Nginx and Apache)

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language, especially suited for web development and can be embedded into HTML. With the release of Ubuntu 24.04, developers often need to install the latest stable version of PHP, currently PHP 8.3, to take advantage of its new features, performance improvements, and security enhancements. This Tutorial will guide you through the process of installing PHP 8.3 on your Ubuntu 24.04 server, configuring it to work seamlessly with both Nginx and Apache web servers.

Before proceeding, ensure you have a non-root sudo user configured on your Ubuntu 24.04 system. It’s also recommended to update your package list to ensure you’re working with the latest available information:

sudo apt update && sudo apt upgrade -y

Step 1: Add Ondrej’s PHP PPA Repository

Ubuntu’s default repositories might not always contain the very latest PHP versions. To get PHP 8.3, we’ll use the reliable Ondrej PHP PPA (Personal Package Archive), which provides up-to-date PHP packages.

First, install the necessary software properties to manage PPAs:

sudo apt install software-properties-common -y

Now, add the Ondrej PHP PPA to your system:

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

After adding the PPA, update your package list again so that `apt` is aware of the new packages:

sudo apt update

Step 2: Install PHP 8.3 and Essential Extensions

With the PPA added, you can now install PHP 8.3. A typical web application requires several PHP extensions. We’ll install some common ones, including `php8.3-fpm` (FastCGI Process Manager for Nginx), `php8.3-cli` (Command Line Interface), and other widely used modules like `mysql`, `curl`, `gd`, `mbstring`, `xml`, and `zip`.

sudo apt install php8.3-fpm php8.3-cli php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip -y

You can add or remove extensions based on your application’s requirements. For example, if you need PostgreSQL support, you’d add `php8.3-pgsql`.

Step 3: Configure PHP 8.3 with Nginx

If you’re using Nginx, it doesn’t process PHP files natively. Instead, it passes PHP requests to PHP-FPM (FastCGI Process Manager). PHP-FPM runs as a service, listening for incoming requests.

Ensure PHP-FPM is running and enabled to start on boot:

sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
sudo systemctl status php8.3-fpm

Next, you need to configure your Nginx server block to process PHP files. Open your Nginx site configuration file (e.g., `/etc/nginx/sites-available/default` or your custom domain’s config):

sudo nano /etc/nginx/sites-available/default

Locate the `location ~ .php$` block. Uncomment or add the following lines to configure Nginx to pass PHP requests to PHP-FPM:

location ~ .php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Save and close the file. Test your Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, restart Nginx to apply the changes:

sudo systemctl restart nginx

To verify, create a `phpinfo.php` file in your web root (e.g., `/var/www/html/phpinfo.php`):

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

Now, open your web browser and navigate to `http://your_server_ip/phpinfo.php` (replace `your_server_ip` with your server’s actual IP address or domain). You should see the PHP 8.3 information page.

Step 4: Configure PHP 8.3 with Apache

If you’re using Apache, you can configure it to use PHP 8.3 either via `mod_php` (less common for modern setups, as it’s not FPM) or by integrating with PHP-FPM, similar to Nginx. We’ll focus on the more performant PHP-FPM integration for Apache.

First, ensure Apache is installed and running. Then, enable the `proxy_fcgi` module, which allows Apache to communicate with PHP-FPM:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-fpm
sudo systemctl restart apache2

The `a2enconf php8.3-fpm` command creates a configuration file (`/etc/apache2/conf-available/php8.3-fpm.conf`) that directs Apache to pass `.php` requests to the PHP 8.3 FPM socket.

To verify, create a `phpinfo.php` file in your web root (e.g., `/var/www/html/phpinfo.php`):

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

Now, open your web browser and navigate to `http://your_server_ip/phpinfo.php` (replace `your_server_ip` with your server’s actual IP address or domain). You should see the PHP 8.3 information page.

Step 5: Adjust PHP Configuration (Optional)

You might need to adjust PHP’s configuration settings, such as `upload_max_filesize`, `post_max_size`, `memory_limit`, or `max_execution_time`, based on your application’s needs. The main PHP 8.3 FPM configuration file is located at `/etc/php/8.3/fpm/php.ini`.

sudo nano /etc/php/8.3/fpm/php.ini

After making any changes, save the file and restart the PHP-FPM service for the changes to take effect:

sudo systemctl restart php8.3-fpm

Conclusion

You have successfully installed PHP 8.3 on your Ubuntu 24.04 server and configured it to work with either Nginx or Apache. This setup provides a robust and performant environment for hosting your PHP applications. Remember to regularly update your system and PHP packages to ensure security and access to the latest features.

Leave a Reply

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