`

使用nginx部署django应用

阅读更多

1、通过pip安装uwsgi

 

$sudo yum install python-devel
$sudo yum install python-pip
$sudo pip install pip --upgrade
$sudo yum install libpcre3 libpcre3-dev
$sudo yum install zlib1g-dev

 如果安装版本错误,先卸载:

 

 

$pip uninstall uwsgi
$sudo yum remove uwsgi

python 版本最好是python 2.7.*

 

pip的版本应该是最新版本。
查看pip 版本:

 

$ pip --version
pip 8.0.2 from /usr/lib/python2.7/site-packages (python 2.7)

 接下来安装uwsgi。

$sudo pip install uwsgi

 

 

 

2、检测django

$django-admin startproject hello //会新建一个目录hello
$python manage.py syncdb
$python manage.py runserver 0.0.0.0:8000

 验证django能否正常运行

 

 

3、检测uwsgi

在hello目录下新建wsgi,py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

 然后运行uwsgi

$uwsgi --http :8000 --module hello.wsgi

 浏览器验证是否“It works”

 

 

4、配置uwsgi

配置一下项目的静态文件目录,因为静态文件不需要uwgsi执行

在hello目录下有个settings.py文件,在此文件加一行

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

 

然后执行命令

$python manage.py collectstatic

 

新建/etc/hello_uwsgi.ini

    # mysite_uwsgi.ini file
    [uwsgi]

    socket = 127.0.0.1:3400
    # Django-related settings
    # the django project directory (full path)
    chdir           = /root/project/hello
    # Django's wsgi file
    module          = hello.wsgi

    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 2

    threads = 2
    max-requests = 6000

    # ... with appropriate permissions - may be needed
    chmod-socket    = 664
    # clear environment on exit
    vacuum          = true
    daemonize = /root/project/hello/uwsgi.log

  启动uwsgi

uwsgi --ini hello_uwsgi.ini

 

 

4、配置nginx

新加一个nginx配置,,mysite.ini

server {
# the port your site will be served on
listen      8000;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset     utf-8;

access_log /root/project/hello/access_log;
error_log  /root/project/hello/error_log;

# max upload size
client_max_body_size 75M;   # adjust to taste

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

location /static {

alias /root/project/hello/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:3400;
    include    /etc/nginx/uwsgi_params; # the uwsgi_params file you installed

}

 然后重启nginx,搞定

 

1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics