How To Install PHP 7, 7.2 & 7.3 On CentOS 7


How to Install PHP 7.2 with Apache on CentOS

Step 1: Choose PHP Version to Install

The newest stable release version of PHP is PHP 7.3.1. However, some software repositories default to older versions of the software.

One advantage of using an older release is its high stability and reliability. Newer releases may include more features, but are often more experimental and could cause system instability. If you cannot 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 the PHP packages. Therefore, you need to 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

Step 3: Enable Repository For PHP Version

Next, use the yum-config-manager to point your installer to the repository of 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 install PHP 7.1, change the entry as follows:

sudo yum-config-manager ––enable remi–php71

Likewise, 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 see, the line includes many of the modules and add-ons 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 your PHP installation meets your expectations for usage.

Like many other Linux applications, you can enhance your system’s PHP functionality using modules.

To search for available modules and generate a list of modules, type in the following command:

sudo yum search php | more

Step 6: Verify PHP Version

Finally, verify 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 Remi’s PHP 7.3 repository the same way 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 a first-time installation and you are confident that your system is compatible, you can enable PHP 7.3. Install the release 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

How To Install the Apache Web Server on CentOS 7


Installing Apache on CentOS

Step 1: Update Software Versions List

Ensure you are using the latest versions of the software. In a terminal window, input the command:

sudo yum update

The system should reach out to the software repositories and refresh the list to the latest versions.

Step 2: Install Apache

To install Apache on your CentOS server, use the following command:

sudo yum install httpd

The system should download and install the Apache software packages.

Step 3: Activate Apache

To activate Apache, start its service first.

1. Enter the following command in a terminal window:

sudo systemctl start httpd

This will start the Apache service.

2. Next, set the Apache service to start when the system boots:

sudo systemctl enable httpd

Step 4: Verify Apache Service

Display information about Apache, and verify it’s currently running with:

sudo systemctl status httpd

Step 5: Configure firewalld to Allow Apache Traffic

In a standard installation, CentOS 7 is set to prevent traffic to Apache.

Normal web traffic uses the http protocol on Port 80, while encrypted web traffic uses the https protocol, on Port 443.

1. Modify your firewall to allow connections on these ports using the following commands:

sudo firewall-cmd ––permanent ––add-port=80/tcp
sudo firewall-cmd ––permanent ––add-port=443/tcp

2. Once these complete successfully, reload the firewall to apply the changes with the command:

sudo firewall-cmd ––reload

Step 6: Configure Virtual Hosts on CentOS 7 (optional)

Virtual hosts are different websites that you run from the same server. Each website needs its own configuration file.

Make sure these configuration files use the .conf extension, and save them in the /etc/httpd/conf.d/ directory.

There are a couple of best practices to use when you’re setting up different websites on the same server:

  • Try to use the same naming convention for all your websites. For example:
    • /etc/httpd/conf.d/
    • /etc/httpd/conf.d/
  • Use a different configuration file for each domain. The configuration file is called a vhost, for a virtual host. You can use as many as you need. Keeping them separate makes troubleshooting easier.

1. To create a virtual host configuration file, enter the following into a terminal window:

sudo vi /etc/httpd/conf.d/vhost.conf

This will launch the Vi text editor, and create a new vhost.conf file in the /etc/httpd/conf.d directory.

2. In the editor, enter the following text:

NameVirtualHost *:80

<VirtualHost *:80>

ServerAdmin [email protected]



DocumentRoot /var/www/html/

ErrorLog /var/www/html/

CustomLog /var/www/html/ combined


Save the file and exit.

3. Next, enter the following command to create a directory for you to store your website files in:

sudo mkdir /var/www/hostiger/{public_html, logs}

4. Restart the Apache service to apply your changes by entering:

sudo systemctl restart httpd

Once the system finishes, you should be able to open a browser window to and see a default Apache test page.

You can replace Hostiger above with the name of your domain. If you are hosting more than one domain, make sure you create a new directory in /var/www/ for each one. You can copy the code block in your /etc/httpd/conf.d/vhost.conf file, and replace Hostiger with another domain name that you’re hosting.

Apache Directories and Files

One of the main ways Apache functions is through configuration files. They are located at /etc/httpd.

Apache has a main configuration file: /etc/httpd/conf/httpd.conf .

If there are any other configuration files, they are included in the main configuration file. They should use the .conf extension and should be stored in the /etc/httpd/conf.d/ directory.

You can enhance Apache’s functionality by loading additional modules.

The configuration files for these modules should be stored in: /etc/httpd/conf.modules.d/ directory.

Log files record all the activity of the Apache service, including client activity on the websites your system is hosting. These logs can be found in:  /var/log/httpd/.

Commands For Managing Apache Service

Other commands that you can use to control the Apache service include:

Stop Apache Service:

sudo systemctl stop httpd

Prevent or disable Apache from starting when the system boots:

sudo systemctl disable httpd

Re-enable Apache at boot:

sudo systemctl enable httpd

Restart Apache and apply any changes you have made:

sudo systemctl restart httpd


Great job, you have installed Apache on your CentOS 7 server. The basic installation and management of the Apache service are simple.