How to install Linux, Apache, MySQL, PHP (LAMP) on Ubuntu 16.04?

In this tutorial we will see how to install LAMP on Ubuntu 16.04 VPS. LAMP is an acronym for Linux, Apatch, MySQL, P. It is a popular stack for building 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 option for this stack.

What do you need?

Before starting the tutorial to install LAMP on , you will need the following:

  • A local machine with the client installed (see tutorial if you are a Windows user)
  • VPS Running Ubuntu 16.04
  • A non-root user with the privileges of sudo

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

adduser

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

usermod -aG sudo

Switch to this new user.

sudo su –

How LAMP works

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

See also  How to install mods in Minecraft using Forge on Windows, macOS and Linux servers

Step 1: Install Apache Web Server

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

sudo apt-get update sudo apt-get upgrade

Now install Apache2 with the following command

sudo apt-get install apache2

Installation check

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

http://

For example, if your VPS IP address is 22.23.24.45 your address should be:

http://195.110.59.211

You should see a page that looks like this:

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

dig +short myip.opendns.com @resolver1.opendns.com

Installation Troubleshooting

If you didn’t see the image above, don’t worry, you may 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

It then allows 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 a command line application called Uncomplicated Firewall. It is used to manage and make rules for the Linux firewall. Now enter your VPS Ip address in your browser to verify the installation. You can check the status of the Apache server with the following command.

sudo systemctl status apache2

Step 2 – Install MySQL

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

sudo apt-get install mysql-server

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

See also  How to sell online: Complete guide step by step

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

sudo systemctl status mysql

For example:

● 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 – PHP Installation

PHP runs your application. Install PHP and additional 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 additional modules that are necessary for the web application to work.

Step 3.1 – Checking the PHP installation

Now that you’ve installed PHP, let’s check if it works correctly 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 for beginners to get started with. 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 web root. This is where Apache looks for the requested file at the website URL by default if it has not been configured to look for another site. See the for information on your settings. Also, you need root privileges to be able to write to this directory. We used sudo before our command. Now enter the following text in the open editor:

See also  Where and how to sell digital products

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

http:///test.php

Your page should look like this:

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

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

Note: It is very important to remove this test file after verifying the installation because it can help an attacker gain critical information about the server’s configuration.

conclusion

You have learned how to install LAMP on Ubuntu. After installation, you can copy the PHP files to the server and deploy your web application. You can also to manage your databases in a web interface. Be sure to check out our other VPS tutorials and if you have any issues, feedback or ideas let us know in the comments section.

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...