Difference between revisions of "RPi webserver"

From eLinux.org
Jump to: navigation, search
(new See Also section with nginx link)
Line 95: Line 95:
http://www.w3schools.com/ > Learn HTML, php and mySQL free on one of the best reference sites.
http://www.w3schools.com/ > Learn HTML, php and mySQL free on one of the best reference sites.
== See Also ==
* [[RPi nginx]] - an alternative webserver on the Raspberry Pi

Revision as of 11:40, 4 July 2012


The board may not look that powerful but there is more than enough grunt to run a small web server supported by php and mysql. I have not covered this here but have also installed wordpress on the server and runs very well too.

This HOWTO is based on the Debian build and has been tested on the first and second release.

Please note you are required to check security setting and access to the server, this is a guide on getting to the first point of having a working server and first few pages.

Install Process

Installing Lighttpd

Lighttpd is one of the smallest web server around and ideal for the raspberry Pi as its uses very little memory.

First you need to set up a group and user that the web server can use:
sudo addgroup --system www-data
sudo adduser www-data www-data

Next run the install, this will take a few moments:
sudo apt-get install lighttpd
You can check that the server is installed correctly by typing:
ps -u www-data u

The main web server is now installed and can be checked by opening a browser and entering the address of your server and checking Lighttpd's default information page:
http://[your raspberry pi address]/index.lighttpd.html

NOTE: Your web sites files are located at /var/www/

However you will also need php and mysql to get the best out of a web site...

Installing php5 and mySQL

This is much bigger than the web server alone so is going to take some time to download and install... for me it was around 10 mins.

These can both be installed in one go by running the following:
sudo apt-get install mysql-server php5-cgi php5-mysql

NOTE: During the install you will be asked for a password for accessing mySQL with. As this will be for root access I suggest something you will not forget! You will need it again at some point!

The apt-get program will have left the downloads on your server and these take up space. If you want to remove them (as its now all installed) you can run the following to remove all apt-get download files:
sudo apt-get clean

Configuration ans Setting Up

Configuration of Lighttpd

So as to access the php server your web server needs to know where its installed. I'll also add a Error 404 handler as there is nothing worse than a visitor getting a Error 404 from your site!

To turn on the cgi mod of the server type the following:
sudo lighty-enable-mod cgi

I think this did more than create a configuration file but found I had to remove the mod and add the php setting directly in the Lighttpd configuration file as follows:-

First remove the cgi reference in the cgi config file by adding a '#' to the start of the line. Edit the file as follows:
sudo pico /etc/lighttpd/conf-enabled/10-cgi-php.conf

change line to read as follows:
#server.modules +=("mod_cgi")

To 'SAVE' changes press Ctrl-O and [enter] followed by Ctrl-X and [enter].

Now edit the main configuration files as follows:
sudo pico /etc/lighttpd/lighttpd.conf

Add the following line to the server modules section:

Add the following to the end of the file:
cgi.assign = (".php" => "/usr/bin/php5-cgi")
server.error-handler-404 = "/index.html"

To 'SAVE' changes press Ctrl-O and [enter] followed by Ctrl-X and [enter].

The first line added enables the cgi module, the second points to the php-cgi server if a .php file is accesses and the last is our Error 404 handler.

You can set the file names to any page you want, I just set it to index.html as a default.

Last thing to do in the configuration is to restart the server as follows:
sudo /etc/init.d/lighttpd force-reload

Setting Up and Test

I would suggest you may want to check the php service is working ok by creating a quick .php file:
sudo pico /var/www/phpinfo.php

And add the following lines:

Now as before use a browser to open your raspberry pi and access phpinfo.php
http://[your raspberry pi address]/phpinfo.php


http://www.lowendbox.com/blog/yes-you-can-run-18-static-sites-on-a-64mb-link-1-vps/ > This HOTO is taken from here and tweaked and made a little clearer for Raspberry Pi owners.

http://www.w3schools.com/ > Learn HTML, php and mySQL free on one of the best reference sites.

See Also

  • RPi nginx - an alternative webserver on the Raspberry Pi