How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04

LAMP Stack

If you wish to host websites on a VPS or an Unmanaged Dedicated Server, or if you have been assigned the task of installing all the required services for one, then a viable option would be to do it all in Linux. It has an advantage of hosting any dynamic website that uses PHP or WordPress with MySQL, Apache Server or any Relational Database. In this article you will get to learn and will know How to Install Linux, Apache, MySQL database, PHP (LAMP) Stack on Ubuntu 14.04 using our step-by-step guide.

First off- LAMP is an acronym for a group or set of open source software that we will use- the Linux OS, the web server Apache, the essential MySQL database and of course, PHP. All of them combined and installed will give a server the power to host web apps and dynamic websites.

Installing Apache Web Server

Apache is a renowned web server that is used all over the world with good reason- it is a great choice by default for hosting your website, and you can just as easily install Apache by utilizing Ubuntu’s versatile package manager, apt. We will use the sudo commands in this guide to ensure you have the proper permissions to execute the commands.

First run

sudo apt- get update
sudo apt- get install apache2 -y

The sudo command will prompt passwords for root privileges. Enter your password and proceed. Voila! the web server is already installed. Confirm that the Apache Server is configured right by checking your server ip in the web browser. Type in

http://your_server_IP_address

A test page will pop-up, saying It Works! or the default page should come up. It will look similar to this:

itworksapache

Managing the web server

Should you ever need to restart or stop your Apache Web Server, type in these text as commands

To Start:

sudo /etc/init.d/apache2 start

To Stop:

sudo /etc/init.d/apache2 stop to Stop

To Restart:

sudo /etc/init.d/apache2 restart to Restart

Installing MySQL

After the Apache configuration is completed, MySQL is the next logical step. MySQL pertains to a database management system that is open source, and will hold all your website’s data and arrange it in a relational table form. The ever-useful apt can help us again here, as other packages come in the fray and assist in communication between our web components.

Type in the following command:

sudo apt-get install mysql-server php5-mysql -y

The server will prompt you to create and set a password to gain admin access as a MySQL root user. Provide a different password from your original Linux root password to protect yourself and your website. Wait a few minutes for MySQL to finish installation. There are still some more environments that are required to be installed for the MySQL to store web information securely. Point out the path for MySQL where it can create a directory structure by typing

sudo mysql_install_db

Then increase protection and make your MySQL more secure by running an interactive script that will lock down database system access and remove dangerous defaults. Type in

sudo mysql_secure_installation

As before, a password will be asked- this will be the password for your MySQL root account, and then it will next ask if you wish to change it. Enter Y for yes and N for no. Other questions can be answered by pressing the ‘Enter’ key, and the default values will then be set. Doing this removes all MySQL sample databases and users, loads the current settings and will disable the remote root log ins. Congratulations, MySQL is ready to use and the database system is complete. Moving on…

Installing PHP

The next and last step will be installing PHP, which is responsible for processing your codes into dynamic content. It is able to run scripts, gather information from MySQL databases, and then transmit the content for display by the web server.

Run the command:

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt -y

Some changes in the Apache configuration are needed to manage your dynamic website. By default, Apache looks for index.html in a website directory, and we would like to command it to look for PHP files. We’ll then have it scan for the index.php file.

Type in this text command to be able to open the file dir.conf and modify it

sudo nano /etc/apache2/mods-enabled/dir.conf

The output would be like this:

<IfModule mod_dir.c>
 DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
 </IfModule>

which you would now modify into this, for the web server to process PHP file first

<IfModule mod_dir.c>
 DirectoryIndex (index.php is added here)
 index.html
 index.cgi
 index.pl
 (index.php is removed from here)
 index.xhtml
 index.htm
 </IfModule>

Once done, press “CTRL-X” to save and close it. Type Y to confirm, then hit ENTER for the file location. Afterwards, restart Apache for the changes to take effect, which you can perform by typing in

sudo service apache2 restart

How to Check if PHP is Working

To check and test the system if it has been configured properly, we need to create a very simple script which is the info.php file in your public directory. Do this by entering

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

This will open a nano blank file wherein you will have to put the following PHP code in it:

<?php
phpinfo();
?>

Once done, save the file and then close it. You can then test whether it correctly displays content by visiting this page on the web browser and you need to enter your server IP address again:

http://your_server_IP_address/info.php

You will see this on your screen, a page which provides information on your server that’s quite handy for debugging and ensuring all settings are accurately applied:
php-info

Note: You may want to delete this file after performing the test since it could release information in regards to your server and made available even to unauthorized users.

Cleanup

Remove it by typing in

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

The page can be recreated anytime if you wish to access and see the information it holds in the future.

If this final step was a success… Congratulations! You have successfully configured LAMP stack on Ubuntu 14.04. Proceed in installing any CMS or host your dynamic website. This enables you to install various websites and a wide range of web software, with some of the more popular options being WordPress, PHPMyAdmin, and many more.

CC BY 4.0 This work is licensed under a Creative Commons Attribution 4.0 International License.

Alex Wacker has written 16 articles

I am the founder and owner of Subnet Labs LLC. Impact VPS is one of our VPS brands. Linux, virtualizaton and the internet amaze me and I enjoy learning new things every day about them.

One thought on “How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 14.04

  1. Dennis Hall says:

    I can never seem to get through a MySQL install until I have first entered a “sudo apt-get install dist-upgrade” first I have 4 VPD’s in my account where 3 are LAMP 1 is MEAN with MySQL. These are all based on the Ubuntu14.04 minimal image. The dist-upgrade is the only one that all the MySQL installs will successfully complete against.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>