Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Python»How to Install Apache with Python mod_wsgi on Debian 10

    How to Install Apache with Python mod_wsgi on Debian 10

    By RahulJune 2, 20202 Mins Read

    The WSGI (Web Server Gateway Interface) is a simple method for the web servers to forward requests to the web applications or frameworks written in the Python. WSGI is the specification wich describes, how a web server communicates with the web applications.

    Advertisement

    The mod_wsgi is an Apache module used to serve Python scripts over HTTP. This tutorial helps you to how to install the Apache mod_wsgi module on Debian 10 Linux system.

    Prerequisites

    Login to your Debian 10 system terminal with the root or sudo privileged account.

    Step 1 – Install Python

    You must have installed required Python version on your system. For the Ubuntu systems, you can use apt package manager to install Python on your system.

    sudo apt update
    sudo apt install python3 libexpat1 
    

    Step 2 – Install Apache Mod_wsgi Module

    Next, you need to install Apache web server including Apache utility package and mod_wsgi Python module on your system. To install these packages run the following commands.

    sudo apt install apache2 apache2-utils ssl-cert libapache2-mod-wsgi
    

    After installation, restart Apache service to reload all modules.

    sudo systemctl restart apache2
    

    Step 3 – Configure Apache with WSGI Module

    Now, create a sample python script to serve with mod_wsgi Apache module. Run the following commands to create a Python script and edit in your favorite text editor:

    mkdir -p /var/www/scripts
    sudo nano /var/www/scripts/test_wsgi_script.py
    

    Add the below python script to file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    def application(environ,start_response):
        status = '200 OK'
        html = '<html>\n' \
               '<body>\n' \
               ' Hooray, mod_wsgi is working\n' \
               '</body>\n' \
               '</html>\n'
        response_header = [('Content-type','text/html')]
        start_response(status,response_header)
        return [html]

    Save your Python script and close file.

    Next, configure Apache server to serve this file over the HTTP protocol. Let’s create a configuration file to serve the wsgi_test_script.py script with the directory URL.

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

    Add the following content:

    WSGIScriptAlias /test_wsgi /var/www/scripts/test_wsgi_script.py
    

    After completing the above steps enable mod-wsgi configuration and restart Apache service.

    sudo a2enconf mod-wsgi
    sudo systemctl restart apache2
    

    Step 4 – Testing Setup

    The Python script is ready to serve over Apache. You can test the script by accessing the following URL in a web browser. Make sure to change SERVER_IP_OR_DOMAIN with actual server IP or domain name pointed to the IP.

     http://SERVER_IP_OR_DOMAIN/test_wsgi
    

    See below screenshot:

    Apache mod_wsgi on Debian 10

    Conclusion

    In this tutorial, you have learned to deploy Python script over Apache web server using mod_wsgi module on Ubuntu system.

    Apache Debian 10 mod_wsgi Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Python Function with Parameters, Return and Data Types

    Understanding Python’s Underscore ( _ ): A Comprehensive Guide

    How to Install Apache CouchDB on Ubuntu & Debian

    View 2 Comments

    2 Comments

    1. feralfer on June 30, 2021 8:06 pm

      https://stackoverflow.com/questions/34838443/typeerror-sequence-of-byte-string-values-expected-value-of-type-str-found

      i get: mod_wsgi (pid=2304): Exception occurred processing WSGI script ‘C:/wsgi_app/wsgi_app.py’. [Mon Aug 10 14:37:25.393918 2015] [wsgi:error] [pid 2304:tid 940] [client 127.0.0.1:49548] TypeError: sequence of byte string values expected, value of type list found\r – Ortal Blumenfeld Lagziel Aug 10 ’15 at 11:37

      Under Python 3, a WSGI application must return a byte string as the variable ‘output’. It cannot be a Unicode object or some other type. – Graham Dumpleton Aug 10 ’15 at 12:09

      Hope this helps

      Reply
    2. Jamie on September 19, 2020 8:40 pm

      THANKYOU!!!! I wasted a whole day trying to do this using 15 other sets of instructions. Yours worked perfectly!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • A Comprehensive Look at the Simple Mail Transfer Protocol (SMTP)
    • Understanding Basic Git Workflow: Add, Commit, Push
    • The Difference Between Git Reset –soft, –mixed, and –hard
    • Understanding the Staging Area in Git’s Workflow
    • Python Function with Parameters, Return and Data Types
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.