There is a new version of this tutorial available for CentOS 7.6.

How to install Apache, PHP 7.1 and MySQL on CentOS 7.3 (LAMP)

This tutorial shows how you can install an Apache webserver on a CentOS 7 server with PHP support (mod_php) and MySQL support. LAMP is short for Linux, Apache, MySQL, PHP.

This updated tutorial shows the installation of the latest PHP versions (7.0 and 7.1) on CentOS 7.3.

1 Preliminary Note

In this tutorial, I use the hostname with the IP p These settings might differ for you, so you have to replace them where appropriate.

I will add the EPEL repo here to install latest phpMyAdmin as follows:

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

To edit files on the shell, I'll install the nano editor. If you prefer vi for file editing, then skip this step.

yum -y install nano

2 Installing MySQL / MariaDB

MariaDB is a MySQL fork of the original MySQL developer Monty Widenius. MariaDB is 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

Then we create the system startup links for MySQL (so that MySQL starts automatically whenever the system boots) and start the MySQL server:

systemctl start mariadb.service
systemctl enable mariadb.service

Set passwords for the MySQL root account:

[[email protected] ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found


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): <--ENTER
OK, successfully used password, moving on...

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

Set root password? [Y/n]
New password: <--yourmariadbpassword
Re-enter new password: <--yourmariadbpassword
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] <--ENTER
 ... 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] <--ENTER
 ... 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] <--ENTER
 - 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] <--ENTER
 ... 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!
[[email protected] ~]#

3 Installing Apache

CentOS 7 ships with apache 2.4. Apache is directly available as a CentOS 7 package, therefore we can install it like this:

yum -y install httpd

Here a screenshot of the installation process.

Now configure your system to start Apache at boot time...

systemctl start httpd.service
systemctl enable httpd.service

To be able 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

Now direct your browser to the IP address of your server, in my case, and you should see the Apache placeholder page:

Apache web server started on CentOS 7

4 Installing PHP

The PHP version that ships 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.

yum -y install yum-utils

and run yum update

yum update

Now you have to chose which PHP version you want to use on the server. If you like to use PHP 5.4, then proceed with the next command. To install PHP 7.0, follow the commands in chapter 4.1 and for PHP 7.1, use chapter 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 / Getting Details About Your PHP Installation

The document root 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. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

nano /var/www/html/info.php

Now we call that file in a browser (e.g.

PHP 7.1 on CentOS 7

As you see, PHP 7.1 is working, and it's working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP. MySQL is not listed there which means we don't have MySQL support in PHP yet.

6 Getting MySQL Support In PHP

To get 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. You can search for available PHP5 modules like this:

yum search php

Pick the ones you need and install them like this:

yum -y install php-mysql

In the next step I will install some common PHP modules that are required by CMS Systems like Wordpress, Joomla, and Drupal:

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. You should now find lots of new modules like curl etc there.:

CentOS 7 with PHP 7.1 and MySQL support

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

rm /var/www/html/info.php

7 phpMyAdmin installation

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

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):

nano /etc/httpd/conf.d/phpMyAdmin.conf
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
# </RequireAny>
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 </Directory>


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

nano /etc/phpMyAdmin/
$cfg['Servers'][$i]['auth_type']     = 'http';    // Authentication method (config, http or cookie based)?

Restart Apache:

systemctl restart  httpd.service

Afterwards, you can access phpMyAdmin under

PHPMyAdmin on CentOS 7

8 Download as virtual machine

This setup is available as virtual machine download in ova/ovf format (compatible with VMWare and Virtualbox) for howtoforge subscribers.

Login details for the VM

  • The Linux root password is: howtoforge.
  • Rhe MySQL root password is: howtoforge

Please change both passwords on the first login.

  • The IP address of the VM is


Share this page:

23 Comment(s)

Add comment

Please register in our forum first to comment.


By: Nadun Ranaweera

Thank you.!

By: Ben

Thanks, but this doesn't work.  PHP 5.4 is installed instead.

By: Tom

I installed my server with this tutorial and I got PHP 7.1 at the end, so you must have done something wrong when you got 5.4. The guide shows to install 3 different PHP versions, use the option for the PHP version that you want to get only.

By: Andreas

Awesome. Thanks!

By: Tim

Thank you bud. Worked Flawlessly

By: xiang

 Thanks for your good service!

By: soufiane

thanks , very helpful

By: Luis

Thank you!!!! very very nice

By: Juan

Excellent tutorial!!! I have installed phpmyadmin succesfully. I had been trying many things helpless, but I have found the final solution here. Thank you very much. This tutorial works.!!! God bless you.

By: ali

thanks for not-so-ancient versions

