Quickly deploying a Django app with uWSGI and NGINX
If you want to learn how to deploy a django app on a Linux server on Debian10 you are at the right place. We proceed the deployement on a ovh VPS but if you have an another provider it should be the same.
Prerequisites
We will assume that the initial server setup is already done and you have Python 3.x installed.
Setting up the environment
Clone your project
let’s install git and clone the project at ~/pathto/your_project/
:
$ sudo apt install git
$ git clone https://your_project.git ~/pathto/your_project/
Create a Python virtual environment
We will work within a Python virtual environment. Let’s install venv and create a new virtual environment:
$ sudo apt install python3-venv
$ python3 -m venv ~/pathto/project_env
$ source ~/pathto/project_env/bin/activate
Your prompt should now look like this:
(project_env) user@host:
Install and setup Django
Now that the virtual environment is activated, let’s install Django and other dependencies using pip (you can run the commands below, assuming that you have written a requirements.txt):
(project_env) $ cd ~/pathto/your_project/
(project_env) $ pip install -r requirements.txt
You can try to run the app to check if everything is OK (you may need to download other prerequisites such as the database back-end).
Install and setup uWSGI
Let’s install uWSGI inside the virtual environment:
(project_env) $ pip install uwsgi
and try it out by running the Django app:
(project_env) $ cd ~/pathto/your_project/
(project_env) $ uwsgi --http-socket :8000 --module your_project.wsgi
Your app should now be running on the port 8000 of your server’s IP address.
We will create a your_project.ini
configuration file for easier management (with the minimal configuration above):
[uwsgi]
http-socket = :8000
chdir = /fullpathto/your_project/ # path to the Django project
module = your_project.wsgi
home = /fullpathto/project_env # path to the virtual environment
and test it by deactivating the virtual environment and run uWSGI by using the binary file of the virtual environment:
(project_env) $ deactivate
$ ~/pathto/project_env/bin/uwsgi --ini ~/pathto/your_project.ini
Your app should be running, and you will find further configuration options on the uWSGI documentation page.
Deploying the app
Configure Firewall Rules
We will set up some basic firewall rules to allow connections from SSH and NGINX (make sure you have SSH allowed otherwise you will lose your connection and not get it back).
$ sudo apt install ufw
$ sudo ufw allow ssh
$ sudo ufw allow 'Nginx Full'
$ sudo ufw enable
Install and configure NGINX
We will first install NGINX and remove the default site configuration:
$ sudo apt install nginx
$ rm /etc/nginx/sites-enabled/default
We will now create a file called your_project.conf
in the /etc/nginx/sites-available/
directory, and write the following lines in it (if you wish to use your domain name instead of the server’s IP address, you should update the file accordingly):
upstream django {
server 127.0.0.1:8000;
}server {
server_name default;location / {
proxy_pass http://localhost:8000;
}
}
and create a symbolic link to this file from /etc/nginx/sites-enabled/
:
$ sudo ln -s /etc/nginx/sites-available/your_project.conf /etc/nginx/sites-enabled/
and finally restart NGINX:
$ sudo systemctl restart nginx
Create a new service with systemd
Create a systemd unit called your_project
to allow your application to behave as a service. Create a new file at /lib/systemd/system/your_project.service
and add the following content:
[Unit]
Description=your_project
# After=postgresql.service # specify your database if needed[Service]
Type=simple
Restart=always
RestartSec=5s
ExecStart=/pathto/venv/bin/uwsgi --ini /pathto/your_project.ini[Install]
WantedBy=multi-user.target
Test the server
Let’s start your project server:
$ sudo systemctl start your_app.service
Your app should now be running, and available at the server’s IP address!
Thanks to Shoei for working with me on development and deployment of the project.