nginx + uwsgi + django 환경 구축

운영 체제 ubuntu 14.04.2
1. uwsgi 를 설치 합 니 다. 제 기계 에 두 개의 python 환경 이 있 습 니 다. 처음에 실제 환경 에서 설치 한 것 이 었 습 니 다. 나중에 문제 가 있 는 것 을 발 견 했 습 니 다. 가상 환경 에 가서 다시 설 치 했 지만 uwsgi 와 django 를 연결 하지 못 했 습 니 다. uwsgi 를 다시 시작 해 야 합 니 다. 다음 날 에 바로 시작 할 수 있 지만 django 의 정적 자원 을 불 러 올 수 없습니다. 정적 자원 은 nginx 에 맡 겨 야 합 니 다.   
pip install uwsgi
uwsgi 연결 을 시도 하기 전에 uwsgi 와 django, django 는 자체 개발 서버 를 사용 하여 uwsgi 는 간단 한 함 수 를 쓸 수 있 습 니 다. 다음 과 같 습 니 다.
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"
uwsgi 시작
<pre name="code" class="plain">uwsgi --http :8000 --wsgi-file django_uwsgi.py
 连接uwsgi和django项目,在项目根目录下创建django_uwsgi.py 
  
  
 
import os
import sys

reload(sys)
sys.setdefaultencoding('utf8')

os.environ.setdefault("DJANGO_SETTINGS_MODULE","project_name.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
uwsgi 를 시작 하여 uwsgi 와 django 의 연결 이 정상 인지 확인 합 니 다.
uwsgi --http :8000 --wsgi-file django_uwsgi.py

2. nginx 설치
apt-get install nginx
nginx 가 정상적으로 작 동 할 수 있 는 지 테스트 합 니 다.
service nginx start
service nginx stop
service nginx restart
서 비 스 를 시작 한 후 홈 페이지 를 정상적으로 방문 하면 성공 적 입 니 다.
3. nginx 설정
nginx 서비스 가 시 작 될 때 / etc / nginx / nginx. conf 의 설정 을 불 러 옵 니 다. 설정 정 보 를 직접 추가 할 수도 있 고, mysite. conf 를 conf. d 폴 더 에 따로 만 들 수도 있 습 니 다. / etc / nginx / nginx. conf 는 conf. d 의 모든 설정 파일 을 불 러 옵 니 다. 설정 내용 은 다음 과 같 습 니 다.
<pre class="hljs php" style="padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; color: rgb(101, 123, 131); border-radius: 4px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; word-wrap: normal; border: 1px solid rgba(0, 0, 0, 0.14902); overflow: auto; background: rgb(253, 246, 227);"><code class="php" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border-radius: 3px; border: none; background-color: transparent;"><span class="hljs-comment" style="color: rgb(147, 161, 161);"># the port your site will be served on</span>
    listen      <span class="hljs-number" style="color: rgb(42, 161, 152);">8000</span>;
    <span class="hljs-comment" style="color: rgb(147, 161, 161);"># the domain name it will serve for</span>
    server_name .example.com; <span class="hljs-comment" style="color: rgb(147, 161, 161);"># substitute your machine's IP address or FQDN</span>
    charset     utf-<span class="hljs-number" style="color: rgb(42, 161, 152);">8</span>;</code>
 
  
 
    #       
    log_format access '$msec $status $request $request_time $http_referer $remote_addr [ $time_local ] $upstream_response_time $host $bytes_sent $request_length $upstream_addr';
    access_log /var/log/nginx/access.log access;    #    ;
    error_log /var/log/nginx/error.log;
    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media    media          
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  127.0.0.1:8077;  #       (    ?)   uwsgi    
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed  /etc/nginx/   
    }
}
 4、配置uwsgi 
  
 

我们设置uwsgi和nginx的通信端口是8077。可以在项目主目录下配置xml文件,(第一步已经设置好django_uwsgi):

<uwsgi>
    <socket>:8077</socket>
    <chdir>project_dir</chdir>
    <module>django_wsgi</module>
    <processes>4</processes> <!--     --> 
    <daemonize>uwsgi.log</daemonize>
</uwsgi>
5. 보충, 절차 3 전에 django 의 정적 파일 디 렉 터 리 를 설정 하고 프로젝트 의 setting. py 에 추가 합 니 다.
STATIC_ROOT = '/static/'
여기 STATICROOT, 이것 은 프로젝트 의 상대 경로 입 니 다. 실행
pyhton manage.py collectstatic
모든 정적 자원 을 설 정 된 디 렉 터 리 로 이동 합 니 다.
6. 운행
service nginx start
uwsgi -x django_uwsgi.xml

내 질문: 코드 수정 후 uwsgi 를 어떻게 다시 시작 합 니까?모든 프로 세 스 를 죽 이 고 다시 시작 하 시 겠 습 니까?
참고:
http://www.jianshu.com/p/e6ff4a28ab5a http://www.django-china.cn/topic/124/#top

좋은 웹페이지 즐겨찾기