Install PHP on CentOS VPS for Apache Hosted Web Sites
"PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML."
How to Set Up PHP on a CentOS Server Running Apache
This tutorial is useful if you are setting up a Virtual Private Server (VPS) or local web server test system to run PHP code. This article is based upon a minimal installation of CentOS, a popular choice of Operating System (OS) to configure a VPS based web server.
Requirements for Installing PHP on CentOS 6.x
This article assumes that a minimal installation CentOS is installed on a VPS, test system or Virtual Machine (VM). A CentOS VPS is normally accessed using a Secure Shell (SSH) and it is assumed that the Apache HTTP Server is installed and running. If not please see these two related articles:
- SSH into VPS Virtual Machine on Windows Using PuTTY
- Install Apache on CentOS to Configure VPS Test System
(Also useful is the article CentOS Version Command and Update CentOS to New Version.)
Install PHP on CentOS
To install PHP is a simple three step process, run the yum install php command, restart Apache with service httpd restart and test the result with a call to phpinfo() in a PHP file in the /var/www/html directory.
(There is also the yum groupinstall "PHP Support" command for a more comprehensive installation of PHP packages. Use the command yum groupinfo "PHP Support" to see the contents. Also yum search php-_ for additional other optional PHP packages.)
With SSH access to CentOS use yum to install PHP. The program PuTTY was used for SSH access (download the installer from the official PuTTY Download Page).
# yum install php
As for all commands in this tutorial if not logged in as root and the user has sudo ability then run the command using sudo and enter the users password:
$ sudo yum install php
Confirm the installation and PHP and associated packages will be installed.
Restart the Apache web server for CentOS 6 with:
# service httpd restart
Or for CentOS 7:
# systemctl restart httpd.service
Use the vi editor to create a simple PHP test file to call the phpinfo() function. This function dumps a HTML page full of PHP information. To make life a little easier change to Apache's default virtual directory at /var/www/html:
# cd /var/www/html
Press the Insert key and type the following:
<?php phpinfo(); ?>
Save the file by pressing the Esc key and entering the vi command :w followed by a file name, in this case php-test.php, then the Return (Enter) key:
Quit vi using :q. Go to the newly created page in the browser, using a domain name (e.g. www.example.com/php-test.php) or IP address (e.g. 18.104.22.168/php-test.php) for a physical server, or localhost (e.g. http://localhost:8000/php-test.php) for a VM (see the Apache article mentioned above):
PHP is now ready to be used on any web sites on the server. Remember to delete the php-test.php file from any live systems to prevent configuration information being made public.
Author:Daniel S. Fowler Published: Updated: