How to Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04


how to install LAMP on Ubuntu 16.04 VPS. LAMP is an acronym for Linux, Apache, MySQL, PHP. It is a popular stack for creating and deploying dynamic web applications.

n this stack, Linux serves as the operating system for the web application. MySQL is used as the database. Apache is used as the web server. PHP is used to process dynamic content. In some other variants of this stack, Perl is used instead of PHP or Python. However, for this tutorial, we are going to install PHP, as it is the most popular choice for this stack.

Before you begin with Ubuntu LAMP tutorial, you will need the following

  • A local machine with SSH client installed
  • VPS Running Ubuntu 16.04
  • A non-root user with the sudo privileges

It is recommended to use a sudo user instead of the root user to install software for security purposes. If you have root access to your VPS server you can create a sudo user with following commands

adduser <username>

The above command creates a user with your provided username. Now make the newly created user a sudoer.

usermod -aG sudo <username>

Switch to this new user.

sudo su - <username>

How LAMP works

Whenever a web page request arrives at a server it is passed on to an application called web server, in our case Apache. Apache looks for the file being requested in the URL of the request and passes this information to the PHP interpreter. It executes the logic written in that file, pulls data from the MySQL database if needed and generates a web page. Apache, our web server sends this generated web page to the client. This whole process is executed with some variations every single time when you request a web page from a LAMP server

1 – Installing Apache Web Server

Before beginning the installation, update your system and make sure you have latest packages.

sudo apt-get update
sudo apt-get upgrade

Now install Apache2 with the following command

sudo apt-get install apache2

Checking your installation

To check the installation, open your browser on your local machine and enter the following address in address bar.

http://<your vps ip address here>

For example,

if your VPS IP address is your address should be

You should see a page that looks like this

apache 2 default page

If you don’t know your VPS’s IP address, the quickest way to find it is by running following the command. This command prints the public IP address of your VPS.

dig +short

Troubleshooting your installation

If you did not see the above image don’t worry, you might have enabled the firewall. You have to enable Apache to serve web requests on port 80 and port 443 in your firewall. Install UFW.

sudo apt-get install ufw

Then allow HTTP and HTTPS traffic through the firewall.

sudo ufw allow http
sudo ufw allow https

This command enables HTTP and HTTPS traffic through the firewall. UFW is command line application called Uncomplicated Firewall. It is used to manage and make rules for Linux firewall. Now enter your VPS Ip address in your browser to check the installation. You can check the status of Apache server with the following command.

sudo systemctl status apache2

2 – Installing MySQL

MySQL is the database for your application. To install MySQL enter the following command.

sudo apt-get install mysql-server

During installation, it will prompt you for the password of the root user. Make sure to use a strong password. Don’t leave it blank.

lamp stack ubuntu mysql installation

The root user is the highest privileged user of MySQL. Using root user you can create other users for the databases. It is a good practice to create a separate user/role for the database of a new web application. You can check the status of MySQL service with the following command.

sudo systemctl status mysql

Example output

mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-07-11 09:39:40 EDT; 1min 39s ago
 Main PID: 9579 (mysqld)
   CGroup: /system.slice/mysql.service
           └─9579 /usr/sbin/mysqld

Jul 11 09:39:39 abandoned-plate systemd[1]: Starting MySQL Community Server...
Jul 11 09:39:40 abandoned-plate systemd[1]: Started MySQL Community Server.

3 – Installing PHP

PHP executes your application. Install PHP and extra modules with the following command

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-cgi php-curl php-json

This command will install the latest stable version of PHP and some extra modules that are required for web application to work.

Checking your PHP installation

Now that you have installed PHP, we will check if it is working properly by creating a test file and opening it in the browser. First, install the nano text editor

sudo apt-get install nano

Nano is a command line text editor and is easier to get started with for beginners. Click here to learn how to use the nano text editor. Now, enter the following command.

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

This command will open nano editor with a blank test.php file for editing. The directory /var/www/html where we are creating our test PHP file is known as the webroot. This is where Apache looks for the file requested in the website URL by default if it has not been configured to look somewhere else. Check out Apache Ubuntu documentation page for information about its configuration. Also, you need root privileges to be able to write to this directory. We have used sudo before our command. Now enter the following text in opened editor:


After entering this text press Ctrl + X (or CMD + X if you are on Mac), and then Y, and then hit ENTER. This will save the file and exit the editor. Now open following web address in your browser

http://<your vps ip adress>/test.php

You should see a page that looks like this:

lamp on ubuntu installed

The phpinfo() function we called inside our test.php script displays information about the PHP installation and its configuration. Now remove this test file by entering following command:

sudo rm /var/www/html/test.php
It is very important to remove this test file after checking installation because it may help an attacker gain critical information about the server configuration.

