How to Setup PHP on a VPS

How to Setup PHP on a VPS
Last updated on May 12, 2017

PHP is a server-side HTML embedded scripting language. It provides web developers with a full suite of tools for building dynamic websites: native APIs to Apache and other web servers; easy access to MySQL, Sybase, Oracle, and other databases; IMAP; LDAP; HTTP headers and cookies. Here’s a step-by-step tutorial on how to setup PHP on a Virtual Private Server (VPS).


– You’ve already acquired a VPS solution.

– You’ve already installed an SSH client (PuTTy is our terminal of choice).

– You’ve already installed Apache on your VPS.

Step 1: Log on to VPS
First, you need to log on to your VPS.

Step 2: Install PHP
You can install PHP using CentOS’s package manager yum. We are going to include the php-mysql package as well. Type the following:

yum install php php-mysql
You need to restart Apache in order for it to work with PHP. You can do this by using:

systemctl restart httpd.service
Step 3: Install PHP Modules
It’s likely you will need some additional PHP modules. To see the available options for PHP modules and libraries, you can use next command:

yum search php-
You will see all optional components that you can install, with a short description for each:

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
. . .
To get more information about each module you can read longer description in the package by typing:

yum info package_name
The “Description” file contains longer explanations for each module’s respective functionalities.

Let’s see what the php-fpmmodule does and install it.

yum info php-fpm
. . .
Summary : PHP FastCGI Process Manager
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 want to install more than one module at one time, you can by typing each module, separated by a space:

yum install package1 package2 …
Step 4: Test PHP Processing on your VPS
Let’s create a very basic PHP script info.phpto test our system. You need to store it at “web root” directory /var/www/html. You will create it in this directory by using the following command:

vi /var/www/html/info.php
Put the following text inside the blank file:

Then save and close the file.

Make sure your firewall allows HTTP and HTTPS traffic. To check this, use the following command:

firewall-cmd –list-all
If ports 80 and 443 are not listed here, type this command:

firewall-cmd –permanent –zone=public –add-service=http
firewall-cmd –permanent –zone=public –add-service=https
firewall-cmd –reload
To test if the PHP script you created works, visit “http://your_server_IP_address/info.php” in your web browser.