How to Install PHP8.4 on Ubuntu 24.04 in Minutes - Complete Guide - Techvblogs

How to Install PHP8.4 on Ubuntu 24.04 in Minutes - Complete Guide

Follow this step-by-step guide to install PHP8.4 on Ubuntu 24.04 and supercharge your web server.


Suresh Ramani - Author - Techvblogs
Suresh Ramani
 

1 day ago

TechvBlogs - Google News

Installing PHP8.4 Ubuntu 24.04 has become much easier thanks to improved package management and streamlined installation processes. This comprehensive guide shows you exactly how to install PHP8.4 on your Ubuntu 24.04 system quickly and efficiently.

Whether you’re setting up a development environment or preparing a production server, this tutorial covers everything you need to know about getting PHP8.4 Ubuntu 24.04 running smoothly. We’ll walk through multiple installation methods, common configurations, and troubleshooting tips.

Why Upgrade to PHP 8.4?

PHP 8.4 brings significant improvements that make upgrading worthwhile:

Performance Improvements

  • Faster execution: Up to 15% performance boost over PHP 8.3
  • Better memory management: Reduced memory usage for large applications
  • Optimized JIT compiler: Improved just-in-time compilation for better performance

New Features in PHP 8.4

  • Property hooks: Simplified getter/setter functionality
  • Asymmetric visibility: Better encapsulation with different visibility levels
  • New array functions: Enhanced array manipulation capabilities
  • Improved error handling: Better debugging and error reporting

Security Enhancements

  • Updated security patches: Latest vulnerability fixes
  • Improved password hashing: Enhanced security algorithms
  • Better input validation: Stricter data validation by default

Prerequisites for Installing PHP8.4 Ubuntu 24.04

Before we install PHP8.4 on your system, make sure you have:

  • Ubuntu 24.04 LTS (fresh installation recommended)
  • Terminal/command line access
  • Sudo privileges on your system
  • Internet connection for downloading packages
  • At least 1GB free disk space

Check Your Current System

First, let’s verify your Ubuntu version:

lsb_release -a

You should see output showing Ubuntu 24.04. If you’re running an older version, consider upgrading first for the best PHP8.4 Ubuntu 24.04 experience.

Method 1: Install PHP8.4 Using Official Ubuntu Repository

The easiest way to install PHP8.4 is through Ubuntu’s official repository. However, Ubuntu 24.04 might not have PHP 8.4 in the default repos yet, so we’ll use the Ondřej Surý PPA (Personal Package Archive).

Step 1: Update System Packages

Start by updating your package list:

sudo apt update && sudo apt upgrade -y

This ensures you have the latest package information and system updates.

Step 2: Install Required Dependencies

Install the necessary packages for adding PPAs:

sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https -y

These packages allow you to securely add external repositories to your system.

Step 3: Add Ondřej Surý PPA Repository

Add the official PHP PPA maintained by Ondřej Surý:

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

This repository is trusted by the PHP community and provides the latest PHP versions for Ubuntu systems.

Step 4: Update Package List Again

Refresh your package list to include the new repository:

sudo apt update

Step 5: Install PHP 8.4

Now you can install PHP8.4 with this simple command:

sudo apt install php8.4 -y

The installation process typically takes 2-3 minutes, depending on your internet connection.

Step 6: Verify PHP 8.4 Installation

Check if PHP8.4 Ubuntu 24.04 installation was successful:

php -v

You should see output similar to:

PHP 8.4.0 (cli) (built: Nov 21 2024 15:37:21) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.4.0, Copyright (c) Zend Technologies

Method 2: Install PHP8.4 with Common Extensions

For web development, you’ll need additional PHP extensions. Here’s how to install PHP8.4 with the most commonly used extensions:

Essential PHP Extensions

Install PHP 8.4 with essential extensions:

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

Web Server Extensions

If you’re running a web server, install these additional extensions:

sudo apt install php8.4-fpm php8.4-opcache php8.4-readline php8.4-json -y

Database Extensions

For database connectivity, install these extensions:

sudo apt install php8.4-mysql php8.4-pgsql php8.4-sqlite3 php8.4-mongodb -y

Development Extensions

