Building a LAMP Stack from Scratch

John Matthews
5 min readApr 8, 2018

--

Choosing your Server

The vast majority of the web is contained in third-party servers from companies like, Amazon, DigitalOcean, and Google. For a relativly low cost you can spin-up a virtual server inside one of their massive data warehouses. For most of my projects I use DigitalOcean, mainly because their’ VMs are easy to set up, the user interface on their site is nice and simple, and it’s only $5 a month for simple server. Amazon and Google also have a similar products for around the same price, but I like to stick with DigitalOcean (they’ve been doing this sort of thing for ages, and they always look out for little developers).

Saying all that, feel free to set up your own server in-house. I won’t be walking through how to install any of the physical hardware (you’ll have to do that on your own), but the rest of the steps will be pretty much the same.

Installing a LAMP Stack

A LAMP stack is a group of open-source software (Linux, Apache, MySQL, PHP) that all work together to make an excellent web server. LAMP is the default server configuration for about 50% of the internet, but that is decreasing over time with the rise of LEMP stacks. We’ll be using Ubuntu 16.04, Apache2, MySQL, and PHP7 for our stack.

Manual Install

This section closely follows the guide created by Brennen Bearnes over at DigitalOcean, with a few personalisation changes along the way.

This guide assumes you have your user accounts and firewall already set up. I won’t go into details on how to do this, but you can follow this guide by DigitalOcean.

Apache

1. SSH into your server.

2. Update the available packages on your system, and install apache2:

sudo apt-get update
sudo apt-get install apache2

Tip: When running certain commands (like install or remove), Ubuntu will ask you to confirm the installation. So if nothing much is happening in the terminal, just check that you've confirmed by entering y.

3. Run sudo ufw allow in "Apache Full" to adjust the firewall to all web traffic.

john@server:~$ sudo ufw allow in "Apache Full"
[sudo] password for john:
Rules updated
Rules updated (v6)

4. You should now be able to view the default apache web page by visiting your server address: http://your_server_ip_address

5. If you don’t know what your IP address is run:

sudo apt-get install curl
curl http://icanhazip.com

It should output the IP address of your server:

dkan@server:~$ curl http://icanhazip.com
138.68.158.46

So that’s the Apache service installed. Now let’s get MySQL sorted.

MySQL

If you don’t already know, MySQL is a database system that a huge chunk of the web runs on. While you don’t have to be fluent in MySQL, knowing a few basic commands can prove useful. I’d suggest taking a look at W3Schools’ SQL tutorials when you get a chance.

1. Download and run the installer

sudo apt-get install mysql-server
mysql_secure_installation

2. Enter a password for your root MySQL account. I recommend using a password manager to generate and store your passwords.

3. Enter n when asked if you want to install the VALIDATE PASSWORD PLUGIN.

4. Enter n when asked if you want to change the root MySQL password.

5. Enter y for the rest of the questions.

john@server:~$ mysql_secure_installation Securing the MySQL server deployment.Enter password for user root: VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: n
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!

PHP

PHP: Hypertext Preprocessor is the backend language that our server will run off. A huge chunk of the web runs on PHP, so I’d advise that you familiarise yourself with it’s syntax at some point. Again, W3Schools has a great tutorial you can follow.

1. Install PHP and it’s dependencies:

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql php-dom php-xml php-mbstring php-gd

2. Next we want to tell Apache to look for .php files before looking for .html files. Open /etc/apache2/mods-enabled/dir.conf in a text editor. I tend to use Vim, but you can use Nano or some other editor if you prefer.

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

3. Change the 2nd line to read:

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

4. Save and exit the file. If you are using Vim press esc and then type :wq, then press enter.

5. Run sudo systemctl restart apache2

That is the final step. You should now have a fully functioning LAMP stack running on your server. You can now visit your website in your browser.

Pre-built Stacks

DigitalOcean offer pre-built Stacks, so you can click and single button and you’ll get a fully-fledged LAMP stack up and running in seconds. If you want to learn more about how to create a LAMP stack from scratch then I’d recommend not using this option. However if you don’t really care about learning how to build a LAMP stack, then by all means use the pre-built option! Select LAMP on 16.04.

All Done!

Happy coding.

--

--