DjangoBB论坛,是基于Django(Python经典框架)进行修改,其安装部署需要依赖于python和Django。很多资料上都是说明采用python的virtualenv,virtualenv就是python的一个虚拟沙盒,用于各项目的隔离。我觉得用不用无所谓。
一、环境准备:
本文使用的操作系统是CentOS7虚拟机。Windows和Ubuntu应该类似,无非依赖的包及依赖的编译环境有差异。
1、基础环境版本
- CentOS 7
- Python 2.7/3.3+
- Django >= 1.7
以下操作都是用的root账号,如果不是root用户,请在命令前家“sudo”
2、软件安装
(1)安装python
yum install -y python
验证是否安装成功
python --version
正确显示版本号,即安装成功。
(2)安装pip
首先,需要安装扩展原EPEL。EPEL(http://fedoraproject.org/wiki/EPEL) 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。
yum -y install epel-release
然后,安装pip
yum -y install python-pip
验证是否安装成功
pip --version
正确显示版本号,即安装成功。
默认的pip使用的是低版本,可以使用下面命令更新pip
pip install -U pip
二、DjangoBB搭建
(1)下载源代码
要搭建DjangoBB,需要从git或hg上,下载两个工程的代码。这主要是由于Django框架本身,有project和app的概念。具体区别,请翻阅参考资料9。
wget https://bitbucket.org/slav0nic/djangobb/get/stable.tar.gz
wget https://bitbucket.org/slav0nic/djangobb_project/get/tip.tar.gz
注:最好到上面的https://bitbucket.org中(JIRA同一家公司开发的类似于github的网站),找到最新版本的包来下载。
下载完成之后,可以通过下面的命令,在根目录下找到下载的包。
ls -l
stable.tar.gz
tip.tar.gz
(2)解压缩和重命名已下载的包
tar zxvf stable.tar.gz
tar zxvf tip.tar.gz
解压后的目录名称与下载的包名不同,而且会很长不方便操作。改个短小的名字,方面操作。我们把:
- stable.tar.gz:解压后的目录改为djangobb-master
- tip.tar.gz:解压后的目录改为djangobb-project
ls -l
mv slav0nic-djangobb-bfcdda967701 djangobb-master
mv slav0nic-djangobb_project-8087a7e42207 djangobb-project
(3)安装默认依赖包
进入到djangobb-master目录
cd djangobb-master
该目录下有三个以requirements开头的txt文件,这些文件中包含所需使用的依赖包。
- requirements.txt
- requirements_optional.txt
- requirements_dev.txt(开发用的依赖,搭建环境用不到)
pip install -r requirements.txt
pip install -r requirements_optional.txt
注:以上是自动安装的方式,这种方式有时可能会因为网络等问题卡住或者出错,所以可以选择提前手动安装。打开requirements.txt,查看里面的依赖包,用 pip install 一条一条执行。
(4)安装其它依赖包
除了requirements中的包以外,还有几个Django依赖的额外的包需要安装。
pip install whoosh
pip install django-mailer
pip install django-nocaptcha-recaptcha
pip install django-pagination-py3
pip install django-static-sitemaps
或者
pip install whoosh django-mailer django-nocaptcha-recaptcha django-pagination-py3 django-static-sitemaps
安装完成后,可以通过下面命令查看,已安装的包
pip list
不合适的包可以用下面命令删除
pip uninstall <packagename>
(5)创建Django工程
切换到根目录
cd ~
或
cd /root
创建Django工程,本例中使用的工程名为“luntan”
django-admin startproject luntan
工程创建好之后,可以通过以下命令进入:
cd ~/luntan
或
cd /root/luntan
注:Django工程创建好之后,会新建一个luntan目录,在该目录下还有一个luntan目录。为了区别,后面在说明的时候,会用 ~/luntan 代表第一层目录,~/luntan/luntan 代表第二层目录。
(6)拷贝资源文件
进入到djangobb-master目录下,将其中的djangobb_forum文件夹复制到~/luntan 目录下
[root@nx-centos7 ~]# cd ~/djangobb-master
[root@nx-centos7 djangobb-master]# cp -r djangobb_forum ~/luntan
进入到djangobb-project/basic_project目录下,其中会包含系统用到的资源文件,将其中的djangobb_index,media,project_static,static,templates和文件forms.py复制到 ~/luntan 目录下
[root@nx-centos7 ~]# cd ~/djangobb-project/basic_project/
[root@nx-centos7 basic_project]# cp -r djangobb_index ~/luntan
[root@nx-centos7 basic_project]# cp -r media ~/luntan
[root@nx-centos7 basic_project]# cp -r project_static ~/luntan/luntan
[root@nx-centos7 basic_project]# cp -r static ~/luntan
[root@nx-centos7 basic_project]# cp -r templates ~/luntan
[root@nx-centos7 basic_project]# cp -r forms.py ~/luntan
(7)拷贝配置文件
djangobb-project/basic_project目录下,除了上一步包含的资源文件外,还包含两个配置文件settings.py和urls.py。将这两个文件拷贝到 ~/luntan/luntan 目录下
[root@nx-centos7 basic_project]# cp -r urls.py ~/luntan/luntan
cp: overwrite ‘~/luntan/luntan/urls.py’? y
[root@nx-centos7 basic_project]# cp -r settings.py ~/luntan/luntan
cp: overwrite ‘~/luntan/luntan/settings.py’? y
(8)修改配置文件
进入到 ~/luntan/luntan 目录下,修改 settings.py 配置文件。部署成功配置文件示例如下:
# -*- coding: utf-8 -*-
import os.path
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DEBUG = True
ALLOWED_HOSTS = ['*']
ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': os.path.join(PROJECT_ROOT,'db.sqlite3'), # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = True
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'zh-cn'
LANGUAGES = (
('ca', 'Catalan'),
('cs', 'Czech'),
('de', 'German'),
('en', 'English'),
('es', 'Spanish'),
('fo', 'Faroese'),
('fr', 'France'),
('it', 'Italian'),
('lt', 'Lithuanian'),
('mn', 'Mongolian'),
('nl', 'Dutch'),
('pl', 'Polish'),
('ru', 'Russian'),
('uk_UA', 'Ukrainian'),
('vi', 'Vietnamese'),
('zh_CN', 'Chinese'),
)
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT,'project_static'),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
if not hasattr(globals(), 'SECRET_KEY'):
SECRET_FILE = os.path.join(PROJECT_ROOT, 'secret.txt')
try:
SECRET_KEY = open(SECRET_FILE).read().strip()
except IOError:
try:
from random import choice
import string
symbols = ''.join((string.lowercase, string.digits, string.punctuation ))
SECRET_KEY = ''.join([choice(symbols) for i in range(50)])
secret = file(SECRET_FILE, 'w')
secret.write(SECRET_KEY)
secret.close()
except IOError:
raise Exception('Please create a %s file with random characters to generate your secret key!' % SECRET_FILE)
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'djangobb_forum.middleware.LastLoginMiddleware',
'djangobb_forum.middleware.UsersOnline',
'djangobb_forum.middleware.TimezoneMiddleware',
)
ROOT_URLCONF = 'luntan.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(PROJECT_ROOT, 'templates')],
'OPTIONS': {
'context_processors': ['django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.request',
'django.contrib.messages.context_processors.messages',
'django_messages.context_processors.inbox',
'djangobb_forum.context_processors.forum_settings',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
]
},
},
]
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.humanize',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.openid',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.twitter',
'haystack',
'django_messages',
'nocaptcha_recaptcha',
'djangobb_forum',
)
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
try:
import mailer
INSTALLED_APPS += ('mailer',)
EMAIL_BACKEND = "mailer.backend.DbBackend"
except ImportError:
pass
FORCE_SCRIPT_NAME = ''
# Haystack settings
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
'PATH': os.path.join(PROJECT_ROOT, 'djangobb_index'),
'INCLUDE_SPELLING': True,
},
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
# Account settings
ACCOUNT_ACTIVATION_DAYS = 10
LOGIN_REDIRECT_URL = '/forum/'
LOGIN_URL = '/forum/account/signin/'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
)
# Cache settings
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
# Allauth
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_SIGNUP_FORM_CLASS = 'forms.SignupForm'
try:
from local_settings import *
except ImportError:
pass
主要修改内容:
1、修改数据库
为了方便,本例中使用的是sqlite3数据库,如果在安装过程发现生成有问题,应该是python环境中没有sqlite,可以提前用命令进行安装。
yum install sqlite-devel
修改内容
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': os.path.join(PROJECT_ROOT,'db.sqlite3'), # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
在ENGINE属性中,增加数据库类型,然后补充其他属性。如果是使用sqlite,在NAME属性中,指定数据库生成的路径和文件名,如上例。
2、修改时区和国际化
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = True
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'zh-cn'
3、修改ROOT URL
ROOT_URLCONF = 'luntan.urls'
这里的 luntan 就是我们用Django建立的工程名。
4、访问安全控制
增加下面属性
ALLOWED_HOSTS = ['*']
“*”代表全部,也可以指定具体IP。
(9)执行命令
以上配置完成之后,就可以执行相关的命令,进行部署。进入到 ~/luntan 目录下,这个目录下有一个 manage.py 文件,这个文件是所有命令执行的主体。该文件包含很多可执行的命令,可以通过下面命令来查看:
./manage.py --help
我们下面执行的命令只是一些必须的命令:
1、
切换到~/luntan/djangobb_forum目录下,在终端中输入如下指令编译message文件
cd ~/luntan/djangobb_forum
django-admin compilemessages
2、切换到~/luntan目录下
./manage.py migrate
注:在执行这个命令的时候,有时会出错,错误信息中指明:在django_messages中引用了过时的patterns包,有RemovedInDjango110Warning。解决办法:(1)打卡错误提示中的XXX/django_messages/urls.py文件。(2)去掉文件中对patterns包的imports。(3)将 XXX = patterns(‘’,url。。),修改为 XXX = [url。。]。参考6
3、
切换到~/luntan目录,执行以下两个命令:
./manage.py collectstatic
./manage.py createsuperuser
(10)运行系统
./manage.py runserver 0.0.0.0:8000
注:(1)指定 0.0.0.0:8000,是为了保证可以在局域网中访问到该论坛。(2)如果要停止系统,必须使用 CTRL+C ,否则系统不会停止还会占用端口,只能用Kill命令杀掉进程。
在浏览器中输入访问论坛:
127.0.0.1:8000/forum/
在浏览器中输入如下地址用之前创建的管理账号登陆对论坛进行管理:
127.0.0.1:8000/admin/
参考资料:
[1]:https://bitbucket.org/slav0nic/djangobb/src
[2]:http://blog.csdn.net/lcyfire/article/details/52141079
[3]:http://blog.csdn.net/onepiece_dn/article/details/46229329
[4]:http://blog.csdn.net/tanzuozhev/article/details/50477711
[5]:http://blog.csdn.net/chenggong2dm/article/details/50457733
[6]:http://www.ziqiangxuetang.com/django/django-settings.html
[7]:http://blog.chinaunix.net/uid-21633169-id-4352454.html
[8]:https://github.com/LLK/s2forums
[9]:http://blog.csdn.net/jiwang1990/article/details/9374855