24/7/365 Support

Installing, configuring, and testing PHP on CentOS

Hypertext Preprocessor (PHP) remains one of the most popular server-side scripting languages designed for web development. It already supports some nice features, such as connecting to relational databases like MariaDB out-of-the-box which can be used to implement modern web applications very fast. While a current trend can be seen for larger enterprises to move away from PHP in favor of some newer technologies such as Node.js (server-side JavaScript), it is still the superior scripting language on the consumer market. Every hosting company in the world provides some kind of LAMP stack (Linux, Apache, MySQL, PHP) to run the PHP code. Also, a lot of very popular web applications are written in PHP, such as WordPress, Joomla, and Drupal, so it’s fair enough to say that PHP represents a must-have feature for almost any Apache web server. Here in this process, we will show you how to get started with installing and running PHP in your Apache web server with the module mod_php.

To Start With: What Do You Need?

To complete this process, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice and an Internet connection. It is expected that your server will be using a static IP address and Apache is installed and currently running, and that your server supports one or more domains or subdomains.

The Process

We will begin this process by installing the PHP Hypertext Processor together with the Apache mod_php module, both not installed by default on CentOS 7 minimal.

  1. To begin, log in as root and type the following command:
    yum install mod_php
  2. Now let’s open the standard PHP configuration file after we have made a backup of the original file first:
    cp /etc/php.ini /etc/php.ini.bak && vi /etc/php.ini
  3. Find the line ; date.timezone = and replace it with your own timezone. A list of all the available PHP time zones can be found at http://php.net/manual/en/timezones.php. For example (be sure to remove the leading ; as this is disabling the interpretation of a command; this is called commenting out) to set the timezone to the city Berlin in Europe use:
    date.timezone = "Europe/Berlin"
  4. To make sure the new module and settings have been properly loaded, restart the Apache web server:
    systemctl restart httpd
  5. To be consistent with the CGI examples from the former recipe, here we will create our first dynamic PHP script which will print out the current local server time in the script vi /var/www/html/php-test.php, and run the popular PHP function phpinfo() that we can use to print out important PHP information:
    <html><head><title>Server time via Mod PHP</title></head>
    <h1>Time</h1>
    <p>The time is <?php print Date("D M d, Y G:i a");?></p><?php
    phpinfo(); ?></body></html>
  6. To actually see what HTML is being generated from the preceding script, you can execute the PHP script directly on the command line; just type: php /var/www/html/php-test.php.
  7. Now open a browser on a computer in your network and run your first PHP script which will print the local time by using the following URL: http://<server name or IP address>/php-test.php.

How Does It Work?

In this process, we showed you how easy it is to install and incorporate PHP into any Apache web server by using the mod_php module. This module enables an internal PHP interpreter, which directly runs in the Apache process and is much more efficient than using CGI, and should always be your preferred method whenever is available.

So what did we learn from this experience?

We began this process by installing the mod_php module using YUM, which will install PHP as a dependency as well as both are not available on any standard CentOS 7 minimal installations. Installing mod_php added the /etc/php.ini configuration file which we then opened after making a backup of the original file first. This file is the main PHP configuration file and should be edited with care because a lot of settings can be security relevant to your web server. If you are just starting out with PHP, leave everything as it is in the file and don’t change anything despite the date.timezone variable. We set this to reflect our current time zone and it is necessary for PHP because it is used by a lot of different time and date functions (we will use some date functions in our first PHP script as well, see below). Next, we restarted the Apache web server which automatically reloads the PHP configurations as well. Afterwards, we created our first PHP script and put it in the main web root folder /var/www/html/php-test.php; this prints out the current server time as well as the result of the phpinfo() PHP function. This gives you a well categorized tabular overview of your current PHP installation, helping you diagnose server-related problems or see which modules are available in PHP.

In comparison to CGI, you may ask yourself why we don’t have to put the PHP scripts into any special folder such as cgi-bin. By installing mod_php, an Apache configuration file called /etc/httpd/conf.d/php.conf gets deployed into the Apache configuration folder, which exactly answers this question, it specifies that PHP scripts will get executed as valid PHP code whenever they get the extension .php from anywhere in every web directory.

 

Help Category:

What Our Clients Say