Difference between revisions of "RPi webserver"

From eLinux.org
Jump to: navigation, search
(Created page with " == Introduction == 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 bu...")
 
m (See Also)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
+
[[Category:RaspberryPi]]
 
== Introduction ==
 
== Introduction ==
 
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.
 
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.
Line 8: Line 8:
  
 
== Install Process ==
 
== 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:<br>
 +
<code>
 +
sudo addgroup --system www-data<br>
 +
sudo adduser www-data www-data
 +
</code><br>
 +
Next run the install, this will take a few moments:<br>
 +
<code>
 +
sudo apt-get install lighttpd
 +
</code><br>
 +
You can check that the server is installed correctly by typing:<br>
 +
<code>ps -u www-data u</code><br>
 +
 +
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:<br>
 +
<code>http://[your raspberry pi address]/index.lighttpd.html</code>
 +
 +
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:<br>
 +
<code>sudo apt-get install mysql-server php5-cgi php5-mysql</code><br>
 +
 +
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:<br>
 +
<code>sudo apt-get clean</code>
 +
 +
== Configuration and Setting Up ==
 +
=== Configuration of Lighttpd ===
 +
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:<br>
 +
<code>sudo lighty-enable-mod cgi</code><br>
 +
 +
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:<br>
 +
<code>sudo pico /etc/lighttpd/conf-enabled/10-cgi-php.conf</code><br>
 +
 +
change line to read as follows:<br>
 +
<code>#server.modules +=("mod_cgi")</code><br>
 +
 +
To 'SAVE' changes press Ctrl-O and [enter] followed by Ctrl-X and [enter].
 +
 +
Now edit the main configuration files as follows:<br>
 +
<code>sudo pico /etc/lighttpd/lighttpd.conf</code><br>
 +
 +
Add the following line to the server modules section:<br>
 +
<code>"mod_cgi",</code><br>
 +
 +
Add the following to the end of the file:<br>
 +
<code>cgi.assign = (".php" => "/usr/bin/php5-cgi")<br>
 +
server.error-handler-404 = "/index.html"
 +
</code><br>
 +
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:<br>
 +
<code>sudo /etc/init.d/lighttpd force-reload</code><br>
 +
 +
=== Setting Up and Test ===
 +
I would suggest you may want to check the php service is working ok by creating a quick .php file:<br>
 +
<code>sudo pico /var/www/phpinfo.php</code><br>
 +
 +
And add the following lines:<br>
 +
<code>
 +
<?php<br>
 +
phpinfo();<br>
 +
?>
 +
</code><br>
 +
 +
Now as before use a browser to open your raspberry pi and access phpinfo.php<br>
 +
<code>http://[your raspberry pi address]/phpinfo.php</code><br>
 +
 +
== References ==
 +
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 Webserver]] - an alternative webserver on the Raspberry Pi

Latest revision as of 12:31, 4 July 2012

Introduction

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 and Setting Up

Configuration of Lighttpd

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:
"mod_cgi",

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:
<?php
phpinfo();
?>

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

References

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