Home / How To / How to run Python scripts with Apache and mod_wsgi on Ubuntu 20.04

How to run Python scripts with Apache and mod_wsgi on Ubuntu 20.04



mod_wsgi is an Apache module that provides an interface for hosting Python-based web applications under Apache. It is suitable for use in host production with high-performance production websites, as well as your average self-managed personal websites run on web hosting services. You can easily distribute applications written with frames and tools such as Django, Web.py, Werkzug, Chery.py, TurboGears and Flask with mod_wsgi.

In this tutorial we will teach you how to install and configure mod_wsgi with the Apache server on the Ubuntu 20.04 server.

conditions

  • A server running Ubuntu 20.04.
  • A root password is configured on the server.

Getting Started

Before you begin, it is a good idea to update your system with the latest version. You can update your system with the following command:

apt-get update -y
apt-get upgrade -y

After updating the system, restart it to make the changes.

Install Python

By default, Python comes pre-installed on an Ubuntu 20.04 server. If it is not installed, you can install it with the following command:

apt-get install python3 libexpat1 -y

When the installation is complete, you can proceed to the next step.

Install Apache and mod_wsgi

Next, you need to install the Apache web server and mod_wsgi Python module on your system. You can install them by running the following command:

apt-get install apache2 apache2-utils ssl-cert libapache2-mod-wsgi -y

Once all the packages have been installed, you can proceed to the next step.

Configure Apache for mod_wsgi

Next, you need to create a new Python script in the default Apache root directory and serve it via mod_wsgi Apache module.

You can create it with the following command:

nano /var/www/html/wsgy.py

Add the following lines:

def application(environ,start_response):
    status="200 OK"
    html="html>n" 
           'n' 
           '
n' 'Welcome to mod_wsgi Test Pagen' '
n' 'n' 'n' response_header = [('Content-type','text/html')] start_response(status,response_header) return [html]

Save and close the file. Then change the ownership of the file to www-data with the following command:

chown www-data:www-data /var/www/html/wsgy.py

Next, create an Apache virtual host configuration file to view this file via HTTP protocol.

nano /etc/apache2/conf-available/wsgi.conf

Add the following line:

WSGIScriptAlias /wsgi /var/www/html/wsgy.py

Save and close the file. Then enable mod-wsgi configuration and restart the Apache service with the following command:

a2enconf wsgi
systemctl restart apache2

Test mod-wsgi

At this time, the Apache web server is configured to serve your Python file via HTTP protocol.

To test it, open your browser and type in the URL http: // your-server-ip / WSGI. You should see the mod-wsgi test page on the following screen:

Apache mod_wsgi

Conclusion

Congratulations! you have successfully distributed Python scripts over the Apache web server using the mod_wsgi module on Ubuntu 20.04 server. I hope you can now easily deploy all Python applications with Apache and mod_wsgi in the production environment.

Hitesh Jethva

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My knowledge includes in-depth knowledge of Redhat / Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, octopus proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud , Postfix Mail Server, Security etc.


Source link