Nginx Unit 部署Django项目

安装 Unit

安装参考**Nginx unit 介绍和官方文档**

配置Django项目

安装django

pip install django==1.11.6

生成项目

django-admin startproject dev

配置 django

1.允许所有地址访问(生产环境请严格配置可访问地址)

$ cd /data/wwwroot/dev/
$ vim dev/settings.py
ALLOWED_HOSTS = ["*"] //修改此行允许访问

2.wsgi配置

因为Nginx Unit目前对python项目仅支持wsgi, 这里就直接使用django的wsgi了。
默认生成django项目的时候会在项目目录下生成dev/wsgi.py,将wsgi.py复制到项目目录下

cp wsgi.py ../

配置 Unit

启动 Unit

不同安装方式,和操作系统请参考官方文档

/usr/sbin/unitd --log /var/log/unitd.log --pid /run/unitd.pid

json配置文件

1.编写json配置文件

$ cat /etc/unit/python.json
{
    "applications": {
        "python": {
            "type": "python",
            "user": "nobody",
            "processes": 2,
            "path": "/data/wwwroot/dev",
            "module": "wsgi"
        }
    },

    "listeners": {
        "*:8400": {
            "application": "python"
        }
    }
}

2.动态加载配置文件

curl -X PUT -d @/etc/unit/python.json --unix-socket /var/run/control.unit.sock http://localhost/

测试项目访问

$ curl -I http://localhost:8400/
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
Content-Type: text/html
Content-Length: 1716
Server: Unit/1.1
Date: Wed, 16 May 2018 03:08:27 GMT

评论

暂无评论