Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Distributions»Ubuntu»How to Install Apache mod_wsgi Module on Ubuntu 18.04 (Bionic)

    How to Install Apache mod_wsgi Module on Ubuntu 18.04 (Bionic)

    RahulBy RahulSeptember 1, 20182 Mins ReadUpdated:February 14, 2020

    The mod_wsgi Apache module is used for serving Python scripts over HTTP via the Apache web server. This tutorial helps you to how to install the Apache mod_wsgi module on Ubuntu 18.04 (Bionic Beaver).

    Step 1 – Prerequisites

    Login to Ubuntu 18.04 server console via SSH and install some prerequisites packages on the system.

    sudo apt-get update
    sudo apt-get install python libexpat1 
    

    Step 2 – Install mod_wsgi Module

    Before starting, you will need to install some prerequisite Apache components in order to work with mod_wsgi. You can install all the required components by simply running the following command:

    sudo apt-get update
    sudo apt-get install apache2 apache2-utils ssl-cert
    

    Now, install mod_wsgi Apache module by running the following command:

    sudo apt-get install libapache2-mod-wsgi
    

    Restart Apache service to get mod_wsgi to work.

    sudo systemctl restart apache2
    

    Step 3 – Configure Apache for WSGI

    Next, create a python script to serve via mod_wsgi Apache module.

    sudo vi /var/www/html/wsgi_test_script.py
    

    Add the following content:

    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]

    After that, you need to configure the Apache server to serve this file over the HTTP protocol. Let’s create a configuration file to serve the wsgi_test_script.py script over a sub URL.

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

    Add the following content:

    WSGIScriptAlias /test_wsgi /var/www/html/wsgi_test_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

    The setup is ready now. You can test the script by accessing the following URL in a web browser.

     http://SERVER_IP/test_wsgi
    

    See below screenshot:

    Apache Apache modules mod_wsgi Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install Jenkins on Debian 10/9/8
    Next Article How To Install PHP 7.3, 7.2, 7.1 on CentOS/RHEL 6.10

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    Setup Selenium with Python and Chrome on Ubuntu & Debian

    Updated:June 20, 20224 Mins Read

    Setup Selenium with Python and Chrome on Fedora

    Updated:June 18, 20223 Mins Read

    How to Install Apache Kafka on Ubuntu 22.04

    Updated:June 5, 20225 Mins Read

    Creating Python Virtual Environment on Windows

    Updated:June 3, 20222 Mins Read

    Setting Up Environment Variables on Ubuntu

    Updated:May 28, 20222 Mins Read

    11 Comments

    1. kOoLiNuS on November 11, 2021 3:38 pm

      I do have a 404 error, file not found … it seems that WSGIScriptAlias isn’t correctly read by the system (the .py file is readable and if called the browser asks me to dowload it

      Reply
    2. Alex T on January 3, 2021 3:45 am

      Fails, error in wsgi_test_script.py – must be html = b’

      Reply
    3. Sean on December 28, 2020 9:34 pm

      Very concise and helpful tutorial, thanks!

      Reply
    4. Hugh Parker on May 29, 2020 6:07 pm

      With Python 3 the example wsgi script gives an error. WSGI expects byte sequences, not strings, e.g.
      b’\n’ \
      b’\n’ \

      Reply
    5. Bob on April 25, 2020 10:41 pm

      This is an excellent tutorial Rahul. I wonder whether you can also write a tutorial or explain how to do this for Webpy + Apache with mod_wsgi on Ubuntu since it is  poorly done here: https://webpy.org/cookbook/mod_wsgi-apache-ubuntu
      Many thanks  

      Reply
    6. Bob on April 25, 2020 10:38 pm

      This is an excellent tutorial Rahul. I wonder whether you can also write a tutorial or explain how to this for Webpy + Apache with mod_wsgi on Ubuntu as the poorly done here: https://webpy.org/cookbook/mod_wsgi-apache-ubuntu
      Many thanks

      Reply
    7. Andrew on December 31, 2019 1:27 am

      This isn’t working for me. I’m using bitnami; so, instead of /var/www/… I have a different directory structure. The mod-wsgi.conf file isn’t at the specified location, and a2enconf isn’t recognized. Any suggestions? Bitnami doesn’t seem to have any instructions for Python or mod-wsgi.

      Reply
    8. suman on May 8, 2019 8:14 am

      thanks it open now from browser,
      but sir others file cant open from /var/www/htnl/ how to open another file from browser
      like, http://192.168.1.23/test1_wsgi

      Reply
    9. Gary on April 15, 2019 12:00 pm

      Thanks, very concise useful instructions. Now to get django working.

      Reply
    10. MarkS on April 8, 2019 7:01 pm

      Thanks. After a great deal of searching and faffing with various Stack Overflow type suggestions, your tutorial was ultimately the one that got it working for me.

      Reply
    11. Tanaka on January 14, 2019 11:54 am

      Your tutorial was very helpful, and concise, mod_wsgi is working now. I have been trying to get mod_wsgi to serve flask apps on my mac for a while now and decided to move to bionic beaver instead. The next step serving flask apps.

      Thanks again!

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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