For development environments, add these helpful extensions:

sudo apt install php8.4-xdebug php8.4-dev php8.4-xml php8.4-soap -y

Method 3: Installing PHP8.4 with Apache Web Server

If you need PHP8.4 Ubuntu 24.04 with Apache, follow these steps:

Step 1: Install Apache

First, install the Apache web server:

sudo apt install apache2 -y

Step 2: Install PHP 8.4 Apache Module

Install PHP 8.4 with Apache integration:

sudo apt install php8.4 libapache2-mod-php8.4 -y

Step 3: Enable PHP Module

Enable the PHP 8.4 module in Apache:

sudo a2enmod php8.4

Step 4: Restart Apache

Restart Apache to apply changes:

sudo systemctl restart apache2

Step 5: Test PHP Installation

Create a test PHP file:

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

Visit http://your-server-ip/test.php in your browser to see the PHP information page.

Method 4: Installing PHP8.4 with Nginx (PHP-FPM)

For PHP8.4 Ubuntu 24.04 with Nginx, use PHP-FPM (FastCGI Process Manager):

Step 1: Install Nginx

Install the Nginx web server:

sudo apt install nginx -y

Step 2: Install PHP-FPM

Install PHP 8.4 with FPM:

sudo apt install php8.4-fpm -y

Step 3: Configure Nginx

Edit the default Nginx configuration:

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

Add this configuration inside the server block:

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

Step 4: Test Nginx Configuration

Test the configuration:

sudo nginx -t

Step 5: Restart Services

Restart both Nginx and PHP-FPM:

sudo systemctl restart nginx
sudo systemctl restart php8.4-fpm

Configuring PHP 8.4 Settings

After installing PHP8.4 Ubuntu 24.04, you might want to optimize its configuration:

Finding PHP Configuration Files

Locate your PHP configuration files:

php --ini

The main configuration file is typically at /etc/php/8.4/cli/php.ini.

Common Configuration Changes

Increase Memory Limit

For larger applications, increase the memory limit:

sudo nano /etc/php/8.4/cli/php.ini

Find and change:

memory_limit = 256M

Upload File Size Limits

For file uploads, adjust these settings:

upload_max_filesize = 50M
post_max_size = 50M
max_execution_time = 300

Enable Error Reporting (Development Only)

For development environments:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL

Restart Web Server

After making changes, restart your web server:

# For Apache
sudo systemctl restart apache2

# For Nginx with PHP-FPM
sudo systemctl restart php8.4-fpm

Managing Multiple PHP Versions

Ubuntu allows you to install PHP8.4 alongside other PHP versions:

Install Multiple Versions

Install both PHP 8.3 and 8.4:

sudo apt install php8.3 php8.4 -y

Switch Between Versions

Use update-alternatives to switch PHP versions:

sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.3 83
sudo update-alternatives --install /usr/bin/php php /usr/bin/php8.4 84

Select Default Version

Choose your default PHP version:

sudo update-alternatives --config php

Installing Composer with PHP 8.4

Composer is essential for modern PHP development. Here’s how to install it with PHP8.4 Ubuntu 24.04:

Download and Install Composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer

Verify Composer Installation

composer --version

You should see Composer version information confirming it’s working with PHP 8.4.

Update Composer

Keep Composer updated:

sudo composer self-update

Common Issues and Troubleshooting

Issue 1: Package Not Found

ProblemPackage 'php8.4' has no installation candidate

Solution: Make sure you’ve added the PPA repository:

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

Issue 2: Extension Not Loading

Problem: PHP extensions not working after installation

Solution: Check if extensions are enabled:

php -m | grep extension_name

If missing, check configuration files:

php --ini

Issue 3: Permission Denied Errors

Problem: Web server can’t access PHP files

Solution: Check file permissions and ownership:

sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

Issue 4: PHP-FPM Not Starting

Problem: PHP-FPM service fails to start

Solution: Check the service status and logs:

sudo systemctl status php8.4-fpm
sudo journalctl -u php8.4-fpm

Security Best Practices for PHP8.4 Ubuntu 24.04

Disable Dangerous Functions

Edit your PHP configuration to disable potentially dangerous functions:

