How to setup Linux, Apache, MySQL & PHP (LAMP) on Ubuntu 16.04

Created by Jordy Leffers at 06-12-2017 11:05:35 +0100

The LAMP stack is a combination of several open-source software packages. In this guide, we will go through all the steps of installing these packages, namely: Linux, Apache, MySQL and PHP. Together they enable your server to host dynamic web applications or host databases. In this tutorial we'll be going through the installation proces step by step.

Before we start, we need a Linux Ubuntu installation. We'll skip this step in this tutorial since you can easily get a default linux installation on one of the containers on the www.cloudcontainers.net site.

This tutorial is based on the cloud containers created on the my.cloudcontainers.net page. This means that you are by default the root user, so all of the command below don't make use of sudo. If however, you're not the root user on your system, you'll have to add "sudo" in front of the commands found in the guide below.

 

First, update your packages so no conflicts will emerge later on:

apt update

apt -y upgrade

In this guide I'll be using nano as the main text editor, you can use any you prefer.

apt install nano


Install Apache

apt-get install apache2

You'll be prompted to allow extra disc space to be used: press Y

Modify server name in /etc/apache2/apache2.conf using

vim /etc/apache2/apache2.conf

Add the following line to the bottom of the document:

To check if correctly configured:

apache2ctl configtest

Restart apache

systemctl restart apache2


Navigating to your IP in the browser should give a message that everything works correctly.

How to find your IP?: ip addr show eth0 | grep inet 


 

 

Install mysql 

apt install mysql-server

prompts password, choose a password.

For further installation:

mysql_secure_installation

This will prompt a few questions to be answered Y/N

You can skip the validate password plugin. This plugin checks the strength of your password and will return errors if your password isn't strong enough. It's mainly annoying, especially if you are using software that automatically generates MySQL user credentials.

You can answer yes to all the other questions.

 

Install PHP

Additionally we will also install some packages that allow PHP to communicate with MySQL and run on Apache.

apt install php libapache2-mod-php php-mcrypt php-mysql

You'll be prompted to allow extra disc space to be used: press Y

By default, Apache will look for an index.html file, you will want to change this so Apache prefers index.php files over .html:

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

 

This is how to file will look:

We are going to bring index.php to the first position, like this:

We must again restart Apache for our changes to take effect. 

systemctl restart apache2

 

Additionally you might want to install some extra functionalities of PHP, you can find a selection of these modules using the command:

apt-cache search php-                 This will display a list of extra modules and their description

apt-cache show PACKAGE_NAME         This will return the selected package's extra information

You can install as many of these packages as you'd like using the following command: 

Note: You can install multiple packages at once by separating the different packages with a single space

apt install PACKAGE_1 PACKAGE_2 


Test PHP

Congratulations, you've just installed and configured LAMP!

In order for use to make sure everything is installed correctly however, we need to do a short test.

Well test this by creating a short PHP script, using a function that will return some information about our newly configured server.

Note: Be sure to delete this file afterwards, so unauthorized users will not get this information.

nano /var/www/html/info.php          

the script will look like this:

Go to this address:

http://YOUR_DOMAIN/IP/info.php

Here you'll find a page containing all kinds of server information, it can be useful for checking your settings.


For now, we'll delete it, so no unauthorized users will have acces to this information:

rm /var/www/html/info.php

 

Congratulations, you've successfully installed LAMP on your Ubuntu server!

Comments

Comments are turned off.