Setting up Raspbian – with a web server in mind

In this tutorial I’m going to explain how to get up and running with Raspbian on your Raspberry Pi, running either from an SD card or a USB Flash Drive. I will provide pointers about optimising your Raspberry Pi as a web server.

I’m going to assume that you have a Windows background and are unfamiliar with the Linux environment and you at least have access to another machine running Windows with an SD card slot.

Step 1 – Download Raspbian

Navigate to the official Raspberry Pi Website and download Raspbian “wheezy” either by direct download or torrent. This is a version of the Debian operating system, one of the most popular Linux distributions, which is optimized for the Raspberry Pi.

Step 2 – Copy the image to your SD card

Now you have the image that represents the contents and structure of the Raspbian operating system we need to write it to an SD card. Navigate to the SourceForge website and download  Win32 Disk Imager – this tool is used for writing images to external media. Extract the contents of the zip file to a folder and run Win32DiskImager.exe.

Select the ‘Device’ ie your SD Card. It selected my SD card by default however you should check to make sure that you have selected the correct letter as this process will erase any existing data you have on your SD card. Click on the folder icon and select the Raspbian image you downloaded then click on ‘Write’ and wait…

As well as writing images to external media Win32 Disk Imager is useful for reading data from external media to an image file. This will be useful later when you have your SD card in a desired state and want to make a backup before making any further changes.

Step 3 – Boot up into Terminal

You are now in a position to boot up your Raspberry Pi! The remainder of this tutorial requires that you boot to the Linux Terminal. There are a couple of ways you can do this, first connect your Raspberry Pi to your digital TV or monitor and plug it in. It should boot in to Terminal. If you are happy interfacing locally through your TV or monitor continue to Step 3. I however prefer a headless setup.

If you’re planning to use your Raspberry Pi as a web server then the headless setup is a more suited approach. A headless setup means that the Raspberry Pi isn’t directly connected to a monitor, mouse or keyboard although it does require a connection to your router via Ethernet. When you’ve connected your Raspberry Pi to your router you should notice the green LED labelled LNK flashing which indicates that a connection has been successfully negotiated. You then need to obtain your Raspberry Pi’s IP address.

sudo ip addr show

ipaddress

Your IP address should be shown in a similar fashion as above. You need to make a note of this.

You now need to download PuTTY to another machine on the network. PuTTY is a Secure Shell (SSH) client that allows you to connect securely to your Raspberry Pi. Once you’ve completed the download; launch putty.exe, enter your IP Address using the default port of 22 and click ‘connect’. The default username is ‘Pi’ with the password ‘raspberry’ – this will bring you to the command line.

Step 3 – Configuring Raspbian

Now you’re ready to configure your Raspberry Pi. You need to launch the Configuration Tool.

sudo raspi-config

You should expand the file system to make full use of the SD card, change the default password, configure the locale (mine is set to ‘en_GB.UTF-8 UTF-8’)  and timezone under ‘Internationalisation Options’. If you want to set your Raspberry Pi up as a web server you should make a couple of other tweaks as explained below.

It is now possible to set an overclock of 1000MHz without voiding your warranty as the clock speed is dynamic and reduces if the internal temperature reaches 85°C. You may need to reduce this if your Raspberry Pi becomes unstable, each chip is unique and has different limitations so you should experiment. You should also navigate to the ‘Advanced Options’ and set the memory split. You should choose the minimum amount of memory (16 MB) to be dedicated to the graphics if your unit is running headless as graphics memory isn’t required and can be better used servicing requests.

When you’re happy everything is as you want it select ‘Finish’ then restart.

sudo reboot

Your Raspberry Pi is now set up and ready to use. If you’ve been following this tutorial with a web server in mind then it’s recommended to move the root partition onto a USB drive.

Although a convenient medium for running Raspbian, SD cards aren’t designed for handling the demands of running a web server i.e. high rates of reading and writing data. I hosted a WordPress site on an SD card and the database became corrupt after a couple of days. If you would like to know how to transfer the root file system to a USB flash drive you should continue with the next step.

Step 4 – Copying the root partition to a USB flash drive.

Plug in a USB stick and load Raspbian to the terminal. You need to copy the root partition from the SD card to the USB drive. List the USB devices connected to the system to make sure your device is recognised.

lsusb

My drive came up as /dev/sda1 – you need to copy your root partition to the USB drive using the dd utility to create a disk dump using /dev/mmcblk0p2 as the input file and /dev/sda1/ as the output file. A block size of 512 bytes was used for the transfer which will take around half an hour…

sudo dd if=/dev/mmcblk0p2 of=/dev/sda1 bs=512

You now need to update cmdline.txt, a file that controls the loading of the Linux Kernel, with the new location of the root partition. You can use Nano, a simple command-line text editor to edit text files.

sudo nano /boot/cmdline.txt

Replace root=/dev/mmcblk0p2 with the location of your USB drive, in my case /dev/sda1. Press ‘ctrl + x’ then press ‘y’ to save when you are prompted.

You also need to update the file system table with the new location of the root partition.

sudo nano /etc/fstab

Again, replace root=/dev/mmcblk0p2 with the location of your USB drive – save and exit. You should now be in a position to reboot and load the root partition from the USB drive.

sudo shutdown -r now

If all has gone well the terminal should boot up in the same way as it did in Step 3. You can show the free space on your disks in human readable format, you will probably find that the root partition isn’t expanded to fill the USB drive.

df -h

You should expand the root file system to fill the disk using the resize2fs program.

sudo resize2fs /dev/sda1

You should reboot before making any other changes. Your Raspberry Pi is now in a good position to start playing with and potentially setting up as a web server – you might want to back up the USB drive to your PC using the ‘Read’ function of the Win32 Disk Imager application you used it Step 2. This means you can restore to a good known state if you happen to break anything (you will)!

2 thoughts on “Setting up Raspbian – with a web server in mind”

  1. “I’m going to assume that you have a Windows background and are unfamiliar with the Linux environment and you at least have access to another machine running Windows with an SD card slot.”

    Those are the best words I’ve heard after searching for 2 weeks for a decent tutorial on how to get WP on my PI. I am redoing everything from scratch as right this second, and your tutorial is the most thorough that I have read.

    Come visit the site when you have a chance.

    Thanks, for a kick-ass tutorial,

    Cheers,

    Rodion

Leave a Reply

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