对于uwsgi+nginx的部署方式,它的访问关系大概是:

1
the web client <-> the web server <-> the socket <-> uwsgi <-> Django

  

如果你需要使用virtualenv:

1
2
3
virtualenv uwsgi-tutorial
cd uwsgi-tutorial
source bin/activate

  

安装django:

1
2
3
pip install Django
django-admin.py startproject mysite
cd mysite

  

这里假设的你域名是:example.com,在后面的你可以换成你的域名或者IP地址。

原教程中使用的是8000端口号,我们这直接使用80端口。

基于uwsgi

安装uwsgi

1
pip install uwsgi

 

先做个测试uwsgi是否能正常使用,创建一个test.py文件(在哪创你自己决定啊,反正配置完要删的):

1
2
3
4
5
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"# python3
    #return ["Hello World"] # python2

  

下面我们使用uwsgi:

1
uwsgi --http :8000 --wsgi-file test.py

  

参数解释:

http :8000:指示用的是8000端口

wsgi-file test.py:加载指定文件 test.py

然后你就可以尝试访问了:

1
http://example.com:8000

  

接下来我们在django项目上尝试一下

新建的django项目需要先:

1
python manage.py migrate<br>python manage.py runserver

  

如果能够运行:

1
uwsgi --http :8000 --module mysite.wsgi

  

参数:

module mysite.wsgi :指定wsgi

尝试访问:

1
http://example.com:8000

  

现在的结构类似于:

1
the web client <-> uWSGI <-> Django

  

基于 nginx

安装nginx

1
2
sudo apt-get install nginx
sudo /etc/init.d/nginx start    # start nginx

  

也可以用nginx服务命令比如

1
2
3
sudo service nginx start
sudo service nginx stop
sudo service nginx restart

  

现在访问http://127.0.0.1/就能看到默认的nginx主页

然后我们来配置nginx文件,先对原始配置文件做个备份

1
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

  

然后编辑配置文件

1
sudo vim /etc/nginx/sites-available/default

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# default
 
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
 
# configuration of the server
server {
    # the port your site will be served on
    listen      80 default_server;
    listen      [::]:80 default_server ipv6only=on;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;
 
    # max upload size
    client_max_body_size 75M;   # adjust to taste
 
    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }
 
    location /static {
        alias /path/to/your/mysite/static# your Django project's static files - amend as required
    }
 
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params# the uwsgi_params file you installed
    }

一下是我的配置信息供参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
upstream django {
    server unix:///home/ubuntu/blogsite/mysite.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
 
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
 
        root /usr/share/nginx/html;
        index index.html index.htm;
 
        # Make site accessible from http://localhost/
        server_name localhost;
 
        charset utf-8;
 
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
 
        client_max_body_size 75M;
 
        location /media {
                alias /home/ubuntu/blogsite/media;
        }
 
        location /static {
                alias /home/ubuntu/blogsite/static;
        }
 
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
                uwsgi_pass      django;
                include         /etc/nginx/uwsgi_params;
        }

  

下面我们对django进行一下配置,编辑django配置文件mysite/settings.py 加上:

1
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

  

这些配置信息具体用处可在我的另一篇文章中看到。

然后执行命令

1
python manage.py collectstatic

  

然后重启nginx

1
sudo /etc/init.d/nginx restart

  

现在可以在django项目中放几个静态文件,看是否能访问:

比如将一个media.png的图片放在mysite/media 文件夹中(没有media文件夹可以自己创建一个)

然后访问

1
http://example.com/media/media.png

  

就能访问到这个图片。

nginx,uwsgi和test.py

还记得我们创建的test.py文件,现在我们再让它发挥一下作用

再test.py 的目录下执行命令:

1
uwsgi --socket :8001 --wsgi-file test.py

  

这里使用的8001端口号跟上面的配置文件中的

1
2
3
4
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

相对应,我们现在是用的端口socket所以使用下面的配置,以后会使用到文件socket

然后我们访问网站就能看到test.py 文件返回的内容了

这次我们的访问顺序类似于:

1
the web client <-> the web server <-> the socket <-> uWSGI <-> Python

  

注:如果上文中的8001端口不能使用可改用其他端口号

使用unix sockets文件代替端口

前面我们使用了tcp端口,十分简单,但是我们最好使用sockets文件,这样能减少资源消耗

我们将default的配置稍作修改

1
2
server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)

  

然后在django目录中使用命令

1
uwsgi --socket mysite.sock --wsgi-file test.py

然后访问网站,

注:如果不能访问(一般来说访问不了!!),我们check一下nginx的错误日志,

1
vim /var/log/nginx/error.log

  

如果在里面看到类似于

1
2
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)

  

那是权限问题,我们改用下面的命令

1
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666

  

如果能正常访问了,那我们来试试使用wsgi来访问django项目

1
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=666

  

然后我们使用.ini文件来配置uwsgi(差不多就能完成了),在项目目录下创建mysite_uwsgi.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# mysite_uwsgi.ini file
[uwsgi]
 
# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
#home            = /path/to/virtualenv
 
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

  

一下是我的配置,供参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[uwsgi]
 
# Django-related settings
# the base directory (full path)
chdir           = /home/ubuntu/blogsite
# Django's wsgi file
module          = blogsite.wsgi
# the virtualenv (full path)
# home            = /path/to/virtualenv
 
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 2
# the socket (use the full path to be safe
socket          = /home/ubuntu/blogsite/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

然后,跑起来

1
uwsgi --ini mysite_uwsgi.ini

  

