How to easily install LAMP on ubuntu 16.04 – DebYum

share on:
install lamp on ubuntu

A LAMP consists of Linux Operating System, Apache web server, MySQL Database Server and PHP, is a web stack in which software are installed together so that it can be used for hosting web content.

For this tutorial, I am using DigitalOcean droplet with 512 MB RAM.

About this page:


Before installing anything we will first update our server.

[email protected]:~# apt-get update &&  apt-get upgrade


INSTALL APACHE 2.4. in Ubuntu 16.04

we will install Apache 2.4 in our Ubuntu 16.04 server.

[email protected]:~# apt-get install apache2 -y


Edit the main Apache configuration file /etc/apache2/apache2.conf  , to adjust the  KeepAlive settings:

sed -i '/KeepAlive On/c\KeepAlive Off' /etc/apache2/apache2.conf


Activate the Rewrite module

This module operates on the full URLs (including the path-info part) both in per-server context (httpd.conf) and per-directory context (.htaccess)and can generate query-string parts on result. The rewritten result can lead to internal sub-processing, external request redirection or even to an internal proxy throughput.

We can enable any module in apache with a2enmod  command

[email protected]:~# a2enmod rewrite


Activate the Prefork Module.

we will enable the PreFork Module as Php uses prefork module by default.

But the default multi-processing module (MPM) for Apache is the event module and before enabling prefork module we need to disable the event module.

To disable this module we will use  the a2dismod  command.

[email protected]:~# a2dismod mpm_event 
[email protected]:~# a2enmod mpm_prefork


Restart Apache to implement your changes.

[email protected]:~#  systemctl restart apache2


Setting  ServerName globally to Suppress Syntax Warnings.

First thing first, we will take care of an annoying warning message about not being able to reliably determine the server’s fully qualified domain name, which appears during restarting Apache server or checking for syntax error.

This warning can be easily removed via :

[email protected]:~#  echo "ServerName localhost" >> /etc/apache2/apache2.conf



If you want to set your IP Address to your server name then you can use.

[email protected]:~# IPOFSERVER=ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'
roo[email protected]:~# echo "ServerName $IPOFSERVER" >> /etc/apache2/apache2.conf


Next, check for syntax errors by typing:

[email protected]:~# apachectl -t
Syntax OK


Restart Apache to implement your changes:

[email protected]:~#  systemctl restart apache2


Allow Apache through Firewall.

we will allow Apache to communicate through Firewall.

[email protected]:~#  firewall-cmd --zone=public --add-service=http --add-service=https --permanent
[email protected]:~#  firewall-cmd --zone=public --add-port=80/tcp --add-port=443/tcp --permanent
[email protected]:~#  firewall-cmd --reload


Also Allow Apache through Ubuntu Firewall.

[email protected]:~# sudo ufw allow in "Apache Full"


To check if Apache is working properly, you can always visit your IP-ADDRESS in your browser.

Or you can install elinks or w3m like packages to check apache’s working from command line.

[email protected]:~# apt-get install elinks -y
[email protected]:~# elinks http://server-ip-address


Installing MySQL 5.7.x.

After installing Apache, we can now proceed to install MySQL server.MySQL is a database management system. The default version of MySQL server provided in Ubuntu 16.04 is MySQL 5.7.x. MariaDB 10 is also available in Ubuntu repositories.

To install MySQL, we can use.

[email protected]:~# sudo apt-get install mysql-server -y


Start Mysql server and also enable it to start at reboot.

[email protected]:~# systemctl enable mysql.service
[email protected]:~# systemctl start mysql.service


Check this post : Complete Guide To Install, Update & Disable Strict SQL Mode In MySQL 5.7.

Securing the Database Server.

We need to secure the Database server by running this script  ( mysql_secure_installation ) in the shell.

[email protected]:~# mysql_secure_installation
Enter current password for root (enter for none):
Set root password? [Y/n]   y

New password:
Re-enter new password:

Remove anonymous users? [Y/n]   y

**** If you don't want to access your database remotely then enter 'y' otherwise enter 'n' ****

Disallow root login remotely? [Y/n]   y

Remove test database and access to it? [Y/n]   y

Reload privilege tables now? [Y/n]   y



Stopping MySQL Service .

If you want to stop MySQL service use,

[email protected]:~# systemctl stop mysql.service


Installing PHP.

PHP is used to run scripts, connect to the Database server ( MySQL ) to fetch data and process all the data to display dynamic content.

we will install additional packages so that our PHP can connect with MySQL database and these packages are often required by most of the applications.

[email protected]:~#  apt-get install php libapache2-mod-php php-gd php-json php-curl php-intl php-mcrypt php-mysql php-imagick php-zip php-xml php-mbstring -y


To make Apache serve php files first instead of html files, we will need to edit  /etc/apache2/mods-enabled/dir.conf

[email protected]:~# echo -e "<IfModule mod_dir.c>\n    DirectoryIndex index.php index.html index.cgi index.xhtml index.htm\n </IfModule>" > /etc/apache2/mods-enabled/dir.conf


To test the working of PHP , we can use this code to display PHP related info of your server.

[email protected]:~# echo -e "<?php\nphpinfo();\n?>" > /var/www/html/info.php


Restart Apache server.

[email protected]:~# systemctl restart apache2



With LAMP stack installed, you can now install a different kind of applications or CMS like PrestaShop  or a website on your server.

And if you want are in little bit hurry to install LAMP on Ubuntu 16.04 test server then you can visit Page 2 of this article.

I have tried to cover all the basic to advance concepts with their examples.

Still, if I have missed anything please update us through comment box. I will keep updating the same based on feedback’s received.

Thanks for visiting this page and Have a Good day. 🙂

share on:


Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Don't forget to leave the feedback. Have a great day. :)

Leave a Response

share on: