How to set up a lightweight web server on Raspberry Pi

There are a variety of web server software available for Linux-based platforms including Raspbian. Using one of those available web server software, we can turn Raspberry Pi into a 24/7 available portable web server. In this case, however, we must understand that Raspberry Pi has hardware limitations in terms of CPU clock speed, memory, etc. As such, we want to avoid running resource-heavy software (e.g., Apache) on Raspberry Pi.

Among web server software available for Raspbian, Nginx and Lighttpd are two of the most widely used lightweight web server engines. This tutorial will describe how to install and set up Nginx or Lighttpd web server on Raspbian-powered Raspberry Pi. Choose one as you prefer.

Install and configure Nginx web server on Raspberry Pi

Update your repository and install Nginx with apt-get command.

$ sudo apt-get update
$ sudo apt-get install nginx

Change the default document root directory of Nginx from /usr/share/nginx/www to /var/www.

$ sudo mkdir /var/www

Edit the default site's configurations as follows.

$ sudo nano /etc/nginx/sites-available/default
server {
        listen 80;
        server_name $domain_name;
        root /var/www;
        index index.html index.htm;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
}

Start Nginx web server.

$ sudo service nginx restart

To test if Nginx web server is working or not, put a simple index.html in /var/www, and open it with your browser.

Any web server with no server side language will not be fully functioning as it cannot serve any dynamic content. For a typical web server, PHP is used as a primary server side language. A PHP request sent by user's web browser to a web server is handed over to PHP for execution. Once PHP has completed its processing, the output is sent back to a web server, which is then finally forwarded to the browser.

With Nginx, php-fpm is used to execute PHP programs. So the next step is to install php-fpm as well as php-apc. The latter is a PHP extension for accelerating PHP performance.

Install php5-fpm and php-apc with apt-get.

$ sudo apt-get install php5-fpm php-apc

Edit the default site's configuration to add the following inside server block.

$ sudo nano /etc/nginx/sites-available/default
server {
        listen 80;
        server_name $domain_name;
        root /var/www;
        index index.html index.htm;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        location ~\.php$ {
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param HTTPS off;
                try_files $uri =404;
                include fastcgi_params;
        }
}

Restart Nginx web server.

$ sudo service nginx restart

As a test, create a simple PHP file in /var/www as follows.

$ sudo nano /var/www/test.php
<?php
phpinfo();
?>

Go to http://<raspberrypi-ip-address>/test.php. If you see information about your PHP setup, it means php-fpm is set up properly.

Install and configure Lighttpd web server on Raspberry Pi

Lighttpd is another speed-enhanced lightweight web server software that can run on Raspbian. Proceed as follows to install and configure Lighttpd on Raspbian.

Create and add user/group named www-data on your Raspberry Pi.

$ sudo addgroup --system www-data
$ sudo adduser www-data www-data

Update your repository, and install Lighttpd with apt-get.

$ sudo apt-get update
$ sudo apt-get install lighttpd

To test whether Lighttpd is working or not, go to http://<raspberrypi-ip-address>/index.lighttpd.html from your browser to verify the page is loading.

Next, install and enable php-cgi for Lighttpd web server.

$ sudo apt-get install php5-cgi
$ sudo lighty-enable-mod fastcgi-php

Create a simple PHP file in /var/www, and open Go to http://<raspberrypi-ip-address>/test.php through your browser. If you see the following PHP page, it means PHP has been set up successfully.

$ sudo nano /var/www/test.php
<?php
phpinfo();
?>

Configure MySQL and phpMyAdmin

In many cases, a web server is operated along with a backend database server. This part describes how to install MySQL as a database server and phpMyAdmin as the management interface for manipulating databases.

Install MySQL, phpMyAdmin, and php5-mysql.

$ sudo apt-get install mysql-server mysql-client php5-mysql phpmyadmin

During MySQL server installation process, you will be asked to configure the password for root user of MySQL. You will also be asked to choose the web server installed in the system (Apache2 or Lighttpd). In case of Nginx, you can leave the web server selection field empty.

During phpMyAdmin installation, you will be asked to configure database for phpMyAdmin. Answer "yes". When asked to enter the password of the administrative user, provide the password.

Make a link of phpMyAdmin from /usr/share/phpmyadmin to /var/www/phpmyadmin.

$ sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin

Open phpMyAdmin from your browser by going to http://<raspberrypi-ip-address>/phpmyadmin, and log in as "root" using the administrative password that you have set earlier.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Kristophorus Hadiono is a Linux enthusiast and a teacher in private higher educations in Indonesia. He uses Linux for his daily computing, also when he teaches his students. He is currently continuing his education with government scholarship at a private university in Bangkok, Thailand.

16 thoughts on “How to set up a lightweight web server on Raspberry Pi

  1. Just install uWSGI and use the internal web server. One package gets you static, PHP, Python, Lua, Ruby, etc etc.

  2. Why does everyone hate on Apache2 and FastCGI? I understand that Apache uses processes and nginx utilizes an asynchronous event model, but seriously neither is going to run WordPress as fast as a traditional server. I have been using Apache2 with FastCGI to host 2 public sites (with a PHP site and MySQL backend) for seven months now with no speed issues. I just don't understand the fascination.

  3. Just curious has anyone tried another lightweight web server "monkey httpd"? Personally film using lighttpd. It's good so and I'm too lazy to test the monkey :p

  4. Just a small comment. You got the link backward the link is from /var/www/phpadmin to /usr/share/phpadmin. You point from the alias to the original.

    Otherwise a great article. Thanks!

    • I think the article is right. You want to create a symbolic link under document root directory (/var/www), which points to the location of installed phpmyadmin (/usr/share/phpmyadmin). Hence:

      $ sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin
  5. I use the RP with pre-configured setups like Raspbian.
    Can someone make such a setup for this use?
    It's too complicated for me, but I would love to experiment with a webserver on my RP.

  6. it doesn't work.
    following the instructions for lighttpd, i get:
    phpMyAdmin - Error
    The mysqli extension is missing. Please check your PHP configuration.

    • You will need to enable mysqli extension for the latest phpmyadmin. Try adding the following line in /etc/php5/cgi/php.ini:

      extension=mysqli.so
      
  7. When doing things according to this article I got error 403 when trying to start phpMyAdmin.
    From another site I got this tip which fixed the problem

    "after this you can get the 403 Forbidden but should add the index.php inside the server tag where index.html and others are:

    /etc/nginx/sites-available/default

    and then restart nginx"

  8. another question:
    browsing test.php from a remote machine works, but doing it from midori or any other browser on the pi itself does not work. it just lists the script instead of running it. this is a problem because the phpBBS installer is browser-based!

  9. hey, whenever i follow these instructions trying to start nginx i get an error because port 80 is already in use. so looking closer there's an apache2 service running -> something - i think it's the php5 package - always installs the apache service -> how can i prevent it from being installed and autorun?

    thank you very much

Leave a comment

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

Current ye@r *