How To Set Up a LAMP Web Server From Scratch on Ubuntu 16.04

In this setup guide, we will show you the fast way to rapidly deploy a functioning web server on Ubuntu 16.04. The web server we will be deploying is the very popular LAMP stack. LAMP stands for Linux, Apache2, MySQL, PHP. We will be utilizing Postfix as a Send-Only SMTP Server.

Purchase the base VPS

We prefer Digital Ocean as our VPS (virtual private server) provider. Digital Ocean utilizes SSD (Solid State Hard Disks) in their machines. These are known for better reliability, uptime and most of all SPEED! For websites with less than 1000 visitors per month, get a VPS that is 2GB 1CPU. For sites with 1000-6000 visitors per month get the 4GB 2CPU VPS.

Create a VPS Droplet

Obtain Your Initial Credentials

By default, once you purchase a VPS from Digital Ocean they will send you an email containing your root login with your encrypted password string. We recommend making your new password very strong. We will cover that in the next steps. Take note of your credentials, you will need them later.

Get a Copy of PuTTY

PuTTY is a SHELL communication tool often used by developers to configure, manage and maintain Linux web servers. We like the way PuTTY operates so this is the one we will run with. You can download PuTTY here.


Connect to your new VPS

Open your new PuTTY application. You will be prompted for the server IP address. Use the IP address that was emailed to you by Digital Ocean and enter it where we show the designation. Make sure your port is set to 22 and you have SSH selected. Once you are ready, click on open.


Log In

Once connected, you will be prompted for the username. This by default will be root. The password you will use is the one that was emailed to you earlier. Copy the password from your email and to paste it into PuTTY you will use the right click button. Once initially logged, UNIX will prompt you to change your password. Please make your password as secure as possible.


In order to keep this post as short and user-friendly as possible, we are going to list only the bare essential commands that are needed to have a basic web server with a brief description. Follow these step by step in the order we say to do them. Any deviation will have you starting over from scratch. Trust us! If you are an experienced web developer, feel free to modify these and add security checkpoints along the way.

sudo apt-get update

This will run the standard update protocol on your LAMP server to get the latest updates and code distributions.

sudo apt-get install apache2

This will install the base Apache2 web server

— Note: Answer Y to the prompt —

sudo apache2ctl configtest

This will test the configuration of your Apache2 installation. You should see a [RUNNING] in green

sudo nano /etc/apache2/apache2.conf

This will open up your apache configuration file


— wHEN FINISHED EDITING HIT CTRL+X and then Y to save and exit —

sudo systemctl restart apache2

This will restart Apache after your configuration changes

sudo ufw app list

This will explain what Apache is and add

sudo ufw app info “Apache Full”

This will update your rules

— Now open a web browser and paste your IP address in the browser. You should see the following screen —

apache2-ubuntu-default-page apache2-ubuntu-default-page

sudo apt-get install curl
sudo apt-get install php-curl

This will install CURL

sudo apt-get install mysql-server

This will install Cthe MySQL Server answer Y and create your password for ROOT this is very important so REMEMBER IT!


This will setup your MySQL server and secure it. Best practice is to answer them in this order: N Y Y Y Y

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

This will install php and the extensions needed

— Answer Y and proceed —

sudo nano /etc/apache2/mods-enabled/dir.conf

Swap the priority of .html and .php save and close the file ctrl+x Y

sudo systemctl restart apache2

Restart Apache

sudo systemctl status apache2

THis will run a quick Apache system test. You should observe a message in Adcive: active (running)

apt-cache search php- | less

Installs Cache and Less

sudo apt-get install php-cli

Installs CLI

sudo nano /var/www/html/info.php

This adds a .php file in the web server to check if php is running see next step on what to paste into this file

<?php phpinfo(); ?>

Paste this code into the file Then save and close the file.

— IN BROWSER ACCESS: http://your_server_IP_address/info.php TEST IF PHP IS RUNNING You should see a page similar to the one below. Notate the PHP version —

PHP Info Page

sudo rm /var/www/html/info.php

This will remove the info.php testing file from your server

sudo rm /var/www/html/info.php

This will remove the info.php testing file from your server

sudo apt-get update

Run update

sudo apt-get install phpmyadmin php-mbstring php-gettext

Installs mbstring and get text

answer Y to the question and answer the config phpMyAdmin questions like this

  • For the server selection, choose apache2.
  • Select yes when asked whether to use dbconfig-common to set up the database
  • You will be prompted for your database administrator’s password
  • You will then be asked to choose and confirm a password for the phpMyAdmin application itself
sudo phpenmod mcrypt

Installs mcrypt extension

sudo phpenmod mbstring

Installs mbstring extension

sudo systemctl restart apache2

Restarts Apache

IN BROWSER ACCESS: https://domain_name_or_IP/phpmyadmin TEST IF PHPMYADMIN IS RUNNING

phpMyAdmin Login Screen phpMyAdmin Login Screen

sudo nano /etc/apache2/conf-available/phpmyadmin.conf

Modify the phpMyAdmin configuration file to allow for .htaccess overrides

ADD: AllowOverride All UNDER DirectoryIndex index.php

AllowOverride All AllowOverride All

sudo systemctl restart apache2

Restart Apache

sudo apt install mailutils

Postfix installation

postfix config

Leave all settings default

sudo nano /etc/postfix/

Configure Postfix

FIND THE LINE inet_interfaces = ALL AND REPLACE WITH inet_interfaces = loopback-only

postfix loopback postfix loopback

sudo systemctl restart postfix

Restart Postfix

echo “This is the body of the email” | mail -s “This is the subject line”

Send a test email from the command line

You should receive a test email. Check your spam as well.

apt-get update

Final update of the server

apt-get upgrade

Final upgrade of the server. Select Y when prompted

apt-get install zip unzip

Installls ZIP and UNZIP

a2enmod rewrite

Enables reqrite in Apache

service apache2 restart

Restart Apache

sudo a2enmod ssl

Installs SSL Engine


Reboots the server and makes it ready for production

Important: This installation guide is meant as a quick deployment solution. You will need to take additional steps to lock down the server and add your desired security methodologies and encryption methods. Use at your own risk