LAMP on CentOS – PHP

A LAMP (Linux, Apache, MySQL, PHP) stack is a common web stack used for hosting web content. Let’s install MariaDB (MySQL) on RHEL/CentOS 7 server.

The CentOS 7 official software repositories have PHP 5.4 which has reached the end of life and no longer actively maintained by the developers.

To keep up with the latest features and security updates, you need a newer (probably the latest) version of PHP on your CentOS 7 system.

For the purpose of this guide, we will be operating the system as root, if that is not the case for you, make use of the sudo command to acquire root privileges.

1. Installing PHP 7

To install PHP 7, you have to install and enable EPEL and Remi repository to your CentOS 7 system with the commands below.

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

2. install yum-utils

2.1. you need to install yum-utils, a collection of useful programs for managing yum repositories and packages. It has tools that basically extend yum’s default features.

It can be used for managing (enabling or disabling) yum repositories as well as packages without any manual configuration and so much more.
# yum install yum-utils

2.2 One of the programs provided by yum-utils is yum-config-manager, which you can use to enable Remi repository as the default repository for installing different PHP versions as shown.
# yum-config-manager --enable remi-php70 [Install PHP 7.0]

If you want to install PHP 7.1 or PHP 7.2 on CentOS 7, just enable it as shown.
# yum-config-manager --enable remi-php71 [Install PHP 7.1]
# yum-config-manager --enable remi-php72 [Install PHP 7.2]

2.3 Now install PHP 7 with all necessary modules with the command below.
# yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo

3. Quick check

Afterwards, double check the installed version of PHP on your system.

# php -v

4. Testing and Getting Details

The document root of the default website is /var/www/html. We will create a small PHP file (info.php) in that directory and call it in a browser to test the PHP installation. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

# nano /var/www/html/info.php
Then type he following code:

phpinfo();

Save, and exit
Now, use your browser to call that file in a browser (e.g. http://192.168.1.100/info.php):

As you see, PHP 7.2 is working, and it’s working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP. MySQL is not listed there which means we don’t have MySQL support in PHP yet.

5. Getting MySQL Support In PHP

To get MySQL support in PHP, we can install the php-mysqlnd package. It’s a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

# yum search php

Pick the ones you need and install them like this:
# yum -y install php-mysqlnd php-pdo

In the next step I will install some common PHP modules that are required by CMS Systems like WordPress, Joomla, and Drupal:

# yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel

Now restart Apache web server:
# systemctl restart httpd.service

Now reload http://192.168.1.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules like curl etc there.:


Step by step, installing Apache, MariaDB, PHP, and phpMyAdmin

  1. Part 1: Install Apache on RHEL/CentOS 7
  2. Part 2: Install MariaDB on RHEL/CentOS 7
  3. Part 3: Install PHP on RHEL/CentOS 7
  4. Part 4: Install phpMyAdmin on RHEL/CentOS 7

Thats all for now!

You may also like: