How To Setup the Ideal LAMP Development Server using Ubuntu

My local development server’s hard drive went out in a blaze recently. I ordered a new drive and went about getting everything set up so I could get back to work.

In the past I’ve used XAMPP on windows as a development environment and connected to a remote server via FTP but nothing beats having a dedicated local server. Here’s a guide detailing how I set up my LAMP (Linux, Apache, MySQL, PHP) stack on Ubuntu. It’ll work for a physical or virtual server (if you use Virtualbox or VM Ware).

Sites will be available at their own .local domains over your local network using VirtualHosts and a quick edit of your OS hosts file, closely matching a live server environment but working much faster than it would connecting to a remote server over the internet.

You’ll need a dedicated network computer you can have running when you’re developing or you can install Ubuntu under VirtualBox (unless you’re already running Ubuntu) which will work just as well but can be quite demanding on system resources.

Install and Configure Ubuntu, Static IP

Obvious one there, but just incase you can get ubuntu from www.ubuntu.com.

Burn the disk, pop it in and follow the insanely simple install process. Us noobs have no idea how lucky we are these days. I ran various forms of Linux in the late 90s and spent countless hours figuring out just how to install it.

Set up your network and ip address to a static ip using the Network Config tool available by right clicking the network icon in the upper right toolbar on your linux desktop.

Choose the connection (probably eth0) and edit the settings under the IPv4 tab.

The first field is your IP, in the same form as your network. In my case, my network settings look like this:

192.168.16.69 | 255.255.255.0 | 192.168.16.1 (that only my router’s ip address)

I set my DNS servers to Open DNS: 208.67.220.220

You can also type sudo pico /etc/resolv.conf at the terminal and add the other open DNS IP

nameserver 208.67.222.222

ctrl+o to save, ctrl+x to quit.

restart networking so the settings will take effect:

$: sudo /etc/init.d/networking restart

Now we can

Install SSH

$: sudo aptitude install ssh

this will install ssh, and openshh-server

Once this is done you’ll be able to go back to your main machine for the majority of the tutorial and execute the commands via any ssh client. (I use Putty)

try connecting to the machine over the network using the ip address you chose for your server.

Open putty.exe and use the ip address 192.168.xx.xx and you should see a similar prompt to enter your login details:

terminal-login

Login and we can start making things happen.

Install Apache2, PHP, MySQL and phpMyAdmin

We’re going to install some essential packages for the server. You can use this command:

$: sudo aptitude install apache2 php5 libapache2-mod-php5 mysql-server libapache2-mod-auth-mysql php5-mysql phpmyadmin

terminal-installing

terminal-mysql-password

You’ll be prompted to choose some passwords. It’s up to you how you choose your passwords so long as you don’t forget which is which.

You’ll also be asked to choose the apache server to install things to. You can select the option you want using the space bar, and use the TAB key to move the selection to the <Ok> button and press Enter.

terminal-server

You’ll see lots of terminal spam as everything unpacks and installs.

Once that’s all done our server should be up and running!

Open your browser and point it to http://your.ip.address

You should see:

browser-works

At this point you could go ahead and start developing websites in sub folders in your servers /var/www/mywebsite directory and visit them over the network using http://192.168.16.69/mywebsite/index.php

Setting up Virtual Hosts and Domain Hosting

I take this a step further so that I can visit my sites using their own local domain name, like http://www.mywebsite.local/index.php which is much better.

So lets jump into some Apache virtual hosts configuration.

First of all lets create our directory structure for our website:

$: cd /var/www

$: sudo mkdir www.mywebsite.local

$:sudo mkdir www.mywebsite.local/htdocs

$: sudo mkdir www.mywebsite.local/logs

$: sudo mkdir www.mywebsite.local/cgi-bin

The simplest way I’ve found to enable virtual hosts in Apache is to create a file called virtual.conf in the /etc/apache2/conf.d directory containing NameVirtualHost *

$: sudo pico /etc/apache2/conf.d/virtual.conf

NameVirtualHost *

Ctrl+O to write out the file, Ctrl+X to quit.

Now we must put our config for the site in /etc/apache2/sites-available and create a symbolic link in /sites-enabled so we can easily enable and disable sites at will.

So lets make our config file.

$: sudo pico /etc/apache2/sites-available/www.mywebsite.local

<VirtualHost *>
ServerAdmin webmaster@mywebsite.local
ServerName  www.mywebsite.local
ServerAlias mywebsite.local

# Indexes + Directory Root.
DirectoryIndex index.html index.php
DocumentRoot /var/www/www.mywebsite.local/htdocs/

# CGI Directory (optional)
ScriptAlias /cgi-bin/ /var/www/www.mywebsite.local/htdocs/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>

# Logfiles
ErrorLog  /var/www/www.mywebsite.local/logs/error.log
CustomLog /var/www/www.mywebsite.local/logs/access.log combined
</VirtualHost>

Basically we set the paths to the server and our files, our index.html or index.php as directory root files, cgi bin and log paths. Done.

Save it. Ctrl+O to write out, Ctrl+X to quit.

We can use the following handy command to create the symbolic link and enable our site like this:

$: sudo a2ensite www.mywebsite.local

It’s also useful to enable any mods you need at this point:

$: sudo a2enmod rewrite

..and then restart Apache.

$: sudo /etc/init.d/apache2 restart

Almost done. Now all we need to do is change our hosts file. This varies from OS to OS.

Ubuntu / Linux: /etc/hosts

Windows: c:\windows\system32\drivers\etc\hosts on windows

MacOS: /private/etc/hosts

Add the following to the file, replacing xxx with your server’s IP address:

192.168.xx.xx          www.mywebsite.local

These files need to be edited as administrator, so on Ubuntu sudo pico /etc/hosts

On Windows Vista/7 run notepad as administrator by right clicking and choosing ‘Run as Administrator’ browse to the folder and make the changes that way.

Set this up for any system on your network that  you’d like to use the clean domains with and that’s it!

Try it in your browser:

browser-mywebsite

Setup Samba and a Network Drive to /www

I also then like to install samba, share my /var/www file and connect with a network drive from my main computer.

$: sudo aptitude install samba

I set up shares using the graphical user by running Nautilus as su and right clicking the /var/www folder, and enabling all options under ‘Share Options’.

Then in Windows I set up a network share by right clicking on ‘Computer’ and setting the location to \\192.168.xx.xx\www

Then I can access the drive I chose (W:\www.mywebsite.local) to edit and create sites directly on the server over the network.

I hope someone finds this useful!

This entry was posted in Tutorial. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>