How to Install and Configure a LAMP Stack on CentOS 7 (Linux, Apache, MySQL, PHP)

How to Install and Configure a LAMP Stack on CentOS 7 (Linux, Apache, MySQL, PHP)
If you like it, Share It!!!

A LAMP stack is a group of software that is installed to enable a server to host dynamic websites and web apps. LAMP represents a Linux operating system, with Apache web server installed, and the site has an MySQL database and dynamic content is processed by PHP.

Before doing anything always make sure you are up to date on your server

# yum update yum -y

# yum update -y

In this guide we will install and configure a LAMP stack on CentOS 7. I recommend installing a few extra programs to help out these are up to you.

vim – is a file editor

wget- will download a file from a web address

rsync- will sync a folder to a destination you specify.

To install those here is a quick command to install them quickly:

# yum install -y vim wget rsync

Installing and Setting-up Apache

Now we will install Apache and get they service up and running:

# sudo yum install httpd

# sudo systemctl start httpd.service

These two commands will install and Start the Apache service now you should be able to go to http://your_IP_address/ You should get a page that looks something like the one below.

If you get the page above we know then that the Apache part of the Web server is running. Now we will enable Apache to start on boot up.

# sudo systemctl enable httpd.service

Install and Setup MySQL (MariaDB)

Now that we have that done we will setup the MySQL Database, we will be installing MariaDB. To do this we use the following commands:

# sudo yum install mariadb-server mariadb

Once it is installed we want to start the service and enable it to start when the server starts up.

# sudo systemctl start mariadb

# sudo systemctl enable mariadb

Now we want to run a simple security script that will remove the defaults and lock down the access to the database. To do so you want to run the following command:

# sudo mysql_secure_installation

You will be prompted for your current root password. Since we have not set it up you wont have one, just leave it blank. Then it will ask you to enter a new password. Once you have the password it will look something like this.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

For the rest of the questions you should be able to just hit “Enter”, once this is done your Database is setup.

Install and Setup PHP

Now we will install PHP, First tho we need to make sure we are installing the correct version. If you skip these first couple steps and just install PHP you will install the old PHP 5.4 version. It is out of date and not supported by many new web services like wordpress or owncloud.

First we will install the EPEL Repo so we can also install phpMyAdmin later to manage our database’s a little easier.

# rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY*

# yum -y install epel-release

# rpm -Uvh

# yum -y install yum-utils

Now we have the Repo’s to install PHP 7 and phpMyAdmin. Below i will show you how to install the 3 most common versions of PHP 7.

Install PHP 7.0

We can install PHP 7.0 and the Apache PHP 7.0 module as follows:

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

Install PHP 7.1

If you want to use PHP 7.1 instead, use:

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

Install PHP 7.2

If you want to use PHP 7.2 instead, use:

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

We must restart Apache to apply the changes:

# systemctl restart httpd.service

Testing PHP

The document root for apache is /var/www/html. We can create a small PHP file (info.php) in the default directory. This will display the version of PHP you have installed and what plugin’s you have installed. To create the file use the following command.

vim /var/www/html/info.php

Enter the Following two lines in the file:


Now we call that file in a browser (e.g. It should look like the following:

Now that we need to install some plugins for PHP to work with the MySQL and some other plugins that are required for wordpress, owncloud and other web apps.

# yum -y install php-mysqlnd php-pdo 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

If you don’t need the php info output anymore, then delete that file for security reasons.

# rm /var/www/html/info.php

phpMyAdmin Installation

phpMyAdmin is a web interface through which you can manage your MySQL databases.

# yum -y install phpMyAdmin

Now we configure phpMyAdmin. We change the Apache configuration so that phpMyAdmin allows connections not just from localhost (by commenting out the <RequireAny> stanza and adding the ‘Require all granted’ line):

# vim /etc/httpd/conf.d/phpMyAdmin.conf

Next, we change the authentication in phpMyAdmin from cookie to http:

# vim /etc/phpMyAdmin/

Change the Fallowing line from “cookie” to “http”

$cfg[‘Servers’][$i][‘auth_type’] = ‘http’; // Authentication method (config, http or cookie based)?

Now we can Restart Apache:

# systemctl restart httpd.service

Afterwards, you can access phpMyAdmin under

Now you are all set to run what ever web apps you want. I use this setup on Multiple web-servers I run. Some are Stand alone for a single page, others are for Multiple website’s on one server. To have Multiple domains we have to make some changes to the Apache Config for that go to the post on Virtual Domains. There I will show you how to setup Multiple domains on a web-server.

If you like it, Share It!!!

Reader Comments

Leave a Reply

Your email address will not be published. Required fields are marked *