How To Install Linux, Apache, MySQL, PHP (LAMP Stack) on Ubuntu

314 0
314 0
How To Install Linux, Apache, MySQL, PHP (LAMP Stack) on Ubuntu

A LAMP  Stacks (Linux, Apache, MySQL, PHP) is a group of open source software that is used to host web content. LAMP Stack allows the creation and hosting of websites and web applications. This guide will show you how to install a LAMP stack on an Ubuntu 14.04 (LTS) server.

This LAMP stack guide is written for a non-root user. Commands that require root privileges are prefixed with sudo.

Before you begin, update your server by running the following command:

Step-by-step LAMP Stack Installation Guide

1. Prerequisites

sudo apt-get update && sudo apt-get upgrade

2. Install & Configure Apache

Apache Install

Apache is a free open source software and it runs on more than 50% of the world’s web servers.

To install Apache on your server, just open the terminal and type the following commands:

sudo apt-get install apache2

Once you are done with Apache installation, edit the main Apache configuration file, apache2.conf, to configure the KeepAlive

Sudo vim /etc/apache2/apache2.conf
KeepAlive Off

The Default multi-processing module for Apache is the event module, but PHP uses the prefork module by default. You need to open the mpm_prefork.conf file located in /etc/apache2/mods-available and edit the configuration by running the following command:

sudo vim /etc/apache2/mods-available/mpm_prefork.conf
<IfModule mpm_prefork_module>
        StartServers            2
        MinSpareServers         6
        MaxSpareServers         12
        MaxRequestWorkers       39
        MaxConnectionsPerChild  3000

Now, disable the event module and then we need to enable the prefork module by using the following command:

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork

Finally, restart the Apache server:

sudo service apache2 restart

To check if Apache is installed correctly, enter the server’s IP address in the browser (eg. The page should display “It works!” texts. In case you do not know the IP address of your server, run the following command:

ifconfig eth0 | grep inet | awk '{ print $2 }'

Apache Configuration

Once you are done with the installation and basic configuration. Now we need to configure Virtual Hosts, just like Server Blocks in NGINX. There are several ways to setup the Virtual Hosts for your website but I am going to show you the most recommended method.

You need to create a new configuration file for your website in the /etc/apache2/sites-available/ directory.

sudo vim /etc/apache2/sites-available/

Note: Replace with your domain name.

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/
     ErrorLog /var/www/html/
     CustomLog /var/www/html/ combined
     <Directory /path/to/public/website/>
        Require all granted

Now, Create the above-referenced directories:

sudo mkdir -p /var/www/html/
sudo mkdir /var/www/html/

Now you need to link your virtual host file from the sites-available directory to the sites-enabled directory. It can be accomplished by running the following command:

sudo a2ensite

Finally, reload Apache:

sudo service apache2 reload

3. Install MySQL

MySQL is database management system will be required for organising and retrieving data. It can be installed by using the following command:

sudo apt-get install mysql-server

During the MySQL installation process, you will be asked to set a root password. Choose a strong password and keep it in a safe place for future reference.

Harden MySQL Server

Hardening the MySQL Server using mysql_secure_installation script addresses several security concerns. This will remove anonymous users and test database to make your MySQL Server more secure. You can harden your MySQL Server using the following command:

sudo mysql_secure_installation

Now, you will have a choice to change the root password of the MySQL. It is highly recommended to remove the anonymous users, and disable root logins outside of localhost. You can change your root password if you feel that it is weak. It is recommended that you answer yes to the other options as it will harden your MySQL.

The MySQL database software is now installed and well configuration. Now you are ready to go.

MySQL Root Login

To log into MySQL as the root user:

mysql -u root -p

Now you get an option where you can enter the root password that you assigned earlier when the mysql_secure_installation script was run.

Create a MySQL Database

Now create a database for your website and assign a user with permissions to it. In this example, I have taken the domain name as a database name i.e., example, the user example-user and password example-password:

create database example;
grant all on example.* to 'example-user' identified by 'example-password';

Finally, Exit MySQL:


4. Install PHP for Processing

Now we are done with Apache and MySQL installation. We need to connect all these things together to generate a dynamic website. For that processing purpose, we need to install PHP.

PHP, PHP Extension and Application Repository can be installed using the following command:

sudo apt-get install php5 php-pear

If you need MySQL support also installs php5-mysql

sudo apt-get install php5-mysql

Once PHP5 is installed, we need to make some configuration inside /etc/php5/apache2/php.ini to enable more detailed error reporting, logging, and performance. Open the terminal and run the following command:

sudo vim /etc/php5/apache2/php.ini

Now make sure these lines are uncommented and follow these setting:

error_log = /var/log/php/error.log
max_input_time = 30

Now we need to create log directory for PHP and assign permission to the Apache user:

sudo mkdir /var/log/php
sudo chown www-data /var/log/php

Reload Apache:

sudo service apache2 reload

Congratulations! You have successfully installed and tested LAMP Stack installation. Now you will be able to host PHP based website using Ubuntu, Apache Web Server, PHP and MySQL.

Read: How To Install Linux, Nginx, MySQL, PHP (LEMP Stack) on Ubuntu

Hope you will find this tutorial useful. If you find any difficulty during LAMP Stack installation, feel free to send me an email at [email protected] or put query in the comment section below.

In this article

Join the Conversation