Optimizing PHP 8.3 on Ubuntu 24.04: Don’t Let Your Server Run Like a Snail

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

Common Mistakes: The ‘Quick and Dirty’ PHP Installation

Do you just type sudo apt install php and wait? You might be making things harder for yourself. This method often installs an outdated PHP version from default repositories along with mod_php – a ‘legacy’ setup that consumes massive amounts of RAM because it forces the Web Server to load the entire PHP core into every Apache process. For a more efficient setup, consider installing and using Nala for package management.

I once handled a VPS with 4GB of RAM that crashed whenever 20 users visited simultaneously. The issue wasn’t the code, but PHP running in default mode without caching and with very poor process management. After switching to PHP-FPM and optimizing, that same server handled 5x the load while the CPU stayed ‘cool’—a must for high-traffic websites.

Why is Your Server Running Slowly?

There are three silent ‘killers’ draining your system’s performance:

  • Legacy processing: Running PHP as a bundled module instead of a dedicated, professional PHP-FPM service.
  • Wasted CPU resources: PHP is an interpreted language. Without OPcache, the server must recompile code for every request, wasting about 30-50% of processing power.
  • Environment conflicts: Improperly installing Composer leads to constant ‘permission denied’ errors when updating libraries.

Choosing a Repository: Don’t Use the Defaults

On Ubuntu 24.04, you have two main paths:

  1. Default Repo: Stable but limited in versions and slow to update.
  2. Ondřej Surý’s PPA: This is a must-have for professionals. Ondřej directly maintains PHP packages for Debian. Using this PPA allows you to install anything from PHP 5.6 to 8.3 with a single command, though you can safely remove PPAs if you need to revert to defaults.

Optimized PHP 8.3 Installation Workflow for Production

Here are the steps I use to build a high-performance environment for Laravel, WordPress, or Symfony.

Step 1: Add the Ondřej Surý PPA

First, update your system and add the repository to get the latest PHP 8.3 version.

sudo apt update && sudo apt upgrade -y
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Step 2: Install PHP 8.3 and Essential Extensions

Don’t install a generic php package. Install php8.3-fpm specifically to decouple PHP processing from the Web Server.

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

Note: The bcmath and intl extensions are mandatory if you are working with Laravel or payment systems requiring high arithmetic precision.

Step 3: Fine-tuning PHP-FPM for High Load

Default configurations are often too conservative. To fully utilize your RAM, open the pool configuration file:

sudo nano /etc/php/8.3/fpm/pool.d/www.conf

For a VPS with 2GB – 4GB of RAM, you can refer to these parameters:

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500

Pro tip: Each PHP-FPM process typically consumes 30-50MB of RAM. A safe formula for max_children is: (Total RAM – 1GB for system) / 50MB.

Step 4: Enable OPcache – Boosting Response Times

This is the most crucial step for reducing TTFB (Time to First Byte) and helping boost system speed. Open the php.ini file:

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

Find and edit the parameters in the [opcache] section (remember to remove the ; at the beginning of the lines):

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0 # Set to 1 if you are in a dev environment

Then, restart the service to apply the changes:

sudo systemctl restart php8.3-fpm

Step 5: Proper Composer 2.x Installation

Forget the apt install composer command as it often installs an ancient version. Use the official script to ensure you have the latest 2.x version, which downloads libraries up to 3 times faster than the old version.

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer

Hardening Security After Installation

Don’t overlook these two small but vital tasks as part of Automating Ubuntu Server Hardening:

  1. Prevent information leaks: Change expose_php = On to Off in php.ini. This prevents hackers from identifying your specific PHP version to exploit vulnerabilities.
  2. Tighten permissions: Ensure PHP-FPM runs only under the www-data user. Never run PHP with root privileges.

Optimizing PHP not only makes your website smoother but also saves you a significant amount on monthly VPS costs. If you encounter a 502 Bad Gateway error after configuration, check the log files in /var/log/php8.3-fpm.log. Good luck!

Share: