In this comprehensive guide, we’ll walk you through the process of installing and configuring mod_wsgi, a popular Apache module used to host Python applications on a web server. Whether you’re deploying a Django, Flask, or Pyramid application, understanding how to set up mod_wsgi is crucial for a successful deployment. Follow these steps to ensure a smooth installation and configuration process.

Advertisement

Prerequisites

Before starting, ensure you have:

  • A server with Apache installed.
  • Access to the terminal or command line.
  • Python installed on your server.
  • Sudo or root privileges.

Step 1: Install mod_wsgi

The first step is to install mod_wsgi. The installation method may vary depending on your operating system.

  • For Debian/Ubuntu:
    sudo apt-get update
    sudo apt-get install libapache2-mod-wsgi-py3
    
  • For CentOS/RHEL 7:
    sudo yum install mod_wsgi
    
  • For CentOS/RHEL 9/8 & Fedora:
    sudo dnf install mod_wsgi
    

If you’re using Python 3, make sure to install the Python 3 version of mod_wsgi (e.g., libapache2-mod-wsgi-py3 on Debian/Ubuntu).

Step 2: Enable mod_wsgi

Once installed, you need to enable mod_wsgi in Apache.

  • For Debian/Ubuntu:
    sudo a2enmod wsgi
    
  • For CentOS/RHEL/Fedora:
    The module should be enabled by default after installation. If not, refer to your system’s documentation on enabling Apache modules.

Step 3: Configure Your Python Application

Before proceeding, make sure your Python application is ready and accessible on your server. For this guide, we’ll assume your application is located in /var/www/myapp.

Your application should include a .wsgi file, which serves as the entry point for the application. Here’s a simple example of what this file might look like:


# myapp.wsgi
import sys
sys.path.insert(0, '/var/www/myapp')

from myapplication import app as application

Replace myapplication with the name of your Python file that creates your application object, and app with the name of the application object itself.

Step 4: Configure Apache to Serve Your Application

Next, you need to configure Apache to serve your Python application using mod_wsgi. This involves editing your Apache configuration file or creating a new one specifically for your application.

Create a new configuration file in /etc/apache2/sites-available/ (Debian/Ubuntu) or /etc/httpd/conf.d/ (CentOS/RHEL/Fedora) named myapp.conf:


<VirtualHost *:80>
    ServerName myapp.example.com

    WSGIDaemonProcess myapp python-path=/var/www/myapp
    WSGIScriptAlias / /var/www/myapp/myapp.wsgi

    <Directory /var/www/myapp>
        Require all granted
    </Directory>
</VirtualHost>

Make sure to replace /var/www/myapp with the actual path to your application and myapp.example.com with your domain name.

Step 5: Enable the Site and Restart Apache

  • For Debian/Ubuntu:
    sudo a2ensite myapp
    sudo systemctl restart apache2
    
  • For CentOS/RHEL/Fedora:
    sudo systemctl restart httpd
    

Step 6: Verify the Installation

After restarting Apache, navigate to your application’s URL to verify that everything is working correctly. If you encounter any issues, check Apache’s error logs for more information.

Conclusion

Congratulations! You’ve successfully installed and configured mod_wsgi with Apache to serve your Python application. This setup allows you to deploy robust Python web applications efficiently and securely. Remember, each application might require additional configuration tweaks based on its specific needs, so always refer to your application’s documentation for more detailed setup instructions.

Share.
Leave A Reply


Exit mobile version