By: SatisfiedIndeed

Thank you, this is the ONLY tutorial that I have ever got to work 100% without any errors during the process. I have used many different tutorials for creating a lamp server and have always had to change at least one command. This is on point, I apprieciate that. 

By: anand

How can I fully hide my IP when redirecting my domain to my web server?

By: Walter

Thanks but cannot get Php to work, even 5.4 . Can I get Php 7 to work with Centos 7.2 ? I need bcmaths which is not available on Php 5.4 .

By: Alberto

Thank you!!! No error, minimal steps and works like a charm.

By: robinhook

Cannot instal phpmyadmin via yum it drains php 5.4 deps

By: hsabrey

it is really very nice tutorial, many thanks.

just wanted to know if i have a server and i wan to: 

- redirect the Server_IP:Port to a spesific folder insude the server /var/www/html/website1

- mandg to set the Server port so Port=1234 redirect user to website1; Port:2345 redirect user to website2 etc

"someone will say google-it, the question; what is this named to google it???

and many thanks

By: Jucema 89

Thank you.! 


a whole week trying to configure my virtual machine with LAMP, but always errors, especially mysql with php7 ... Thanks! everything runs well for Centos 7 VPS of GoDaddy

By: Larry

Thank you for your effort and time

Excellent job as usual. I have to tell it like it is, EXCELLENT ARTICLE. Not a flaw anywhere.

By: Ugur

Thanks. Worked 7.1

By: Lazy Admin

Flawless walkthrough. Thank you for saving my time- now I just walk out to the real life!

By: Damir Zubovic

Thanks for great manual.

By: buddyo

Thanks, it works!

By: Barclay

everything works great till I try to install myadmin.  I ran  yum -y remove php* before doing anything above. When I get to phpmyadmin install I get this error 


 --> Processing Dependency: php-zip for package: phpMyAdmin-

Package php72w-common-7.2.2-1.w7.x86_64 is obsoleted by php-common-7.2.2-1.el7.remi.x86_64 which is already installed

--> Finished Dependency Resolution

Error: Package: phpMyAdmin- (epel)

           Requires: php-zip

           Available: php-common-5.4.16-42.el7.x86_64 (base)


           Available: php-common-5.4.16-43.el7_4.x86_64 (updates)


           Available: php-pecl-zip-1.15.1-1.el7.remi.7.0.x86_64 (remi-php70)

               php-zip = 1:1.15.1-1.el7.remi.7.0

           Available: php-pecl-zip-1.15.1-1.el7.remi.7.1.x86_64 (remi-php71)

               php-zip = 1:1.15.1-1.el7.remi.7.1

           Available: php-pecl-zip-1.15.1-2.el7.remi.7.2.x86_64 (remi-php72)

               php-zip = 1:1.15.1-2.el7.remi.7.2

           Available: php-pecl-zip-1.15.2-1.el7.remi.7.0.x86_64 (remi-php70)

               php-zip = 1:1.15.2-1.el7.remi.7.0

           Available: php-pecl-zip-1.15.2-1.el7.remi.7.1.x86_64 (remi-php71)

               php-zip = 1:1.15.2-1.el7.remi.7.1

           Available: php-pecl-zip-1.15.2-1.el7.remi.7.2.x86_64 (remi-php72)

               php-zip = 1:1.15.2-1.el7.remi.7.2

           Available: php55w-common-5.5.38-1.w7.x86_64 (webtatic)


           Available: php56w-common-5.6.33-1.w7.x86_64 (webtatic)


           Available: php70w-common-7.0.27-1.w7.x86_64 (webtatic)


           Available: php71w-common-7.1.13-1.w7.x86_64 (webtatic)


           Available: php71w-common-7.1.14-1.w7.x86_64 (webtatic)


           Available: php72w-common-7.2.1-1.w7.x86_64 (webtatic)


           Available: php72w-common-7.2.2-1.w7.x86_64 (webtatic)


           Installed: php-common-7.2.2-1.el7.remi.x86_64 (@remi-php72)

               Not found

           Available: php-common-7.0.26-1.el7.remi.x86_64 (remi-php70)

               Not found

           Available: php-common-7.0.27-1.el7.remi.x86_64 (remi-php70)

               Not found

           Available: php-common-7.1.13-1.el7.remi.x86_64 (remi-php71)

               Not found

           Available: php-common-7.1.14-1.el7.remi.x86_64 (remi-php71)

               Not found

           Available: php-common-7.2.1-1.el7.remi.x86_64 (remi-php72)

               Not found

 You could try using --skip-broken to work around the problem

 You could try running: rpm -Va --nofiles --nodigest