How to run Python Scripts with Apache and mod_wsgi on Linuxmint 18.03
- 00:44 apt-get install python libexpat1 apache2 apache2-utils ssl-cert
- 00:55 apt-get install libapache2-mod-wsgi
- 01:08 vim /var/www/html/wsgy.py
- 01:25 chown -R www-data:www-data /var/www/html/wsgy.py
- 01:48 vim /etc/apache2/conf-available/wsgi.conf
- 02:01 a2enconf wsgi
- 02:11 systemctl restart apache2.service
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 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
Comments ( 0 )
No comments available