VPS

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

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

Introduction

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.

[optin-monster-shortcode id="stzwvu2okxyhpualnkg8"]

What you’ll need

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

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.

Switch to this new user.

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.

Now install Apache2 with the following command

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 195.110.59.211 your address should be:

You should see a page that looks like this:

Installing LAMP on Ubuntu - Apache 2 Default Page

 

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.

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.

Then allow HTTP and HTTPS traffic through the firewall.

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.

Step 2 – Installing MySQL

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

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.

Installing LAMP on Ubuntu - MySQL Password

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.

Example output:

Step 3 – Installing PHP

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

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.

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.

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

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:

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.

Conclusion

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!

1 Comment

Click here to post a comment

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

More in VPS
SSH tutorial How Does SSH Work
How does SSH Work

Close