LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

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

Introduction

In this article, we will show you how to install LAMP stack on Ubuntu 16.04. It is a common web stack used for hosting contents on the web.
The term LAMP is an acronym for the group of open source software which represents Linux, Apache, MySQL and PHP.
In this case, we are using Ubuntu 16.04 as Linux. In the following paragraphs, we will be explaining the steps involved in the installation of Apache as a web server, MySQL as database management system and PHP to process dynamic content.

Prerequisites to install LAMP on Ubuntu 16.04

Before you begin installing the LAMP stack on your machine, Please make sure that you have the following things.

– A machine with Ubuntu 16.04 installed
– A user account with sudo privileges
– Putty to connect to server

Installing LAMP on Ubuntu 16.04

The installation of LAMP on ubuntu 16.04 involves the following three steps

  1. Installing Apache
  2. Installing MySQL
  3. Installing PHP

Before you start with installation, you should run the following command on the server to make sure that the system is up to date.

sudo apt-get update

Since we are using sudo command, it will ask us to enter the password for the user upon hitting enter. Just type the user password and hit enter to continue.

It may take a while to complete depends on the size of update available. Wait until it completes. Once it is done, move on to next step!
update Ubuntu 16.04 system

Installing Apache on Ubuntu 16.04

Installing Apache on Ubuntu is very easy and first step in the LAMP installation process. Ubuntu has included Apache in the package by default and is available in the default repository. Just issue the following command to start the installation of Apache.

sudo apt-get install apache2

Don’t forget to type “Y” and hit enter when it asks for confirmation

install apache on ubuntu 16.04Now you have successfully installed apache on Ubuntu 16.04 server. Check whether Apache is running by issuing the following command.

sudo service apache2 status

If the apache service is not running after successful installation. Just enter the following commands to stop and start Apache.

sudo service apache2 stop

sudo service apache2 start

apache status runningYou can verify whether Apache is working by opening server IP on a web browser.

ie, http://youripaddress

If you don’t know the server IP address, either check the information mail from web server provider or run any of the following commands in putty

ip addr show

ifconfig -a

A default apache web page will be loaded in your web browser with basic information about apache web server when you visit your server IP address on web browser

LAMP stack installation Ubuntu apache

Installing MySQL on Ubuntu 16.04

Now the Apache web server is up and running. It’s the time to install MySQL database to store and manage information.

The MySQL package also comes up with default Ubuntu repository. we can easily install MySQL by simply issuing the apt-get command.

Just paste the following command and hit enter to continue with MySQL installation.

sudo apt-get install mysql-server

It will list the package information with a confirmation question to continue. Just type Y and hit enter to continue.

installing MySQL database on Ubuntu 16.04

The installation process will ask to enter MySQL root password. Just type a strong password for MySQL root. This password is similar as that of your web server root password. It is better to use a strong and unique password.

Just issue the following command to the terminal to secure your MySQL installation by removing some default settings.

sudo mysql_secure_installation

This will take you through a series of questions. I request you to read all the questions and respond. Type Y for the question they ask except for the password strength. For password strength, you need to type a value.

Wait until you receive the following message on the terminal.

MySQL secure installation on Ubuntu 16.04

You can check whether MySQL server is running by typing the following command

sudo service mysql status

MySQL successfully installed on Ubuntu 16.04You can stop and start the MySQL server by applying the following commands

sudo service mysql stop

sudo service mysql start

Now it’s time to move on with the final part of LAMP stack installation on Ubuntu 16.04.

Install PHP on Ubuntu 16.04

The following command will install the latest version of PHP on your server. Please note that the following command will install the packages libcurl3 librtmp1 php php-cgi php-common php-curl php-json php-mysql php7.0 php7.0-cgi php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-json php7.0-mysql
php7.0-opcache php7.0-readline

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

installing PHP on Ubuntu 16.04

You can install additional PHP packages by apending to the existing command. Even you can install it later when required by running a sudo command.

To find all the Php modules and libraries, just run the following command

apt-cache search php- | less

The detailed information regarding a module can be retrieved by using the below command

apt-cache show Packagename

Install any module by using the following command

sudo apt-get install packagename

You can install multiple packages using single command by placing the package names continuously by separating with space

ie, sudo apt-get install package1 package2 package3

You can check the current version of PHP after installation by using the below command

php -v

Check PHP version in Ubuntu 16.04

Test PHP installation on Ubuntu 16.04

In this step, we will check whether the web server is processing PHP files correctly. To make sure that we will create a simple PHP file which will list the PHP info page.

By default, Ubuntu will not give access to any file that is kept outside /var/www/ directory trough web browser. The default document root is /var/www/html. So we need to place the test php file in the /var/www/html directory to output in the web browser.

Just follow the below steps to create a test php file on the web root.

sudo vi /var/www/html/phptest.php

A blank page with edit option will be opened. Just copy paste the following code to the editor

<?php
phpinfo();
?>

After pasting the code press Esc : wq and hit enter. This will save the file and come back to the command window.

Now you can check whether the php is working fine by accessing the created file from a web browser.

To check, just visit http://ipaddress/phptest.php

This will give a result like below. If you are not getting the php parsed result, we should conclude that there was an error in the package installation. Just try again!

successfull installation of PHP 7 in Ubuntu 16.04

Finally, I request you to remove the php info page after testing. Just release the below command to delete the phptest.php file

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

Congratulations! Now you have successfully installed LAMP stack on Ubuntu 16.04 and your server is ready for basic use. You can start using this server to host your websites by simply creating a virtual hosts file. This will be discussed in the upcoming articles.

Thanks for following our How to Install Apache, MySQL, PHP (LAMP) Stack on Ubuntu 16.04 tutorial!

 

Frank

7 Comments

  1. Ian:

    Somebody must forgot to put “Read More” tag somewhere…

    April 11, 2017 @ 3:26 am | Reply
  2. LowEndHuh:

    Pretty much copypasta tuto without trying out by the author himself.

    You can’t do “service blah status” on a systemd system without aliasing everything (or let a package doing that) but that’s dumb because it’s not future-proof. How about to rewrite this tuto by using systemctl?

    April 11, 2017 @ 9:28 pm | Reply
  3. datako:

    This is one of the worst LEB articles…

    April 12, 2017 @ 2:19 pm | Reply
  4. Sam:

    Good. Very good.

    Why post something original right? Why post something that hasn’t been posted million times in the internet right?

    I really wonder what the admins are thinking…

    No words…

    April 12, 2017 @ 5:41 pm | Reply
  5. jvnadr:

    Next article will be about how to put a power plug to a home pc. Evolving…

    April 12, 2017 @ 9:55 pm | Reply
    • Freddy:

      I found it very helpful. (@jvnadr == twat)

      May 9, 2017 @ 5:13 am | Reply
  6. Matt Zelasko:

    We really should have posted a disclaimer, the next few tutorials are going to be posted as “beginner posts” that we have wanted to reference in other tutorials so that we didn’t need to include entire tutorials for something as basic as this. Kind of prerequisite material, even if it is remedial.

    If anyone of you would like to step up to the plate and submit some tutorials of your own, we’re always on the lookout for content writers :-)

    Interested parites can Email me at mzelasko at colocrossing . com and we can talk topics and compensation.

    April 13, 2017 @ 1:20 pm | Reply

Leave a Reply to jvnadr Cancel reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *