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.