Introduction
PHP is a programming language that is often used to automate server tasks. It is part of the LAMP stack (Linux, Apache, MySQL, PHP), which is a software package used to run servers and Internet services. PHP handles dynamic content, database requests, and data processing and visualization.
This step-by-step guide shows how to install PHP on CentOS 7.
prerequisites
Access to a
- user account with sudo privileges Access
- a terminal/command line window
- The yum package manager, included by default
- A third-party software repository, detailed below How to
to
install PHP
7.2 with Apache on CentOS
Step 1: Choose PHP version to install
The latest stable version of PHP is PHP 7.3.1. However, some software repositories use older versions of the software by default.
An advantage of using an older version is its high stability and reliability. Newer versions may include more features, but they are often more experimental and could cause system instability. If you can’t decide which version is right for you, version 7.2 is a great place to start.
Step 2: Enable
additional software repositories
By default, the yum package manager does not include access to the repositories that store PHP packages. Therefore, you must enable access to these software packages.
1. First, start by installing the yum-utils
package by entering the following command in a terminal window: sudo yum install yum-utils –
y
2. Then, enable the epel-release repository by entering the following: sudo yum install epel-release –
y
3. Finally, add the following software repositories that contain the PHP packages
: sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Step 3: Enable
repository for PHP version Next, use yum-config-manager to
point your installer to the repository for the PHP version you want to install: sudo yum-config-manager -enable remi-php70
This command configures your system to download and install PHP 7.0.To installing PHP 7.1, change the entry as follows
: sudo yum-config-manager -enable remi-php71
Similarly, replace the last two characters with 72 to install PHP 7.2.
Step 4: Install PHP and its dependencies
At this point, you can install PHP. Enter the following command in the terminal
: sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql -y
As you can see, the line includes many of the modules and plugins that help PHP integrate with your local server configuration
.
The output will also indicate which dependencies have been installed.
Step 5: PHP Modules
You may have noticed that the installation script included more than just the base PHP package. Many of these are modules that provide basic functionality to PHP. Installing this set helps ensure that the PHP installation meets your usage expectations.
Like many other Linux applications, you can enhance the PHP functionality of your system by using modules.
To search for available modules and generate a list of modules, type the following command:
sudo yum search php | More
Step 6: Check PHP Version
Finally, verify that your installation was successful. Check which version of PHP you are running with the command
: php -v
Optional: Install
PHP 7.3
You can enable the Remi PHP 7.3 repository in the same way that you enabled other repositories in Step 3. However, this installation may fail if you have extensions that are not compatible with PHP 7.3.
If this is your first time installing and you are sure your system is supported, you can enable PHP 7.3. Install the version with the following commands:
sudo yum-config-manager -enable remi-php73 sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql -y
Conclusion
As you can see, installing PHP on CentOS is pretty straightforward. More information about the latest downloadable version can be found on the developer’s downloads page.