How to Deploy Your Django App on Centos 7 Server with Nginx and Gunicorn

Photo by Kevin Horvat on Unsplash

For Windows user, it might be a bit challenging to work with linux for the first time. But don’t worry, just follow the instructions below and you will be fine. This time, we are going to use Centos 7 as the server. So make sure you are using this exact linux distro. Alright then, without further a do, let’s get started!

A. Preparations

For security reason I usually don’t use root user to deploy my apps, so let’s start with creating a new linux user.

# adduser your_username

Then give it the ability to become sudoer.

# usermod -a -G wheel your_username

Also create a new password for this new linux user.

# passwd your_username

To make life easy let’s create the ssh key!
# cd /home/your_username
# mkdir .ssh
# cd .ssh
# nano authorized_keys
(you may need to install nano beforehand, or you may use vi instead)

Copy paste the ~/.ssh/ from your computer, then save. After that, exit from your root terminal, and relogin using your new linux user. Voilaa! Let’s continue to the next step!

B. Software Installations

The softwares we are going to install are Python 3, Nginx, GCC, and MariaDB. Here are the steps:

  1. Enable the EPEL repository
    $ sudo yum install epel-release
  2. Update linux packages
    $ sudo yum update
  3. Install Python 3 and Nginx
    $ sudo yum install python3-pip python3-devel nginx
  4. Install GCC and MariadB
    $ sudo yum install gcc mariadb-server mariadb-devel
    $ sudo systemctl start mariadb
    $ sudo systemctl enable mariadb
    $ sudo mysql_secure_installation
  5. After the MariaDB installation, we need to create a new database for the app, along with the database user. This step is not necessary if you’re using sqlite for your database.
    Run this command from your terminal:
    $ mysql -u root -p
    Create a new database and a new user with mysql:
    mysql> CREATE DATABASE your_db_name;
    mysql> CREATE USER your_db_username@localhost IDENTIFIED BY ‘your_db_password’;
    mysql> GRANT ALL PRIVILEGES ON your_db_name.* TO your_db_username@localhost;
  6. Copy your django app into your home directory. Or you may clone your app from github (you need to install git to do it).
  7. Create virtual environment inside your project.
    $ python -m venv ./venv
  8. Then activate the virtual environment.
    $ source venv/bin/activate
  9. Install the required packages into your virtual environment by using pip. This time, we are going to install gunicorn, mysqlclient, and Django 3.1.
    (venv)$ python -m pip install gunicorn mysqlclient Django==3.1
  10. And then you need to do some configuration to your app, like your database setting, makemigrations, migrate and createsuperuser.

C. Gunicorn Configurations

Let’s start with testing the gunicorn.
(venv)$ gunicorn — bind <ip:port> your_main_app.wsgi:application

If you open the app with the ip and port in your browser, and it shows your app (though a bit messy/without styling) then congrats. Continue to the next step!

Since we’re gonna configure gunicorn, so let’s first deactivate the virtual environment.
(venv)$ deactivate

Create gunicorn service file.
$ sudo nano /etc/systemd/system/gunicorn.service

Then paste this script below, and adjust accordingly.

Then start the gunicorn.
$ sudo systemctl start gunicorn
$ sudo systemctl enable gunicorn

D. Nginx Configurations

First, open the nginx configuration file.
$ sudo nano /etc/nginx/nginx.conf

Then find the http section, and then nested inside it, there would be server section.

Paste the script below or replace the server {} (like the highlighted in the picture), and adjust accordingly.

With that our nginx is good, we only need a bit more to do. Let’s continue!
Run this on your terminal:
$ sudo usermod -a -G your_linux_user nginx
$ chmod 710 /home/your_linux_user

Then do nginx test:
$ sudo nginx -t

If no errors, start nginx:
$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Finally, our app is running!

If you have done all of the instructions above, and yet, your app is still not running. It is probably because the SELinux is blocking the access. If you don’t know how to configure your SELinux, you may as well set it into permissive. Here is how:
$ setenforce 0

Up to now, I still don’t know how to make it work without setting SELinux into permissive. So, if you know how to do it, please let me know in the comment below.

Okay, I hope it helps. Good luck!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adhi Setyatuhu

A Kotlin android developer and a full stack web developer (familiar with Django) based in Indonesia. Interested in remote jobs.