How to install LAMP stack (Apache, MariaDB/MySQL and PHP) on CentOS

Last updated on November 4, 2014 by Dan Nanni

LAMP stack is a popular server-side software stack which is used to build and run dynamic web sites and web applications on Linux platforms. The LAMP stack is composed of Apache (as an HTTP server), MariaDB or MySQL (as a database backend), and PHP, Perl or Python (as a server-side programming language), and hence the acronym "LAMP." Other variants of the LAMP stack exist, such as LEMP (Nginx, MySQL, PHP), LAPP (Apache, PostgreSQL, PHP), LLPR (Lighttpd, PostgreSQL, Ruby on Rails), and so forth.

In this tutorial, I describe how to install and set up the LAMP stack with Apache, MariaDB/MySQL and PHP on CentOS server. This tutorial is applicable to CentOS 6 as well as CentOS 7 platforms. If you are looking to install LAMP stack on Ubuntu server, refer to this tutorial instead.

Step One: Apache HTTP Server

As the first step, let's install Apache HTTP server on CentOS. We will also do basic configuration for Apache server afterwards, such as adding Apache service to auto-start list, and opening an HTTP port in the firewall.

Install Apache HTTP Server

$ sudo yum install httpd

Start Apache HTTP Server and Configure Firewall

On CentOS 7:

$ sudo systemctl start httpd
$ sudo systemctl enable httpd
$ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
$ sudo firewall-cmd --reload

On CentOS 6:

$ sudo service httpd start
$ sudo chkconfig httpd on
$ sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$ sudo service iptables save

Test Apache HTTP Server

To test the installation, check if httpd daemon is up and running successfully.

On CentOS 7:

$ sudo systemctl status httpd

On CentOS 6:

$ sudo service httpd status
httpd (pid  2069) is running...

After confirming the status of httpd, open a web browser, and go to http://<web-server-ip-address> to see if you can load the default Apache web page. The screenshot below shows the default Apache web page on CentOS 6 ( and CentOS 7 (

Note that the default document root directory of httpd is /var/www/html on both CentOS 6 and 7. Let's move on to the next step.

Step Two: MariaDB/MySQL

The next step is to set up a database backend for the LAMP stack, for which we have two choices: MySQL and MariaDB. While CentOS/RHEL 6 ships with MySQL server/client packages, CentOS/RHEL 7 moves away from MySQL, and instead offers MariaDB, a community-developed fork of MySQL, as a default database.

Below is how to install MariaDB/MySQL server, and set it up to start automatically upon boot.

On CentOS 7:

$ sudo yum install mariadb-server
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb

On CentOS 6:

Install MySQL server/client package, and start MySQL server as follows.

$ sudo yum install mysql-server
$ sudo service mysqld start
$ sudo chkconfig mysqld on

As MariaDB and MySQL are compatible with each other in terms of APIs and command-line usage, the LAMP stack can be configured and operated pretty much the same way regardless of whether you choose MariaDB or MySQL.

As a security precaution, run the following add-on script which is included in the MariaDB/MySQL server package.

$ sudo mysql_secure_installation

This script will reconfigure the database server for server hardening purposes. For example, it will change (empty) root password, remove anonymous user, disallow remote root login, and remove a default test database.

Step Three: PHP

The last step in setting up the LAMP stack is to install PHP, a server-side scripting language which is responsible for creating dynamic web pages for users. At a minimum, the LAMP stack requires the following two packages installed.

$ sudo yum install php php-mysql
The php package adds PHP support to Apache HTTP server, and the php-mysql package allows PHP applications to access MariaDB/MySQL server. Besides those two required packages, there are many other useful PHP modules you can install depending on your requirements. For example:

You can get a full list of available PHP modules by running:

$ yum search php-

Next, let's change the default timezone used by PHP applications. You will need to find out your timezone by using tzselect command.

$ tzselect

After you answer a series of questions, the tzselect will print out your timezone string (e.g., America/New_York). Open /etc/php.ini file with a text editor, and add the following line.

date.timezone = "America/New_York"

Don't forget to restart httpd after installing PHP.

On CentOS 7:

$ sudo systemctl restart httpd

On CentOS 6:

$ sudo service httpd restart

Finally, let's check whether PHP is working properly. For this, use the following command, and check if the output of phpinfo() shows up correctly.

$ php -r "phpinfo();" | more

Once you verify PHP command-line output, let's create a test PHP file as follows, and verify that the PHP file is loaded successfully by Apache HTTP server.

$ sudo vi /var/www/html/test.php
<?php phpinfo(); ?>

Go to http://<web-server-ip-address>/test.php in your web browser. You should see the following output.

Now you have successfully set up the LAMP stack!

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean