Install LAMP in CentOS 7

Alexandru Calcatinge bio photo By Alexandru Calcatinge

Introduction

In this post I will show you how to install LAMP in CentOS 7. You may think that it is as simple as running several yum install commands, but it is not. Mostly because CentOS is delivered with an outdated PHP version. It is version 5.4, which already reached End Of Life.

Therefore, if you want to stay secure and up to date, you will have to use a third party repository to have the latest version of PHP installed. Other than that, you will install Apache and MariaDB as usual, using simple yum install commands.

Installing Remi’s RPM Repository for latest version of PHP

Remi’s RPM Repository is the one you need in order to install the latest version of PHP in CentOS or RHEL. By accessing the link, you will have available a very nice Configuration Wizard in which you can select the Operating system, the PHP version that you want and the Type of Installation that you require. After selecting that, you will have the Wizard answer available. Below is an example of a wizard answer for the latest version of PHP to be installed on CentOS 7.

remi-php-config

Now, let’s follow the instructions from the Remi’s website in more detail. If you didn’t already install the EPEL repository, you can do it with the command:

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Then install the Remi repository with the command:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

You will then install yum-utils package to have the yum-config-manager available:

sudo yum install yum-utils

Enable the Remi repository:

sudo yum-config-manager --enable remi-php72

Update all the repositories:

sudo yum update

To install PHP run the command:

sudo yum install php

If you want to install additional packages, just search for them using the command:

sudo yum search php- | less

Then, if you find a package that you would like to install, just use the yum install package-name command. One recommended package would be php-mysql. To install it, use the command:

sudo yum install php-mysql

To check the php version installed, run the command:

php --version

Now that you have the latest version of PHP installed, you can proceed and install the rest of the stack.

Install Apache

Installing apache is very straight forward. Just execute the following commands to install, start and enable the Apache service:

sudo yum install httpd
sudo systemctl start httpd.service
sudo systemctl enable httpd.service

Install MariaDB

Installing MariaDB, the open-source fork of MySql, you should run the following commands:

sudo yum install mariadb-server mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb

To setup MariaDB run the secure installation script with the following command:

sudo mysql_secure_installation

and follow the on-screen messages.

All you have to do now is to restart the Apache service with the command:

sudo systemctl restart httpd.service

Test the PHP installation

You can test the PHP installation in your browser by creating a info.php file inside /var/www/html/ (default DocumentRoot). Then you add the following line inside the file:

<?php phpinfo(); ?>

Configure the Firewall for Apache

If you have the firewall installed, as I showed you in a previous post, you should configure it for the Apache webserver. To do this, execute the following commands:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Now you could do a system reboot and start creating your Virtual Hosts for your websites. You can then install Wordpress, Drupal or Jekyll to create your web content.

The options are endless.

Enjoy!