How to Setup Apache With Python WSGI on Ubuntu

Setting up Apache with Python WSGI on Ubuntu is a great way to deploy and serve web applications. WSGI, which stands for Web Server Gateway Interface, is a standard for Python web applications to communicate with web servers. Apache is a popular web server that supports WSGI, making it a great choice for hosting Python applications. In this article, we will walk you through the steps to set up Apache with Python WSGI on an Ubuntu server.

Step 1: Install Apache
The first step is to install Apache on your Ubuntu server. You can do this by running the following command in your terminal:

sudo apt update
sudo apt install apache2

After the installation is complete, start the Apache service by running:

sudo systemctl start apache2

Step 2: Install mod_wsgi
Next, you will need to install the mod_wsgi module for Apache, which will allow Apache to serve Python web applications. You can install mod_wsgi by running:

sudo apt install libapache2-mod-wsgi-py3

Step 3: Configure Apache to use mod_wsgi
Once mod_wsgi is installed, you will need to configure Apache to use it. Open the Apache configuration file in a text editor:

sudo nano /etc/apache2/apache2.conf

Add the following lines at the end of the file to enable mod_wsgi:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIPythonHome /usr

Save and close the file. Next, enable the WSGI module by running:

sudo a2enmod wsgi

Step 4: Create a WSGI script file
Now, you will need to create a WSGI script file for your Python web application. Create a new directory for your application and navigate to it:

sudo mkdir /var/www/myapp
cd /var/www/myapp

Create a new Python script file in the directory:

sudo nano myapp.wsgi

Add the following code to the file. Replace myapp with the name of your Python script:

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/myapp/")

from myapp import app as application

Save and close the file.

Step 5: Set up a virtual host
Finally, you will need to set up a virtual host in Apache to serve your Python web application. Create a new virtual host configuration file:

sudo nano /etc/apache2/sites-available/myapp.conf

Add the following configuration to the file. Replace myapp.com with your domain name and /var/www/myapp with the path to your application:

<VirtualHost *:80>
    ServerName myapp.com
    ServerAdmin admin@myapp.com
    WSGIScriptAlias / /var/www/myapp/myapp.wsgi
    <Directory /var/www/myapp>
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file. Enable the virtual host by running:

sudo a2ensite myapp

Restart Apache to apply the changes:

sudo systemctl restart apache2

That’s it! You have successfully set up Apache with Python WSGI on your Ubuntu server. You can now visit your domain name in a web browser to access your Python web application. Happy coding!

Tags: 548548548548