Pushing a Django Application from development to production is a demanding process, with multiple steps to configure.
In this tutorial, we are going to host a Django Application on a remote ubuntu server. And run the Gunicorn as a web server locally and set up a reverse proxy using the Nginx web server.
Please connect to your Ubuntu server to proceed with the following steps.
To connect with the server using SSH, refer to the article on Connect to your server with SSH.
To connect with the server using vs code, refer to the article on Connect to your server using vs code.
Set up the Django project on your server and check if it is running.
Please refer to the article Getting started with Django on how to set up a Django project on a remote server.
By referring to the above article I've set up my Django project in the path
/home/ubuntu/django_example_repo/ and my virtual environment in the path
/home/ubuntu/.django_venv/ as shown in the following reference screenshot.
We need to collect the static files used in our Django project into a single directory because from now on we are using external servers to serve the files.
Now navigate to the Django core app directory.
Open the settings.py file and change the variable STATIC_ROOT as follow snippet, if the variable was not found then add the following line.
Now navigate to the Django root directory and run the following command.
It’ll copy the static files to a folder named static under the Django project directory.
An example screenshot of the above command is shown below for reference.
Navigate to the Django core app directory.
Open the settings.py and append the following code to the file.
An example django project for your reference can be found in the following git repository.
Gunicorn can be installed by using either APT package manager or python-pip.
We are going to install gunicorn using python-pip, to do so run the following command in the terminal.
Now the gunicorn package will be installed as shown in the below screenshot.
Now we run the Django application using gunicorn in port 8000 which can be accessed from the public internet.
Navigate to the django_project_directory.
Run the following command to run gunicorn.
ip_address: tells gunicorn to run the application from which host. For the application to be accessed from the public internet give 0.0.0.0
port: from which port, are we going to run the application? We'll give 8000.
core_app: name of the Django core app. (My core app name is named core. Give the name of your django_core_app name)
The gunicorn will run the Django application as shown in the below screenshot.
Check the status by visiting the IP address with the port number. (<ip_address>:8000)
We need to create a systemd for gunicorn for running it as a service so that the gunicorn can be run automatically and independently without manual start.
Navigate to the systemd directory and create a file named gunicorn.service.
Now open the above-said file and add the following snippet.
WorkingDirectory Director path of Django project (in my case
ExecStart The command for starting gunicorn,
gunicorn --workers 3 --bind 0.0.0.0:8000 core.wsgi:application, (full gunicorn path within the virtual environment, run the command,
which gunicornand paste the output it returns). An example command execution is given below for reference.
Run the following command so that the gunicorn service is loaded into the system.
To start the gunicorn service run the below command.
We need to enable the gunicorn service so that it can be started automatically when the server reboots.
Again check the status by visiting the IP address with the port number. (<ip_address>:8000)
An example gunicorn service file can be found in the following git repository.
Install Nginx by referring to the following article How to install Nginx on an Ubuntu server?
Disable the default site by running the following command.
Now we need to create a new nginx configuration file for our (new endpoint/ domain/ site).
Navigate to the nginx (configuration/ sites-available) directory and create a file with the name of your convenience(usually domain_name is used, I’m using my domain name api.iternerays.com).
Now open the file
api.iternerays.com and add the following snippet.
location /static redirects the URL whose path starts with /static (like http://api.iternerays.com/static/) to the static file location.
to make nginx serve the static files.
proxy_pass redirects the request coming to the server to the locally running gunicorn. We are redirecting the request to port 8000 at localhost in the above example.
An example site configuration file can be found in the following git repository.
Now link the file
api.iternerays.com to the sites-enabled directory.
Restart both nginx and gunicorn to load the configuration changes by running the following commands.
Now check the status by visiting the domain name (In my case api.iternerays.com)
To create SSL certificates we need a domain mapped to our server’s IP address.
Please refer to the article How to point an A record to IP address and point a domain to your server’s IP address.
Install the certbot and nginx plugin using APT manager by running the below command.
Then create SSL certificates using certbot by running the following command.
Enter the details asked by the certbot with your relevant data respectively.
After installing the SSL certificates, the nginx configuration file we created will now look like the below snippet.
A reference file can be found in the following git repository.
Now check the status by visiting the domain name. (In my case https://api.itinerarys.com/)