Ashok Raja's Blog
Serving a Python Flask Web Application via Apache Webserver in Raspberry Pi
by Ashok Raja T 19. April 2016 01:08

This is a continuation of my pervious article on gathering System information of a Raspberry Pi with Python and Flask. In this article we can see how to run that application via Apache Web Server.

In my previous article , I have deployed the Python Flask application into a folder named “/home/pi/www/sysinfo” in which “pi” is the username which may change in your case if you have configured the Raspbian OS with different user name. To configure routing via Apache, I have added a folder named “logs” to the existing project folders and have added a new file named sysinfo.wsgi . This is an Apache wsgi configuration file and this requires the “wsgi” modeule for Apache to be installed in Raspberry Pi. If you have not installed this module before, you can install it by executing the below command in the terminal

sudo apt-get install libapache2-mod-wsgi

Add the below script to the newly created file “sysinfo.wsgi” ( change the folder path in the below script according to your file location)

 import sys
 sys.path.insert(0, '/home/pi/www/sysinfo')
 from index import app as application

Note: You can create this file with leafpad editor or inbuilt terminal editor nano. If you are using nano , execute the command “nano /home/pi/www/sysinfo/sysinfo.wsgi” in terminal and copy paste the above script

The next step is to create a configuration file for Apache Web Server. This requires the default ‘pi” user to be added to the “www-data” user group of your Linux ( Raspbian) system. To check the user’s groups, execute the command “id pi” in terminal. This displays the list of user groups in which the user ‘pi’ is a member of. If the “pi” user is not a member of “www-data” group add the user to this group by executing the below command

sudo usermod –a -G "www-data" pi

The above command adds the existing user “pi” to the user group “www-data”

To create the config file execute the below command in terminal

sudo nano/etc/apache2/sites-available/sysinfo.conf

Copy paste the below scripts to the file and save it.

 WSGIDaemonProcess sysinfo user=pi group=www-data threads=5
 WSGIScriptAlias /sysinfo /home/pi/www/sysinfo/sysinfo.wsgi
 <Directory "/home/pi/www/sysinfo">
 	WSGIProcessGroup sysinfo
 	WSGIScriptReloading On
 	WSGIApplicationGroup %{GLOBAL}
 	Require all granted
 ErrorLog /home/pi/www/sysinfo/logs/error.log

Note: Change the folder location based on the location of your site

The final step is to tie the knot between your python website and Apache web server. This is done by executing the below command in terminal. This adds a virtual path to Apache and the Python application would be served as a WSGI application by Apache web server.

sudo /usr/sbin/a2ensite sysinfo.conf

The above command expects the Apache configuration to be re-loaded. This can be done by executing the below command in terminal.

sudo service apache2 reload

Now we would be able to access the python flask web application by accessing http://[defaultapachewebsiteurl]/sysinfo . In my case it’s You can port forward this site as a regular Apache website and access it from outside your environment.

The below is output of the application accessed on LAN


Currently I am behind a double NATed network and my service provider does not support port forwarding. But I have a work around with different set of tools to access my Raspberry Pi outside my home network which I will explain in detail in subsequent articles.

blog comments powered by Disqus