;

How to setup a basic LAMP Stack on Ubuntu 14

Try it in our public cloud & Get $5 Credit
CLAIM NOW

The LAMP software stack refers to a setup on a Linux node that includes the Apache HTTP web server, the MySQL database management system (RDBMS), and finally, the PHP programming language. These four components come together in order to allow dynamic web applications and content to be run from the Ubuntu 14 server. While the components themselves are actually largely interchangeable with other tools that serve the same functions, the original LAMP stack remains highly popular due to its robust character.

This guide will show you exactly how you can install and configure LAMP on your Ubuntu 14 server.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Servers) with Ubuntu 14.04 LTS installed.
• All commands should be run as the root user

If you don't want to go through the whole process of setting up a basic LAMP for your Ubuntu 14.04 server, you can always try our One-Click Apps and get a fresh LAMP in seconds.

Tutorial

This guide will cover how to install each aspect of the LAMP stack on your Linux system. However, before we can proceed it is necessary to update the local package repository information on your server using the command apt-get update as root. This command will fetch the latest information about what packages and package versions are available for installation on your system:

apt-get update

Once this command completes, you are ready to continue.

Installing Apache

The Apache HTTP web server can be easily installed using the default package manager apt-get. With this single command, which uses the -y flag to simplify the installation by removing the “yes” prompt, Apache will be installed on your Ubuntu 14 server using the apache2 package:

apt-get -y install apache2

After the installation completes, you can verify that the Apache web server is working as it should. Open up the following page in your web browser, replacing the text YOUR_SERVER_IP with the IP address of your server:

http://YOUR_SERVER_IP

You will see the default Ubuntu 14 Apache webpage displayed with some basic configuration information if everything installed as it should.

Installing MySQL

To install the MySQL database management system, we will again use the apt-get utility, this time for the mysql-client and mysql-server packages:

apt-get -y install mysql-client mysql-server

You will be prompted to set a root password for MySQL in the installation process. This password will be valid on your server for the root@localhost account. By setting the password now, you will not have to set it at a later step so it is recommended to choose a secure and strong password that you will remember, as we will need it later on in the tutorial.

To create the MySQL database directory structure where MySQL will store information, type the following:

mysql_install_db

If your Ubuntu 14 server is for production use, or you wish to enhance your server security for any other reason, there is an additional step that you can take that will enable higher security levels for your MySQL database by applying several security tweaks. Enter the following command, follow the interactive script, and when prompted, provide the MySQL root user password that you just set in the above step.

mysql_secure_installation

One example of the security modifications that are made while running the secure MySQL installation is the removal of several sample users and databases. It is important to note that it will also disable remote root logins. Such changes will be immediately loaded after the process completes so that MySQL will run as secure as you need it on the spot.

Having completed the these steps, the MySQL database management system is installed on your Ubuntu 14 server.

Installing PHP

The final component of the LAMP platform stack is the programming language PHP, which is what makes dynamic content possible on your web server. PHP is highly customizable, and multiple modules exist for the numerous PHP extensions that you can use. However, you can use this command to install PHP with its default core modules and to use with SQL as a start:

apt-get php5 php5-mysql

If you would like to also install further modules for PHP, you can add them by modifying the above command to fit the following format, listing the additional packages separated by spaces:

apt-get package_1 package_2 ... package_n

For example, this means that if you want to also download and install the helper packages, libapache2-mod-php5 and php5-mcrypt, your call to apt-get should look as follows:

apt-get libapache2-mod-php5 php5-mcrypt

The apt package manager also possesses a command that will list all the available PHP modules and libraries for you, so that you can better decide what you need and what is available. Execute the following command, which uses apt-cache to search for all packages that begin with the text php5- in the Linux repository:

apt-cache search php5-

The output of the search command will be a list that will look something like the following, listing the module first, followed by a brief description:

php5-cgi - server-side, HTML-embedded scripting language (CGI binary)
php5-cli - command-line interpreter for the php5 scripting language
php5-common - Common files for packages built from the php5 source
php5-curl - CURL module for php5
php5-dbg - Debug symbols for PHP5
...

For further information about each module, you have the option of either searching online or using another handy command from the package manager apt:

apt-cache show package_name

The show command will display a long description for your desired package alongside with other metadata.

Configuring Apache

Now that PHP is installed on the Ubuntu 14 server, you will need to make some changes to the Apache configuration in order for your web server to be able to handle PHP. When Apache serves the files within the webroot directory located by default in /var/www/html/, it will look first for a file called index.html. However, we do not want this behavior when we are using PHP. Instead, we want the Apache server to prefer PHP files and look first for a file called index.php.

To accomplish this modification to the files that the Apache web server prioritizes, open up the following Apache configuration file in the nano text editor:

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

This file should look as the following:

<ifmodule mod_dir.c="mod_dir.c">
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</ifmodule>

Note the location of index.php and index.html in the Apache configuration file. The order in which the files appear dictates the preference of Apache, so the leftmost files listed will always be attempted to be served first before ones to the right. What we want to do is to change the preference in order for PHP files to be served first. Do this by moving the index.php text within its line to the left, until it appears immediately following DirectoryIndex and to the left of index.html. Shown below is what the modified line should look like:

<ifmodule mod_dir.c="mod_dir.c">
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</ifmodule>

After making this change, save the file and close the nano editor. In order for the changes we just made to be recognized, we will need to restart the Apache web server using this command:

service apache2 restart

Testing the Setup

With all the necessary LAMP components installed and configured, you can verify your stack is set up and running as it should be by creating a basic PHP file that you can then view in your browser.

Go to the webroot folder at /var/www/html/. Remember, it is from this directory that the files will be served by Apache, so you will need to create the test file in there. Change directories:

cd /var/www/html/

Next, open up the nano text editor to the new file called index.php with the following command:

nano index.php

Type the following into the open index.php file. This short PHP script will display the PHP info when viewed from its webpage:

<?php
phpinfo();
?>

Save and close the file. Next, open up the relevant webpage in your browser, replacing YOUR_SERVER_IP with the IP address of the Ubuntu 14 server you are using:

http://YOUR_SERVER_IP/info.php

If the page correctly opens and displays information concerning the PHP version of your server, then congratulations, your setup is working!

Conclusion

With the basic LAMP stack installed, configured, and ready to use on your Ubuntu 14 server, you are now able to easily host websites and web applications with dynamic content. You can now go ahead and explore your options for what your web server can do, through applications such as Moodle or WordPress. Don’t forget, feel free to share this tutorial with others who may be interested if you found it useful!