Tek Eye Logo

Tek Eye

Install PHP on CentOS VPS for Apache Hosted Web Sites

This article is a short tutorial on configuring PHP to work with Apache, adding programming and template functionality to a web server. The following is taken from What is PHP?

"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.

Install PHP on CentOS - CentOS Logo

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:

(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

Start vi:

# vi

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:

:w php-test.php

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. 93.184.216.34/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):

Install_PHP_centos

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.

See Also

  • For a full list of the articles on Tek Eye see the full site Index

Author:  Published:  Updated:  

ShareSubmit to TwitterSubmit to FacebookSubmit to LinkedInSubmit to redditPrint Page

Do you have a question or comment about this article?

(Alternatively, use the email address at the bottom of the web page.)

 This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.


Articles on:

Android Programming and Android Practice Projects, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS, C# Programming, Using Windows for Programming


Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples



Tek Eye Published Projects