阿里云部署Django项目(超详细图文教程)—— Part3. Django settings修改、PostgreSQL配置
2015-11-14 18:44
1131 查看
阿里云部署Django项目(超详细图文教程)
Part3. Django settings修改、PostgreSQL配置
前言:
花了一个月的空闲时间,终于成功把Django网站部署到了阿里云ECS上,包含以下功能:不使用任何第三方工具,直接用网页连接阿里云ECS
使用GIT进行源码控制和上传到服务器
使用git hooks实现自动部署
用的是时下比较流行的一套部署方案——Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL
可以在同一台服务器上部署两个、甚至多个网站
光说不练假把式,建议大家边看边操作!
Django 发布设置
发布环境下修改Debug为False这里根据主机名来判断是否在发布环境
import socket if socket.gethostname() == 'iZ94m6lerh2Z': DEBUG = TEMPLATE_DEBUG = False else: DEBUG = TEMPLATE_DEBUG = True
服务器主机名有两种方式获得:
在管理控制台查看
在管理终端用命令查看
在服务器上创建数据库并进行配置
sudo su - postgres createuser --interactive -P
按照提示输入名称和密码
Enter name of role to add: hello_django Enter password for new role: Enter it again: Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) n Shall the new role be allowed to create more new roles? (y/n) n
为了提高数据库安全,可以去https://www.grc.com/passwords.htm生成一个复杂度更高的密码
创建数据库
createdb --owner hello_django hello logout
修改settings文件数据库配置
if DEBUG: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'hello', 'USER': 'hello_django', 'PASSWORD': '1Ak5RTQt7mtw0OREsfPhJYzXIak41gnrm5NWYEosCeIduJck10awIzoys1wvbL8', 'HOST': 'localhost', 'PORT': '', } } else: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
修改Admiin、Host
ADMINS = ( ('woniu02141', '380238062@qq.com'), ) ALLOWED_HOSTS = ['localhost', '.woniu02141.com'] MANAGERS = ADMINS
修改时区
TIME_ZONE = 'Asia/Shanghai' LANGUAGE_CODE = 'zh-cn'
修改StaticRoot等
STATIC_ROOT = '/webapps/woniu/static/' STATIC_URL = 'http://woniu02141.com/static/' MEDIA_ROOT = '/webapps/woniu/media/' MEDIA_URL = 'http://woniu02141.com/media/'
在服务器端,同步数据库,收集静态文件
/webapps/woniu/bin/python3.4 manage.py syncdb /webapps/woniu/bin/python3.4 manage.py collectstatic
在这里根据virtualenv创建的python环境来选择python路径
备注: 连接数据库时出现一些认证情况的解决办法:
如果出现下面的错误:
FATAL: Ident authentication failed for user “mypguser”
请编辑你的pg_hba.conf,这个文件一般位于/etc/postgresql/X.Y/main/pg_hba.conf,X.Y是你的PostgreSQL的版本号,将下面行的peer改为trust:
local all all trust
如果出现下面的错误:
FATAL: Peer authentication failed for user “mypguser”
请仍然修改pg_hba.conf文件,该下面行的peer为md5:
local all all md5
完成上面的修改后请重新加载postgresql:
/etc/init.d/postgresql reload
相关文章推荐
- RPC failed; result=22, HTTP code = 411
- nginx代理指定目录
- git更新已經刪除的文件
- 访问Nginx发生SSL connection error的一种情况
- 提取Git每次提交后Commit的文件
- GIT迁移服务器
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- 分布式版本管理git入门指南使用资料汇总及文章推荐
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 阿里云这群疯子
- 网站502与504错误分析
- PostgreSQL新手入门教程
- PostgreSQL教程(十):性能提升技巧