• HOME
  • ABOUT US
  • JOBS
    • Find Jobs
    • Job Dashboard
    • Post a Job
  • SERVICES
  • BLOG
    • VIDEOS
    • DOWNLOADS
  • SEARCH
  • SHOP
  • Log In
Toggle navigation Hey! Homeworx
  • HOME
  • ABOUT US
  • JOBS
    • Find Jobs
    • Job Dashboard
    • Post a Job
  • SERVICES
  • BLOG
    • VIDEOS
    • DOWNLOADS
  • SEARCH
  • SHOP
  • Log In

How To Set Up Django with Nginx in CentOS and RedHat 7

December 14, 2020December 14, 2020 Hey! Admin 0
Posted in Home School, Learning, Linux, School, Technology

How To Set Up Django with Nginx in CentOS and RedHat 7

December 14, 2020December 14, 2020 Hey! Admin 0

Install the packages from NGINX repository

Create repository file under yum repository directory. Use you favorite text or file editor such as nano, vi and etc. In this example, I am going to use “vim”.

In addition, I am using direct root user. You may need to use “sudo”, if your account is a sudoer user.

vim /etc/yum.repos.d/nginx.repo

Add following contents to enable nginx repository.

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

By default, the repository for stable nginx packages is used. In case you need to use mainline nginx packages instead, you may run the below command;

yum-config-manager --enable nginx-mainline

Install nginx.

yum install nginx

Configure NGINX

If you need to run nginx with different user aside from nginx user account. You need to change nginx.conf file and modify user.

vim /etc/nginx/nginx.conf

In this example, I change “user” to root instead of nginx.

user  root;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

I have set listening port to 8080 with IP address of 192.168.1.210. Below configuration will be accessible via browser via http://192.168.1.210:8080.

You need to indicate your website’s directory. In this example, it is /var/www/myproject.

vim /etc/nginx/conf.d/default.conf

server {
    listen 8080;
    server_name 192.168.1.210;
    location = /favicon.ico {
        access_log off;
        log_not_found off;
        alias /var/www/myproject/favicon.ico;
    }
    location /static/ {
        root /var/www/myproject;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/run/myproject.sock;
    }
}
systemctl enable nginx
systemctl restart nginx

Create systemctl service file

Create service file under /etc/systemd/system/multi-user.target.wants directory.

cd /etc/systemd/system/multi-user.target.wants/
vim myproject.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/var/www/myproject
ExecStart=/usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/myproject.sock myproject
.wsgi:application

[Install]
WantedBy=multi-user.target
systemctl enable myproject.service
systemctl start myproject.service

Articles By Hey! Admin

Author Archives Author Website
Posted in Home School, Learning, Linux, School, Technologycentos code django linux nginx programming python redhat

Post navigation

Install Python3 with higher version of Sqlite3 3.34 in CentOS and RedHat
Create graph using Matplotlib in Django

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Appreciation Gift

Thank you for sending your gift. It would help me a lot to maintain and update this site.

Follow us ProKids shop on instagram
Follow us ProKids shop on instagram
Bentoshibox like and subscribe to youtube channel
Hey! HomeWorx official logo

Developed by Code Themes .