TECHONOLGY

How to Set Up Django, Gunicorn, Nginx Configuration

You will learn in this article how to install Django in Linux & Python Django is an open-source framework that is widely used for API development. Gunicron is an upstream provider in the back end we will make a connection from Nginx. Gunicorn will take requests from the front end and give a response back to the frontend How to Set Up Django, Gunicorn, Nginx celery configuration.

We are using celery for email notification and beat service configuration for scheduling that task. In this article, we will show you How to Set Up Django, Gunicorn, Nginx celery configuration.

So now we are going to set up the Set Up Django, Gunicorn, Nginx celery configuration. You can choose different types of python versions as per your project requirement.

System Requirement For Set Up Django, Gunicorn, Nginx celery configuration

  1. Centos 7.0 or higher with the latest update 
  2. Python 2.7 or as per project requirement
  3. Nginx webserver

Required Packages Installations for Set Up Django, Gunicorn, Nginx Configuration

  1. Launch EC2 Instance with Centos 7 or you can choose your own Virtual machine with centOS 7
  2. Login to Centos server with the private key or user name and password
  3. Install the latest update and the latest security patches also do packages upgrade.

Installation Of Python 3.5

  1. Download and install the ius-release repository.

# yum install -y https://centos7.iuscommunity.org/ius-release.rpm

# yum install -y python35u python35u-libs python35u-devel python35u-pip

Installation of Nginx Webserver

Above all commands, we used to install python. Now we are going to install the Nginx webserver. Nginx webserver we are using for proxied the user request and you can find proxy pass configuration topic link.

# yum install nginx

Project Django environment setup

Please follow the below command to know about how to install Django in Linux & one by one for the project setup. Firstly you need to install virtualenv package. Why we are installing virtualenv package because in-case we need to set up multiple projects on a single Linux OS.

After that, we will choose multiple virtualenv where we can install project-specific packages and modules as per requirement.

  1. First, we will install virtualenv. Then we will proceed further with the help of below command

# pip3.5 install virtualenv

2. Create HTML directory in /var/www/ folder because to clarify the project location. By default, Nginx doesn’t create an HTML folder Similarly to apache so you need to create one.

# mkdir -p /var/www/html

3. Below Command is used to create an environment folder where we can install the required packages. Before installing the required package. In addition, we need to activate this environment.

# virtualenv -p python3.5 environment_name

4. Commands to activate the environment by following this command secondly create a project folder name.

# source environment_name/bin/activate
# mkdir project-folder

Migration Process From Staging To Production

  1. Go to staging server compress source code project-folder.zip. After that do SCP or copy to the production server.
  2. Put code from staging or from dev server to production server  location: “/var/www/html”
  3. unzip project-folder.zip
  4. cd source folder where code located
  5. find requirement.txt file


Python Django Gunicorn And Celery Configuration

Install project requirement module use below command. Before running this command. You should update the requirement.txt file which includes a packing list with a version is required for your project to make working.

# pip install -r requirement.txt

    6. Check installed packages by running the below command also check command output during running command.

# pip list

Gunicorn Service Configuration

Above all topics used for Django app configuration now let’s do gunicorn configuration. Gunicorn service use to run our application on web URL using sock file. sock file to be created in the application directory.

To run this service you need to install gunicorn packages in the python environment which we create earlier.

First, activate the python environment then run the below command. First, activate your environment after that run the below command. Activate the project environment by using the below command

# source environment_name/bin/activate
(env)# pip install gunicorn

We have a sample file of gunicorn service configuration. In this file. We will change only the directory and file path. Create this service file in a system folder like below.

(env)# vi /etc/systemd/system/projectname.service

The service file extension should be (.service) and the project name could be any name for example projectname.service. Edit this file and copy the below configuration. Change the directory path and file path as per your environment. 

[unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/var/www/html/projectname/source/
ExecStart=/var/www/html/projectname/env_development/bin/gunicorn --workers 3 --bind unix:/var/www/html/projectname/source/ypr.sock ypr.wsgi:application
[Install]
WantedBy=multi-user.target

To enable and start this service you should run the below command.

 

(env)# systemctl reload daemon-reload

Start gunicorn service, in addition, we will also configure celery and beat service configuration

(env)# systemctl start projectname.service

Celery And Beat Configuration For Email Notification And Scheduling

We need email notifications in this project therefore we will use celery service. We can also use the supervisor for this but we will use celery. Celery is used to send an email notification to who will sign up on web application and other subscription notification.

For the celery service, you need to install celery packages in the python environment. Please below command to install celery packages.

(env)# pip install celery

After the celery packages installation, you need to install gunicorn package with the same pip command.

(env)# pip install gunicorn

A worker is an implementation of Celery in Python. A Celery worker is just a program connecting to the broker to process messages. That is to say notification service.

Create two services beat and worker in /etc/systemd/service/ folder. 

To create  worker service run the below command:

(env)# vi /etc/systemd/system/celery-worker.service

For beat service run the below command: 

(env)# vi /etc/systemd/system/celery-beat.service

Celery configuration for worker service.

[Unit]
Description=lqi-celery-Worker Service
After=network.target

[Service]
Type=forking
User=centos
Group=root
EnvironmentFile=-/etc/default/celeryenv-file
WorkingDirectory=/var/www/html/projectname/lqi_qc
ExecStart=/usr/bin/bash -c '${CELERY_BIN} multi start ${CELERYD_NODES}  -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'

ExecStop=/usr/bin/bash -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} --pidfile=${CELERYD_PID_FILE}'

ExecReload=/usr/bin/bash -c '${CELERY_BIN} multi restart ${CELERYD_NODES}  -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE}  --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'

[Install]
WantedBy=multi-user.target

After that create a beat service configuration

[Unit]
Description=celery-Celery-Beat Service
After=network.target

[Service]
Type=simple
User=centos
Group=root
EnvironmentFile=-/etc/default/celeryenv-file
WorkingDirectory=/var/www/html/projectname/lqi_qc
KillMode=control-group
ExecStart=/usr/bin/bash -c '${CELERY_BIN} beat  -A ${CELERY_APP} --pidfile=${CELERYBEAT_PID_FILE}  --logfile=${CELERYBEAT_LOG_FILE} --loglevel=${CELERYBEAT_LOG_LEVEL} --schedule=${CELERYBEAT_SCHEDULE_FILE}'
ExecStop=/usr/bin/systemctl kill gunicorn_lqi-celery-beat.service
[Install]
WantedBy=multi-user.target

Congratulation after following all the above Process you will be able to configure Python Django Application on Linux Server. Also, you will able to do celery, beat, and Gunicorn Service Configuration.

In Conclusion

Now you have successfully know how to install Django in Linux & Set Up Django, Gunicorn, Nginx celery configuration With Gunicorn and celery configuration and you can configure it on Any Linux-based OS like CentOS and Redhat with help of the above all configuration.

We hope you are like this article and this is useful for you. If you learn more about Linux, AWS Tips, and Tricks Please Bookmark this Website.

We are Daily Publish New Article in this Website WWW.BPMTECHGURU.IN and all post on this website is practically tested and 100% Work if you have any Problem Please Drop a Comment we are trying to resolve your problem.

 

About the author

Alex

Leave a Comment