We'll be using the following services and packages to deploy/host our Django application in the production environment.
- Digital Ocean as VPS
- Ubuntu 18.04
- Nginx Server
Step 1. Create a Digital Ocean Account
- You'll get an IP address for your droplet(ubuntu machine) and a password on your .
- SSH to the machine.
- You'll be prompted to change the default password. Do it.
- Next, add a user with sudo privileges.
- Update the machine
Step 2. Installing the necessary packages
- Install Nginx
- Install Supervisor
- Enable and start the supervisor
- Install Virtual Environment
Step 3. Application setup
- create a virtualenv
- copy your project source folder inside the project-venv directory.
- Install the requirements
- Make migrations
- Test your app by opening the IP in your browser.
- HIT Ctrl + C to stop the Django development server.
Step 4. Configure Gunicorn, Supervisor
- Install Gunicorn inside the virtualenv
- Create a file gunicorn_start inside the bin/ folder
- Add the following bash code to the gunicorn_start (/home/username/project-venv/bin/gunicorn_start)
- Make the gunicorn_start file is executable:
- Create a directory named run, for the unix socket file:
- Create a file for logging application errors:
- Configure Supervisor
- Now what we want to do is configure Supervisor to take care of running the gunicorn server.
- Create a new Supervisor configuration file (/etc/supervisor/conf.d/mysite.conf):
- Put the following bash inside the mysite.conf file.
- Reread Supervisor configuration files and make the new program available:
- Check the status:
- Now you can control your application using Supervisor. If you want to update the source code of your application with a new version, you can pull the code from GitHub and then restart the process:
Step 5. Configure NGINX
- Add a new configuration file named mysite inside /etc/nginx/sites-available/:
- Put the following bash inside the mysite file
- Create a symbolic link to the sites-enabled dir:
- Remove the NGINX default website:
- Restart the nginx server