Building an Ubuntu LAMP Web Server

Recently I was setting up my Mac OS X with a kick ass development environment and jotted down all the cool steps I took to build an Ubuntu LAMP web server in a virtual machine environment. Here is this in-depth guide translated from paper to this digital copy. Hope you find this guide valuable and it saves you time when you need to do the same.

LAMP (Linux, Apache, MySQL and PHP)

Ubuntu Server

What is Ubuntu

Ubuntu , is a secure, intuitive operating system that powers desktops, servers, netbooks and laptops. It is based on the Debian GNU/Linux distribution. Ubuntu is also named after the Southern African ethical ideology Ubuntu (“humanity towards others”) and is distributed as free and open source software with additional proprietary software available.

Step by Step – your 1st web server

This guide assumes you have already installed Ubuntu Server. If not, go here and do it first. I recommend you install Ubuntu Server in a VM. I use VMware Fusion to run my instances when developing and Amazon EC2 for production. This guarantees that whatever I do locally in a VM will be compatible when pushed into production.

Install Webmin – a web-based interface for system administration for Unix.

Allow WWW for sFTP so you can remotely manage your website using a GUI.

Setup access to MySQL using MySQL Workbench.

1. Install LAMP

SSH into your box as root on Port 22 (default post install).

Update your OS software (just in case you are missing some dependencies):

sudo apt-get update

From the terminal window, install LAMP using this 1 line of code (the caret (^) must be included):

sudo apt-get install lamp-server^

The apt package manager will display what it is installing and ask you a bunch of standard questions. Just say yes to all. You will also be asked for a password for your new MySQL database. Type that in and note this down for future.

When this finishes you are done. Easy hey! Port 80 (default web server port) is now enabled and pointing to ‘/var/www’. ‘/var/www’ is where your site(s) should be placed.

Hit the Public DNS URL of your server (typically your IP) to verify that it’s up. It should show up a page with “It works!” If you are not sure what your box’s IP is, type this in and hit enter (similar to ipconfig on a Windows box).

ip route

Before moving to the next step, you may want to know information about PHP’s configuration inc. installed extensions. You can grab this by creating a PHP file from your terminal window like this:

sudo nano /var/www/phpinfo.php

then adding this into it, save it, and quit nano (the editor your in):

<?php phpinfo(); ?>

restart Apache:

sudo /etc/init.d/apache2 restart

Hit the IP in your browser again with this new file name appended to the end eg. http://170.10.105.110/phpinfo.php – it should show you what is running.

Webmin should now be accessible from your browser using the server’s ip address followed by port 10,000 eg. https://170.10.105.110:10000
Note that you do not have HTTPS cert so your browser will throw a warning since https is (and has to be) the protocol. Ignore it and move forward.

3. Allow WWW for sFTP

You need to make sure the group www-data is added to “/var/www”. Run this in your terminal window:

sudo chgrp www-data /var/www

Make “/var/www” writable for the group.

sudo chmod 775 /var/www

Set the GID for www-data for all sub-folders.

sudo chmod g+s /var/www

Your directory should look like this on an ‘ls -l’ output.

drwxrwsr-x root www-data

Last, add your user name to the www-data group (secondary group) where USERNAME is the “new” username you will use to sFTP. Note that we follow it by “passwd” to give new account a password.

sudo useradd -G www-data NEW_USERNAME
sudo passwd NEW_USER

OR if the username is “existing” one use the command below. Also don’t forget to add “ubuntu” user if you have set this up on an EC2:

sudo usermod -a -G www-data EXISTING_USERNAME

You should now be able to SFTP to your server using this USERNAME and upload data to “/var/www” with no problems.

4. Access to MySQL using MySQL Workbench

MySQL Workbench is a nice free GUI tool by the folks at mysql.com to manage your MySQL database. It can be downloaded from here: http://wb.mysql.com/

By default MySQL listens on localhost (127.0.0.1) so if you are going to manage your Ubuntu VM instance from say OS X, MySQL wont allow you entry. Here’s what to do to grant remote management of MySQL.

Add port 81 (where your new host is configured on) to ‘Listen on addresses and ports’.

Save, apply changes and restart Apache.

Done. You can now access your website via http://IP_DNS:81

Now go and build kick ass products!

There you have it folks. How simple is that. That’s why I love Ubuntu so much. It’s simple and powerful all under the 1 umbrella. That’s how software should be. All the complexities removed so us engineers can get to work and build kick ass products!

If you found this post useful let me know in comments section below. Super!!

Hi
I found this article very helpful and almost have this installation running on my Mac. Unfortunately I can not write to the remote server. I followed your steps as above and have access to the remote host through sftp, but when I want to write I have an error message saying:
“Could not write to sftp://localhost” Permission denied”
I can’t change the permission too to the files on the remote host. Any suggestions?

SFTP is not working for me after using your guide. I went through the steps listed and even checked “less /etc/groups” and the user I created showed up www-data:x:33:USER…. googling…. still to new to LAMP management to know correct terminology grrr!