decorative image for blog on installing php on linux distros
July 20, 2023

Getting Started With PHP and Linux: Installing PHP on Linux

PHP Development

PHP and Linux, as complementary open source technologies, are a common foundation for web applications of all shapes and sizes. PHP’s Unix-based systems make it natural fit for Linux, with the combination serving as a scalable and cost-effective combination for deploying performant and secure web applications.

In this blog, we dive in on why PHP and Linux are commonly used together, discuss popular Linux distributions used with PHP, walk through PHP installation instructions for various Linux distros, and discuss PHP Linux container images for teams developing container-based applications.

Back to top

Why Use PHP and Linux Together?

PHP stands for Hypertext Preprocessor and is a widely used server-side scripting language. It's both platform-independent and open-source, making it a preferred choice for web developers. PHP is popular for its capabilities to create dynamic content and interact with various databases, and its simplicity and speed make it an essential asset of most web development stacks. According to recent research by W3Techs, over 77% of all live websites worldwide use PHP. 

Linux is a reliable, open-source operating system, which has established itself as the leading foundation for web servers. It's secure, stable, and has an incredibly active development community, allowing for quick patching of any newly discovered breaches. This makes Linux the perfect platform for mission-critical tasks. W3Techs reports that over 81% of the websites whose operating system is known use Unix-based operating systems.

Together, PHP and Linux form a powerful open-source duo. They integrate seamlessly and provide a sturdy environment, allowing developers to create, test, and deploy web applications with ease.

Back to top

Popular Linux Distributions With Easy PHP Installation

There are multiple ways to install PHP on Linux, ranging from pre-packaged methods to building directly from the source. In this blog, we will focus on the simplest and most popular procedure, using the package manager of your chosen Linux distribution.

At the time of writing of this article, the community-supported PHP versions are 8.0, 8.1 and 8.2. If you're looking for secure PHP runtimes that include long-term support for EOL PHP releases, be sure to try ZendPHP.

PHP RHEL Installation Guide

This section will walk you through the installation process of PHP on Red Hat Enterprise Linux (RHEL). Root access to the system is required. We will use the Remi repository, which contains the packages for the latest versions of PHP. The repository is maintained by Remi Collet, a senior software engineer at Red Hat and longtime PHP contributor. It's stable, free to use, and the preferred repository for PHP packages on RHEL.

1. Update the System Packages

Before installing any new software, it's recommended to update the system packages by executing:

$ sudo dnf update

2. Configure the Remi Repository

To configure the Remi Repository, execute the command:

$ sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm

3. Choose Your PHP Version

The Remi repository contains packages for multiple supported PHP versions. List the available ones by executing:

$ sudo dnf module list php

Then enable your chosen version by the following command:

$ sudo dnf module enable php:remi-<VERSION>

In the above command, replace <VERSION> with the actual number (e.g. 8.0, 8.1,or 8.2).

4. Install PHP

Next, you’ll install PHP by executing:

$ sudo dnf install php 

After the process is completed, you can verify the installation of PHP with the following command:

$ php -v 

If you receive an output with a PHP version - congratulations, you have successfully installed PHP on RHEL.

Installing PHP on RHEL Derivatives

Many of the popular Linux distributions today are based on RHEL. This includes, but is not limited to CentOS, AlmaLimux, Rocky Linux, Oracle Linux, Amazon Linux. The PHP installation on these distributions is identical to the RHEL installation process described above.

PHP Debian Installation Guide

In this section we will install PHP on Debian, using the Sury repository for the PHP packages. The repository is maintained by Ondrej Sury - an experienced developer, who has been packaging PHP for Debian for nearly 20 years. The Sury repository focuses mainly on providing the latest PHP packages, and it's the most popular method of installing PHP on Debian.

1. Update the System Packages

Before installing any new software, it's recommended to update the system packages by executing:

$ sudo apt update

2. Add and Configure the Sury Repository 

First, install the dependencies:

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

Then add the repository and update the package index:  
 

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
$ wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
$ sudo apt update

3. Install PHP

Install PHP by executing:

$ sudo apt install php<VERSION>

In the above command, replace <VERSION> with the actual number, (e.g. 8.0, 8.1, or 8.2).

After the process is completed, you can verify the installation of PHP with the following command:

$ php -v 

If the above command returns a PHP version number, then you have successfully installed PHP on Debian.

Installing PHP on Debian Derivatives

The most popular Linux distribution based on Debian is Ubuntu, but the list also includes Linux Mint, Knoppix and SparkyLinux, to name a few. The PHP installation on these distributions is identical to the Debian installation process described above.

PHP Alpine Linux Installation

This section describes the installation of PHP on Alpine Linux, which has become a popular choice for running containers. 

NOTE: Zend offers PHP Alpine Linux containers at cr.zend.com.

1. Update the System Packages

Before installing any new software, it's recommended to update the system packages by executing:

$ sudo apk update

2. Install PHP

Next, install PHP via the following command:

$ sudo apk add php<VERSION>

In the above command, replace <VERSION> with the actual number, (e.g. 8, 81, or 82).

After the process is completed, you can verify the installation of PHP with the following command:

$ php -v

If you see an output with a PHP version number, your installation of PHP in Alpine Linux was successful.

Back to top

PHP Linux Container Images

Containers provide a platform for developers that greatly improves portability, scalability, and speed of delivery. The vast majority of containers are based on Linux, so it's no surprise that the rise in popularity of containerization, led by Docker and Kubernetes, also includes PHP. PHP containers allow developers to deploy their applications anywhere instantly and easily share their work environments.  

PHP containers can be built in all sizes and forms, running on different foundations, and featuring different extensions. If you'd like to learn more about PHP containers, there are a few good places to start:

Official Docker Image Repository

The Docker community maintains an official PHP image repository at https://hub.docker.com/_/php. Though, as veteran PHP container image users will note, the community-provided images have some limitations that can cause headaches. As we discuss in our blog on the importance of easy-to-use PHP Linux images, the official PHP images from Docker Hub are minimal to a fault. This means that configuring, compiling, and installing them can turn into a time-intensive and complicated project.

ZendPHP Container Registry

For teams looking for more PHP and Linux container options, Zend offers a full selection of easy-to-use PHP Linux containers at our ZendPHP container registry

Back to top

Final Thoughts

PHP and Linux can combine as a stable platform for all manner of web applications. And, because PHP and Linux are both community-supported, they have a low barrier for entry and plenty of documentation available to help you along the way. 

For further reading on this topic, be sure to check out our additional resources linked below, and bookmark our blog to stay up to date with the latest in PHP.

Need to Ensure Security and Compliance for Your PHP App?

With fully-supported PHP images available via our container repository, and a full suite of professional services, Zend can help make your PHP and Linux deployment secure and compliant. Contact us today to learn more about how we can support your project.

Contact Us

Additional Resources

Back to top