Installing LAMP (Web Server) on Linux

When hosting a website, you can choose between buying a cheap shared web-host, or you can choose to manage your own webserver which is what we will cover today on AdminTuts.

What is LAMP?

LAMP stands for

  • (L)inux
  • (A)pache
  • (M)ySQL / (M)ariaDB
  • (P)HP

These are all the essentials to your webserver.

Installing Apache

First, we’re going to update all packages on our server.

sudo apt-get update

Now we need to install the Apache webserver package from the package manager.

sudo apt-get install apache2

Now we need to adjust the Firewall to allow web traffic to our server.

sudo ufw app list

Your output should look something like

Available applications:
  Apache Full
  Apache Secure

Take a peek at the Apache Full profile, by default it will allow traffic to ports 80 and 443.

sudo ufw app info "Apache Full"
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web


Now let’s allow incoming traffic for Apache:

sudo ufw allow in "Apache Full"

Now open up a browser and visit your IP address or domain name, you should see the default Apache landing page. Awesome.


Installing MySQL or MariaDB

An important aspect of any website is a database sytem. We will take a look at installing two of the most popular choices.


First, we’ll use the package manager to install the MySQL server:

sudo apt-get install mysql-server

Data is a very important thing that needs to be kept secure. Skipping over data security will screw you over.

To secure our MySQL installation, we’re going to run the built in security system:


Just go through those steps. It should take mere minutes.

And that’s it for MySQL!


MariaDB is simpler to install than MySQL, it only takes one command:

sudo apt-get install mariadb-server

Installing PHP

We’ll just be installing the basic PHP package and a few addons that you should have, it is up to you to install other modifications.

First, update your packages. Then, we’ll install PHP.

apt-get update
sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

PHP is very easy to install, you shouldn’t have any problems.

Most web developers prefer PHP over HTML, so we’re going to make Apache prefer PHP files.

To do this, open your Apache directory configuration file:

sudo nano /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.php index.xhtml index.htm

Simply move index.php to the front of the list.

Your completed file will look something like this:

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

Now we need to restart the Apache web server to apply our changes.

sudo systemctl restart apache2

If you want to check the status of Apache at any time, just run

sudo systemctl status apache2


Now that you have a LAMP stack installed, you have many choices for what to do next. Basically, you’ve installed a platform that will allow you to install most kinds of websites and web software on your server.