Skip to main content

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:
    Server time via Mod PHP

    Time

    The time is

    phpinfo(); ?>

Help Category