解析Mac OS下部署Pyhton的Django框架项目的过程

WBOY
Lepaskan: 2016-06-10 15:05:04
asal
1359 orang telah melayarinya

一、安装软件包并创建项目

$sudo pip install django
$sudo python -c "import django;print django.VERSION"
(1, 7, 0, 'final', 0)
$sudo django-admin startproject cmdb #创建项目
$sudo django-admin startapp cmdb #创建应用

Salin selepas log masuk

二、修改配置
1、修改settings.py,添加cmdb应用,以及其他设置

INSTALLED_APPS = (
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'cmdb',
)
DATABASES = {
 'default': {
  'ENGINE': 'django.db.backends.mysql',
  'NAME': 'cmdb',
  'USER': 'cmdb',
  'PASSWORD': 'cmdb',
  'HOST': 'localhost',
  'PORT': '3306',
 }
}
LANGUAGE_CODE = 'zh-cn'
TIME_ZONE = 'Asia/Shanghai'

Salin selepas log masuk

2、修改urls.py和views.py
urls.py内容如下:

from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
 # Examples:
 # url(r'^$', 'cmdb.views.home', name='home'),
 # url(r'^blog/', include('blog.urls')),
 url(r'^admin/', include(admin.site.urls)),
 url(r'^index/','cmdb.views.index'),
)
Salin selepas log masuk

views.py内容如下:

from django.shortcuts import render
from django.http import HttpResponse
def index(req):
 return HttpResponse('<h1>hello welcome to django!</h1>')
Salin selepas log masuk

3、测试
启动django

#sudo python manage.py runserver
Salin selepas log masuk

访问:
http://localhost:8000/index

PS:gunicorn结合nginx来部署django应用
说明:gunicorn部署django程序,前端用nginx处理服务器请求,静态资源直接处理,动态资源转发到后端。

目录结构:

cmdb/

├── cmdb

│  └── migrations

├── device_manage

├── idcroom_manage

├── operation

│  └── migrations

└── static

  └── admin

    ├── css

    ├── img

    │  └── gis

    └── js

      └── admin

Salin selepas log masuk

1、安装gunicorn和django

pip install gunicorn
pip install django
Salin selepas log masuk

2、安装MySQLdb

wget https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip
cd MySQL-python-1.2.5
python setup.py install
Salin selepas log masuk

3、用gunicorn启动django程序

[root@backup cmdb]# gunicorn --version
gunicorn (version 19.1.1)
gunicorn cmdb.wsgi:application --bind=127.0.0.1:8000 --daemon
Salin selepas log masuk

gunicorn参数:

–bind指定侦听地址

–daemon放到后台运行

更多参数:gunicorn –help

nginx反向代理:

 server {
   listen 8080;
   server_name 192.168.3.21;
   location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for ;
    proxy_set_header Host $http_host ;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
   } 
 location /static {
alias /opt/wwwroot/cmdb/static;
 }
 access_log logs/cmdb.access.log;
  }
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan