Monday, 3 June 2013

Setting up a Web Server - Part Two: Installing & setting up a LAMP server

Following on from the previous post I'm now going to explain how to install and set up a LAMP server. Personally I recommend the Ubuntu version of Linux, it's Open Source (i.e. free) and well supported within the development community so there is a lot of support available.

Minimum Hardware Requirements

Current minimum hardware requirements for Ubuntu Desktop are:
  • 700Mhz processor (Intel Celeron or better)
  • 512MB RAM
  • 5GB of hard drive space
  • VGA capable of 1024x768 screen resolution
  • A CD/DVD drive or a USB port for installation
For up to date information on system requirements please check the Ubuntu website.

In terms of hard drive space for your Web Server, this will vary depending on how many sites you are going to develop and what type of resources you are going to be using - images, video, flash, audio, documents. I use a 150GB drive, which is also a networked document & multimedia server.

Installing the Operating System

There are two versions of Ubuntu available, the traditional 32-bit for older computers and 64-bit for newer computers. Download the latest version of Ubuntu. You can select either the Desktop version or the Server version of the Operating System. We use the Desktop version so that we can fully utilise the benefits of the Ubuntu Operating System, but if you simply need a Web Server then opt for the Server version.

Ubuntu comes as an ISO which you will need to either burn to CD/DVD or put onto a bootable USB stick. Once you have done this you can boot the computer you will be using as your Web Server from the newly created installation device (CD/DVD or USB). Select your language and follow the onscreen instructions to install Ubuntu - read the full installation guide for more details.

Now that the operating system is installed you need to make sure that it's up to date.

Click on the Ubuntu Dash icon at the top of the screen and type "terminal" into the search box. This will launch the command line interface (CLI) for Ubuntu. In the Terminal window type Sudo apt-get update

Explaining this command:
Typing sudo will ensure that the administrator user account is used for the following command - you may be prompted to enter the administrator password before the system will carry out your command.
apt-get is the default Ubuntu program for installing software, as you work with your LAMP server you will find that you use sudo apt-get sudo apt-get install a lot!

If you don't want to work with the Ubuntu CLI you can use the Ubuntu Software Centre to run updates and install software - but most advice you will come across for maintaining your Ubuntu server will reference CLI instructions, so it's not a bad idea to start using the CLI from the start.

Setting up the Web Service

Now that you've successfully installed Ubuntu and made sure that everything is up to date you need to install your Web Service. On LAMP servers, as mentioned in the previous article, you will be installing Apache. Installation of Apache in Ubuntu is straightforward as it is an option during the installation process or can be added later. 

To install Apache 2 on your Ubuntu box open Terminal and type sudo apt-get install apache 2. You shouldn't need to interact with the installation. Once completed you can open the installed web browser on your server and enter http://localhost in the address bar. You should see a simple web page with the message "It Works!" on the screen.

If you have an advanced set up using multiple hard drives, as I do, then you may need to make some modifications to the Apache server settings. The Ubuntu website has detailed information on configuring Apache 2.

Adding your databases - MySQL

The next step is to add MySQL so that you can run databases with your websites. If you're only ever going to be working with static page websites, all hard coded, then this step can be skipped but these days most of us are working with a combination of static sites, bespoke content management systems (CMS) and Open Source, or purchased, CMS packages such as WordPress & Joomla.

Open Terminal and type sudo apt-get install mysql-server. During the installation you will be prompted to enter a password for the MySQL root (administrator) user. Although you don't have to do this, if your server is going to be accessible from the outside workd it's best to create a password. If you are only ever going to use the web server as a development platform within the confines of your own network then you can leave this blank.

After installation the service should start automatically, however you can can check this by entering the following in Terminal: sudo netstat -tap | grep mysql

If everything is running correctly you should see a message something like this:
tcp   0      0     localhost:mysql    *:*     LISTEN      2556/mysqld

Adding scripting support - PHP

So far we've installed the L A & M parts of the LAMP server. So the final step is to install scripting support. By doing this we will be able to run PHP files on our web server and create pages that interact with our databases and are capable of getting information back from the server to generate dynamic pages.

Open Terminal and type sudo apt-get install php5. After installation Apache 2 will automatically restart. Apache 2 is automatically configured to run PHP scripts and the installation of the PHP5 module will automatically be enabled in Apache 2 once you've installed it. 

To make sure that everything will run correctly you can check that the php5.conf and php5.load files exist by using the CLI to change to the /etc/apache2/mods-enabled/ folder and viewing the files. In Terminal type cd /etc/apache2/mods-enabled/

If the files are missing you can enable the module by typing the following in Terminal: sudo a2enmod php5

You should now restart Apache 2, you may find that you have to do this on a fairly regular basis. To restart  type the following into Terminal: sudo /etc/init.d/apache2 restart


That's it. You've now installed, and set up, your LAMP server. Go ahead and play!

If you want to use friendly URL rewriting offered by many CMS packages - or write your own - then there is one last thing you'll need to do...

Adding the Apache mod_rewrite rule

Enable the mod_rewrite rule by entering the following in Terminal: sudo a2enmod rewrite

Change your directory to /etc/apache2/sites-available/default by using the cd command in the CLI and look for the section that starts with <Directory /var/www>

Modify the line containing AllowOverride None to read AllowOverride All. This instructs the web server to allow access to the .htaccess file on your webserver to over-ride previous instructions. You can find out all about .htaccess files and what they do here.

Restart Apache 2 for the changes to come into effect.