Installing LAMP on Ubuntu

LAMP is an acronym which consists of: Linux (in our case Ubuntu), Apache web server, MySQL database for storing our website data and PHP for handling the dynamic content.

Creating a non-root user with super user privileges

Before we begin with the installation of the LAMP instance we have to make sure we have a non-root user with super user privileges. This step is important because in Linux managing your system while logged in as root user is considered bad practice. The reason behind this is that the root user has very broad privileges which allow him to gain access and to execute critical commands-commands that can possibly lead to system failure.

In order for us to make a non-root user with super user privileges we need to be logged in as a root user.

Type su in your shell for logging in as root user.


If you don’t have a root account you have to create one.

In order to do so you need to run the following commands

sudo passwd root

You will be prompted for a password for your root account. Then you need to execute the following command in order to unlock your root account.

sudo passwd -u root

Now let’s create the user. Execute the following command:

adduser yourusername

You’ll be asked some questions. Other than the password the other fields aren’t mandatory.

In order to give your user root privileges execute the following:

gpasswd -a yourusername sudo

This will add your user to the sudo group. As a consequence every time when you run a command starting with sudo you’ll be executing it with root privileges.

In order to log in with your newly created user type this:

su – yourusername

Now let us get back to installing the LAMP instance.

Installing Apache Web Server

First we need to install the Apache web server. We’ll be using the Ubuntu repository for the task.

First we have to make sure that our repository is updated. We should run the following.

sudo apt-get update

After our repository has been updated we are ready to install the apache web server. Type this command:

sudo apt-get install apache2

Now our Apache Web Server is installed. To check if your Apache web server installation was successful type your server ip address in your browser. You have to see the Apache2 Ubuntu Default Page.

If you don’t know your server public ip address type the following:

Ip addr show

Next to inet you”ll find your ip address

Inet youripaddress/

When a directory is requested by default Apache first looks for index.html. We want to change that so as a result apache will look first for index.php. To do so type this:

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

We want to move the index.php in the first place next to the DirectoryIndex

DirectoryIndex index.php index.html index.cgi index.xhtml index.htm

Now we need to restart the Apache web server in order for our changes to take effect.

sudo service apache2 restart

Installing MySQL

MySQl is a database management system. With it you can easily store, access and organize your website information.

We need to run the following command in order to install MySQL:

sudo apt-get install mysql-server php5-mysql

During the installation you’ll be prompted for the MySQL root user password. Just generate one and enter it. After the installation we have to generate MySQL database directory structure.

sudo mysql_install_db

After that we have to run a security script in order to secure our MySQL installation.

sudo mysql_secure_installation

Remove the anonymous user, disallow the remote root login and remove the test database. After that reload the privileged tables. That’s it.

Installing PHP

PHP is going to be used in order to display our dynamically generated content. Again we are going to use the apt-get.

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

Now our php package has been installed. Php comes with optional modules you can install. In order to search them up type:

apt-cache search php5-

From the additional modules we will install php5-cli- command-line interpreter for the php5 scripting language.

sudo apt-get install php5-cli

Our last task is to test how php handles things. We have to create a test file in a specific directory (web root). on our server.

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

Now we want to put the following and to save the file:


In order to test our php we need to navigate to the newly created test.php file in our browser.


You have to see something like this:

In order to keep your server secured you should remove the test.php file

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

Now you have a working LAMP instance ready to host your websites.

About Georgi Ivanov

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.