DjangoBB论坛搭建

迷南。 2022-07-14 02:29 333阅读 0赞

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

  1. yum install -y python

验证是否安装成功

  1. python --version

正确显示版本号,即安装成功。

(2)安装pip

首先,需要安装扩展原EPEL。EPEL(http://fedoraproject.org/wiki/EPEL) 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质量软件包的项目。

  1. yum -y install epel-release

然后,安装pip

  1. yum -y install python-pip

验证是否安装成功

  1. pip --version

正确显示版本号,即安装成功。

默认的pip使用的是低版本,可以使用下面命令更新pip

  1. pip install -U pip

二、DjangoBB搭建

(1)下载源代码

要搭建DjangoBB,需要从git或hg上,下载两个工程的代码。这主要是由于Django框架本身,有project和app的概念。具体区别,请翻阅参考资料9。

  1. wget https://bitbucket.org/slav0nic/djangobb/get/stable.tar.gz
  2. wget https://bitbucket.org/slav0nic/djangobb_project/get/tip.tar.gz

注:最好到上面的https://bitbucket.org中(JIRA同一家公司开发的类似于github的网站),找到最新版本的包来下载。

下载完成之后,可以通过下面的命令,在根目录下找到下载的包。

  1. ls -l
  2. stable.tar.gz
  3. tip.tar.gz

(2)解压缩和重命名已下载的包

  1. tar zxvf stable.tar.gz
  2. tar zxvf tip.tar.gz

解压后的目录名称与下载的包名不同,而且会很长不方便操作。改个短小的名字,方面操作。我们把:
- stable.tar.gz:解压后的目录改为djangobb-master
- tip.tar.gz:解压后的目录改为djangobb-project

  1. ls -l
  2. mv slav0nic-djangobb-bfcdda967701 djangobb-master
  3. mv slav0nic-djangobb_project-8087a7e42207 djangobb-project

(3)安装默认依赖包

进入到djangobb-master目录

  1. cd djangobb-master

该目录下有三个以requirements开头的txt文件,这些文件中包含所需使用的依赖包。
- requirements.txt
- requirements_optional.txt
- requirements_dev.txt(开发用的依赖,搭建环境用不到)

  1. pip install -r requirements.txt
  2. pip install -r requirements_optional.txt

注:以上是自动安装的方式,这种方式有时可能会因为网络等问题卡住或者出错,所以可以选择提前手动安装。打开requirements.txt,查看里面的依赖包,用 pip install 一条一条执行。

(4)安装其它依赖包

除了requirements中的包以外,还有几个Django依赖的额外的包需要安装。

  1. pip install whoosh
  2. pip install django-mailer
  3. pip install django-nocaptcha-recaptcha
  4. pip install django-pagination-py3
  5. pip install django-static-sitemaps
  6. 或者
  7. pip install whoosh django-mailer django-nocaptcha-recaptcha django-pagination-py3 django-static-sitemaps

安装完成后,可以通过下面命令查看,已安装的包

  1. pip list

不合适的包可以用下面命令删除

  1. pip uninstall <packagename>

(5)创建Django工程

切换到根目录

  1. cd ~
  2. cd /root

创建Django工程,本例中使用的工程名为“luntan”

  1. django-admin startproject luntan

工程创建好之后,可以通过以下命令进入:

  1. cd ~/luntan
  2. cd /root/luntan

注:Django工程创建好之后,会新建一个luntan目录,在该目录下还有一个luntan目录。为了区别,后面在说明的时候,会用 ~/luntan 代表第一层目录,~/luntan/luntan 代表第二层目录。

(6)拷贝资源文件

进入到djangobb-master目录下,将其中的djangobb_forum文件夹复制到~/luntan 目录下

  1. [root@nx-centos7 ~]# cd ~/djangobb-master
  2. [root@nx-centos7 djangobb-master]# cp -r djangobb_forum ~/luntan

进入到djangobb-project/basic_project目录下,其中会包含系统用到的资源文件,将其中的djangobb_index,media,project_static,static,templates和文件forms.py复制到 ~/luntan 目录下

  1. [root@nx-centos7 ~]# cd ~/djangobb-project/basic_project/
  2. [root@nx-centos7 basic_project]# cp -r djangobb_index ~/luntan
  3. [root@nx-centos7 basic_project]# cp -r media ~/luntan
  4. [root@nx-centos7 basic_project]# cp -r project_static ~/luntan/luntan
  5. [root@nx-centos7 basic_project]# cp -r static ~/luntan
  6. [root@nx-centos7 basic_project]# cp -r templates ~/luntan
  7. [root@nx-centos7 basic_project]# cp -r forms.py ~/luntan

(7)拷贝配置文件

djangobb-project/basic_project目录下,除了上一步包含的资源文件外,还包含两个配置文件settings.py和urls.py。将这两个文件拷贝到 ~/luntan/luntan 目录下

  1. [root@nx-centos7 basic_project]# cp -r urls.py ~/luntan/luntan
  2. cp: overwrite ‘~/luntan/luntan/urls.py’? y
  3. [root@nx-centos7 basic_project]# cp -r settings.py ~/luntan/luntan
  4. cp: overwrite ‘~/luntan/luntan/settings.py’? y

(8)修改配置文件

进入到 ~/luntan/luntan 目录下,修改 settings.py 配置文件。部署成功配置文件示例如下:

  1. # -*- coding: utf-8 -*-
  2. import os.path
  3. PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
  4. DEBUG = True
  5. ALLOWED_HOSTS = ['*']
  6. ADMINS = (
  7. # ('Your Name', 'your_email@domain.com'),
  8. )
  9. MANAGERS = ADMINS
  10. DATABASES = {
  11. 'default': {
  12. 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  13. 'NAME': os.path.join(PROJECT_ROOT,'db.sqlite3'), # Or path to database file if using sqlite3.
  14. 'USER': '', # Not used with sqlite3.
  15. 'PASSWORD': '', # Not used with sqlite3.
  16. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
  17. 'PORT': '', # Set to empty string for default. Not used with sqlite3.
  18. }
  19. }
  20. # Local time zone for this installation. Choices can be found here:
  21. # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
  22. # although not all choices may be available on all operating systems.
  23. # If running in a Windows environment this must be set to the same as your
  24. # system time zone.
  25. TIME_ZONE = 'Asia/Shanghai'
  26. USE_TZ = True
  27. # Language code for this installation. All choices can be found here:
  28. # http://www.i18nguy.com/unicode/language-identifiers.html
  29. LANGUAGE_CODE = 'zh-cn'
  30. LANGUAGES = (
  31. ('ca', 'Catalan'),
  32. ('cs', 'Czech'),
  33. ('de', 'German'),
  34. ('en', 'English'),
  35. ('es', 'Spanish'),
  36. ('fo', 'Faroese'),
  37. ('fr', 'France'),
  38. ('it', 'Italian'),
  39. ('lt', 'Lithuanian'),
  40. ('mn', 'Mongolian'),
  41. ('nl', 'Dutch'),
  42. ('pl', 'Polish'),
  43. ('ru', 'Russian'),
  44. ('uk_UA', 'Ukrainian'),
  45. ('vi', 'Vietnamese'),
  46. ('zh_CN', 'Chinese'),
  47. )
  48. SITE_ID = 1
  49. # If you set this to False, Django will make some optimizations so as not
  50. # to load the internationalization machinery.
  51. USE_I18N = True
  52. # If you set this to False, Django will not format dates, numbers and
  53. # calendars according to the current locale
  54. USE_L10N = True
  55. # Absolute filesystem path to the directory that will hold user-uploaded files.
  56. # Example: "/home/media/media.lawrence.com/media/"
  57. MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
  58. # URL that handles the media served from MEDIA_ROOT. Make sure to use a
  59. # trailing slash.
  60. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
  61. MEDIA_URL = '/media/'
  62. # Absolute path to the directory static files should be collected to.
  63. # Don't put anything in this directory yourself; store your static files
  64. # in apps' "static/" subdirectories and in STATICFILES_DIRS.
  65. # Example: "/home/media/media.lawrence.com/static/"
  66. STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
  67. # URL prefix for static files.
  68. # Example: "http://media.lawrence.com/static/"
  69. STATIC_URL = '/static/'
  70. # Additional locations of static files
  71. STATICFILES_DIRS = (
  72. os.path.join(PROJECT_ROOT,'project_static'),
  73. )
  74. # List of finder classes that know how to find static files in
  75. # various locations.
  76. STATICFILES_FINDERS = (
  77. 'django.contrib.staticfiles.finders.FileSystemFinder',
  78. 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
  79. # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
  80. )
  81. # Make this unique, and don't share it with anybody.
  82. if not hasattr(globals(), 'SECRET_KEY'):
  83. SECRET_FILE = os.path.join(PROJECT_ROOT, 'secret.txt')
  84. try:
  85. SECRET_KEY = open(SECRET_FILE).read().strip()
  86. except IOError:
  87. try:
  88. from random import choice
  89. import string
  90. symbols = ''.join((string.lowercase, string.digits, string.punctuation ))
  91. SECRET_KEY = ''.join([choice(symbols) for i in range(50)])
  92. secret = file(SECRET_FILE, 'w')
  93. secret.write(SECRET_KEY)
  94. secret.close()
  95. except IOError:
  96. raise Exception('Please create a %s file with random characters to generate your secret key!' % SECRET_FILE)
  97. MIDDLEWARE_CLASSES = (
  98. 'django.middleware.cache.UpdateCacheMiddleware',
  99. 'django.middleware.common.CommonMiddleware',
  100. 'django.contrib.sessions.middleware.SessionMiddleware',
  101. 'django.middleware.csrf.CsrfViewMiddleware',
  102. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  103. 'django.contrib.messages.middleware.MessageMiddleware',
  104. 'django.middleware.locale.LocaleMiddleware',
  105. 'django.middleware.cache.FetchFromCacheMiddleware',
  106. 'djangobb_forum.middleware.LastLoginMiddleware',
  107. 'djangobb_forum.middleware.UsersOnline',
  108. 'djangobb_forum.middleware.TimezoneMiddleware',
  109. )
  110. ROOT_URLCONF = 'luntan.urls'
  111. TEMPLATES = [
  112. {
  113. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  114. 'DIRS': [os.path.join(PROJECT_ROOT, 'templates')],
  115. 'OPTIONS': {
  116. 'context_processors': ['django.contrib.auth.context_processors.auth',
  117. 'django.template.context_processors.debug',
  118. 'django.template.context_processors.i18n',
  119. 'django.template.context_processors.media',
  120. 'django.template.context_processors.static',
  121. 'django.template.context_processors.request',
  122. 'django.contrib.messages.context_processors.messages',
  123. 'django_messages.context_processors.inbox',
  124. 'djangobb_forum.context_processors.forum_settings',
  125. ],
  126. 'loaders': [
  127. 'django.template.loaders.filesystem.Loader',
  128. 'django.template.loaders.app_directories.Loader',
  129. 'django.template.loaders.eggs.Loader',
  130. ]
  131. },
  132. },
  133. ]
  134. INSTALLED_APPS = (
  135. 'django.contrib.auth',
  136. 'django.contrib.contenttypes',
  137. 'django.contrib.sessions',
  138. 'django.contrib.sites',
  139. 'django.contrib.messages',
  140. 'django.contrib.staticfiles',
  141. 'django.contrib.sitemaps',
  142. 'django.contrib.admin',
  143. 'django.contrib.admindocs',
  144. 'django.contrib.humanize',
  145. 'allauth',
  146. 'allauth.account',
  147. 'allauth.socialaccount',
  148. 'allauth.socialaccount.providers.openid',
  149. 'allauth.socialaccount.providers.google',
  150. 'allauth.socialaccount.providers.github',
  151. 'allauth.socialaccount.providers.twitter',
  152. 'haystack',
  153. 'django_messages',
  154. 'nocaptcha_recaptcha',
  155. 'djangobb_forum',
  156. )
  157. # A sample logging configuration. The only tangible logging
  158. # performed by this configuration is to send an email to
  159. # the site admins on every HTTP 500 error when DEBUG=False.
  160. # See http://docs.djangoproject.com/en/dev/topics/logging for
  161. # more details on how to customize your logging configuration.
  162. LOGGING = {
  163. 'version': 1,
  164. 'disable_existing_loggers': False,
  165. 'filters': {
  166. 'require_debug_false': {
  167. '()': 'django.utils.log.RequireDebugFalse'
  168. }
  169. },
  170. 'handlers': {
  171. 'mail_admins': {
  172. 'level': 'ERROR',
  173. 'filters': ['require_debug_false'],
  174. 'class': 'django.utils.log.AdminEmailHandler'
  175. }
  176. },
  177. 'loggers': {
  178. 'django.request': {
  179. 'handlers': ['mail_admins'],
  180. 'level': 'ERROR',
  181. 'propagate': True,
  182. },
  183. }
  184. }
  185. try:
  186. import mailer
  187. INSTALLED_APPS += ('mailer',)
  188. EMAIL_BACKEND = "mailer.backend.DbBackend"
  189. except ImportError:
  190. pass
  191. FORCE_SCRIPT_NAME = ''
  192. # Haystack settings
  193. HAYSTACK_CONNECTIONS = {
  194. 'default': {
  195. 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
  196. 'PATH': os.path.join(PROJECT_ROOT, 'djangobb_index'),
  197. 'INCLUDE_SPELLING': True,
  198. },
  199. }
  200. HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
  201. # Account settings
  202. ACCOUNT_ACTIVATION_DAYS = 10
  203. LOGIN_REDIRECT_URL = '/forum/'
  204. LOGIN_URL = '/forum/account/signin/'
  205. AUTHENTICATION_BACKENDS = (
  206. 'django.contrib.auth.backends.ModelBackend',
  207. 'allauth.account.auth_backends.AuthenticationBackend',
  208. )
  209. # Cache settings
  210. CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
  211. # Allauth
  212. ACCOUNT_LOGOUT_ON_GET = True
  213. ACCOUNT_EMAIL_REQUIRED = True
  214. ACCOUNT_SIGNUP_FORM_CLASS = 'forms.SignupForm'
  215. try:
  216. from local_settings import *
  217. except ImportError:
  218. pass

主要修改内容:

1、修改数据库

为了方便,本例中使用的是sqlite3数据库,如果在安装过程发现生成有问题,应该是python环境中没有sqlite,可以提前用命令进行安装。

  1. yum install sqlite-devel

修改内容

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  4. 'NAME': os.path.join(PROJECT_ROOT,'db.sqlite3'), # Or path to database file if using sqlite3.
  5. 'USER': '', # Not used with sqlite3.
  6. 'PASSWORD': '', # Not used with sqlite3.
  7. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
  8. 'PORT': '', # Set to empty string for default. Not used with sqlite3.
  9. }
  10. }

