Advertisement

How to Use a Raspberry Pi as a Local Web Server

by

Raspberry Pi users love starting new projects, and one of the fundamental but relatively simple projects is to turn your Pi into a Local Web Server.

Requirements

  • Raspberry Pi (Model B)
  • SD Card (running Raspbian)
  • Micro-USB Power Cable
  • Ethernet Cable or USB WiFi Adapter
  • USB Keyboard
  • USB Mouse (preferable, but not required)

Setting Up SSH

Step 1

Using SSH, or Secure Shell, will allow you to control the Raspberry Pi from another computer. In order to enable SSH, you must obtain the IP address of the Pi. To do so, type the following command into the LXTerminal application:

ifconfig

You'll then see something similar to this:

If you're using a wireless connection, in the 'wlan0' section, the IP address can be found on the second line, labelled inet addr. In this case, my IP address is 192.168.1.80.

If you're using an ethernet connect, the IP address of the Pi can be found in the eth0 section.

Step 2

Enable SSH and then restart the Pi by typing the following commands into the LXTerminal window:

sudo /etc/init.d/ssh start
sudo apt-get install ssh
sudo shutdown -r now

You should enter each line one at a time, and press the Return key after each line has been typed.

Step 3

On your Mac, open up Terminal, which can be found within the Applications > Utilities folder. Type the following command into the window:

ssh pi@192.168.1.80

You should replace 192.168.1.60 with your own IP address. You'll then be asked for the password. Unless you've previously changed it, the password will be raspberry. Whilst you won't see each character being typed on the screen what you're typing is being entered.

If a security warning is displayed, double check that everything is ok, and then type either y or yes to continue the connection.

If you've connected successfully to the Pi, you'll see pi@raspberrypi ~ $ being displayed at the bottom of the Terminal window, similar  this:

Setting Up the Web Server

Step 1

You'll first want to install Apache and PHP. In order to do so, you should type in the following command into the Terminal window:

sudo apt-get install apache2 php5 libapache2-mod-php5

Step 2

You need to restart the Apache service. Type the following command to do so:

sudo service apache2 restart

Step 3

You can test the Web Server by typing the IP address into a web browser. If everything's gone well, you should see a message saying It works! looking like this:

Installing MySQL

Step 1

It's important to have MySQL installed, so you can run anything which needs a database, such as WordPress. To install MySQL, type the following command into the Terminal window:

sudo apt-get install mysql-server mysql-client php5-mysql

Setting Up FTP

Step 1

In order to allow the transfer of files between your Mac and your Raspberry Pi, you'll need to set up an FTP connection. To do so, type the following command which will allow you to take full control of the web server's root directory:

sudo chown -R pi /var/www

Step 2

Install vsftpd which is one of the fastest and most secure FTP servers for UNIX-like systems. Enter the following command into the Terminal window to install:

sudo apt-get install vsftpd

Step 3

I recommend that you edit the configuration file for vsftpd to make it more secure and user friendly. There are three changes you need to make:

  1. Change anonymous_enable=YES to anonymous_enable=NO to disable Anonymous FTP)
  2. Uncomment local_enable=YES and write_enable=YES to allow you to make changes to the FTP server
  3. At the bottom of the configuration file, add force_dot_files=YES to enable files such as .htaccess

Then save and exit the configuration file by pressing Control-X, then Y and then the Return key.

Step 4

You need to restart the new FTP server in order to be able to use it. To do so, enter the following command into the Terminal window:

sudo service vsftpd restart

Step 5

You may also wish to create a shortcut from the Pi user's home folder to the web root folder. If you want to do this, type this command:

ln -s /var/www/ ~/www

Summary

That's it! You've now successfully set up a Local Web Server for your Raspberry Pi!

You began by enabling SSH, then installing the Web Server, MySQL and FTP.

Using a Raspberry Pi as a web server is one of the fundamental uses of a Pi. It opens up a number possibilities.

The best way to find out what you can do with your new Web Server would be to play around with it, and see what it can do!

Advertisement