24 C
Jaipur
Saturday, October 24, 2020

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

Must read

US sanctions Russian institute linked to dangerous malware – Latest News

Washington imposed sanctions on a Russian research institute tied to the development of a dangerous computer program capable of causing catastrophic industrial damage, a...

Dell: Data is fuel, 5G fabric for digital transformation: Michael Dell – Latest News

Reiterating that technology has never been more central than it is in the pandemic times, Dell Technologies chairman and CEO Michael Dell has said...

8K TV: Samsung launches The 8K Festival with QLED 8K TVs

Samsung has launched 'The 8K Festival' under which its super premium QLED 8K TVs will be available at special prices with discounts. The...

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

In this tutorial, we will learn how to install and set up of mod_wsgi with the Apache server on Ubuntu 20.04 server.

Prerequisites

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

Getting Started

Before starting, 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 implement the changes.

Install Python

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

apt-get install python3 libexpat1 -y

Once the installation has been completed, you can proceed to the next step.

Install Apache and mod_wsgi

Next, you will 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 are installed, you can proceed to the next step.

Configure Apache for mod_wsgi

Next, you need to create a new Python script inside Apache default web 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" 
           '<body>n' 
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">n' 
           'Welcome to mod_wsgi Test Pagen' 
           '</div>n' 
           '</body>n' 
           '</html>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 serve this file over 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 Apache service with the following command:

a2enconf wsgi
systemctl restart apache2

Test mod-wsgi

At this point, Apache web server is configured to serve your Python file over HTTP protocol.

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

Conclusion

Congratulations! you have successfully deployed Python script over Apache web server using mod_wsgi module on Ubuntu 20.04 server. I hope you can now easily deploy any Python application 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 skills include a depth knowledge of Redhat/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

US sanctions Russian institute linked to dangerous malware – Latest News

Washington imposed sanctions on a Russian research institute tied to the development of a dangerous computer program capable of causing catastrophic industrial damage, a...

Dell: Data is fuel, 5G fabric for digital transformation: Michael Dell – Latest News

Reiterating that technology has never been more central than it is in the pandemic times, Dell Technologies chairman and CEO Michael Dell has said...

8K TV: Samsung launches The 8K Festival with QLED 8K TVs

Samsung has launched 'The 8K Festival' under which its super premium QLED 8K TVs will be available at special prices with discounts. The...

iPhone 12 Users Can Now Download OS Updates Over 5G Data

Apple finally introduced 5G support with the launch of its latest iPhone 12 series. Now, as iPhones support high-speed 5G networks, the Cupertino tech...