You have learned how to install LAMP on Ubuntu. After installation, you can copy your PHP files to the server and deploy your web application. You can also install phpMyAdmin to manage your databases on a web interface. Be sure to check our other VPS tutorials


  1. Link exchange is nothing else but it is only placing the other person’s
    web site link on your page at suitable place and other person will also do same for you.

  2. Hello! I could have sworn I’ve been to this website before but after browsing through some of the post I realized it’s new
    to me. Anyways, I’m definitely glad I found it and I’ll be book-marking and
    checking back frequently!

  3. Have you ever considered writing an ebook or guest authoring
    on other sites? I have a blog centered on the same subjects you
    discuss and would really like to have you share some stories/information. I know my subscribers
    would appreciate your work. If you are even remotely interested, feel free to shoot
    me an email.

  4. Hi there would you mind stating which blog platform you’re working with?
    I’m going to start my own blog in the near future but I’m having a difficult time deciding
    between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique.
    P.S My apologies for being off-topic but I had to ask!

    • We use wordpress for this and We use the newspaper v11 theme, we use the blog section of the daily newspaper type to make it fresh, simple and easy to read, the first part of the home section is based on a service website.

  5. Hey, I’m Ghazni. Recently your website caught my attention while analyzing newly registered domain name list. After entering the website, It seems that your website is brand new or not complete yet. So I thought I should try to contact you by leaving a comment on your website. Because as a Freelancer, I provide WordPress-related services like:
    1) Website Design & Development
    2) Theme Setup & Customization
    3) Full E-Commerce Website Setup
    4) Speed Optimization
    5) On-Page SEO Setup
    6) Malware Removal
    7) Landign Page Design
    8) WordPress Virtual Assistant
    I provide all kinds of premium tools like Divi, Astra, Elementor, Yoast and WooCommerce related premium themes and plugins included at no additional cost with 1 Year free updates. Usually I bought those tools from official developers with unlimited license. I can guarantee that the Plugins or Themes weren’t nulled or cracked by third party. These are completely clean and original files. The main purpose of this is to ensure that clients do not have to spend more money separately for different kind of premium tools.
    If you want to know more details about pricing, please contact me via email or whatsapp.
    You can also check my protfolio website:-
    Best regards,
    Mahmud Ghazni
    Web Design & Developer
    WhatsApp: +8801322311024

  6. Hello, this weekend is good in support of me, as this time i am
    reading this wonderful informative article here at my home.

  7. First of all I would like to say terrific blog! I had a quick question that I’d like to ask if you do not mind.
    I was curious to know how you center yourself and clear your head before writing.

    I have had difficulty clearing my thoughts in getting my thoughts out there.
    I do take pleasure in writing however it just seems like the first 10 to 15 minutes are wasted just trying to figure out
    how to begin. Any suggestions or hints? Thank you!

    • In fact, there are different methods. Before writing, make a plan to understand what you are writing for, for what group, and for what level of knowledge. Then make a plan in the article. In particular, start the article briefly to match the reading time so that the reader has a maximum reading time of 15 minutes

  8. Hello! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog article or vice-versa?
    My site covers a lot of the same subjects as yours and I believe we could greatly benefit from each
    other. If you’re interested feel free to send me an e-mail.
    I look forward to hearing from you! Excellent blog by the way!

  9. Greetings from California! I’m bored at work so I decided to browse your blog on my
    iphone during lunch break. I enjoy the knowledge you present here and can’t
    wait to take a look when I get home. I’m amazed at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, excellent site!

  10. After going over a few of the articles on your site, I honestly appreciate your
    way of blogging. I book-marked it to my bookmark webpage list and will be checking back in the near future.
    Please check out my website too and tell me your opinion.

  11. Asking questions are genuinely fastidious thing if you
    are not understanding something entirely, except
    this article gives good understanding yet.

  12. Hi there to all, how is all, I think every one is getting more from this web site, and your views are pleasant designed
    for new people.

  13. Pretty section of content. I just stumbled upon your site and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access consistently fast.

  14. Nice post. I used to be checking continuously this weblog and I’m impressed!
    Very useful info particularly the final phase 🙂 I
    care for such information a lot. I was looking for this particular info for a very lengthy time.
    Thanks and good luck.

  15. Everything is very open with a very clear explanation of the challenges. It was definitely informative. Your website is extremely helpful. Thank you for sharing!

  16. Thanks a lot for sharing this with all people you
    really know what you’re speaking about! Bookmarked.
    Please also talk over with my web site =). We can have a link change agreement between us

  17. magnificent post, very informative. I wonder
    why the opposite specialists of this sector do not
    notice this. You must proceed your writing. I’m sure, you have a huge readers’ base already!

  18. We are a group of volunteers and opening a new scheme in our community.
    Your site provided us with helpful information to work on. You’ve performed
    an impressive job and our whole community will be thankful to you.


Please enter your comment!
Please enter your name here