To install PHP 8.4 on a Linux, Nginx, and MySQL setup on an Ubuntu 24.04 Server, follow these steps:
1. Update and Upgrade Your System
Ensure your system is up to date before proceeding:
sudo apt update && sudo apt upgrade -y
2. Add the Ondřej PHP Repository
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
3. Install PHP 8.4
-
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 -vYou should see output showing PHP 8.4.x as the installed version.
4. Configure PHP-FPM for Nginx
-
Ensure PHP-FPM is running:
sudo systemctl status php8.4-fpmIf 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
5. Test PHP Functionality
-
Create a PHP info file to test:
echo "<?php phpinfo();" | sudo tee /var/www/your-site/info.php -
Access
http://your-domain.com/info.phpin your browser. You should see the PHP information page showing PHP 8.4.
6. Secure Your Installation
-
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.iniCommon adjustments:
memory_limit = 256Mupload_max_filesize = 50Mpost_max_size = 50Mmax_execution_time = 300
-
Restart PHP-FPM:
sudo systemctl restart php8.4-fpm
7. Install and Secure MySQL
If MySQL isn't already installed:
sudo apt install mysql-server -y
Secure the installation:
sudo mysql_secure_installation
8. Connect PHP to MySQL
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.phpand load it in your browser.
9. Install Additional Tools (Optional)
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! 🎉