Setting up a web server to run Python applications can seem intimidating, especially for beginners. However, with the right steps, it can be a straightforward process. This guide will walk you through setting up Apache, a popular web server, with Python’s Mod_WSGI on Ubuntu 24.04 and 22.04. Mod_WSGI is an Apache module that allows you to run Python web applications.
By following this guide, you’ll have your server up and running in no time. Whether you’re new to server management or just need a refresher, this guide is for you.
Prerequisites
Before we start, ensure you have the following:
A server running Ubuntu 24.04 or 22.04
A non-root user with sudo privileges
Basic knowledge of the command line
Step 1: Update Your Server
First, update your server’s package index. Open a terminal and run:
sudo apt update
sudo apt upgrade -y
This ensures that you have the latest software versions.
Step 2: Install Apache
Next, install Apache. Apache is a reliable web server that’s easy to use.
sudo apt install apache2 -y
After installation, start Apache and enable it to run at boot:
sudo systemctl start apache2
sudo systemctl enable apache2
Check if Apache is running by visiting your server’s IP address in a web browser. You should see the Apache2 Ubuntu Default Page.
Step 3: Install Mod_WSGI and Python
Install Mod_WSGI and Python packages:
sudo apt install libapache2-mod-wsgi-py3 python3 -y
These packages allow Apache to serve Python applications.
Step 4: Create a Python Application
Create a simple Python web application to test the setup. First, create a directory for your app:
mkdir ~/myapp && cd ~/myapp
Inside this directory, create a file named myapp.py with the following content:
def application(environ, start_response):
status = ‘200 OK’
output = b’Hello From TecAdmin!’
response_headers = [(‘Content-type’, ‘text/plain’), (‘Content-Length’, str(len(output)))]
start_response(status, response_headers)
return [output]
Ensure that the files and directories in your application are owned by the correct user and group. For example, if the Apache user is www-data, you can set the ownership as follows:
sudo chown -R www-data:www-data ~/myapp
sudo chmod o+x ~/myapp/myapp.py
Step 5: Configure Apache to Serve Your Python App
Create an Apache configuration file for your Python WSGI application:
sudo nano /etc/apache2/sites-available/myapp.conf
Add the following content:
<VirtualHost *:80>
ServerName myapp.local
WSGIDaemonProcess myapp threads=5
WSGIScriptAlias / /home/yourusername/myapp/myapp.py
<Directory /home/yourusername/myapp>
Require all granted
</Directory>
Alias /static /home/yourusername/myapp/static
<Directory /home/yourusername/myapp/static/>
Require all granted
</Directory>
</VirtualHost>
Replace yourusername with your actual username.
Enable the new site and disable the default site:
sudo a2ensite myapp
sudo a2dissite 000-default
Enable the WSGI module and restart Apache:
sudo a2enmod wsgi
sudo systemctl restart apache2
Step 6: Test Your Application
To test your application, you need to edit your hosts file to point myapp.local to your server’s IP address. On your local machine, edit the /etc/hosts file and add:
your_server_ip myapp.local
Replace your_server_ip with your server’s IP address. Now, open a web browser and navigate to http://myapp.local. You should see “Hello World!†on the page.
Displaying Page Content
Conclusion
Congratulations! You have successfully set up Apache with Python Mod_WSGI on Ubuntu 24.04 or 22.04. This setup allows you to run Python web applications on your server efficiently. Remember, this is a basic setup. As you get more comfortable, you can explore more advanced configurations and optimizations. Happy coding!
The post How to Setup Apache with Python WSGI on Ubuntu 24.04 & 22.04 appeared first on TecAdmin.
Source: Read More