Installing LAMP on CentOS 7 (Apache, MySQL, and PHP)

This tutorial shows, Installing LAMP on CentOS 7 (Apache, MySQL, and PHP)

Lets see the installation of the latest PHP versions (7.0 and 7.1) on CentOS 7.3.

1.Adding EPEL repo

As a first step we need to add EPEL repo to install latest phpMyAdmin by using.

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

2.Installing Mysql

MariaDB is most compatible with MySQL and I’ve chosen to use MariaDB here instead of MySQL.

Run this command to install MariaDB with yum

#yum -y install mariadb-server mariadb

Now, we have to create the system startup links for MySQL and start the MySQL.

#systemctl start mariadb.service
#systemctl enable mariadb.service

Set passwords for the MySQL root account


In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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 authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]y

... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]y

... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@webhostingchennai bin]#

3.Installing Apache

CentOS 7 arrives with apache 2.4. Apache is directly available as a CentOS 7 package, therefore we can install it as

#yum -y install httpd

Screenshot of the installing process.

installing lamp on centos 7

Now, We can configure the system to start Apache at boot time.

#systemctl start httpd.service
#systemctl enable httpd.service

To access the web server from outside, we have to open the HTTP (80) and HTTPS (443) ports in the firewall. The default firewall on CentOS is firewalld which can be configured with the firewalld-cmd command.

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

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

If firewalld is not installed you can install it by using

#yum install firewalld

Now, Enter  your browser to the IP address of your server, in my case with your IP), and you should see the Apache placeholder’

4.Installing PHP

The PHP version that arrives with CentOS is quite old (PHP 5.4), therefore I will show you in this step some options to install newer PHP versions like PHP 7.0 or 7.1 from Remi repository.

Add the Remi CentOS repository.

#rpm -Uvh

Install yum-utils as we need the yum-config-manager utility, and run yum update

#yum -y install yum-utils
#yum update

Now, we have to choose which PHP version we want to use on the server. If we like to use PHP 5.4, then proceed with the next command.

To install PHP 7.0, follow the commands given in 4.1 and for PHP 7.1, use commands in 4.2 instead.

To install PHP 5.4, run this command:

#yum -y install php

4.1 Install PHP 7.0 (optional)

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

4.2 Install PHP 7.1 (optional)

If you want to use PHP 7.1 instead, use:

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

In this example and in the downloadable virtual machine, I’ll use PHP 7.1.

We must restart Apache to apply the changes:

#systemctl restart httpd.service

5 Testing PHP

The root document 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.

nano /var/www/html/phpinfo.php

Now, Enter the below line in phpinfo.php file


Now, access, to get the php details (Refer the screenshot).


6.MySQL Support In PHP

we can install the php71w-mysql package. It’s a good idea to install some other PHP modules as well as you might need them for your applications.

#yum search php

#yum -y install php-mysql #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 in your browser and scroll down to the modules section again.

7. Installing phpMyAdmin

phpMyAdmin, a web interface through which you can manage your MySQL databases.
phpMyAdmin can now be installed as follows:

#yum -y install phpMyAdmin

Configuring phpMyAdmin

nano /etc/httpd/conf.d/phpMyAdmin.conf

Configuring phpMyAdmin as follows

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8

<IfModule mod_authz_core.c>
# Apache 2.4
# <RequireAny>
# Require ip
# Require ip ::1
Require all granted
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Deny from All
Allow from
Allow from ::1

<Directory /usr/share/phpMyAdmin/>
Options none
AllowOverride Limit
Require all granted


Now, we change the authentication in phpMyAdmin from cookie to http

nano /etc/phpMyAdmin/

Locate $cfg[‘Servers’][$i][‘auth_type’] = ‘cookie’;  and modify it as below


$cfg['Servers'][$i]['auth_type'] = 'http';


Restart Apache:

#systemctl restart httpd.service

Now, you can access phpMyAdmin in browser under

Finally the LAMP is ready on our new Centos 7.3 Sever.

Hope you like this post, please share your comments to improve us.

Click here : For know about the Useful Tools for Monitoring MySQL/MariaDB

You may also like...

2 Responses

  1. Jagan says:

    Nice, this works like charm..

  2. This post will assist the internet users for building up new weblog or even a weblog from start to

Leave a Reply

Your email address will not be published.