The Complete Guide to Installing PHP 8 Version and Newer on Major Linux Distributions.

PHP is a widely-used open-source general-purpose scripting language that is especially suited for web development. Regularly updating to the latest PHP versions ensures access to new features, performance improvements, and the most recent security patches. PHP 8.1 and newer introduce significant enhancements, such as:

  • Just-In-Time (JIT) Compiler: The JIT compiler significantly improves performance by compiling frequently executed code into machine code.  
  • Enums: Enums allow developers to define a type that has a fixed set of possible values, improving code readability and maintainability.
  • Fibers: Fibers provide a lightweight concurrency mechanism for improving the performance of asynchronous operations.

This guide focuses on installing PHP 8.1 and newer versions on several popular Linux distributions.

Installation Procedures

RHEL-based Distributions (RHEL, CentOS, AlmaLinux)

Step 1: Install the EPEL and Remi Repositories

PHP 8.1 and newer versions are not available in the default repositories of RHEL-based distributions. To install them, you need to add the EPEL and Remi repositories. These repositories provide the necessary PHP packages and their dependencies.

Bash
sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm

Step 2: Enable the PHP Module Stream

After adding the repositories, enable the PHP module stream for the desired PHP version. For example, to enable PHP 8.1, run:

Bash
sudo dnf module enable php:remi-8.1 -y

Step 3: Install PHP

Now you can install PHP using the following command:

Bash
sudo dnf install php

This command installs the core PHP packages. You can also install additional extensions based on your requirements.  

Step 4: Integrate PHP with Apache Web Server (RHEL)

If you are using the Apache web server, you need to integrate PHP with Apache. To do this, install the php-apache package:

Bash
sudo dnf install php-apache

This package provides the necessary modules for Apache to process PHP files.  

Step 5: Manage PHP-FPM Services (CentOS)

For users working with Nginx or other web servers that utilize PHP-FPM, it’s essential to know how to manage the PHP-FPM service. Here are the commands to start, stop, restart, and check the status of PHP-FPM:

Bash
systemctl status php-fpm
systemctl enable php-fpm
systemctl start php-fpm
systemctl stop php-fpm
systemctl restart php-fpm

These commands allow you to control the PHP-FPM service and ensure it’s running correctly.  

Step 6: Verify the Installation

To verify the PHP installation, run:

Bash
php -v

This command displays the installed PHP version.

Software Collections (RHEL)

Red Hat Software Collections (SCL) is a repository that provides a way to install multiple versions of software, including PHP, alongside the default version. This is useful for maintaining compatibility with different applications that may require different PHP versions. To install PHP 8.1 using SCL, you would use the php81 prefixed packages. For example, to install the PHP 8.1 CLI, you would run: sudo yum install php81-php-cli.  

Debian/Ubuntu

Step 1: Update and Upgrade the System

Before installing PHP, ensure your Debian system is up-to-date:

Bash
sudo apt update && sudo apt upgrade -y

Step 2: Install Required Dependencies

Install the necessary dependencies for PHP:

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

Step 3: Add the Sury Repository

Add the Ondřej Surý PPA repository, which provides the latest PHP versions:

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

Step 4: Install PHP

Install PHP 8.1 or a newer version using the following command:

Bash
sudo apt install php8.1

Step 5: Switch Between PHP Versions

If you have multiple PHP versions installed, you can use the update-alternatives command to switch between them. For example, to switch to PHP 8.1 for the CLI, run:

Bash
sudo update-alternatives --config php

This command presents a list of installed PHP versions, allowing you to choose the default one.  

Step 6: Verify the Installation

Verify the PHP installation by checking the version:

Bash
php -v

Install PHP 8.1 with Nginx

If you are using Nginx as your web server, you need to install PHP-FPM to handle PHP files. To install PHP 8.1 with Nginx, run:

Bash
sudo apt install php8.1 php8.1-fpm

This command installs PHP 8.1 and the PHP-FPM package.  

Configure PHP-FPM

After installing PHP-FPM, you need to configure it. The main configuration file for PHP-FPM is located at /etc/php/8.1/fpm/php-fpm.conf. You can also configure individual pools in the /etc/php/8.1/fpm/pool.d/ directory. For example, the default pool configuration file is /etc/php/8.1/fpm/pool.d/www.conf.  

Default PHP Versions in Ubuntu LTS

