In this tutorial we will be setting up a LAMP stack , which includes Apache 2.4, MySQL 8 or MariDB 10, PHP 7 on CentOS 7.

This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

Prerequisite

Since we have to install MySQL or Maria DB and PHP modules, we need to set up EPEL repository. EPEL (Extra Packages for Enterprise Linux) provides us the extra packages we need to install on our CentOS 7 server.

By default, CentOS 7 does not have these packages as native, so we will enable the EPEL.

Using wget, we will download and install the EPEL RPM to enable it.

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -ivh epel-release-latest-7.noarch.rpm

Once we have enabled the EPEL, we can verify it using yum.

yum repolist

We should see a list, where all the EPEL packages were shown. Now lets move forward and install our LAMP stack.

Install Apache Server

Using yum, we will be installing our Apache web server. Also, by default CentOS 7 comes with native Apache 2.4 package.

yum install httpd

Now start the http service and enable it to start at boot .

systemctl start httpd.service
systemctl enable httpd.service

CentOS 7 server comes with firewall service for network filtering. If firewall is enabled, then we need to perform the below steps.

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

Now, open web browser on a remote PC and type in the URL http://<Your server IP>. You should see a page similar to the following.

We have successfully installed our Apache web server. Lets move forward with further installation process.

Install MySQL / MariaDB

Install MariaDB

Since MariaDB is a MySQL fork of the original MySQL developer Monty Widenius, We will go with MariaDB.

yum -y install mariadb-server mariadb

After installation we need to start and enable the DB services.

systemctl start mariadb.service
systemctl enable mariadb.service

Now we need to configure the Database to complete the installation

mysql_secure_installation

We will be asked to enter the password for the MySQL root account, since
we haven’t set this yet, so just hit ENTER . Then we will be asked to set that password. we should type Y to set a root password.

To rest of all the questions, the script asks, you should press Y, followed by the ENTER key at each prompt. During this process we will remove anonymous users and the test database, disable remote root logins, and load these new rules MySQL.

Install MySQL

NOTE :This is an optional step, since MariaDB is already installed. If you want to go with MySQL instead of MariaDB, then this for you.

We need to download and add the MySQL community repository to proceed further.

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update

Now we need to install, start and enable MySQL. During the installation, we will be asked to accept the results from the .rpm file’s GPG verification. If no error or mismatch occurs, enter y.

yum install mysql-server
systemctl start mysqld

Further installation is similar to MariaDB..

mysql_secure_installation

Install PHP

Now to generate dynamic content, we need to install PHP. We can get a full list of available PHP modules and extensions using the following command.

yum search php

To install PHP, we need to install the modules for a basic MariaDB support in PHP and PhpMyAdmin. we will be installing the following modules

yum install -y php php-mysql php-pdo php-gd php-mbstring

we need to create a test PHP file in our document root. We will create a codesposts.php file in the document root to test our configuration

vi /var/www/html/codesposts.php
/var/www/html/codesposts.php
<?php phpinfo(); ?>      

After pasting the above code to our codesposts.php file, save and close the file.

Restart the httpd service and we can visit this web page in a web browser from the below shown URL.

systemctl restart httpd.service
http://<Your_serverIP>/codesposts.php

we should see a web page that has been generated by PHP with information about your server.

Install phpMyAdmin

phpMyAdmin is a web interface, used to mange our MySQL/MariaDB database.

To avoid using MySQL command line to manage our database we will install PhpMyAdmin package by enabling CentOS 7.0 rpmforge repository by below commands.

yum install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
yum install phpmyadmin

We need to configure phpMyAdmin to allow connections from remote hosts by editing phpmyadmin.conf file

vi /etc/httpd/conf.d/phpmyadmin.conf

Comment out the following lines, using “#” and save the file.

/etc/httpd/conf.d/phpmyadmin.conf
# Order Deny,Allow
# Deny from all
# Allow from 127.0.0.1

We will be using http authentication instead of cookie based authentication. Open /etc/phpMyAdmin/config.inc.php

vi /etc/phpMyAdmin/config.inc.php

Modify the below line as follows.. and restart the apache service.

/etc/phpMyAdmin/config.inc.php
[...]
$cfg['Servers'][$i]['auth_type']     = 'http';    // Authentication method (config, http or cookie based)?
[...]
systemctl restart  httpd.service

Now after the configuration, we can check our PHP configuration from the below URL

http://<Your_serverIP>/phpmyadmin
This is a dashboard of phpMyAdmin page after fresh installation.

We will get an output something similar to the above. We have successfully completed the installation process of LAMP stack on CentOS 7.

Thanks for being with us, hope this help you in gaining knowledge and we are always ready to help you. For furthers queries or suggestions, kindly drop a comment below.