时间:2021-05-22
环境搭建
1.安装uwsgi、nginx和django
2.测试uwsgi和nginx的连接
PS:下面的例子采用的是 unix socket 的链接发送
创文件foobar.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2创文件foobar_uwsgi.ini
[uwsgi]# Django-related settings# the base directory (full path)chdir = /home/dmd/project/ENV/mysite# Django's wsgi filemodule = foobar# process-related settings# mastermaster = true# maximum number of worker processesprocesses = 10# the socket (use the full path to be safesocket = /home/dmd/project/ENV/mysite/foobar.sock# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exit# 这个配置本来是true,即退出就删掉socket,但这会导致nginx的启动失败,所以改为falsevacuum = false创文件foobar_nginx.conf
server { listen 8099; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass unix:///home/dmd/project/ENV/mysite/foobar.sock; # 用unix socket # uwsgi_pass 127.0.0.1:8000 # 用TCP socket uwsgi_read_timeout 2; } }将这个文件链接到/etc/nginx/sites-enabled,这样nginx就可以看到它了
启动nginx
启动uwsgi
访问127.0.0.1:8099,如果出现“Hello world”就说明下面连接栈是成功的。
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
3.建立整个连接栈
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
建立django项目
在项目的根目录建立mysite_uwsgi.ini
# myweb_uwsgi.ini file[uwsgi]# Django-related settingssocket = mysite.sock# the base directory (full path)chdir = /home/dmd/project/ENV/mysite# Django s wsgi filemodule = mysite.wsgi# process-related settings# mastermaster = true# maximum number of worker processesprocesses = 4# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = false在项目根目录建立mysite_nginx.conf
server { listen 8099; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass unix:///home/dmd/project/ENV/mysite/mysite.sock; # 用unix socket # uwsgi_pass 127.0.0.1:8000 # 用TCP socket uwsgi_read_timeout 2; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /home/dmd/project/ENV/mysite/static/; } }链接sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
运行
# 运行uwsgiuwsgi --ini mysite_uwsgi.ini# 开启niginxsudo service nginx start测试。访问 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以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
环境MacBookAir阿里云ESC:Centos7.0nginx+1.16.1uwsgi=2.0.18django=2.2.7服务器进入阿里云网站注册账号进行
如何在生产上部署Django?Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。uwsgi介绍uWSGI是一个
如何在生产上部署Django?Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式。uwsgi介绍uWSGI是一个
本文介绍了使用Docker搭建Django,Nginx,R,Python部署环境的方法,分享给大家,具体如下:基本环境:Ubuntu16.10docker17.
本文实例讲述了uwsgi+nginx部署Django项目操作。分享给大家供大家参考,具体如下:uWSGI概述uWSGI是一个全功能的HTTP服务器,可以把HTT