To install PHP 8.4 on a Linux, Nginx, and MySQL setup on an Ubuntu 24.04 Server, follow these steps:
Ensure your system is up to date before proceeding:
sudo apt update && sudo apt upgrade -y
Ubuntu typically does not include the latest PHP versions in its default repositories. Use the Ondřej Surý repository, which provides the latest PHP packages.
Install required dependencies:
sudo apt install software-properties-common -y
Add the Ondřej PHP PPA:
sudo add-apt-repository ppa:ondrej/php
Update your package list:
sudo apt update
Install PHP 8.4 along with commonly used extensions:
sudo apt install php8.4 php8.4-fpm php8.4-cli php8.4-mysql php8.4-curl php8.4-xml php8.4-mbstring php8.4-zip php8.4-bcmath php8.4-soap php8.4-intl php8.4-readline -y
Verify the installation:
php -v
You should see output showing PHP 8.4.x as the installed version.
Ensure PHP-FPM is running:
sudo systemctl status php8.4-fpm
If not active, start it:
sudo systemctl start php8.4-fpm
Configure Nginx to use PHP-FPM. Open your Nginx site configuration file:
sudo nano /etc/nginx/sites-available/your-site.conf
Add the following block (or modify if already present):
server {
listen 80;
server_name your-domain.com;
root /var/www/your-site;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Test the Nginx configuration:
sudo nginx -t
Reload Nginx to apply changes:
sudo systemctl reload nginx
Create a PHP info file to test:
echo "<?php phpinfo();" | sudo tee /var/www/your-site/info.php
Access http://your-domain.com/info.php
in your browser. You should see the PHP information page showing PHP 8.4.
Remove the PHP info file after testing:
sudo rm /var/www/your-site/info.php
Adjust PHP configuration if needed:
sudo nano /etc/php/8.4/fpm/php.ini
Common adjustments:
memory_limit = 256M
upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300
Restart PHP-FPM:
sudo systemctl restart php8.4-fpm
If MySQL isn't already installed:
sudo apt install mysql-server -y
Secure the installation:
sudo mysql_secure_installation
Test your PHP-MySQL connection:
Create a test PHP file:
<?php
$conn = new mysqli("localhost", "username", "password", "database");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Save it in /var/www/your-site/test-db.php
and load it in your browser.
For development or debugging:
sudo apt install php8.4-xdebug php8.4-dev
Your Ubuntu 24.04 server is now ready with PHP 8.4, Nginx, and MySQL! 🎉