Skip to content

How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04

Netcloud24
Cloud Infrastructure Expert
NetCloud24 Expert Guides Open the dedicated video page

Video transcript: NetCloud24 Expert Guides. Practical knowledge for a reliable cloud. Cloud and VPS expertise, security, performance, and step-by-step tutorials. Learn, deploy, and keep building with NetCloud24.

 

 

mod_wsgi is an Apache HTTP Server module that provides a WSGI-compliant interface for hosting Python web applications. It is commonly used to deploy Python-based web applications with Apache. In this guide, we will walk you through how to run Python scripts using Apache and mod_wsgi on Ubuntu 18.04 LTS. Whether you’re using a or another hosting provider, this setup will allow you to efficiently deploy Python applications on your server.

Prerequisites

Before starting, ensure you have the following:

Step 1: Install Apache and mod_wsgi

First, update your system’s package index and install Apache with mod_wsgi:

sudo apt update
sudo apt install apache2 libapache2-mod-wsgi-py3 -y

After installation, start and enable Apache:

sudo systemctl start apache2
sudo systemctl enable apache2

Step 2: Install Python 3

Ubuntu 18.04 comes with Python 3 pre-installed. To ensure Python 3 is available, verify the installation with the following command:

Visual overview of How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04
Visual overview: How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04
python3 --version

If Python 3 is not installed, you can install it using:

sudo apt install python3 -y

Step 3: Create a Python Application

In this step, you will create a basic Python WSGI application to test mod_wsgi. First, create a directory to hold your Python app:

sudo mkdir /var/www/html/myapp

Navigate to the newly created directory and create a Python WSGI application file:

cd /var/www/html/myapp
sudo nano myapp.wsgi

Add the following content to the myapp.wsgi file:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello from WSGI!'

    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

This is a basic WSGI application that returns a “Hello from WSGI!” message when accessed via a web browser.

Step 4: Configure Apache to Serve Python Application

Now you need to configure Apache to serve your Python WSGI application. Create a new virtual host configuration file for your app:

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

Add the following configuration:

<VirtualHost *:80>
    ServerName your-domain.com
    DocumentRoot /var/www/html/myapp

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

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

Replace your-domain.com with your actual domain. Save the file and exit.

Key topics covered in How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04
Key topics covered in this NetCloud24 guide.

Enable the new virtual host and disable the default one:

sudo a2ensite myapp.conf
sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Step 5: Test the Python Application

To test if everything is working, open your web browser and navigate to http://your-domain.com. You should see the message “Hello from WSGI!” displayed in the browser.

Step 6: Secure Your Application with Let’s Encrypt SSL (Optional)

If you want to secure your Python web application with SSL, you can use Let’s Encrypt. Install Certbot by running:

sudo apt install certbot python3-certbot-apache -y

Obtain an SSL certificate for your domain:

sudo certbot --apache -d your-domain.com -d www.your-domain.com

Follow the prompts to install and configure SSL for your Apache server. Certbot will automatically configure Apache to redirect HTTP traffic to HTTPS.

Conclusion

By following these steps, you have successfully set up Apache with mod_wsgi to run Python scripts on Ubuntu 18.04 LTS. This allows you to deploy Python web applications efficiently, whether you are using a , Microsoft SQL VPS Windows, or another Windows VPSVirtual Private Server Hosting solution. With mod_wsgi and Apache, you can ensure high performance and scalability for your Python applications.

How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04
How to Run Python Scripts with Apache and mod_wsgi on Ubuntu 18.04
Explore more

More on this topic

Netcloud24
Netcloud24
Cloud Infrastructure Expert · NetCloud24

Comments are closed.