您的位置:首页 > 编程语言 > Go语言

django 部署

2015-12-09 21:15 531 查看

一、部署前创建工程、应用

工程目录:/srv/http/

1、新建project-name 工程

#cd /srv/http/
#django-admin startproject project-name

 

2、在应用中views.py写个简单的回复request请求的方法,修改views.py如下 :

#cd /srv/http/project-name/app-name
#vim views.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def index(request):
return HttpResponse("Hello World!")

3、注册应用

#cd /srv/http/project-name/project-name

在setting.py文件中增加:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'app-name',

)

在setting.py文件最后一行添加以下内容

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

SSTATICFILES_DIRS = (
os.path.join(BASE_DIR, "common_static"),
'/srv/http/static',
)

MEDIA_URL = '/MEDIA/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

4、修改url.py

添加对用的(r^$ 'app-name.views.index', name='index'),的过滤器:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'app_nginx.views.index', name='index'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
)

5、python manage.py collectstatic 搜集静态文件到static目录中 。

6、python manage.py runserver 运行是否成功。

二、apache2 + wsgi + django 配置
1. 安装 apache2 和 mod_wsgi

sudo apt-get install apache2 libapache2-mod-wsgi

2、新建新网站 apache2配置文件在 /etc/apache2/ 下 新建一个网站配置文件

#sudo vi /etc/apache2/sites-available/.conf

内容:

ServerName www.yourdomain.com
ServerAlias otherdomain.com
ServerAdmin tuweizhong@163.com

Alias /media/ /srv/http/project-name/media/
Alias /static/ /srv/http/project-name/static/

Order allow,deny
Allow from all

Order allow,deny
Allow from all

WSGIScriptAlias / /srv/http/project-name/project-name/wsgi.py

Order allow,deny
Allow from all

修改wsgi.py文件

import os
from os.path import join,dirname,abspath

#####
PROJECT_DIR = dirname(dirname(abspath(__file__)))
import sys
sys.path.insert(0,PROJECT_DIR)
####
os.environ["DJANGO_SETTINGS_MODULE"] = "blog.settings" # 7

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

3、激活新网站

sudo a2ensite sitename 或 sudo a2ensite sitename.conf

三、nginx + uWSGi + django 配置

1、在nginx配置中新建一个server文件

#cd /etc/nginx/site-avilable/
#sudo vim django
server {
listen 80;

root /var/www/html;
#root /srv/http/static;

# Add index.php to the list if you are using PHP
#index index.php index.html index.htm index.nginx-debian.html;

#the same port 80 , distinct server_name
server_name 192.168.0.212;

location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
#uwsgi_pass  unix:/tmp/project_nginx.sock;
}

location /static/ {
alias /srv/http/project_nginx/static/;
}

}

2、写uWSGI配置文件

#cd /srv/http/project-name
#vim name.ini

uwsgi 通过 pip install uwsgi安装

[uwsgi]
chdir =/srv/http/project_nginx
module = /srv/http/project-name/project-name/wsgi.py
wsgi-file = /srv/http/project-name/project-name/wsgi.py
master =true
processes = 4
socket = 127.0.0.1:9090
logto = /var/log/uwsgi/project_name.log
pidfile = /run/project_name.pid
vacuum = true

启动

uwsgi --ini name.ini
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: