Monday, 9 September 2013

How to build a Raspberry Pi webserver

Building a Raspberry Pi web server

Prepare your Pi for being a web server

You should follow the following tutorials from this blog before turning your Pi into a web server.

  1. Set a root password
  2. Assign a static IP address
  3. Perform some basic security updates
We'll assume that you've done that, and that you created a www-data user account as per the security tutorial. If not, do that now. Login to your Pi and:

$ sudo useradd www-data
$ sudo passwd www-data

You might also want to follow the tutorial on this blog for overclocking your Pi, but be aware of the risks of overheating.

Install the web server software

We're going to use Lighttp as a web server. You could use Apache but your Pi isn't really powerful enough for that. Lighttp is a lightweight web server.

Login to your Pi as www-data, then:

$ sudo apt-get install lighttpd

Change the directory owner and group:
$ sudo chown www-data:www-data /var/www

Allow the group to write to the directory$ sudo chmod 775 /var/www


Logout and back in again to pick up the new permissions. Now let's test the web server. Point your PC's browser to the ip address of your Pi. You should get a default placeholder page saying something like "The owner of this web site has not put up any web pages yet. Please come back later". This is fine, it tells us that the web server is installed and running.

On your Pi remove the default index page that gave this default page:

$ rm /var/www/index.lighttpd.html

If all you want is a basic web server for some home photos, or to play around with learning HTML then you don't need to do anything else. Your web server is running. It's only visible to other computers on your home network though. To make it visible on the internet you'll need the tutorial on publishing your Pi onto the internet. I'm going to write that soon.

If, you want to create or use dynamic database driven web pages - perhaps your own Word Press server, or anything requiring logins - perhaps a shopping cart? Then you'll need MySQL for the database(s), and PHP for the server side (clever) stuff.

Install the MySQL database (optional)

$ sudo apt-get install mysql-server

During this process it will ask you to set a password for the MySQL root user. This is different and separate from your root login account to the Pi. The installation will take about 5-10 minutes.

Install PHP (optional)

$ sudo apt-get install php5-common php5-cgi php5

If you installed MySQL above then also:

$ sudo apt-get install php5-mysql
This will also take about 5-10 minutes.

Fix the PHP config file for some common problems
$ sudo vi /etc/php5/cgi/php.ini
Uncomment the line cgi.fix_pathinfo=1

Now restart the web server again:
$ sudo service lighttpd force-reload

Testing the PHP installation:
Create a file /var/www/index.php

With the following code:

<?php
  print <<< EOT
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test successful</title>
</head>
<body>
<h1>Test successful</h1>
<p>Congratulations.</p>
<p>Your webserver and PHP are working.</p>
</body>
</html>
EOT;

?>

Point your PC's browser to the IP address of your Pi. You should see a screen showing your PHP installation is working.

Error 403 - Forbidden?
If you get error 403 - Forbidden then you also need to enable PHP-cgi. Get a root login and:

# cp /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/
# cp /etc/lighttpd/conf-available/15-fastcgi-php.conf /etc/lighttpd/conf-enabled/
# service lighttpd restart

Try again with your PC pointing to the Pi's IP address. You should now have the installation working screen.

Now let's make really sure it's working. Create a file /var/www/test.php

And in it, put:

<?php
phpinfo();
?>

Save it, and point your browser to it's IP address/test.php You will now see your PHP configuration file if everything has worked.

If you installed both MySQL and PHP, then in the next tutorial, we'll set up phpmyadmin which is a browser based MySQL configuration and admin tool.

No comments:

Post a Comment