环境搭建
1.安装uwsgi、nginx和django
apt install nginx pip install uwsgi pip install 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 file module = foobar # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = /home/dmd/project/ENV/mysite/foobar.sock # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit # 这个配置本来是true,即退出就删掉socket,但这会导致nginx的启动失败,所以改为false vacuum = 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就可以看到它了
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
启动nginx
sudo service nginx start
启动uwsgi
uwsgi --ini foobar_uwsgi.ini
访问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项目
django-admin startproject mysite
在项目的根目录建立mysite_uwsgi.ini
# myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = mysite.sock # the base directory (full path) chdir = /home/dmd/project/ENV/mysite # Django s wsgi file module = mysite.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = 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/
运行
# 运行uwsgi uwsgi --ini mysite_uwsgi.ini # 开启niginx sudo 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“nginx+uwsgi+django环境搭建的方法步骤”评论...
更新动态
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]