How to deploy django project using Postgres, Nginx on digitalocean ubuntu server

What is DigitalOcean?

Digitalocean is a server hosting company.(Online Deployment)
They offer virtual private server (VPS) at budget-friendly cost which is good if you are just trying to experiment with setting up your own server, installing applications on the server, running your own website, hosting a small to medium sized web application. As your app/website grow, you can expand your plans which might make it ideal for startups, freelancers or students.

 

Creating Digitalocean account and setting up server

Once we have the server up and ready, we will get the IP address of this server, and we will login to our server using SSH

Once logged in, let’s update all the packages that are currently installed in our server

Create a user on this server and give him a sudo permission. Remaining steps we will be performing as a user, not

as root – for security purposes.

Now ssh in to the server as dydevops user

Security & Access

Creating SSH keys (Optional)

You can choose to create SSH keys to login if you want. If not, you will get the password sent to your email to login via SSH

To generate a key on your local machine

Hint enter all the way through and it will create a public and private key at

Add SSH key for new user

Navigate to the new users home folder and create a file at ‘.ssh/authorized_keys’ and paste in the key

Possible Deployment Issues

To avoid deployment errors, we will take following actions:

Add allowed host, release media folder, run collectstatic and create requirements.txt from the local project.

  • Replace psycopg2 package with psycopg2-binary
  • Install backports.zoneinfo only if the server python version is less than 3.9
  • backports.zoneinfo==0.2.1;python_version<"3.9"
  • Add gunicorn to requirements.txt

Download FileZilla Server for Windows (64bit x86)

Django file upload with FTP backend

Project contains source code that was made originally for the Django file upload example at StackOverflow.

The goal with minimal-django-file-upload-example is to demonstrate and teach how file uploading is done with Django. Because of the academic nature of the project all the extra functionality is left out. Otherwise you would have hard time to guess what is important and what is not.

Backup data from local machine and restore in to live database

Local machine

While you are logged in to the server via SSH, create a project directory as myproject inside /home/dydevops/ folder

Now check the remote directory, our project is pushed to the server. That’s all, we have set up Filezilla to upload the code from local machine to ftp server.

Install & Configure PostgreSQL

Install

While you are logged into the server, run following commands to install, start, enable and see the status of postgresql database:

Postgres Database & User Setup

You should now be logged into the pg shell

Create a database

Create user

Set default encoding, tansaction isolation scheme (Recommended from Django)

Give User access to database

Quit out of Postgres

Open Django Project settings.py

cd /home/dydevops/myproject

Setup Virtual Environment run the server

In the myproject folder, create virtual environment and activate it

Install the packages from requirements.txt file

Run makemigrations and migrate commands

Allow port 8000

Check status of ufw

Run the server

Remote server

python manage.py shell

Configuring Gunicorn

Install Gunicorn on our server

Tell gunicorn to bind to our Django application and start running

Test the link and make sure the site is working

exit

Go to this location and paste below code:

Location:

Restart and enable Gunicorn socket

Configuring Nginx as a reverse proxy

Location:

Open port 80 and close 8000

Go to sites-enabled directory and delete the default site

Fix Static Files on Server

Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin

Leave a Comment