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

作者:陈某君 时间:2022-01-12 16:34:59 

环境搭建

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

来源:https://www.jianshu.com/p/22beb24b3841

标签:nginx,uwsgi,django
0
投稿

猜你喜欢

  • Python全栈之模板渲染详解

    2021-06-07 12:39:48
  • python运行其他程序的实现方法

    2022-11-15 05:18:09
  • Python测试开源工具splinter安装与使用教程

    2022-07-22 01:56:36
  • 用JavaScript实现单继承和多继承的简单方法

    2024-04-23 09:15:33
  • vue-property-decorator用法详解

    2024-05-29 22:49:55
  • mysql多表查询的几种分类详细

    2024-01-28 14:43:21
  • 理解JavaScript变量作用域更轻松

    2024-04-16 09:25:07
  • JavaScript属性操作

    2024-04-16 09:52:52
  • 解决antd Form 表单校验方法无响应的问题

    2024-05-28 16:00:23
  • python的setattr函数实例用法

    2023-08-17 18:49:59
  • Python 读取串口数据,动态绘图的示例

    2021-11-15 19:36:24
  • JavaScript面向对象之Prototypes和继承

    2024-04-23 09:14:54
  • MySQL中易被我们忽略的细节

    2024-01-21 09:54:48
  • 完美解决python3.7 pip升级 拒绝访问问题

    2023-08-10 07:45:33
  • python实现微信小程序的多种支付方式

    2021-03-26 21:38:58
  • 关于vuex状态刷新网页时数据被清空问题及解决

    2024-04-30 10:22:18
  • 下载golang.org/x包的操作方法

    2023-07-11 16:54:04
  • 500行代码使用python写个微信小游戏飞机大战游戏

    2023-01-16 06:58:09
  • DWCS3-CSS布局之二CSS规则定义

    2008-06-16 13:36:00
  • python编程实现清理微信重复缓存文件

    2022-06-20 02:12:12
  • asp之家 网络编程 m.aspxhome.com