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

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.

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 (192.168.1.8) and CentOS 7 (192.168.1.11).

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:

  • php-gd: needed for image processing in PHP applications.
  • php-odbc: needed for ODBC database access in PHP applications.
  • php-pecl-memcache: needed when setting up Memcached caching daemon.
  • php-pgsql: needed for PostgreSQL database access in PHP applications.
  • php-snmp: needed for querying SNMP-managed devices in PHP applications.
  • php-xml: needed for parsing XML in PHP applications.
  • php-soap: needed to support SOAP protocol in PHP applications.
  • php-xmlrpc: needed to support XML-RPC protocol in PHP applications.

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!

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.


Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

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

  1. The easiest way to install the LAMP stack is when doing the initial installation, one of the options is to set the server up as a web server.

    Second way (assuming that the server is already up), is to use Yum to install the proper groups:

    yum groupinstall "Web Server" "MySQL Database"

    The Web Server group includes php and apache, however you may want to install more php packages as well by using:

    yum install php\*

Leave a comment

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