Installing Web Server on Raspberry Pi – Part2

We are in part 3 of tutorial series of Building Raspberry Pi Line Follower Robot! If you want to jump back and forth or missed any of my previous tutorials – here is the consolidated list.

All Tutorials to Build IOT Raspberry Pi Robot

  1. Build Raspberry Pi Robot controlled by Internet – IOT Robot
  2. Setup Static IP Address on Raspberry Pi
  3. Installing Web Server on Raspberry Pi
  4. Controlling Raspberry Pi LED via internet
  5. Raspberry Pi LED controlled by Internet
  6. Build REST API using Python and Flask
  7. Raspberry Pi Robot Logitech Camera Server
  8. Create Web Dashboard for IOT Robot Control

Entire project Video Tutorial


 

The biggest challenge is to decide which server to go for. We have memory limitations, CPU Clock speed etc on Raspberry Pi. So, choosing the server is going to be very critical if you are thinking of scaling at a later point. Utilizing this tutorial, you could make your very own Raspberry Pi Web Server which enables you to get to your documents from any piece of the world utilizing the web. No compelling reason to stress over the DDNS Client, you can get it for nothing from no-ip.com. Below are the options that I looked for:

  1. Apache
  2. Nginx
  3. Lighttpd

Apache

It is a process based server meaning each simultaneous connection requires a thread which incurs significant overhead. If there is a low volume of connections then there isn’t much difference between these three servers. But Apache is undoubtedly the best process based server. Apache 2 has lot of improvements but again it needs lot of configuration knowledge. As we talked about scaling above, I will not go for this one.

Nginx

It is an asynchronous system meaning it handles requests in a single (or at least, very few) threads. This causes less use of resource and hence is faster. The main advantage of the asynchronous approach is scalability.  Serving 10,000 simultaneous connections would probably only cause Nginx to use a few megabytes of RAM whereas Apache would probably consume hundreds of megabytes (if it could do it at all). Nginx is best choice for me as I want to build scalable solution. Quora.com uses Nginx.

Lighttpd

Lighttpd is an asynchronous server. It runs as single process with a single thread and non-blocking I/O.  Nginx and Lighttpd are very alike and bear similar features. Lighttpd runs as a single process with a single thread and non-blocking I/O where as nginx works as one master process but delegates its work unto worker processes. Various websites have clearly suggested that Nginx is faster and more reliable than Lighttpd. Isohunt.com uses Lighttpd.

Installing Nginx on Raspberry Pi 3

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 configuration:

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;
}

Restart Nginx web server.

sudo service nginx restart

Any web server with no server side language will not be fully functioning as it cannot serve any dynamic content. We are going to continue using python as server side scripting language. We want to execute the scripts from client – browsers. I am using Nginx with uWSGI. Here is the step by step guide to setup:

Installing uwsgi plugin on Raspberry Pi 3

First of all Install uwsgi plugin

sudo apt-get install uwsgi
sudo apt-get install uwsgi-plugin-cgi

Make a uWSGI Config .ini

cd /etc/uwsgi/apps-available
sudo nano uwsgi_config.ini

copy paste below code:

      [uwsgi]
      plugins = cgi
      socket = 127.0.0.1:8000 
      cgi = /var/www
      cgi-allowed-ext = .py
      cgi-helper = .py = python
      die-on-term = true

Link config file in apps-enabled to apps-available. Linking the file in apps-available will auto update file
in apps-enabled, so you don’t have to mv apps-available
file to apps-enabled.

cd ../apps-enabled/
sudo ln -s ../apps-available/uwsgi_config.ini

Restart uWSGI service

sudo service uwsgi restart

To see if uWSGI service id running

ps aux | grep uwsg

Now, it is time to modify the nginx config file to support uwsgi

sudo nano /etc/nginx/sites-available/default

Within (server) section after the last (location) entry type below:

      location ~ .py$ {
            uwsgi_pass 127.0.0.1:8000;
            include uwsgi_params;
            uwsgi_modifier1 9;
      }

Testing Python Script from browser

First of all, create a new file test.py in /var/www/

sudo nano /var/www/test.py

Copy and paste below code. It is just for printing hello world in browser:

#!/usr/bin/env python 
print "Content-type: text/html\n\n" 
print "
<h1>Hello World on Nginx Server</h1>

" 

Provide execute permissions

sudo chmod +x /var/www/test.py 

Restart Uwsgi / NGinx

sudo service uwsgi restart
sudo service nginx restart

Test Web Server the .py cgi
http:///test.py in my case as we configured static address it is http://192.168.1.100/test.py

Please follow and like us:

Leave a Reply

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