Oct 12, 2021
How to Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04
This tutorial teaches 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.
In 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.
What you’ll need
Before you begin with Ubuntu LAMP tutorial, you will need the following:
- A local machine with SSH client installed (see tutorial on how to use putty ssh client if you are Windows user)
- 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
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.
Step 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.
For example, if your VPS IP address is 126.96.36.199 your address should be:
You should see a page that looks like this:
Note: 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 myip.opendns.com @resolver1.opendns.com
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
Step 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.
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
● 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: Starting MySQL Community Server... Jul 11 09:39:40 abandoned-plate systemd: Started MySQL Community Server.
Step 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.
Step 3.1 – 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:
<?php phpinfo(); ?>
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
You should see a page that looks like this:
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
Note: 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 and if you have any problems, comments, ideas do let us know in the comments sections. Happy coding!