How to Install PHP on Your CentOS 7 Server

PHP is an open-source server based on HTML script language. It’s an essential software for web developers who want a variety of useful tools for building websites. It also provides the native APIs for Apache and other servers.

In this tutorial, we look at a detailed process on how to install and configure PHP on

your VPS (Virtual Private Server).

Requirements

  • VPS installed on your machine
  • SSH client installed (PuTTY is our choice for this tutorial)
  • Installed Apache on VPS

Step 1: Log on the VPS

The first step is to log on to your VPS.

Step 2: Installing PHP

PHP is the component that will process codes to display dynamic content. With PHP, it’s easy to run scripts, link with the MySQL databases to collect information, and present the content to our web server to display.

It’s possible to install PHP using yum which is a CentOS package manager. In this case, we will also include the php-mysql package. Run the command below:

$ yum install php php-mysql

This should successfully install PHP without any problems. But you need to restart Apache to ensure it works well with PHP. Use the command below to apply the changes:

$ systemctl restart httpd.service

Once you’re finished, save and close the file.

Step 3; Installing PHP Modules

To ensure the functionality of PHP, you will need to install additional modules.

To view the options available for PHP modules, you can use apt-cache search to pipe the results into less, a page that allows the user to see and scroll through other commands:

$ yum search php-

Using the arrow keys scroll up or down, and use q to quit.

It will show the following output:

php-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php-cli.x86_64 : Command-line interface for PHP
php-common.x86_64 : Common files for PHP
php-dba.x86_64 : A database abstraction layer module for PHP applications
php-devel.x86_64 : Files needed for building PHP extensions
php-embedded.x86_64 : PHP library for embedding in applications
php-enchant.x86_64 : Enchant spelling extension for PHP applications
php-fpm.x86_64 : PHP FastCGI Process Manager
php-gd.x86_64 : A module for PHP applications for using the gd graphics library
. . .

For more information about the role of each module, you can search online, or just look at the long description of the package using the command below:

$ yum info package_name

You will see a lot of information from the output, with an example of a field known as Description-en which will comprise of detailed explanation of the functions of each module provided.

For instance, if you want to find out what the module php-fpm does, just type the command below:

$ yum info php-fpm

You should get the output below:

. . .
Summary : PHP FastCGI Process Manager
URL : http://www.php.net/
License : PHP and Zend and BSD
Description : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
 : implementation with some additional features useful for sites of
 : any size, especially busier sites.
yum install php-fpm

If you decide to install more than one module, you can do so by listing each one of them, separated by a space as shown below:

$ yum install package1 package2 ...

At this point, PHP modules are installed, now we test whether PHP is working properly.

Step 4: Testing PHP Processor on your VPS

To test whether the system is completely configured for PHP, we start by creating a basic PHP script.

We call the script info.php.To ensure Apache accesses the file, you need to be save it to directory known as “web root.”

In Ubuntu, this directory is found in /var/www/html. Create a file using the command below:

$ vi /var/www/html/info.php

Now, you should see a blank file where you will type the following text:

info.php
<?php
phpinfo();
?>

Once you’re finished, save and close the file.

Ensure the firewall allows for HTTP and HTTPS traffic. To verify this, use the command below:

$ firewall-cmd --list-all

In case port 80 and 443 are not in the list, type the command below:

$ firewall-cmd --permanent --zone=public --add-service=http 
$ firewall-cmd --permanent --zone=public --add-service=https
$ firewall-cmd --reload

Now, test whether the web server can display the information generated by your PHP script by typing the address below in your browser.

http://your_server_IP_address/info.php

You should see the following page with all the information you need:

This shows that PHP script is working properly.

If you decide to remove this file (which is recommended as it may provide the web server’s information to unauthorized users), run the command below:

$ rm /var/www/html/info.php

Conclusion

Now that you have a fully installed PHP on VPS, you can choose what to do next. With this platform, it will be easier to install most types of website and other software on your server.

If you would like to use php for the websites on your server, you may benefit from using a hosting plan that includes support and other special features. Find the best PHP hosting services here on HostAdvice.

Check out these top 3 PHP hosting services:

Was this article helpful?