disable_functions = exec,passthru,shell_exec,system,proc_open,popen

Hide PHP Version

Prevent PHP version disclosure:

expose_php = Off

Session Security

Improve session security:

session.cookie_httponly = 1
session.cookie_secure = 1
session.use_strict_mode = 1

Keep PHP Updated

Regular updates are crucial for security:

sudo apt update && sudo apt upgrade php8.4*

Performance Optimization

Enable OPcache

OPcache significantly improves PHP performance:

sudo apt install php8.4-opcache -y

Configure OPcache in /etc/php/8.4/mods-available/opcache.ini:

opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60

Tune PHP-FPM

Optimize PHP-FPM settings in /etc/php/8.4/fpm/pool.d/www.conf:

pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Testing Your PHP8.4 Installation

Create a Test Script

Create a comprehensive test script:

sudo nano /var/www/html/phptest.php

Add this content:

<?php
echo "<h1>PHP 8.4 Installation Test</h1>\n";
echo "<p><strong>PHP Version:</strong> " . phpversion() . "</p>\n";
echo "<p><strong>Server API:</strong> " . php_sapi_name() . "</p>\n";

// Test some PHP 8.4 features
echo "<h2>Testing PHP 8.4 Features</h2>\n";

// Property hooks example (PHP 8.4 feature)
class Example {
    private string $_name;
    
    public string $name {
        get => $this->_name;
        set => $this->_name = ucfirst($value);
    }
}

$obj = new Example();
$obj->name = "test";
echo "<p><strong>Property Hooks Test:</strong> " . $obj->name . "</p>\n";

// Show loaded extensions
echo "<h2>Loaded Extensions</h2>\n";
$extensions = get_loaded_extensions();
sort($extensions);
echo "<ul>\n";
foreach($extensions as $ext) {
    echo "<li>$ext</li>\n";
}
echo "</ul>\n";

phpinfo();
?>

Access the Test Page

Visit http://your-server-ip/phptest.php to verify everything is working correctly.

Uninstalling PHP 8.4 (If Needed)

If you need to remove PHP8.4 Ubuntu 24.04:

Remove PHP 8.4 Packages

sudo apt purge php8.4* -y
sudo apt autoremove -y

Remove Configuration Files

sudo rm -rf /etc/php/8.4/

Remove PPA Repository

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

Advanced PHP 8.4 Configuration

Setting Up Development Environment

For development work, create a separate PHP configuration:

sudo cp /etc/php/8.4/cli/php.ini /etc/php/8.4/cli/php-dev.ini

Edit the development configuration:

display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php_errors.log

Production Environment Setup

For production servers, use stricter settings:

display_errors = Off
display_startup_errors = Off
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
log_errors = On
error_log = /var/log/php_errors.log

Monitoring PHP 8.4 Performance

Install System Monitoring Tools

sudo apt install htop iotop nethogs -y

PHP-FPM Status Page

Enable PHP-FPM status page by editing /etc/php/8.4/fpm/pool.d/www.conf:

pm.status_path = /status
ping.path = /ping

Log File Monitoring

Monitor PHP error logs:

sudo tail -f /var/log/php8.4-fpm.log

Backup and Migration

Backup PHP Configuration

Before making changes, backup your configuration:

sudo tar -czf php8.4-config-backup.tar.gz /etc/php/8.4/

Migration from Older PHP Versions

When migrating from older PHP versions:

  1. Test compatibility with your applications
  2. Update deprecated functions in your code
  3. Verify all extensions are available for PHP 8.4
  4. Test thoroughly in a staging environment

Conclusion

Installing PHP8.4 Ubuntu 24.04 is straightforward when you follow the right steps. Whether you chose the basic installation or the full web server setup, you now have a modern, fast, and secure PHP environment.

Key takeaways from this guide:

  • Multiple installation methods available for different use cases
  • Easy PPA setup makes getting the latest PHP versions simple
  • Proper configuration is essential for security and performance
  • Regular updates keep your system secure and efficient

Your PHP8.4 Ubuntu 24.04 installation is now ready for development or production use. The performance improvements and new features in PHP 8.4 will help you build better, faster web applications.

Comments (0)

Comment


Note: All Input Fields are required.