ubuntu 18.04 에서 django 프로젝트 에 nginx+gunicorn 환경 구축

3219 단어 djangonginxgunicorn
오늘 은 ubuntu 18.04 에서 django 프로젝트 를 위해 nginx+gunicorn 환경 을 구축 하 는 방법 을 소개 합 니 다.
구체 적 인 절 차 는 다음 과 같다.1.gunicorn:pip install gunicorn 2.gunicorn 을 사용 하여 django 프로젝트 를 시작 합 니 다.gunicorn 프로젝트 이름.wgi:application–bid 127.0.0.1:80003.nginx 를 설치 하고 설정 합 니 다.컴 파일 설치 할 때 nginx 를 설치 하기 전에 다른 패 키 지 를 설치 해 야 합 니 다.구체 적 으로 다음 과 같이 설치 합 니 다.1)openssl 을 설치 합 니 다.
wget https://www.openssl.org/source/openssl-1.1.0e.tar.gz
tar zxvf openssl-1.1.0e.tar.gz

압축 해제 가 완료 되면 openssl 이 있 는 디 렉 터 리 에 들 어가 순서대로 실행 합 니 다./config,make 와 make install 을 설치 합 니 다.
2)zlib 설치:
wget https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz

압축 해제 가 완료 되면 zlib 가 있 는 디 렉 터 리 에 들 어가 순서대로 실행 합 니 다./configure,make 와 make install 을 설치 합 니 다.
3)pcre 설치:
wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
tar zxvf pcre-8.40.tar.gz

압축 해제 가 완료 되면 pcre 가 있 는 디 렉 터 리 에 들 어가 순서대로 실행 합 니 다./configure,make 와 make install 을 설치 합 니 다.
물론 이 패 키 지 는 다른 버 전 을 다운로드 하고 설치 할 수 있다.
4)nginx 를 설치 하면http://nginx.org/en/download....를 클릭 하여 필요 한 nginx 버 전 을 다운로드 할 수 있 습 니 다.다운로드 완료 후 tar 명령 으로 압축 해제:tar zxvf nginx-1.14.0.tar.gz 압축 해제 완료 후 nginx 디 렉 터 리 에 들 어가 다음 명령 을 실행 하여 설정 합 니 다.
./configure --with-http_ssl_module --with-cc-opt="-Wno-error" --with-pcre=/home/wyzane/install_package/pcre-8.40 --with-zlib=/home/wyzane/install_package/zlib-1.2.11 --with-openssl=/home/wyzane/install_package/openssl-1.1.0e

이상 설정 에서 저 는 패 키 지 를 모두/home/wyzane/install 에 설 치 했 습 니 다.패키지/디 렉 터 리 아래 다른 디 렉 터 리 에 도 설치 할 수 있 습 니 다.이상 설정 을 실행 한 후에 저 는 컴 파일(make)할 때 오류 가 발생 했 습 니 다.오류 정 보 는 대체적으로 다음 과 같 습 니 다.
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough

나중에https://trac.nginx.org/nginx/에서 해결 방법 을 찾 았 습 니 다.즉,실행./configure 에–with-cc-opt="-wno-error"라 는 인 자 를 추가 하 는 것 입 니 다.나중에 nginx 를 설치 하 는 데 문제 가 생 겼 을 때 위의 사이트 에서 해결 방안 을 찾 을 수 있 습 니 다.
실행 이 끝 난 후 make 와 make install 을 실행 하여 컴 파일 하고 설치 합 니 다.설치 가 완료 되면 ngin 을 시작 하고 nginx 홈 페이지 에 방문 하여 설치 성공 여 부 를 검증 할 수 있 습 니 다.
4.nginx 설정:nginx.conf 파일 에 들 어가 서 설정 아래 부분 을 수정 하면 됩 니 다.
server {
        listen       80;
        server_name  192.168.114.113;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
             proxy_pass http://192.168.114.113:8000;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /static {
             alias /home/wyzane/pyprojects/test/test/static;
        }
}

주요 설정 listen,servername,location/location/staticlisten 은 감청 이 필요 한 포트 입 니 다.servername 은 gunicorn 시작 후 서버 의 ip,location/지정 루트 경로,location/static 지정 정적 파일 경로 입 니 다.
5.nginx 설정 후 정적 파일 수집:python manage.py collectstatic 6.nginx 와 gunicorn 을 시작 하면 항목 에 접근 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기