Chris Mendez in For Developers, Hardware

Turning Raspberry Pi into a web server

I've been tinkering with Raspberry Pi for two years and it's pretty remarkable what you can build with a $35US computer. Aside from being able to create apps using bash, pearl, python, or even javascript. What I also like to include in my Raspberry Pi device is a web server. Web servers are great because they can help you install or host web apps and in our case, Lighttpd is a good choice for Raspberry Pi because of its very low memory footprint.

Here's how to install Lighttpd on Raspberry Pi.


Publishing Static Web Pages

First things first. Update your package manager called Advanced Package Tool.

sudo apt-get update && sudo apt-get upgrade  

Time to install a web server so that you can publish a static web page.

Install the Lighttpd web server.

sudo apt-get install lighttpd  

Add the PI user to www-data group and set the permission flags of the /var/www directory. This is the default document root.

sudo adduser pi www-data  
sudo chown -R www-data:www-data /var/www  
sudo chmod -R 775 /var/www  

Once you're logged in and you've completed setting up a few permissions, go ahead and create your first web page.

nano /var/www/index.html  

Publishing Dynamic Web Pages

PHP is a good choice for Raspberry Pi because it's easy to install and it doesn't consume a lot of resources.

Update apt-get package manager for Raspberry Pi

sudo apt-get update  

Install PHP

sudo apt-get install php5-cgi  

Install FastCGI module in the Lighttpd to speed things up

sudo lighty-enable-mod fastcgi  

 
Reload lighttpd web server.

sudo /etc/init.d/lighttpd force-reload  

Use nano to modify the web server config file

sudo nano /etc/lighttpd/lighttpd.conf  

Enable PHP and FastCGI

​​fastcgi.server = (".php" => ((​​
     
​​  "bin-path" => "/usr/bin/php-cgi",​​
     
​​  "socket"   => "/tmp/php.socket"​​
     
​​)))​​

Restart the web server

sudo service lighttpd restart  

Test your work by adding this to the file /var/www/index.php

<?php  
     phpinfo();
?>

Troubleshooting Wifi

Configuring your Raspberry Pi to a wireless network is a little bit harder than going to your "Settings" on iPhone or Android. Here are a few helpful commands.

After installing a Wifi USB dongle to your USB port, use this command to find it's location.

lsusb  

You can take a closer look at the Pi’s boot message with the dmesg command and see whether the WLAN stick has been initialized properly.

dmesg | less  

Get the current status of your Pi’s wireless network interfaces.

iwconfig  

Once your Wifi USB dongle is working properly, you'll need to search for your Wifi network.

sudo iwlist scan | grep ESSID