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 Windows VPS UK 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:
- An Ubuntu 18.04 LTS server, which can be hosted on a UK Windows VPS, Windows Virtual Private Servers, or another Windows VPS Hosting UK solution.
- Root or sudo privileges on your server.
- Python 3 installed on your server.
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:
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.
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 Windows VPS UK, Windows VPS Italy, or another Windows Virtual Private Server Hosting solution. With mod_wsgi and Apache, you can ensure high performance and scalability for your Python applications.