Apache and mod
wsgi provide an excellent solution for hosting Python-based web applications. Whether you’re running a small site or a professional hosting system, mod wsgi offers high-performance capabilities. In this tutorial, we’ll walk you through the process of installing and configuring mod_wsgi with the Apache web server on Ubuntu 22.04 LTS.
Prerequisites
Before we dive into the installation and configuration steps, make sure you have the following:
- Ubuntu 22.04 Server or Desktop
- Administrator access with root or sudo permissions
Let’s get started!
Step 1: Update Your System
First, let’s ensure that your system is up to date. Open a terminal and run the following commands:
sudo apt update -y sudo apt upgrade -y
These commands will update your system with the latest software versions. Once the update is complete, restart your system to apply the changes.
Step 2: Install Python
Python usually comes pre-installed on Ubuntu 22.04. However, if you don’t have it installed, you can do so by running the following command:
sudo apt install python3 libexpat1 -y
This command will install Python 3 and the necessary dependencies. Once the installation is complete, you can move on to the next step.
Step 3: Install Apache and mod_wsgi
Now, let’s install the Apache web server and the mod_wsgi Python module. Run the following command:
sudo apt install apache2 apache2-utils ssl-cert libapache2-mod-wsgi-py3 -y
This command will install both Apache and mod wsgi, along with any required dependencies. Once the installation is complete, you’re ready to configure mod wsgi.
Step 4: Configure mod_wsgi
To configure mod_wsgi, you’ll need to create a Python script that will be served by the Apache web server. For demonstration purposes, we’ll create a simple “Hello World” script.
Open a terminal and run the following command to create the script:
sudo nano /var/www/html/wsgitest.py
This command will open a text editor. Paste the following lines into the editor:
def application(environ, start_response): status = '200 OK' output = b'Hello !\n' response_headers = [ ('Content-type', 'text/plain'), ('Content-Length', str(len(output))) ] start_response(status, response_headers) return [output]
Save the file and exit the text editor. Next, change the ownership of the file to www-data
using the following command:
sudo chown www-data:www-data /var/www/html/wsgitest.py sudo chmod 775 /var/www/html/wsgitest.py
Now, let’s edit the Apache virtual host configuration file to serve the Python script. Run the following command:
sudo nano /etc/apache2/sites-enabled/000-default.conf
This command will open the configuration file in a text editor. Add the following line before the </VirtualHost>
line:
WSGIScriptAlias /wsgi /var/www/html/wsgitest.py
The complete virtual host file should look like this:
# The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf WSGIScriptAlias /wsgi /var/www/html/wsgitest.py # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Save the changes and exit the text editor. Finally, restart the Apache web server to apply the configuration changes:
sudo systemctl restart apache2
Step 5: Test the Configuration
With the Apache web server configured, let’s test our Python script. Open a web browser and enter the following URL, replacing your-server-ip
with the IP address of your server:
http://your-server-ip/wsgi
You should see a white page with the words “Hello !” displayed. Congratulations! You now have a properly configured Apache web server to run Python applications using mod_wsgi.
In this tutorial, we covered the installation and configuration of modwsgi with the Apache web server on Ubuntu 22.04. With modwsgi, you can easily deploy and run Python web applications. Remember to deploy your own Python web application in the /var/www/html/
directory and update the WSGIScriptAlias
line in the /etc/apache2/sites-enabled/000-default.conf
file accordingly.
If you’re looking for reliable and scalable cloud hosting solutions, consider Shape.host. With their Linux SSD VPS, you can ensure high-performance hosting for your Python applications.