以上,使用uWSGI+nginx部署Django项目就算是完成了,还有其它的配置可参考官方文档(比如怎样服务开机自启)

http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

欢迎多来访问博客:http://liqiongyu.com/blog

使用uWSGI+nginx部署Django项目(Ubuntu)的更多相关文章

  1. 使用uWSGI+nginx部署Django项目

    最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,总体思路是按照官方文档走的. 原文地址:http://uwsgi-doc ...

  2. ubuntu18+uwsgi+nginx部署django项目

    更新系统软件源 sudo apt-get update pip3安装 sudo apt install python3-pip 安装virtualenvwrapper pip3 install vir ...

  3. Ubuntu+Django+uWSGI+Nginx部署Django项目

    安装uWSGI,pip依据自己要使用的python版本自行选择,python2.x版本使用pip进行安装,python3.x版本使用pip3进行安装 pip install uwsgi 配置uWSGI ...

  4. uwsgi+nginx部署django项目

    1. 概念解析(wsgi协议,uwsgi协议,uWSGI) 参考:https://www.cnblogs.com/wspblog/p/8575101.html 1.1 现实世界的web请求: 1.2  ...

  5. 基于腾讯云CentOS7.4+MySQL5.7+Python3+uwsgi+nginx的Django项目部署

    准备知识 1.django一个基于python的开源web框架,请确保自己熟悉它的框架目录结构. 2.uWSGI一个基于自有的uwsgi协议.wsgi协议和http服务协议的web网关 3.nginx ...

  6. uwsgi+anaconda+nginx部署django项目(ubuntu下)

    conda 环境不必多说: conda(或source)  activate  test 进入test虚拟环境 接下来安装uwsgi: pip install uwsgi 在conda环境下大概率安装 ...

  7. vue+uwsgi+nginx部署luffty项目

    在部署项目之前本人已经将前端代码和后端代码发布在了一个网站上,大家可自行下载,当然如果有Xftp工具也可以直接从本地导入. django代码 https://files.cnblogs.com/fil ...

  8. uwsgi + nginx 部署python项目(一)

    uWSGI uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uws ...

  9. 阿里云轻量级服务器和NGINX部署Django项目

    部署条件: 1.一台阿里云服务器(本人的是CentOS系统的服务器) 2.已经构建好的项目 3.服务器上安装并配置Nginx 首先第一步:在服务器上安装并配置Nginx 进入服务器 $ ssh roo ...

  10. 关于Nginx部署Django项目的资料收集

    参考:https://www.cnblogs.com/chenice/p/6921727.html 参考:https://blog.csdn.net/fengzq15/article/details/ ...

随机推荐

  1. WPF开发快速入门【2】WPF的基本特性(Style、Trigger、Template)

    概述 本文描述几个WPF的常用特性,包括:样式.触发器和控件模板. 样式/Style Style就是控件的外观,在XAML中,我们通过修改控件的属性值来设置它的样式,如: <!--直接定义sty ...

  2. C# wpf 实现Converter定义与使用

    1.  本身的值0, 如何转换为"男" 或"女"呢,可以定义sexConverter继承自IValueConverter即可,代码如下: [ValueConve ...

  3. .Net Core 静态类获取注入服务

    由于静态类中无法使用有参构造函数,从而不能使用常规的方式(构造函数获取) 获取服务,我们可以采取通过IApplicationBuilder 获取 1.首先创建一个静态类 using Microsoft ...

  4. 挨个配置资源组太麻烦?ROS伪参数一步搞定!

    介绍 伪参数 伪参数是资源编排服务ROS的编排引擎提供的固定参数,即在编写模板时可以使用的一系列预定义的参数,它们为模板提供了资源部署过程中的环境和执行上下文信息. 更多伪参数介绍请查看:ROS伪参数 ...

  5. Swift 属性装饰器

    import ArgumentParser @propertyWrapper struct WrapperTest { internal var innerValue: Int { didSet { ...

  6. 大厂边缘组VS小厂核心组,要怎么选?

    有问必答 最近有粉丝提问:大厂边缘组VS小厂核心组,怎么选? 这确实是个好问题,读者老爷们可以先问下自己:如果有一份月薪2W在大厂边缘组打螺丝的Offer且不加班,另外还有一份月薪2W5,在小厂核心组 ...

  7. 【Socket】解决TCP粘包问题

    一.介绍 TCP一种面向连接的.可靠的.基于字节流的传输层协议. 三次握手: 客户端发送服务端连接请求,等待服务端的回复. 服务端收到请求,服务端回复客户端,可以建立连接,并等待. 客户端收到回复并发 ...

  8. 如果设备不支持vulkan,就用swiftshader,否则就加载系统的vulkan的正确姿势(让程序能够智能的在vulkan-1.dll和libvk_swiftshader.dll之间切换)

    一些老的显卡设备没有更新驱动,甚至根本就不支持Vulkan的显卡,遇到静态链接的vulkan-1.lib文件,启动exe就会崩溃. 你以为从别的机器拷贝这个vulkan-1.dll就可以了吗? 太傻太 ...

  9. a标签的title属性 换行

    使用 title 属性,可以让鼠标悬停在超链接上的时候,显示该超链接的文字注释. <a href="#" title = "123">超链接< ...

  10. 贝壳找房: 为 AI 平台打造混合多云的存储加速底座

    贝壳机器学习平台的计算资源,尤其是 GPU,主要依赖公有云服务,并分布在不同的地理区域.为了让存储可以灵活地跟随计算资源,存储系统需具备高度的灵活性,支持跨区域的数据访问和迁移,同时确保计算任务的连续 ...