How to turn your Raspberry Pi into a web server

Why Raspberry Pi?

 

 

Raspberry Pi

 

Many people would say that the Raspberry Pi is not suitable to be used as a web server because of its relatively low hardware performance and resources. I tend not to agree. The Raspberry Pi is the perfect piece of hardware for being a web server! First of all it’s one of the cheapest (if not the cheapest) regular Linux computers on the market. In other words the Raspberry Pi makes it possible for virtually everybody to have their own low-cost web server. Second of all, its extremely low power consumption allows you to keep it running 24/7 without having to pay a lot of money for electricity. A regular high-end server running all the time would definitely make its presence noticed on the electricity bill (and have an environmental impact too). Finally, in contrast with powerful servers, the Raspberry Pi is small and quiet. It has no fans or any other moving components. You can fit it into the smallest corner of your house and you’ll never hear it making any noise. You can even put it under your pillow and sleep on it while the Pi keeps serving your web pages. Well, yes, it’s true, its processor is only running at 700 MHz and even model B only has 512 MB of RAM, but usually that will not be a problem. Unless you’re going to host some ultra-busy portal on it or some online store with tens of thousands of visitors, the little Pi will rise to the expectations and do its job well enough. If you only intend to host a personal page, a blog or any other website that will not have more than a few thousand visitors a day, the Raspberry Pi is the perfect hardware for your web server.

 

The LAMP server

So how exactly do you turn a Raspberry Pi into a web server? Without a doubt, there are many ways to achieve this. We are going to turn it into a LAMP server, in other words we are going to install Apache, MySQL and PHP on it (LAMP is an acronym for Linux + Apache + MySQL + PHP). These applications are  usually more than enough for most of the websites and they are all free. If you plan to host something very special, there are always ways to extend this system. We are going to assume that you already have Linux running on your Raspberry Pi (Raspbian, to be more specific, but this tutorial should be usable with other distributions as well, perhaps with some minor modifications). So all we have to do is to get Apache, PHP and MySQL up and running.

Before you try installing anything, you should make sure that your repositories are up to date. Open a command line and run the command:

apt-get update

If you’re not logged in as root, you’ll need to add “sudo” before this command and before many other commands that will follow.

 

I. Installing and configuring Apache

Apache LogoThe Apache HTTP server is the most important software component of your Raspberry Pi web server. It will serve basic content to the users, such as static HTML and images. Installing it under Raspbian is very easy. Just follow the steps below:

1. Install Apache v2:

apt-get install apache2

It will tell you that it will download and install a few packages that will take up a number of megabytes on your SD card. Just say yes (“y”) and let it do its job automatically.

2. (Optional) Additional configuration:

When the Apache2 service starts, you’ll most likely see the following warning: “Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName”. This can be solved by editing one of its main configuration files:

nano /etc/apache2/apache2.conf

Add the following line to the end of the file and save it:

SeverName localhost

Save the file (CTRL+O) and restart the Apache2 service:

service apache2 restart

3. (Optional) Set up port forwarding:

if your Raspberry Pi is in a local network, behind a router, don’t forget to set up port forwarding in your router for port 80 (the default HTTP port) to your Raspberry Pi (to the local IP of your Raspberry Pi in your local network). To obtain this IP, type:

/sbin/ifconfig

In the eth0 or wlan0 entry (depending on how your Raspberry Pi is connected to the network – through the Ethernet port or through WiFi) you should see an IP address like 192.168.1.100 (or similar). that’s the Pi’s local IP address.

4. (Optional) Test:

Let’s check that Apache is indeed working well. Change the current directory to the website’s root directory:

cd /var/www

This is where all the content will have to be stored in order for the web server to be able to show it to the outside world. Create the index.html, which is shown in the web browsers whenever somebody visits your site:

nano index.html

Paste the following test code into it:

<html>

<head><title>Hello! This website is working!</title></head>

<body>Welcome to my website! It’s working!</body>

</html>

Save the file.

