Install LAMP on Ubuntu – Linux, Apache, MySQL, PHP

In this post we are going to complete the LAMP stack on our server. Complete, since we have already a server running Linux.

To complete the LAMP stack, we need to install Apache, MySQL, and PHP.

The commands in this post are intended for Ubuntu 14.04, but will also work on Debian servers.

Ubuntu is based on Debian. For this reason, these two distributions share the same package management as well as the way they organize and handle files related to Apache.

It is a good habit to update the source.list on your server prior to installing software.

Updating the source.list assures you that you are using the latest version of the software. Otherwise, you might use an already cached, but older, version of the software.

To update the sources.list, type

Now we are ready to install the necessary software.

You do not have to run apt-get update for every single command. Once a day, and only when you are going to use apt-get should be sufficient.

Installing Apache

To install Apache, enter:

The server will check any dependencies, list what will be installed, and ask for a confirmation:

The capitalized letter indicates the default choice, which you can select by hitting the enter key.

Let’s check whether we have installed Apache2 succesfully. Just enter the IP address of the server in the address bar of your browser:

Replace the variable $serveripaddress with the actual IP address of your server.

The Apache2 Ubuntu Default Page will appear:

Apache2 Ubuntu Default Page
Part of the Apache2 Ubuntu Default Page

Success!

Installing PHP5

This command installs the PHP5 core and an Apache library for PHP5 support.

In order to activate PHP5, we need to restart the Apache server:

Besides the PHP5 core, there are about 6 dozen modules available for PHP5, each module with a specific function. You can list these modules by entering:

The server will answer with an impressive list:

Installing them all might be a safe bet, but overdone. For example, installing the php5-sybase module is only sensible when you are planning to install and deploy the Sybase database.

You can either check the requirements of the software that you are planning to use, or ignore this for the moment. You can always install a module later when needed.

Now that we have installed PHP5, we need to verify that it is working. For this purpose we are going to create a web page with a single PHP function:

The Nano editor opens a blank php file.

Paste the following code into that blank file with Shift+Ctrl+V:

Press Ctrl+O to safe the file, and exit with Ctrl+X.

The info.php file is saved in the default document root of the Apache2 server.

Time to test our PHP5 configuration. Enter in the address bar of your browser:

outcome phpinfo() function
Part of the page generated by the phpinfo() function

As the name of the phpinfo function implies, it presents us a comprehensive overview of our server configuration. That is also the reason why it is good practice to remove the info.php file as soon as possible. When necessary we can always recreate it.

To remove the file, type on the command line:

That is it.

Installing MySQL

We are ready to continue with the database. Besides the MySQL server itself, we will also install the MySQL module for PHP5. This module makes it possible to communicate with the database via PHP scripts.

During the installation of MySQL appears a windows where you have to enter a password for the root. Not the root of you Linux server, but the root of the MySQL database!

MySQL Root Password

Enter your MySQL root password and jump to <Ok> with the TAB-key. A second screen pop-ups where you have to enter the password again for verification purposes.

The MySQL server is now installed, but we need to do a little housekeeping. This requires two more steps.

We need a directory where MySQL can store its data. This step is called initializing the MySQL data directory. Enter:

Finally, we run a script to remove some defaults. This increases the security level of the MySQL database:

This security script will ask you a couple of questions:

Enter current password for root (enter for none):

When you hit enter, the system will ask you to enter a new password. When you are happy with the MySQL root password as entered in the screen above, reply with ‘n’,

Four more questions will show up. Just enter through all four – or answer with ‘y’.

Here is the complete transcript of the interaction (stripped from a couple of blank lines):

MySQL is installed. Now we are done.

Ready to Run

Even more, we have completed our LAMP stack. The web server is ready for our (first) website.

More Details

Apache Software Foundation

MySQL or MariaDB

PHP: Hypertext Preprocessor