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

How to install Apache, PHP 7.2 and MySQL on CentOS 7.4 (LAMP)

This tutorial shows how to install an Apache web server on CentOS 7 server with PHP (mod_php) and MySQL database. The acronym LAMP is short for Linux, Apache, MySQL, PHP.

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

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


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.

Install Apache http server on CentOS

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 as default is quite old (PHP 5.4). Therefore I will show you in this chapter 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 to chapter 4.1. To install PHP 7.0, follow the commands in chapter 4.2, for PHP 7.1 chapter 4.3 and for PHP 7.1, use chapter 4.4 instead. Follow just one of the 4.x chapters and not all of them as you can only use one PHP version at a time with Apache mod_php.

4.1 Install PHP 5.4

To install PHP 5.4, run this command:

yum -y install php

4.2 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

4.3 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

4.4 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

In this example and in the downloadable virtual machine, I'll use PHP 7.2.

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.2 installed on CentOS 7

As you see, PHP 7.2 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 php-mysqlnd 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-mysqlnd php-pdo

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

MySQL support added to PHP 7.2 on CentOS

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 installed on CentOS 7.4

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:

32 Comment(s)

Add comment

Please register in our forum first to comment.


By: Tristan

thank you so much work directly :-)

By: Coeus

 Very useful! Thank you so much!

By: Dev


Can you please tell how can we install specific version of MySQL. I need to install MySQL 5.6.23 on CentOS Linux 7.4.1708.


By: vagg

Why are you changing phpmyadmin from cookie authentication to http authentication?

By: Ron Christie

Hmmm... Unless I missed it somehow, it seems you're short of the instructions on how to configure httpd to load and make use of



By: till

There is nothing missing, just follow the tutorial and you will see that it works.

By: Luiz De Paula

Boa noite à todos!

Esse é um dos melhores tutoriais que encontrei. Simples, direto e didático.

Obrigado, obrigado, obrigado!

Um abraço do Brasil!

"Good evening everyone!

This is one of the best tutorials I have found. Simple, straightforward and didactic.

Thank you, thank you, thank you!

A hug from Brazil!"PS: Forgive me the translation via Google! ;O)

By: trust sibanda

Thanks worked as expected

By: rockanguro

Thanks so much

By: Aneesh

After the installation I tried to upgrade plugins from wordpress admin, getting permission denied.  The permission and ownership is of all files and folders are correct. (I am not suing FTP or vSFTP)

How to fix this?

By: Anand

very nice & helpful article, thanks

By: Amaton

qué va gracias 

By: Jim

 Thanks, terrific help.

By: John Leger

Excellent and accurate walkthough!  Thank you.

By: Jose Antony


Thanks for the tutorial, it has been a great help, greetings from Peru

By: Udi Burg

Great tutorial!!!

Thanks a lot!

By: Marcos

PHP 7 listen to port number 9000. Didn´t work for me until i´ve added this line to configuration file

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://$1There´s an workaround to this method?Thanks!

By: till

This tutorial installs and uses the apache mod_php module. You seem to use php-fpm, that's a completely different kind of setup that you use and not related to the setup described in this tutorial. For the tutorial above, the line you described is neither useful nor required.

By: Aimen

Step 4.4, installs 7.3 not 7.2. is there a fix for that cause 7.3 has some issues with other dependencies

By: till

The PHP version is selected by the yum-config-manager command that you run before you install PHP. Are you sure that you've run "yum-config-manager --enable remi-php72" and not "yum-config-manager --enable remi-php73" ?

By: chaiwat

Your tutorial save my life. Thanks brother.

By: Steve T

Thank you

By: jerome

This tutorial is perfect for settings up everything you need for a development server. Thank you for the great article.

Regards Jerome

By: jeroenvip

This is working like a charm. Thanks for the effert !


Regards Jerome

By: ryan

Hey! Great tuttorial, however when i get to testing the index.php i get a "Error establishing a database connection" error. I do run MySQL instead of MariaDB however. Any clue of what that would cause?I did: "yum -y install php-mysqlnd php-pdo"and: yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel" and restarted httpd (after also the whole server machine). Many thanks!

By: Randeer

Amazing stuff, way u are describing it much easy to understand.

Very useful!; thank you so much work directly

By: moses

thank you so much it's work directly

By: faruk yetkin

add to optional http2/mod_http2 installation proccess/steps,

i'm tryed

"yum install httpd2" -> getting error: No package httpd2 available.

"yum install mod_http2" -> getting error: No package mod_http2 available.

any solution ?

By: Guillermo Macias

All was cute; but when trying to start phpmyadmin it does not functions

By: Rjeibi Omar

Thank you very much Brehm

By: Raghav

In general, I always browse the net with ads blocked on my browser. But, not on howtoforge! Quality work needs to be apreciated and disabling ad blocker for this site is the least we can do to apreciate this high quality work!

Keep it up Till! Great Job!

By: till

Thank you very much. I really appreciate that! I would not be able to get howtoforge going without the earnings from ads and subscriptions.