首页 > 代码库 > nginx+uwsgi 部署 django
nginx+uwsgi 部署 django
预装:nginx, django
Django站点tree
|Site
|-----Blog(自建的项目)
|-----Manage.py
|-----Site(setting url wsgi….)
| |-----wsgi.py
|-----static
| |-----js
| |-----css
|-----uwsgi.log
|-----uwsgi-socket.xml
1. libxml2、libxml2-devel、uwsgi
1.1 安装
注:必须先安装libxml2、libxml2-devel,再安装uwsgi
rpm –ivh Libxml2*****
pip: pip install uwsgi
tar: python setup.pu install
1.2测试uwsgi
vim testuwsgi.py
def application(env, start_response):
start_response(‘200 OK‘,[(‘Content-Type‘,‘text/html‘)])
return "uwsgi ok!"
命令行执行:
uwsgi --http :8001 --wsgi-file testuwsgi.py
浏览器测试:127.0.0.1:8001
2. 配置Site/wsgi.py
import sys
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE","logSite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
sys.path.append(‘/Site‘)
sys.path.append(‘/Site/Site‘)
3.测试 uwsgi +django
执行命令:
uwsgi --http :8001 --chdir /Site/Site --module wsgi
/Site/Site是wsgi.py的目录
wsgi作为module
浏览器测试:127.0.0.1:8001
4. nginx + uwsgi + django
4.1 创建nginx + uwsgi 进行socket连接xml文件
vim/Site/uwsgi-socket.xml
<uwsgi>
<socket>:8001</socket> <!-- 别用8000端口,预留8000方便解释器测试 -->
<chdir>/Site/Site</chdir>
<module>wsgi</module>
<processes>4</processes><!-- 进程数 -->
<daemonize>uwsgi.log</daemonize><!-- 创建日志文件 -->
</uwsgi>
4.2 修改nginx.conf 配置文件
server {
listen 8080;
server_name www.site.com;
access_log/logs/access.log;
error_log/logs/error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
uwsgi_paramUWSGI_CHDIR /Site/Site;
uwsgi_paramUWSGI_SCRIPT wsgi;
}
#静态文件路径
location /static {
alias/Site/static;
}
}
4.3 重启nginx 服务
cd /usr/local/nginx/sbin
./nginx -s reload
4.4 启动uwsgi 服务器
uwsgi -x django-socket.xml
4.5 访问服务
ip:8080
www.site.com
nginx+uwsgi 部署 django