Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: How to install LAMP stack (ApacHow to instalhe, MariaDB/MySQL and PHP) on CentOS  (Read 1689 times)

0 Members and 1 Guest are viewing this topic.

akhilt

  • Guest
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 platform.

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
Code: [Select]
$ yum install httpd
Start Apache HTTP Server and Configure Firewall
Code: [Select]
$ service httpd start
$ chkconfig httpd on
$ iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
$ service iptables save

Test Apache HTTP Server

To test the installation, check if httpd daemon is up and running successfully.
Code: [Select]
$ 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.

Note that the default document root directory of httpd is /var/www/html on both CentOS 6.

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.

Below is how to install MariaDB/MySQL server, and set it up to start automatically upon boot.
Code: [Select]
$ yum install mysql-server
$ service mysqld start
$ 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.
Code: [Select]
$ 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.
Code: [Select]
$ 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:
Code: [Select]
$ 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.
Code: [Select]
$ 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.
Code: [Select]
date.timezone = "America/New_York"
Don't forget to restart httpd after installing PHP.
Code: [Select]
$ 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.
Code: [Select]
$ 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.
Code: [Select]
$ 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!  8)