侧边栏壁纸
博主头像
墨雪飘影博主等级

玫瑰是我偷的,你爱的人是我杀的,不爱你是假的。

  • 累计撰写 55 篇文章
  • 累计创建 16 个标签
  • 累计收到 78 条评论

mooder知识共享平台搭建

墨雪飘影
2024-09-22 / 0 评论 / 0 点赞 / 123 阅读 / 1,605 字
温馨提示:
本文最后更新于 2024-09-22,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

准备服务器

【阿里云】99计划,2核2G,3M固定带宽,40G ESSD Entry盘新老同享服务器,只需99/年,续费同价!
https://www.aliyun.com/daily-act/ecs/activity_selection?userCode=0uqn2sq5

点击购买

【腾讯云】2核2G,4M带宽 50GB SSD盘 300GB月流量云服务器新老同享 99元/年,续费同价,云服务器3年机/5年机限时抢购,低至 2.5折!
https://curl.qcloud.com/5PVw9cHd

点击购买

准备源码与环境

Django 3.x

Python 3.x(部分版本需要手动降低urllib3版本)

下载源码:https://github.com/phith0n/mooder

安装环境pip install requirements.txt

安装依赖(以实际为准)apt-get update && apt-get install libpq-dev libjpeg-dev zlib1g-dev libfreetype6-dev

配置

配置数据库

配置文件:.envsettings_production.py

建议使用.env配置

PostgreSQL配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

MySQL配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

SQLite配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

注意事项

PostgreSQL依赖项

sudo apt-get install postgresql-client
pip install psycopg2

MySQL依赖项

sudo apt-get install mysql-client
pip install mysqlclient

Python>3.9后分页错误

需要修改pure_pagination组件,python大于3.9后需要修改pure_pagination文件,替换遍历方法

collections.Iterable==>collections.abc.Iterable

配置加密密钥

settings.py中有一个SECRET_KEY(加密密钥)配置项,可以直接配置一个值。

SECRET_KEY = 'any random string'

Mooder会从项目根目录读取一个文件“.secretkey”,将其内容作为项目的密钥。

可以使用

sudo apt-get install openssl
openssl rand -out .secretkey 64

来生成一个64位长的密钥。

python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
from django.core.management.utils import get_random_secret_key
secret_key = get_random_secret_key()
print(secret_key)

配置环境变量

默认mooder启动会使用settings.py作为配置文件,为了让其使用我们自定义的配置文件,可以使用

DJANGO_SETTINGS_MODULE=mooder.settings_custom

来指定配置文件,其中值为某py文件。如:settings_production.py

如果不想搞环境变量,也可以在.env中加一行:DJANGO_SETTINGS_MODULE=mooder.settings_production

配置邮箱(*)

邮箱必须配置

.env中配置如下内容

EMAIL_HOST SMTP服务地址
EMAIL_PORT SMTP服务端口
EMAIL_HOST_USER 登录SMTP服务的邮箱。
EMAIL_HOST_PASSWORD 邮箱密码或授权码
EMAIL_USE_SSL 是否使用SSL
EMAIL_USE_TLS 是否使用TLS

其他配置

程序配置

settings_production.py中默认打开debug,建议上线关闭,根据下文替换文件会关闭debug。

ACCOUNT_ACTIVATION_DAYS激活邮件有效期,默认3天。

SITE网站SEO信息

SITE = {
    'title': '安全团队贡献平台',
    'description': '一个团队使用的贡献平台',
    'keyword': '安全,团队,贡献'
}

title为总标题。

管理地址修改

编辑:mooder/urls.py

from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.contrib import admin
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('accounts.urls', namespace='accounts')),
    path('', include('archives.urls', namespace='archive')),
    path('captcha/', include('captcha.urls')),

    path('management/', include('managements.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

其中

url(r'^admin/', admin.site.urls),即为Django自带的后台管理应用,修改这个’^admin/'为任意其他地址即可。

url(r'^management/', include('managements.urls')),也类似,修改management为其他名称即可。

web容器配置(Windows)

在windows下可使用waitress+nginx来配置生成环境

安装waitress:pip install waitresspip3 install waitress

启动命令:waitress-serve --port=9000 mooder.wsgi:application

port为程序端口,mooder.wsgi:application为启动应用程序,勿修改。

nginx配置如下,可直接替换文件。

worker_processes  1;

events {
  worker_connections  1024;
}

http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  upstream src {
    server 127.0.0.1:9000;
  }
  server {
    listen 80;
    server_name wiki.shikangsi.com;
    location / {
      # 301 跳转到https带上url参数$request_uri;
      return 301 https://$server_name$request_uri;
    }
  }
  server {
    listen 443 ssl;
    server_name  wiki.shikangsi.com;
    location / {
      proxy_pass http://src;
      proxy_set_header HOST $host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    #静态文件配置
    location /static/ {
      alias  C:/mooder/static_cdn/;
    }
    location /media/ {
      alias  C:/mooder/media_cdn/;
    }
    location /article/images/ {
      alias  C:/images/;
    }
    #SSL配置
    #从腾讯云获取到的第一个文件的全路径
    ssl_certificate ../cert/wiki.shikangsi.com.crt;
    #从腾讯云获取到的第二个文件的全路径
    ssl_certificate_key ../cert/wiki.shikangsi.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }
  }

}

根据实际情况修改即可,上文配置的证书文件位于nginx目录下的cert内,cert文件夹自行新建。

Linux配置

Linux的配置比较简单,可用uwsgi或其他支持的web容器启动,可自行百度。

更多配置

本程序配置项见mooder/settings.pymooder/settings_production.py

Django配置项见https://docs.djangoproject.com/en/dev/ref/settings/

初始化服务

初始化数据库

python3 ./manage.py migratepython ./manage.py migrate

移动静态文件

python3 ./manage.py collectstaticpython ./manage.py collectstatic

初始化/创建管理员

python3 manage.py createsuperuserpython manage.py createsuperuser

修改文件

下载修改后的文件,除.env外全部替换。

测试环境启动

python3 manage.py runserverpython manage.py runserver即可启动。

若需要修改端口,可使用:python3 manage.py runserver localhost:端口python manage.py runserver localhost:端口

生产环境启动

启动服务

  1. 启动nginx
  2. 启动waitress

初始化分类

登录管理员后台http(s)://yoursite.com/admin/添加分类即可。

示例

https://src.sjtu.edu.cn

null

https://wiki.shikangsi.com

null

参考资料

https://phith0n.github.io/mooder/

https://github.com/phith0n/mooder

https://github.com/cnskis/mooder_next

https://docs.djangoproject.com/en/dev/ref/settings/

https://cloud.tencent.com/developer/article/1549684?from=15425

0

评论区