在ENGINE属性中,增加数据库类型,然后补充其他属性。如果是使用sqlite,在NAME属性中,指定数据库生成的路径和文件名,如上例。

2、修改时区和国际化

  1. # Local time zone for this installation. Choices can be found here:
  2. # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
  3. # although not all choices may be available on all operating systems.
  4. # If running in a Windows environment this must be set to the same as your
  5. # system time zone.
  6. TIME_ZONE = 'Asia/Shanghai'
  7. USE_TZ = True
  8. # Language code for this installation. All choices can be found here:
  9. # http://www.i18nguy.com/unicode/language-identifiers.html
  10. LANGUAGE_CODE = 'zh-cn'

3、修改ROOT URL

  1. ROOT_URLCONF = 'luntan.urls'

这里的 luntan 就是我们用Django建立的工程名。

4、访问安全控制

增加下面属性

  1. ALLOWED_HOSTS = ['*']

“*”代表全部,也可以指定具体IP。

(9)执行命令

以上配置完成之后,就可以执行相关的命令,进行部署。进入到 ~/luntan 目录下,这个目录下有一个 manage.py 文件,这个文件是所有命令执行的主体。该文件包含很多可执行的命令,可以通过下面命令来查看:

  1. ./manage.py --help

我们下面执行的命令只是一些必须的命令:

1、
切换到~/luntan/djangobb_forum目录下,在终端中输入如下指令编译message文件

  1. cd ~/luntan/djangobb_forum
  2. django-admin compilemessages

2、切换到~/luntan目录下

  1. ./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目录,执行以下两个命令:

  1. ./manage.py collectstatic
  2. ./manage.py createsuperuser

(10)运行系统

  1. ./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

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读

    相关 DjangoBB论坛

    DjangoBB论坛,是基于Django(Python经典框架)进行修改,其安装部署需要依赖于python和Django。很多资料上都是说明采用python的virtualen