Python/Django

5. Django - Nginx 연동

상쾌한기분 2019. 11. 11. 16:34
반응형

참고 : https://victorydntmd.tistory.com/257

 

Nginx 설치

# Nginx 설치

> vi /etc/yum.repos.d/nginx.repo

# Insert this parhase
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1


# Firewall 설정

> yum install nginx

> firewall-cmd --permanent --zone=public --add-service=http
> firewall-cmd --permanent --zone=public --add-service=https

> firewall-cmd --reload


# Nginx 등록 및 시작
> systemctl enable nginx
> systemctl start nginx

http://아이피주소 로 접속확인
[Welcome to Nginx!]

uwsgi 설치

yum install -y gcc

pip install uwsgi

uwsgi 설정 파일

# mkdir /etc/uwsgi/sites
# /etc/uwsgi/sites/django.ini

[uwsgi]
project = django_sample
username = nginx
base = /home

### Django Settings
# base directory
# 프로젝트 경로
chdir = /home/django_sample/django_sample

# python path
# 가상 env python 폴더 혹은 서버 python
home = /usr/local/bin/

# virtualenv path
# 가상 env path
#virtualenv = /home/django_sample/sampleenv

# wsgi.py path
# django 프로젝트 wsgi.py 경로
module = django_sample.wsgi:application

master = true
processes = 1

uid = nginx
socket = /run/uwsgi/django.sock
chown-socket = nginx:nginx
chmod-socket = 660
vacuum = true

#logto = /var/log/uwsgi/djangoTest.log
logto = /home/django_sample.log

 

# vi /etc/systemd/system/uwsgi.service


[Unit]
Description=uWSGI service

[Service]
ExecStartPre=/bin/mkdir -p /run/uwsgi
ExecStartPre=/bin/chown nginx:nginx /run/uwsgi
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target




# systemctl daemon-reload

--emperor

uwsgi는 emperor 모드에서 실행할 수 있습니다.
이 모드에서는 uWSGI 설정 파일의 디렉토리를 감시하고 발견 한 각각에 대한 인스턴스를 생성하게 됩니다.
즉, emperor 모드를 통해 여러 응용 프로그램을 관리 할 수 있습니다.

# /etc/nginx/conf.d/default.conf


server {
    listen       8000;
    server_name  192.168.1.229;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/django.sock;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 

# systemctl start uwsgi

# systemctl enable uwsgi
728x90
반응형