使用Nginx Unit 部署Django

本文将介绍如何在Nginx Unit上部署Django项目。

  1. 首先安装Unit 和相关依赖
apt-get install unit
apt-get install unit-dev unit-go unit-jsc11 unit-perl unit-php unit-python2.7 unit-python3.7 unit-ruby
yum install unit
yum install unit-dev unit-go unit-jsc11 unit-perl unit-php unit-python2.7 unit-python3.7 unit-ruby

2. 将代码上传到当前用户目录下,确保能在调试模式下正常工作

3. 通过Curl配置项目

sudo curl -X PUT --data-binary @config.json --unix-socket /var/run/control.unit.sock http://localhost/config

config.json内容

{
	"routes": [{
		"match": {
			"uri": [
				"*.css",
				"*.ico",
				"*.jpg",
				"*.js",
				"*.png",
				"*.xml"
			]
		},
		"action": {
			"share": "/home/tusdasa/project/django/website/static/"
		}
	}],
	"listeners": {
		"0.0.0.0:8080": {
			"pass": "applications/django"
		}
	},
	"applications": {
		"django": {
			"type": "python 3",
			"path": "/home/tusdasa/project/django/website/",
			"home": "/home/tusdasa/project/django/venv/",
			"module": "website.wsgi"
		}
	}
}

访问8080即搭建完成。

5. 删除一些配置的方法

sudo curl -X DELETE --data-binary @config.json --unix-socket /var/run/control.unit.sock http://localhost/config

curl -X PUT -d "" http://localhost/config/listeners/
sudo curl -X DELETE --unix-socket /var/run/control.unit.sock 'http://localhost/config/listeners/0.0.0.0:8080'

sudo curl -X DELETE --unix-socket /var/run/control.unit.sock 'http://localhost/config/applications/django'

sudo curl -X DELETE --unix-socket /var/run/control.unit.sock 'http://localhost/config/routes'