Hosting a Django Application on NGINX server on Digital ocean.-Apache Vs NGINX
I was always a fan of Apache server, my go to server was apache, because I was trained and learned only to host on apache until very recently. There was a lot of inertia on changing to nginx until a situvation arrives and now am compleatly loving the nginx server. Digital Ocean made it so easy to, maybe that is why I am loving this now.
How was Apache?
If you ask me it was quite tiresome eventhough I have hosted more than a 20 websites on it. Whenever I think about a day spent on the hosting part, because there will be editing the conf file, then there will be errors when we are entering the path, then that and this. A lot of stuff to be done, It is me doing all these, don’t know if there is an easy way. I was always manually configuring everything from Website source directory path to the SSL configuration(Which is again a lot of work). I have a blog on that too, do check that out. But today we will going to see how to host a django application on a nginx server-super fast!!
I hope you already have your django application ready to host, as am using digital ocean here, I have signed up for digital ocean and created droplet which is a term for a virtual cloud machine, I choose ubuntu as my os on the droplet.
I have already pushed my code to git lab so i can clone it where ever i want.
Steps:
Setting up the source code and virtualenv
- Digital ocean provide a browser based terminal or console, which we can access our droplet. Accessed through the browser.
- Created a folder ‘/srv/django/’ to organise my file structure, and cloned my django application inside the django folder.
- Created a virtual environment inside my cloned source code using the command ‘virtualenv venv -p python3’
- Activated my virtualenv and installed all my requirement into the venv folder
- After that created a database in postgres and granted all the privileges.
- So the initial part is done, if everything is done correctly, do .‘/manage.py runserver’ can see that the server is running on the droplet at a port. After this is the game begins.
Nginx Server configuration
7. Install gunicorn
pip install gunicorn
(The Gunicorn “Green Unicorn” is a Python Web Server Gateway Interface HTTP server. It is a pre-fork worker model, ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with a number of web frameworks, simply implemented, light on server resources and fairly fast)
8. Test Gunicorn was installed correctly
To test this, you need to allow gunicorn to run your application. Run the following command:
gunicorn --bind 0.0.0.0:8000 yourproject.wsgi
in youproject give the project folder name which contains your wsgi file.
9. Deactivate the virtualenv and Create Gunicorn systemd file
Basically a systemd file is a system file that will run your application automatically. So instead of of running the command above with gunicorn every-time and watching your app, you will let the systemd file manage that.
10. Start by creating a systemd socket file for gunicorn.
sudo nano /etc/systemd/system/gunicorn.socket
The contents of the file should look like this:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Next we create a systemd service file for gunicorn with the following command:
sudo nano /etc/systemd/system/gunicorn.service
Paste this inside the file, I have highlighted in bold the sections that you need to change. Replace yourusername — with you droplet username. And make sure the path-toprojectdir is correct for the root of the project (where the manage.py file is) and the projectenv folder. These are very important.
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target[Service]
User=yourusername
Group=www-data
WorkingDirectory=/home/yourusername/path-to-your-projectdir
ExecStart=/home/yourusername/path-to-your- projectdir/yourprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
yourproject.wsgi:application[Install]
WantedBy=multi-user.target
When you have completed and checked your file, you can run the following commands to start your systemd file.
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
You can check for the status of your gunicorn socket with:
sudo systemctl status gunicorn.socket
Check the systemd status:
sudo systemctl status gunicorn
Everytime you make changes to your python files, if you want to add or update any project file — you will need to restart the systemd files:
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
Configure Nginx to server your Django Webapp on digitalocean ubuntu
If you do not already have nginx installed:
sudo apt update
sudo apt install nginx
Create a new server block for your project
sudo nano /etc/nginx/sites-available/yourproject
Inside the file, paste the following:
server {
listen 80;
listen [::]:80; server_name yourdomain.com www.yourdomain.com; location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/yourusername/path-to-youprojectdir;
} location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Close the file. Enable it using the command:
sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled
Check for nginx configurations, make sure you have no errors:
sudo nginx -t
Restart nginx:
sudo systemctl restart nginx
If you have the firewall activated, allow ngix full access:
sudo ufw allow 'Nginx Full'
Install Python Certbot for FREE SSL certificates for your website from Letsencrypt
This part is optional, you can purchase an SSL or install a free one with certbot. Run the commands to install:
sudo apt-get update
sudo apt-get install python3-certbot-nginx
Then you can install an SSL certificate for your website with just one command, pretty neat 😃
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Your website should be up and running with a secure SSL.
That is it for now, you have a fully functional django application hosted with secure SSL certificate without wasting a day. Thanks for reading, if you found this helpful please give a follow.
Sarath Kumar R S
Efuturex Technologies Private Limited.