Ubuntu LTS VersionRelease DateDefault PHP Version
Ubuntu 18.04April 2018PHP 7.2
Ubuntu 20.04April 2020PHP 7.4
Ubuntu 22.04April 2022PHP 8.1
Ubuntu 24.04April 2024PHP 8.3

Dependencies

PHP 8.1 and newer versions have various dependencies depending on the Linux distribution and desired extensions. Here’s a breakdown of dependencies for different distributions:

RHEL/CentOS/AlmaLinux:

  • EPEL release package  
  • Remi repository  
  • Specific libraries for extensions like php-gd  

Debian/Ubuntu:

  • lsb-release  
  • ca-certificates  
  • apt-transport-https  
  • software-properties-common  
  • gnupg2  
  • Libraries like libpcre2-8-0  

It’s crucial to install these dependencies before installing PHP to avoid errors during the installation process.  

Common Issues and Troubleshooting

While installing PHP, you might encounter some common issues. Here are a few troubleshooting tips:

  • Missing Dependencies: Ensure all required dependencies are installed before installing PHP. Use the package manager’s dependency checking tools to identify and install any missing dependencies.  
  • Conflicting Packages: If you have older PHP versions installed, they might conflict with the newer version. Remove or disable the older versions before installing the new one.  
  • PHP-FPM Issues: If you’re using PHP-FPM, ensure the service is running and configured correctly. Check the PHP-FPM configuration files for any errors.  
  • Apache/Nginx Configuration: Verify that your web server (Apache or Nginx) is configured to use the correct PHP version. Update the web server configuration files to point to the new PHP installation.  
  • Extension Errors: If you encounter errors with specific extensions, ensure the extension is compatible with the installed PHP version and that its dependencies are met.  
Elsewhere On TurboGeek:  Getting Started With Ansible

Always check for compatibility issues with existing applications before upgrading to a newer PHP version. Upgrading might introduce breaking changes that could affect your applications. Thoroughly test your applications after upgrading to ensure everything functions as expected.  

If you face any other issues, consult the official PHP documentation and support forums for your Linux distribution.

Best Practices for Production Environments

When installing PHP in a production environment, consider these best practices:

  • Use the Latest Stable Version: Always use the latest stable PHP version to benefit from performance improvements and security updates.  
  • Install Only Required Extensions: Install only the PHP extensions required for your application to minimize the attack surface and resource usage.
  • Configure PHP-FPM: If using PHP-FPM, optimize its configuration for your server’s resources and application requirements. This includes adjusting the number of child processes, tuning process management settings, and configuring the listening socket.
  • Secure Your PHP Installation: Harden your PHP installation by disabling unnecessary functions in the php.ini file, setting appropriate file permissions, and implementing security best practices like input validation and output encoding.
  • Implement Opcode Caching: Utilize opcode caching mechanisms like OPcache to improve performance by storing compiled bytecode in memory, reducing the need to recompile PHP scripts on each request.
  • Monitor PHP Performance: Regularly monitor PHP performance and resource usage to identify and address any potential issues. Use monitoring tools to track metrics like request throughput, memory usage, and CPU utilization.

Alternative Installation Methods

Besides the standard package manager installations, you can explore alternative methods for installing PHP:

  • Building from Source: This method provides more control over the installation process and allows for customization. However, it requires more technical expertise and can be time-consuming. You need to download the PHP source code, configure the build options, compile the code, and install the binaries. This method is generally recommended for advanced users who need specific customizations or want to build PHP with non-standard options.  
  • Containerization (Docker): Using Docker containers provides a consistent and isolated environment for running PHP applications. This approach simplifies dependency management and deployment by packaging all the necessary dependencies, including the PHP runtime, extensions, and libraries, within the container. Docker containers also offer portability, allowing you to easily move your PHP applications across different environments.  

Choose the installation method that best suits your needs and technical expertise.

Conclusion

This guide provides a comprehensive overview of installing PHP 8.1 and newer versions on various Linux distributions. By following the instructions and considering the best practices, you can successfully install and configure PHP for your web development projects. Upgrading to the latest PHP versions is crucial for ensuring optimal performance, security, and access to new features. However, it’s essential to carefully plan the upgrade process, considering potential compatibility issues and performing thorough testing, especially in production environments. Remember to consult the official PHP documentation and support forums for your specific Linux distribution for any further assistance.

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

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

Translate »