Django with Gunicorn on Ubuntu
This tutorial is aimed at the Django user who wants to set up a production web server. It takes you through the steps required to set up Django so that it works nicely with uWSGI and Gunicorn.
We will be setting up a PostgreSQL database instead of using the default SQLite database. We will configure the Gunicorn application server to interface with our applications.
To begin the process, we’ll download and install all of the items we need from the Ubuntu repositories. We will use the Python package manager pip to install additional components a bit later.
We need to update the local apt package index and then download and install the packages.
Django Gunicorn Install Package
Create the PostgreSQL Database and User By Typing :
$sudo -u postgres psql
Create Database User
Afterwards, we’ll modify a few of the connection parameters for the user we just created
Alter Postgres Role
Now, we can give our new user access to administer our new database:
Grant All Privileges
When you are finished, exit out of the PostgreSQL prompt by typing:
postgres=#\q
Upgrade pip install virtualenv
Within the project directory, create a Python virtual environment by typing:
Create Virtualenv
Before we install our project’s Python requirements, we need to activate the virtual environment. You can do that by typing:
Activate virtualenv
With your virtual environment active, install Django, Gunicorn, and the psycopg2 PostgreSQL adaptor with the local instance of pip:
Install Gunicorn for Django
Create and Configure a New Django Project
Create Django Project
The first thing we should do with our newly created project files is adjust the settings. Open the settings file in your text editor:
Allowed Port
Change the settings with your PostgreSQL database information.
Add Database
Next, move down to the bottom of the file and add a setting indicating where the static files should be placed.
Add Static Root
Now, we can migrate the initial database schema to our PostgreSQL database using the management script
Apply Migrations
Create an administrative user for the project by typing:
Create Super User
We can collect all of the static content into the directory location we configured by typing:
Copy to static
If you followed the initial server setup guide, you should have a UFW firewall protecting your server. In order to test the development server, we’ll have to allow access to the port we’ll be using.
Create an exception for port 8000 by typing:
$sudo ufw allow 8000
Run on 00 port
In your web browser, visit your server’s domain name or IP address followed by :8000:
Working port
The last thing we want to do before leaving our virtual environment is test Gunicorn to make sure that it can serve the application.
Load WSGI module
Create and open a systemd service file for Gunicorn with sudo privileges in your text editor :
$sudo nano /etc/systemd/system/gunicorn.service
Open Service File
With that, our systemd service file is complete. Save and close it now.
We can now start the Gunicorn service we created and enable it so that it starts at boot:
Start gunicorn service
Check the status of the process to find out whether it was able to start:
Check Status
If you make changes to the /etc/systemd/system/gunicorn.service file, reload the daemon to reread the service definition and restart the Gunicorn process by typing:
Reload Daemon
Now that Gunicorn is set up, we need to configure Nginx to pass traffic to the process.
Start by creating and opening a new server block in Nginx’s sites-available directory:
$sudo nano /etc/nginx/sites-available/myproject
Open sites available dir
Save and close the file when you are finished. Now, we can enable the file by linking it to the sites-enabled directory:
Enable Linking
Test your Nginx configuration for syntax errors by typing:
Test nginx config
If no errors are reported, go ahead and restart Nginx by typing:
$sudo systemctl restart nginx
Now we can remove the rule to open port 8000 as well:
Allow port rule update
Now Its Run on: http://192.168.2.124 (Our local)
Working with ip address