Open a web browser and navigate to your website (http://yourwebsite.com for example or you could enter your IP address). You should see in the browser the test messages that you have just saved into index.html.

 

II. Installing and configuring PHP

PHP LogoIf your website only uses static content (HTML, images), then installing Apache is enough. But if it uses dynamic content, then you will also need to install something that can generate that dynamic content. In the LAMP solution we will use PHP for this purpose.

1. Install PHP v5:

apt-get install php5

2. Configure PHP to work with Apache:

In your php5 directory look for a sub-directory which contains a date in its name:

ls /usr/lib/php5/

You should see something like “20100525+lfs” among the listed files and directories.

Put this in the php.ini file:

nano /etc/php5/apache2/php.ini

Find the line which begins with “extension_dir” (use F6 to search). Uncomment it (remove the “#” from the beginning of the line) if it’s commented and make sure that it looks like this:

extension_dir = /usr/lib/php5/20100525+lfs/

(if your directory is not exactly “20100525+ifs”, add your own directory name here instead)

3. Configure Apache to work with PHP:

nano /etc/apache2/apache2.conf

Make sure that it contains the following line:

Include conf.d/*.conf

It should be below the comment line “Include generic snippets of statements”. You can also add it to the end of the file.

Create the PHP configuration file for Apache:

nano /etc/apache2/conf.d/php.conf

and paste the following contents into it:

# PHP is an HTML-embedded scripting language which attempts to make
# it easy for developers to write dynamically generated webpages.
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter to handle files with a .php extension.
AddHandler php5-script .php
AddType text/html .php
#
# Add index.php to the list of files that will be served as
# directory indexes.
DirectoryIndex index.php

4. (Optional) Test PHP:

Test from command line by requesting the PHP version information:

php -v

This should give you some information about the installed PHP.

Now let’s create a test PHP script, to see if it’s working in the browser too:

cd /var/www

nano phptest.php

Paste the following test code into it:

<html>
<head><title>PHP Test</title></head>
<body><?php phpinfo();?></body>
</html>

Save the file and set the access rights:

chmod 775 phptest.php

Open a web browser and try to access the test script (enter http://yourwebsite.com/phptest.php or yourip/phptest.php in the address bar). You should see a long table with various information about yours system, PHP credits, information about Apache, etc.

5. (Optional) Install APC:

APC (Alternative PHP Cache) is an application which caches the PHP code and user variables, which results in more responsive, faster websites and less congested servers. This is particularly important on the Raspberry Pi because it has limited hardware resources and we need to optimize the web server as much as possible. To install APC, simply type:

apt-get install php-apc

6. (Optional) Test APC:

You should have a PHP script named apc.php inside your /var/www directory. Test it in your web browser by entering http://yourwebsite.com/apc.php or yourip/apc.php in the address bar.

You should also see some APC-related info if you test with the phptest.php script created in step 2.

7. Restart Apache:

service apache2 restart

 

III. Installing and configuring MySQL

MySQL LogoIf your website needs to save, store and retrieve data, for example user names, login data, etc, then you will need a database server. In the LAMP solution MySQL plays this role.

 

 1. Install the MySQL sever and client:

apt-get install mysql-server mysql-client

At some point during the installation it’s going to ask for the MySQL root password. Give it a password which is complicated enough to be secure and make sure you don’t forget it because you’ll need it later when you’ll continue developing your website with parts that communicate with the database.

2. Install the PHP MySQL module:

apt-get install php5-mysql

This module will allow PHP to communicate with MySQL.

3. Restart Apache:

service apache2 restart

 

And you’re done! At this point you should have a fully functional LAMP server running on your Raspberry Pi.

 


Comments

How to turn your Raspberry Pi into a web server — 19 Comments

  1. I cant control the GPIO of the PI by means of a bowser ..
    ie. thru apache !!
    Wat should i do ??
    I execute a normal php program and it runs fine..
    PS. I ve written the GPIO code also using PHP.. Also this works fine wen run thru terminal! help !!

  2. Sugan,

    Most likely you don’t have the permissions on the device node set for the apahce user to run it. Either add the user that runs apache to a group with permissions on the device node, or set the group for your device node to apaches default group. Make sure the perms are correct on the device node for this as well.

  3. In the instructions for Apache configuration you say to add a line “SeverName localhost” to the config file. I don’t know much Linux but I suspect this is a typo?

    • apc.php is located in “/usr/share/doc/php-apc/apc.php” if isnt in that directory try to find it with “find / -name apc.php* 2>/dev/null”
      then
      sudo cp /usr/share/doc/php-apc/apc.php /var/www/ or

      sudo cp /find/your/directory/apc.php /var/www/

  4. Pingback: Raspberry Pi + Wordpress => PiPress | Dev Leader

  5. Hi, I am a high school student and I have a web development class and I just wanted to ask if A website done in Adobe dreamweaver can be use with or can be place on a raspberry pi working as a web server. If anyone has any idea send me an email at salem2@live.com thank you.

    • Hi Josh!

      I haven’t worked with Dreamweaver for at least 8 years now, so I’m not sure that it does exactly these days, but in the past it used to simply generate HTML code, which should work with any web server just fine, including the one that will run on the Pi, for example Apache.

  6. Hi,
    Can you please confirm whether the performance of Raspberry Pi is suitable for day to day software development work in LAMP environment or not?

    Best Regards …
    Pankaj Kumar

    • Hi! Well, I haven’t exactly done software development on the Pi in the sense of writing code directly on it. I’m writing the code on my desktop computer and then I just upload it to the Pi. But if I had to guess, I’d say that the Pi’s performance is more then enough for developing software. Many years ago I developed software on a 300 Mhz computer with 64 MB of RAM. The Pi’s CPU runs at 700 Mhz and it has 256/512 MB of memory, so I think the performance is pretty much good.

  7. Pingback: Home Automation: Using the Raspberry Pi to control the lights in your home over Wi-Fi | IQ Jar

  8. Above you have the command “/sbin/ifconig” listed, I think you meant ifconfig (note the missing second “f”)

Leave a Reply

Your email address will not be published. Required fields are marked *