Deploying django website on ubuntu server
We will be using digitalocean for vps. You can follow along with me
https://m.do.co/c/c2ce8c26e4d2 click and get $100 credit on digitalocean droplet.
Here in this blog post we will be using basic droplet 1gb of RAM & 1vCPU 25gb ssd.
Above config can manage upto 4 django sites on devlopement server.
number of website depends on the project.
Lets start with deployment process.
Step 1: Create fresh new droplet on digitalocean. Login to your droplet with $ssh [email protected]_server_ip.
Update the vps by below command
$sudo apt-get update.
Install the basic modules required for python3 & Django .
$sudo apt install python3-pip python3-dev python3-venv libpq-dev nginx curl
Step 2 : Create a seprate linux user with sudo privileges.
Granting the sudo privileges.
$usermod -aG sudo username
switch to new user
$sudo su username
create a virtualenv
$python3 -m venv myproject_env
$pip3 install django
$django-admin startproject demo_project
make sure you don’t use django keywords eg.test
$python3 manage.py migrate
$python3 manage.py runserver 0.0.0.0:8000
Now you can see you new django app is running on
Till now we configure our django app as it works on local system.
Lets dive on main process. I assume you have FQD routed on on your current droplet ip address.Eg. $ping aruntiwari.in return my server_ip_address.
NOTE: You can also continue without FQD. All you have to do is replace domain_name with your ip address.
open your settings.py file located demo_project/settings.py
Press Esc butten and then :wq for saving the file.
Step 3: Settingup gunicorn
$pip3 install gunicorn
$sudo vim /etc/systemd/system/demo.service
$sudo systemctl enable demo
$sudo systemctl restart demo
$sudo systemctl status demo
It should be working fine. If it is not working please check the directory structure properly.
you must seee demo.sock in your working directory.
Step 4: Setting up Nginx webserver.
Create a directory in /home/username/myproject_env/demo_project
Open settings.py and configure static files.
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Note: don’t add “/static/” it will give permission error.It should be same as it is “static/”
$python3 manage.py collectsatic
$sudo vim /etc/nginx/sites-availabel/demo
Save the file :wq
lets create a symlink for this file.
$ sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enable/
Test the nginx for syntax
$ sudo nginx -t
If test is successfull restart the nginx.
$ sudo systemctl restart nginx.
your website should be live on your domain and ip.
Conclusion: Although we have coverd how to deploy django website with nginx and gunicorn this is not still not that good for production. For Production you might have add some additional configuration in nginx to secure your website. We will be covering those topics in my next blog till that time create something cool.