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.
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.
$ sudo yum install httpd
$ sudo systemctl start httpd $ sudo systemctl enable httpd $ sudo firewall-cmd --zone=public --add-port=80/tcp --permanent $ sudo firewall-cmd --reload
$ sudo service httpd start $ sudo chkconfig httpd on $ sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT $ sudo service iptables save
To test the installation, check if
httpd daemon is up and running successfully.
$ sudo systemctl status httpd
$ 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 (
Note that the default document root directory of
/var/www/html on both CentOS 6 and 7. Let's move on to the next step.
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.
$ sudo yum install mariadb-server $ sudo systemctl start mariadb $ sudo systemctl enable mariadb
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.
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-mysqlThe
phppackage adds PHP support to Apache HTTP server, and the
php-mysqlpackage 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
After you answer a series of questions, the
tzselect will print out your timezone string (e.g.,
/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.
$ sudo systemctl restart httpd
$ 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(); ?>
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!
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 Xmodulo.com 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 Xmodulo.com as the original source.