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.
- 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
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:
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.
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.
On your Mac, open up Terminal, which can be found within the Applications > Utilities folder. Type the following command into the window:
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
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
You need to restart the Apache service. Type the following command to do so:
sudo service apache2 restart
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:
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
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
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
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:
- Change anonymous_enable=YES to anonymous_enable=NO to disable Anonymous FTP)
- Uncomment local_enable=YES and write_enable=YES to allow you to make changes to the FTP server
- 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.
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
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
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!