WSGI

How To Setup uWSGI on Ubuntu: A Step-By-Step Guide

uWSGI is a versatile application server that can serve web applications written in Python, Ruby, and other programming languages. It’s highly regarded for its performance and flexibility, particularly with Python web frameworks like Flask and Django. If you’re looking to deploy your web application on an Ubuntu server, this guide will walk you through the process of setting up uWSGI on Ubuntu.

Prerequisites

Before we begin, make sure you have the following:

  1. An Ubuntu server (version 18.04 or later).
  2. A domain name (optional, but recommended).
  3. Root or sudo access to your server.
  4. The web application you want to deploy (Django or Flask for this tutorial).

Step 1: Update Your System

Start by updating your package list and upgrading your installed packages to their latest versions. Use the following commands:

sudo apt update
sudo apt upgrade

Step 2: Install Python and pip

If you haven’t already installed Python, do so by running:

sudo apt install python3 python3-pip python3-venv

This command installs Python 3, pip (Python package installer), and venv (to create virtual environments).

Step 3: Set Up Your Python Environment

Next, create a virtual environment for your web application:

mkdir ~/myapp
cd ~/myapp
python3 -m venv venv
source venv/bin/activate

You’ll see the terminal prompt change, indicating you are now working inside the virtual environment.

Step 4: Install Your Application and uWSGI

With the virtual environment activated, install your application along with uWSGI. If you’re using a specific web framework, install it accordingly. For instance, for a Django application, run:

pip install django
pip install uwsgi

If you’re using Flask:

pip install flask
pip install uwsgi

Step 5: Create uWSGI Configuration File

In your application directory, create a uWSGI configuration file. Here’s an example of what that file might look like for a Django application. Create a file named myapp.ini:

[uwsgi]
module = myapp.wsgi:application
home = /home/yourusername/myapp/venv
chdir = /home/yourusername/myapp
socket = myapp.sock
chmod-socket = 660
vacuum = true
die-on-term = true

For a Flask application, the config might look like this:

[uwsgi]
module = wsgi:app
master = true
processes = 5
socket = myapp.sock
chmod-socket = 660
vacuum = true
die-on-term = true

Note: Replace yourusername and myapp with your actual username and app name.

Step 6: Install and Configure Nginx

Nginx will serve as a reverse proxy to pass web traffic to uWSGI. To install it, run:

sudo apt install nginx

Next, create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/myapp

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com;  # Replace with your domain name

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/yourusername/myapp;  # Adjust accordingly
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/yourusername/myapp/myapp.sock;
    }
}

Enable your site configuration and test for syntax errors:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled
sudo nginx -t

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 7: Start uWSGI

You can start uWSGI with the following command:

uwsgi --ini myapp.ini

For production, it is recommended to create a systemd service to manage uWSGI. Create a new service file:

sudo nano /etc/systemd/system/uwsgi.service

Add the following content:

[Unit]
Description=uWSGI instance to serve myapp
After=network.target

[Service]
User=yourusername
Group=www-data
WorkingDirectory=/home/yourusername/myapp
Environment="PATH=/home/yourusername/myapp/venv/bin"
ExecStart=/home/yourusername/myapp/venv/bin/uwsgi --ini myapp.ini

[Install]
WantedBy=multi-user.target

Enable and start the uWSGI service:

sudo systemctl start uwsgi
sudo systemctl enable uwsgi

Step 8: Test Your Application

At this point, your application should be up and running. Open your web browser, and navigate to your domain (or http://your_server_ip). You should see your web application live!

Conclusion

Setting up uWSGI on Ubuntu is straightforward. By following the above steps, you can deploy your Python web applications with ease. uWSGI is a powerful tool that, when combined with Nginx, offers an efficient setup for serving web applications. Don’t forget to secure your application further with SSL, which you can easily set up using Let’s Encrypt.