AMP AMP

How to run Python Scripts with Apache and mod_wsgi on Linuxmint 18.03

Python Scripts with Apache and mod_wsgi on Linuxmint 18.03

mod_wsgi is an Apache module that can be used for serving Python scripts over HTTP via Apache web server. You can easily deploy applications written with frameworks and tools like Web.py, Werkzug, Chery.py, TurboGears, and Flask using mod_wsgi.In this tutorial, we have to see about how to install and set up of mod_wsgi with the Apache server.

Install Apache and mod_wsgi

Before starting, install the required packages to your system. You can install them by running the following command:

linuxhelp ~ # apt-get install python libexpat1 apache2 apache2-utils ssl-cert 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ssl-cert is already the newest version (1.0.37).
apache2 is already the newest version (2.4.18-2ubuntu3.9).
apache2-utils is already the newest version (2.4.18-2ubuntu3.9).
.
.
.
 (Reading database ... 220633 files and directories currently installed.)
Preparing to unpack .../python_2.7.12-1~16.04_amd64.deb ...
Unpacking python (2.7.12-1~16.04) over (2.7.11-1) ...
Processing triggers for doc-base (0.10.7) ...
Processing 41 changed doc-base files...
Error in `/usr/share/doc-base/xapian-python3-docs', line 9: all `Format' sections are invalid.
Note: `install-docs --verbose --check file_name' may give more details about the above error.
Registering documents with scrollkeeper...
Processing triggers for man-db (2.7.5-1) ...
Setting up python (2.7.12-1~16.04) ...

Once all the required packages are installed, proceed to install mod_wsgi with the following command:

linuxhelp ~ # apt-get install libapache2-mod-wsgi 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  libapache2-mod-wsgi
0 upgraded, 1 newly installed, 0 to remove and 399 not upgraded.
Need to get 77.5 kB of archives.
After this operation, 248 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial/main amd64 libapache2-mod-wsgi amd64 4.3.0-1.1build1 [77.5 kB]
Fetched 77.5 kB in 0s (81.9 kB/s)        
Selecting previously unselected package libapache2-mod-wsgi.
(Reading database ... 220633 files and directories currently installed.)
Preparing to unpack .../libapache2-mod-wsgi_4.3.0-1.1build1_amd64.deb ...
Unpacking libapache2-mod-wsgi (4.3.0-1.1build1) ...
Setting up libapache2-mod-wsgi (4.3.0-1.1build1) ...
apache2_invoke: Enable module wsgi
Configure Apache for mod_wsgi

Now create a python script inside the Apache web root directory to serve via mod_wsgi Apache module.

linuxhelp ~ # vim /var/www/html/wsgy.py
def application(environ,start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<div data-style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'Welcome to mod_wsgi Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]

Change ownership and permission

linuxhelp ~ # chown -R www-data:www-data /var/www/html/wsgy.py
linuxhelp ~ # chmod -R 775 /var/www/html/wsgy.py

configure Apache to serve this file over HTTP protocol. You can do this by creating wsgi.conf file

linuxhelp ~ # vim /etc/apache2/conf-available/wsgi.conf
WSGIScriptAlias /wsgi /var/www/html/wsgy.py

Then, enable mod-wsgi configuration and restart Apache service with the following command:

linuxhelp ~ # a2enconf wsgi 
Enabling conf wsgi.
To activate the new configuration, you need to run:
service apache2 reload

Once configuration completed restart the apache server.

linuxhelp ~ # systemctl restart apache2.service

Now open the web browser and type the URL http://domain name/wsgi or ip address/wsgi. You will be redirected to the following page

With this, the method to run Python Scripts with Apache and mod_wsgi on Linuxmint 18.03 comes to an end

FAQ
Q
What is mod_wsgi?
A
mod_wsgi is an Apache HTTP Server module by Graham Dumpleton that provides a WSGI compliant interface for hosting Python based web applications under Apache
Q
What is “connection reset by peer” in error logs?
A
When the server is serving any ongoing Apache request and end user terminates the connection in between, we see “connection reset by peer” in the Apache error logs.
Q
What is mod_perl and mod _php?
A
mod_perl is an Apache module which is compiled with Apache for easy integration and to increase the performance of Perl scripts.
mod_php is used for easy integration of PHP scripts by the web server, it embeds the PHP interpreter inside the Apache process. Its forces Apache child process to use more memory and works with Apache only but still very popular.
Q
What is Loglevel debug in httpd.conf file?
A
With the help of Loglevel Debug option, we can get/log more information in the error logs which helps us to debug a problem.
Q
What’s the use of mod_ssl and how SSL works with Apache?
A
Mod_ssl package is an Apache module, which allows Apache to establish its connection and transfer all the data in a secure encrypted environment. With the help of SSL certificates, all the Login details and other important secret details get transferred in an encrypted manner over the Internet, which prevents our data from Eavesdropping and IP spoofing.