Knowledgebase:
Installing And Configuring Apache MariaDB And PHP On CentOS 7 (LAMP Stack)
Posted by Daniel Naval on 11 May 2019 06:33 PM

In this article, we will be installing Apache, MySQL/MariaDB with PHP 7.1 and 7.2 (mod_php) on CentOS 7 Server. This is also called as the LAMP Stack or Linux, Apache, MySQL, PHP.

Before anything else, we will try to update your CentOS 7 kernel and packages by using this command:

yum -y update

I will then add the EPEL repository for us to install the latest phpMyAdmin by using the commands below:

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
yum -y install epel-release

 

Installing MySQL / MariaDB
Next is installing the MariaDB which is a MySQL fork from the developer of MySQL. To install the MariaDB, just run the command below:

yum -y install mariadb-server MariaDB

After the installation, you need to enable and start the MariaDB. These commands will start configure it to start whenever the server starts up.

systemctl start mariadb.service
systemctl enable mariadb.service

 

Configuring MySQL Installation

mysql_secure_installation

This will ask you to enter current password for MySQL, just hit enter as there is none since it was just installed.
The system will then ask you to set the new password that you wish to use. Just enter the new password and confirm it and hit enter.
On the next part of the process, just choose Yes and hit enter to continue the configuration. The system will show a message that the MariaDB has been successfully configured.

 

Installing Apache Web Server
Now, to install the Apache Web Server, we will be using the command below:

yum -y install httpd

Once installed, we will be configure the Apache to enable and start whenever the server boots up.

systemctl start httpd.service
systemctl enable httpd.service

For the Apache Web Server to be accessed from the outside network, we will have to open the HTTP (80) and HTTPS (443) ports from the firewall. By default, the CentOS7 is using the firewalld and can be configured by using the commands below:

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

After you configure it, you can try to visit your IP address or the hostname you used for your server and the browser should show the page as below.

 

Installing PHP
Your CentOS 7 is has pre-installed PHP with version 5.4. In this part, we will show you how to install the newer versions of PHP which will be required by some applications you may use. Newer versions like PHP 7.0, 7.1 and 7.2 from the REMI Repository.
Adding the Remi Repository:

rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

We will be needing the yum-config-manager utility for enabling and disabling the PHP versions:

yum -y install yum-utils
yum -y udpdate

Once you have added the repository, you will then be installing the PHP versions available.

To install and enable PHP 7.0:

yum-config-manager --enable remi-php70
yum -y install php php-opcache

To install and enable PHP 7.1:

yum-config-manager --enable remi-php71
yum -y install php php-opcache

To install and enable PHP 7.2:

yum-config-manager --enable remi-php72
yum -y install php php-opcache

After you have enabled your desired PHP version, you need to restart the Apache Web Service to apply the changes.

systemctl restart httpd.service

 

Testing your PHP Accessibility and Informations
To test your PHP if it is working and to see the PHP informations, you need to create a simple PHP file that will display all the PHP informations of your server. Filename would be info.php and just use nano or vi then paste the script below and save the file:

nano /var/www/html/info.php
vi /var/www/html/info.php
< ?php
phpinfo();

Now, you can access the info.php on your browser and should show the page below:

yourserveripaddress/info.php

 

Congratulations! You have now setup your LAMP Stack server.