05 Ubuntu, Django 요약 1 uWSGI 배포

Ubuntu에서의 Django 배포 요약

2. uWSGI 및 nginx로 웹 서버 구성

Nginx 서버는 정적 파일 (HTML, 이미지, CSS 등)을 직접 처리 할 수 ​​있으며 부하 분산을 달성하기 위해 역방향 프록시 서버 역할을 할 수 있습니다. 그러나 Django 응용 프로그램과 직접 상호 작용할 수 없으므로 WSGI 프로토콜을 구현하지 않습니다 .uWSGI는 WSGI 구현이므로 두 가지가 결합되어 Djang 프로젝트 배포를 구현합니다.

정적 파일에 대한 클라이언트 요청은 Nginx에서 직접 처리하고 Django의 동적 부분은 uWSGI로 전달됩니다.

django 프로젝트 mysite가 / 아래에 생성되었다고 가정합니다.

yake@Ubuntu:/$ sudo django-admin startproject mysite
yake@Ubuntu:/$ cd mysite
yake@Ubuntu:/$ tree
/mysite
    ├── manage.py
    └── mysite
        ├── asgi.py
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

nginx.conf 파일 생성 구성 파일은 서버 블록과 업스트림 블록 만 정의합니다.

yake@Ubuntu:/mysite$ sudo touch nginx.conf
yake@Ubuntu:/mysite$ sudo gedit nginx.conf

# the upstream component nginx needs to connect to
upstream django {
	# server unix:///mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server 
server {
    # the port your site will be served on
    listen      8000;  # 默认 80 
    # the domain name it will serve for
    server_name localhost; # substitute your machine's IP address or FQDN
    charset     utf-8;
 
    # max upload size
    client_max_body_size 75M;   # adjust to taste
 
    # Django media 静态文件
    location /media  {
        alias /mysite/media;  # your Django project's media files - amend as required
    }
 	# 静态文件
    location /static {
        alias /mysite/static; # your Django project's static files - amend as required
    } 	
    # Finally, send all non-media requests to the Django server. 
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

이 구성 파일을 / etc / nginx / sites-enabled /에 연결합니다.

yake@Ubuntu:/mysite$ sudo ln -s /mysite/nginx.conf /etc/nginx/sites-enabled/

ln은 Windows 바로 가기와 유사한 다른 위치에있는 특정 파일 또는 디렉터리에 대한 동기화 링크를 설정하는 것입니다.

Nginx 기본 구성 파일 /etc/nginx/nginx.conf 파일에 다음 행이 있습니다.

include /etc/nginx/sites-enabled/*;

정확성 테스트

yake@Ubuntu:/mysite$ sudo nginx -t

정적 파일 배포

nginx를 실행하기 전에 정적 폴더에있는 모든 Django 정적 파일을 수집해야합니다. 먼저 mysite / settings.py를 편집하여 다음을 추가해야합니다.

STATIC_ROOT = BASE_DIR / "static/"

Django 일반 구성 :

DEBUG = False
ALLOWED_HOSTS = ['*']

그런 다음 다음을 실행하십시오.

python manage.py collectstatic

nginx는 또한 포트 8001에서 uWSGI와 통신하도록 구성되어 있으며 외부 포트는 8000입니다.
다른 콘텐츠는 uWSGI 서버에 남겨 둡니다.

yake@Ubuntu:/mysite$ sudo uwsgi --socket :8001 --module mysite.wsgi

포트 대신 유닉스 소켓 사용

포트보다 Unix 소켓을 사용하는 것이 더 낫습니다. 비용이 적게 듭니다.

nginx.conf를 편집하고 다음과 일치하도록 수정하십시오.

server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)

그리고 nginx를 다시 시작하십시오.

yake@Ubuntu:/mysitesudo nginx -s reload
yake@Ubuntu:/mysite$ sudo uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666

추천

출처blog.csdn.net/weixin_43955170/article/details/112187701