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

 

 

Introduction

Apache is a widely used web server that can host Python web applications using the mod_wsgi module. In this guide, you will learn how to run Python scripts with Apache and mod_wsgi on Ubuntu 22.04, which can be effectively hosted on a Windows VPS UK.

Prerequisites

  • An Ubuntu 22.04 server with root access
  • Basic knowledge of Linux commands
  • An active internet connection
  • Python installed on your server

Step 1: Update Your System

Start by updating your package index:

sudo apt update -y

Step 2: Install Apache

Install the Apache web server:

sudo apt install apache2 -y

Start and enable Apache to run at boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Step 3: Install mod_wsgi

Install the mod_wsgi package:

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

Step 4: Create a Python Script

Create a simple Python script to test the setup:

echo "def application(environ, start_response):
    status = '200 OK'
    headers = [('Content-Type', 'text/plain')]
    start_response(status, headers)
    return [b'Hello, World!']" > ~/myapp.wsgi

Step 5: Configure Apache to Serve the Python Script

Create a new Apache configuration file:

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

Add the following configuration to the file:

<VirtualHost *:80>
    ServerName your_domain_or_IP
    WSGIScriptAlias / /home/your_username/myapp.wsgi
    </VirtualHost>

Replace your_domain_or_IP and your_username with your actual domain name or IP address and your username.

Step 6: Enable the New Site Configuration

Enable the new site configuration and reload Apache:

sudo a2ensite myapp
sudo systemctl reload apache2

Step 7: Adjust Firewall Settings

If you have a firewall running, allow HTTP traffic:

sudo ufw allow 'Apache'

Step 8: Test the Configuration

Open your web browser and navigate to your server's IP address or domain:

http://your_domain_or_IP

You should see the message Hello, World!.

Step 9: Conclusion

You have successfully set up Apache with mod_wsgi to run Python scripts on Ubuntu 22.04. This configuration can significantly benefit from being hosted on a Windows VPS. For additional options, explore various VPS UK Windows solutions, including Windows Virtual Private Server Hosting and Windows VPS Hosting UK for optimal performance and security.

© 2024 Apache and mod_wsgi Installation Tutorial. All rights reserved.

  • 0 Uživatelům pomohlo
Byla tato odpověď nápomocná?

Související články

Boost Your Ubuntu System's Performance with a Swap File: A Step-by-Step Guide

What is a Swap File? A swap file in Ubuntu serves as dedicated virtual memory on your hard...

How to Migrate ISPConfig 2, ISPConfig 3.x, Confixx, CPanel or Plesk to ISPConfig 3.2 (single server)

Introduction Migration from other control panels like ISPConfig 2, ISPConfig 3.x, Confixx,...

How to Install and Configure Zabbix Server and Client on Rocky Linux 9

Introduction Zabbix is an open-source monitoring solution that provides real-time...

How to Install CockroachDB Cluster on Debian 12

Introduction CockroachDB is a distributed SQL database built to handle large-scale,...

How to Install Joomla with Apache and Let's Encrypt SSL on AlmaLinux 9

Introduction Joomla is a popular open-source content management system (CMS) used to build...