首页 > python web

nginx+uwsgi+django环境搭建的方法步骤

时间:2021-02-02 python web 查看: 1065

环境搭建

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/```

<p>启动nginx<br>
</p>

sudo service nginx start

<p>启动uwsgi<br>
</p>

uwsgi --ini foobar_uwsgi.ini

<p>访问127.0.0.1:8099,如果出现“Hello world”就说明下面连接栈是成功的。<br>
</p>
<p><code>the web client <-> the web server <-> the socket <-> uWSGI <-> Python</code></p>
<p><strong>3.建立整个连接栈</strong><br>
</p>
<p><code>the web client <-> the web server <-> the socket <-> uwsgi <-> Django</code></p>
<p>建立django项目<br>
</p>

```python  
django-admin startproject mysite```

<p>在项目的根目录建立mysite_uwsgi.ini</p>

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

<p>在项目根目录建立mysite_nginx.conf</p>

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/; } }

<p>链接<code>sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/</code></p>
<p>运行</p>

运行uwsgi

uwsgi --ini mysite_uwsgi.ini

开启niginx

sudo service nginx start

<p>测试。访问 127.0.0.1:8099 ,如果看到django的页面,说明成功。</p>
<p>完整的目录树</p>

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 ```

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。

展开全文
上一篇:基于python中__add__函数的用法
下一篇:Django 自定义分页器的实现代码
输入字:
相关知识
django学习之ajax post传参的2种格式实例

AJAX除了异步的特点外,还有一个就是:浏览器页面局部刷新,下面这篇文章主要给大家介绍了关于django学习之ajax post传参的2种格式的相关资料,需要的朋友可以参考下

Python djanjo之csrf防跨站攻击实验过程

csrf攻击,即cross site request forgery跨站(域名)请求伪造,这里的forgery就是伪造的意思。这篇文章主要给大家介绍了关于Python djanjo之csrf防跨站攻击的相关资料,需要的朋友可以参考下

django admin实现动态多选框表单的示例代码

借助django-admin,可以快速得到CRUD界面,但若需要创建多选标签字段时,需要对表单进行调整,本文通过示例代码给大家介绍django admin多选框表单的实现方法,感兴趣的朋友跟随小编一起看看吧

Flask登录注册项目的简单实现

一个简单的用户注册和登录的页面,涉及到验证,数据库存储等等,本文主要介绍了Flask登录注册项目的简单实现,从目录结构开始,感兴趣的可以了解一下