nginx+uwsgi+django环境搭建的方法步骤
环境搭建
1.安装uwsgi、nginx和django
aptinstallnginx pipinstalluwsgi pipinstalldjango
2.测试uwsgi和nginx的连接
PS:下面的例子采用的是unixsocket的链接发送
创文件foobar.py
defapplication(env,start_response): start_response('200OK',[('Content-Type','text/html')]) return[b"HelloWorld"]#python3 #return["HelloWorld"]#python2
创文件foobar_uwsgi.ini
[uwsgi] #Django-relatedsettings #thebasedirectory(fullpath) chdir=/home/dmd/project/ENV/mysite #Django'swsgifile module=foobar #process-relatedsettings #master master=true #maximumnumberofworkerprocesses processes=10 #thesocket(usethefullpathtobesafe socket=/home/dmd/project/ENV/mysite/foobar.sock #...withappropriatepermissions-maybeneeded #chmod-socket=664 #clearenvironmentonexit #这个配置本来是true,即退出就删掉socket,但这会导致nginx的启动失败,所以改为false vacuum=false
创文件foobar_nginx.conf
server{ listen8099; server_name127.0.0.1 charsetUTF-8; access_log/var/log/nginx/myweb_access.log; error_log/var/log/nginx/myweb_error.log; client_max_body_size75M; location/{ includeuwsgi_params; uwsgi_passunix:///home/dmd/project/ENV/mysite/foobar.sock;#用unixsocket #uwsgi_pass127.0.0.1:8000#用TCPsocket uwsgi_read_timeout2; } }
将这个文件链接到/etc/nginx/sites-enabled,这样nginx就可以看到它了
sudoln-s~/path/to/your/mysite/mysite_nginx.conf/etc/nginx/sites-enabled/
启动nginx
sudoservicenginxstart
启动uwsgi
uwsgi--inifoobar_uwsgi.ini
访问127.0.0.1:8099,如果出现“Helloworld”就说明下面连接栈是成功的。
thewebclient<->thewebserver<->thesocket<->uWSGI<->Python
3.建立整个连接栈
thewebclient<->thewebserver<->thesocket<->uwsgi<->Django
建立django项目
django-adminstartprojectmysite
在项目的根目录建立mysite_uwsgi.ini
#myweb_uwsgi.inifile [uwsgi] #Django-relatedsettings socket=mysite.sock #thebasedirectory(fullpath) chdir=/home/dmd/project/ENV/mysite #Djangoswsgifile module=mysite.wsgi #process-relatedsettings #master master=true #maximumnumberofworkerprocesses processes=4 #...withappropriatepermissions-maybeneeded #chmod-socket=664 #clearenvironmentonexit vacuum=false
在项目根目录建立mysite_nginx.conf
server{ listen8099; server_name127.0.0.1 charsetUTF-8; access_log/var/log/nginx/myweb_access.log; error_log/var/log/nginx/myweb_error.log; client_max_body_size75M; location/{ includeuwsgi_params; uwsgi_passunix:///home/dmd/project/ENV/mysite/mysite.sock;#用unixsocket #uwsgi_pass127.0.0.1:8000#用TCPsocket uwsgi_read_timeout2; } location/static{ expires30d; autoindexon; add_headerCache-Controlprivate; alias/home/dmd/project/ENV/mysite/static/; } }
链接sudoln-s~/path/to/your/mysite/mysite_nginx.conf/etc/nginx/sites-enabled/
运行
#运行uwsgi uwsgi--inimysite_uwsgi.ini #开启niginx sudoservicenginxstart
测试。访问127.0.0.1:8099,如果看到django的页面,说明成功。
完整的目录树
mysite/ ├──db.sqlite3 ├──manage.py ├──mysite │├──__init__.py │├──__pycache__ ││├──__init__.cpython-36.pyc ││├──settings.cpython-36.pyc ││├──urls.cpython-36.pyc ││└──wsgi.cpython-36.pyc │├──settings.py │├──urls.py │└──wsgi.py ├──mysite.sock ├──mysite_nginx.conf ├──mysite_uwsgi.ini └──uwsgi_params
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。