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
相关文章推荐
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
- Learning ROS for Robotics Programming Second Edition学习笔记(七) indigo PCL xtion pro live
- Django 简介
- Django 基础教程
- 推荐一下django学习的网址!!!
- go程序性能优化
- go-nsq使用简述
- Go语言关于chan理解的实验
- 在windows平台下安装MangoDB3.0.7 设置超级管理员服务启动
- BestCoder Round #63 (div.2)
- Go语言beego框架环境搭建
- mongo 附近的点按指定字段排序
- 解决无法访问Google问题.
- 【转】no resource found @integer/google_play_services_version whats this?
- Django 学习笔记<二>
- Google 开源项目的风格指南
- 谷歌重返中国:已在华注册“壳公司”
- Golang Import使用入门
- flex polygon 序列化为txt 文本