discourse社区国内/国外服务器搭建全指南(全网最细)

:kissing_heart:本文由 @林青枫 撰写,部分素材来自discourse社区文档,发布于Linux do 和starorigin.net,转载请注明作者。

在云端轻松部署 Discourse,30 分钟内搞定,无需 Rails 或 Linux shell 等高深的知识。这些步骤适用于任何兼容 Docker 的云服务提供商或本地服务器。本指南将详细介绍以下步骤:

准备工作

  1. 准备您的域名
  2. 设置电子邮件服务(smtp)
    国内强烈推荐飞书国际版,每个月15000封额度。

安装

  1. 创建新的云服务器
  2. 访问您的云服务器
  3. 安装 Docker 的先决条件(以 Debian/Ubuntu 为例)
  4. 安装 Discourse
  5. 编辑 Discourse 配置
  6. 启动 Discourse
  7. 注册新账户并成为管理员
  8. 安装后维护
  9. (可选) 添加更多 Discourse 功能

:bell: (大款可选择)如果您没有 30 分钟来完成设置?只需一次性支付 150 美元,社区即可为您在云端安装 Discourse。点击此处购买自助式社区安装服务

准备工作

1. 准备您的域名

:bell: Discourse 无法通过 IP 地址工作,您必须拥有一个域名(如 example.com)才能继续。

  • 已经拥有域名?太棒了。

  • 没有域名?赶紧注册一个!我们推荐 NameCheap,当然还有许多其他优秀的域名注册商可供选择。

  • 您应能从购买域名的平台访问 DNS 控制面板。您将在此处为 discourse.example.com 主机名创建一个 DNS A 记录,一旦您知道安装 Discourse 的云服务器的 IP 地址,并输入您的电子邮件的 SPF 和 DKIM 记录

在此步之前,你必须配置好smtp服务。

设置飞书邮箱

一:启用电子邮件服务

  1. 访问管理控制台:

  2. 导航至邮箱设置:

  3. 输入您的域名:

    • 在弹出的窗口中,输入您的域名。
    • 点击 提交 按钮。
  4. 选择“启用新的邮箱服务”:

    • 选择启用新邮箱服务的选项。
  5. 配置 DNS 记录:

    • 按照说明配置您域名的 DNS 记录。
    • 转到您的域名管理(例如,Cloudflare)。
    • 添加飞书显示的必要 DNS 记录。
  6. 验证 DNS 记录:

    • 点击飞书设置页面上的 立即验证 按钮。
      • 如果验证失败,请仔细检查记录并稍等片刻。
    • 验证通过后,点击 下一步
  7. 配置邮箱入口和地址:

    • 点击 立即配置 按钮。
    • 在弹出的窗口中,点击 添加(通常是一个红色的按钮或链接以添加邮箱)。
  8. 为成员分配邮箱:

    • 在成员详情窗口中,找到企业邮箱字段。
    • 输入企业成员的邮箱地址,然后保存。
  9. 完成设置:

    • 返回到“配置邮箱入口和企业邮箱地址”页面。
    • 点击 完成 以完成设置。

二:配置用于 SMTP 的公共邮箱

  1. 访问公共邮箱设置:
    • 在飞书管理控制台中,导航至 产品设置邮箱公共邮箱
  2. 创建新的公共邮箱:
    • 点击 创建公共邮箱
  3. 配置公共邮箱:
    • 输入所需的 名称地址
    • 启用 IMAP/SMTP 服务
    • 点击 创建
  4. 查看 IMAP/SMTP 设置:
    • 创建邮箱后,在列表中找到它并点击 编辑
    • 记下提供的 IMAP/SMTP 配置信息。

2. 对照检查电子邮件设置要求

:warning: 电子邮件对于 Discourse 中的账户创建和通知至关重要。 如果您在引导启动前未正确配置电子邮件,您的网站将会出现故障!

:bulb: 此处的电子邮件指的是企业电子邮件,而不是像 Gmail、Outlook 或 Yahoo 这样的常用电子邮件服务。

  • 没有现有的邮件服务器?请查看我们为 Discourse 推荐的电子邮件提供商

  • 已经有邮件服务器?太好了。使用您现有的邮件服务器凭据。(像 Gmail/Outlook/Yahoo 这样的免费电子邮件服务不支持smtp企业服务。)

  • 为确保邮件的可送达性,您必须在 DNS 中添加有效的 SPF 和 DKIM 记录。您需要从电子邮件提供商处获取 SMTP 凭据,包括 SMTP 用户名和密码。登录您的电子邮件提供商的账户,转到 SMTP 设置或电子邮件 API 部分,找到/生成您唯一的 SMTP 凭据。请妥善保管,因为您将在 Discourse 配置期间使用它们。有关具体信息,请参阅您的邮件提供商的说明。

  • 如果您在使电子邮件正常工作时遇到问题,请遵循我们的电子邮件故障排除指南

安装

3. 创建新的云服务器

创建您的新云服务器:

  • 当前支持的 Ubuntu Server LTS 版本的默认设置即可。最低要求是具有现代内核版本的 64 位 Linux 操作系统。

  • 1 GB RAM 的默认设置对于小型 Discourse 社区来说足够了。我们建议较大的社区使用 2 GB RAM。实际上discourse社区的插件数量非常多,功能非常强大,2GB内存是底线。

  • 香港、新加坡国内尽量选离大陆近距离的。

创建您服务器设置 SSH 密钥,更安全。

:warning: 现在您已经创建了您的云服务器!返回您的 DNS 控制面板,并使用 IP 地址为您的 discourse.example.com 主机名设置一个 A 记录

4. 访问您的云服务器

使用 SSH 通过其 IP 地址连接到您的服务器,或者在 Windows 上使用 Putty

ssh [email protected]

使用服务器设置时发送给您的电子邮件中的 root 密码,或者在您的本地计算机上配置有效的 SSH 密钥。

5. 安装 Docker 的先决条件(以 Debian/Ubuntu 为例)

sudo apt install docker.io
sudo apt install git

6. 安装 Discourse

官方 Discourse Docker 镜像 克隆到 /var/discourse

sudo -s
git clone https://github.com/discourse/discourse_docker.git /var/discourse
cd /var/discourse
chmod 700 containers

在剩余的设置和引导启动过程中,您需要以 root 用户身份操作。

7. 编辑 Discourse 配置

启动简便安装工具(不推荐)
这种方法是官方提供的简便安装,但是只是搭建了一个论坛的壳子,后续还要返工十几次,可能才能达到你想要的效果。

./discourse-setup

当提示时,回答以下问题:

回答域名:Hostname for your Discourse? [discourse.example.com]: 
回答默认管理员邮箱,多个邮箱由逗号隔开:Email address for admin account(s)? [[email protected],[email protected]]: 
SMTP服务商地址:SMTP server address? [smtp.example.com]: 
端口:SMTP port? [587]: 
默认发信邮箱:SMTP user name? [[email protected]]: 
默认给你的发信邮箱密码:SMTP password? [pa$$word]: 
跳过去:Let's Encrypt account email? (ENTER to skip) [[email protected]]: 
跳过去:Optional Maxmind License key () [xxxxxxxxxxxxxxxx]:

您将从您的电子邮件设置中获取 SMTP 详细信息,请务必完成该部分。

Let’s Encrypt 账户设置是为了给您的网站提供免费的 HTTPS 证书,如果您希望您的网站安全,请务必进行设置。这一步直接用cloudflare的免费证书。

这将为您生成一个 app.yml 配置文件,然后启动引导启动。引导启动需要 2-8 分钟 来设置您的 Discourse。如果您需要在引导启动后更改这些设置,您可以再次运行 ./discourse-setup(它将重用文件中以前的值),或者使用 nano 手动编辑 /containers/app.yml,然后运行 ./launcher rebuild app

相信我,你以后打交道最多的文件就是:

nano /var/discourse/containers/app.yml

使用app.yml 文件建立容器部署discourse——一步到位

安装 1panel (后续配置反向代理用)

运行命令,1panel会自动添加docker镜像,所以后面不需要再添加。

curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sudo bash quick_start.sh`

cd /var/discourse/containers
nano app.yml

输入以下内容,里面要设置好你的各种配置


## this is the all-in-one, standalone Discourse Docker container template
##
## After making changes to this file, you MUST rebuild
## /var/discourse/launcher rebuild app
##
## BE *VERY* CAREFUL WHEN EDITING!
## YAML FILES ARE SUPER SUPER SENSITIVE TO MISTAKES IN WHITESPACE OR ALIGNMENT!
## visit http://www.yamllint.com/ to validate this file as needed

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  ## Uncomment the next line to enable the IPv6 listener
  #- "templates/web.ipv6.template.yml"
  - "templates/web.ratelimited.template.yml"
  ## Uncomment these two lines if you wish to add Lets Encrypt (https)
  #- "templates/web.ssl.template.yml"
  #- "templates/web.letsencrypt.ssl.template.yml"
  #- "templates/cloudflare-static.template.yml"
## which TCP/IP ports should this container expose?
## If you want Discourse to share a port with another webserver like Apache or nginx,
## see https://meta.discourse.org/t/17247 for details
expose:
  - "8080:80"   # http
# - "8443:443" # https,你选择暴露的容器端口,不懂就不要动。

params:
  db_default_text_search_config: "pg_catalog.english"

  ## Set db_shared_buffers to a max of 25% of the total memory.
  ## will be set automatically by bootstrap based on detected RAM, or you can override
  db_shared_buffers: "768MB"

  ## can improve sorting performance, but adds memory usage per-connection
  #db_work_mem: "40MB"

  ## Which Git revision should this container use? (default: tests-passed)
  #version: tests-passed

env:
  LC_ALL: en_US.UTF-8
  LANG: en_US.UTF-8
  LANGUAGE: en_US.UTF-8
  # DISCOURSE_DEFAULT_LOCALE: en

  ## How many concurrent web requests are supported? Depends on memory and CPU cores.
  ## will be set automatically by bootstrap based on detected CPUs, or you can override
  UNICORN_WORKERS: 4  

  ## TODO: The domain name this Discourse instance will respond to
  ## Required. Discourse will not work with a bare IP number.
  DISCOURSE_HOSTNAME: 'starorigin.net'

  ## Uncomment if you want the container to be started with the same
  ## hostname (-h option) as specified above (default "$hostname-$config")
  #DOCKER_USE_HOSTNAME: true

  ## TODO: List of comma delimited emails that will be made admin and developer
  ## on initial signup example '[email protected],[email protected]'
  DISCOURSE_DEVELOPER_EMAILS: '[email protected],[email protected]'
  #在上面设置好你的管理员邮箱。

  ## TODO: The SMTP mail server used to validate new accounts and send notifications
  # SMTP ADDRESS, username, and password are required
  # WARNING the char '#' in SMTP password can cause problems!
  DISCOURSE_SMTP_ADDRESS: smtp.larksuite.com  #smtp服务器地址,默认飞书
  DISCOURSE_SMTP_PORT: 587  #smtp服务器端口
  DISCOURSE_SMTP_USER_NAME: [email protected]   #默认发信邮箱名字
  DISCOURSE_SMTP_PASSWORD: abcdef   #飞书系统默认的smtp密码,一定要填正确。
  DISCOURSE_SMTP_ENABLE_START_TLS: true           # (optional, default true)
  #DISCOURSE_SMTP_DOMAIN: discourse.example.com    # (required by some providers)
  DISCOURSE_NOTIFICATION_EMAIL: [email protected]    # (默认通知邮箱的地址)

  ## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
  #LETSENCRYPT_ACCOUNT_EMAIL: [email protected]

  ## The http or https CDN address for this Discourse instance (configured to pull)
  ## see https://meta.discourse.org/t/14857 for details
  #DISCOURSE_CDN_URL: https://discourse-cdn.example.com

  ## The maxmind geolocation IP account ID and license key for IP address lookups
  ## see https://meta.discourse.org/t/-/173941 for details
  #DISCOURSE_MAXMIND_ACCOUNT_ID: 123456
  #DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456

## The Docker container is stateless; all data is stored in /shared
volumes:
  - volume:
      host: /var/discourse/shared/standalone
      guest: /shared
  - volume:
      host: /var/discourse/shared/standalone/log/var-log
      guest: /var/log

## Plugins go here
## see https://meta.discourse.org/t/19157 for details
hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git
          - git clone https://github.com/discourse/discourse-gamification.git
          - git clone https://github.com/discourse/discourse-saved-searches.git
          - git clone https://github.com/discourse/discourse-category-experts.git
          - git clone https://github.com/discourse/discourse-tooltips.git
          - git clone https://github.com/discourse/discourse-bbcode.git
          - git clone https://github.com/discourse/discourse-follow.git
          - git clone https://github.com/discourse/discourse-yearly-review.git
          - git clone https://github.com/discourse/discourse-doc-categories.git
          - git clone https://github.com/discourse/discourse-cakeday.git
          - git clone https://github.com/discourse/discourse-signatures.git
          - git clone https://github.com/discourse/discourse-reactions.git
          - git clone https://github.com/discourse/discourse-math.git
          - git clone https://github.com/discourse/discourse-calendar.git
          - git clone https://github.com/discourse/discourse-ai.git
          - git clone https://github.com/discourse/discourse-post-voting.git
          - git clone https://github.com/discourse/discourse-topic-voting.git
          - git clone https://github.com/discourse/discourse-data-explorer.git
          - git clone https://github.com/scavin/discourse-bilibili-onebox.git
          - git clone https://github.com/discourse/discourse-livestream
          - git clone https://github.com/paviliondev/discourse-journal
          - git clone https://github.com/angusmcleod/discourse-events
          - git clone https://github.com/spirobel/discourse-matheditor.git
          - git clone https://github.com/discourse/discourse-subscriptions
          - git clone https://github.com/discourse/discourse-bcc
          - git clone https://github.com/discourse/discourse-assign
          - git clone https://github.com/paviliondev/discourse-tickets
          - git clone https://github.com/discourse/discourse-surveys
          - git clone https://github.com/discourse/discourse-solved

##上面一大堆是我安装的插件,已经是把95%有用的插件全安装了,记得国内的服务器要看最下面,把github地址改掉。
## Any custom commands to run after building
run:
  - exec: echo "Beginning of custom commands"
  ## If you want to set the 'From' email address for your first registration, uncomment and change:
  ## After getting the first signup email, re-comment the line. It only needs to run once.
  #- exec: rails r "SiteSetting.notification_email='[email protected]'"
  - exec: echo "End of custom commands"

在discourse目录下、建立容器

./launcher bootstrap app

./launcher start app

进入 1panel 配置反向代理

安装OpenResty

跟着我做,配置网站的端口,对应到容器的端口。

到你把网站搭建完了之后,先搞cloud flare,再回来搞这个:

8. 大功告成!启动 Discourse

配置反向代理完成后,您应该可以通过您之前输入的域名 discourse.example.com 在您的 Web 浏览器中访问您的 Discourse。

9. 注册新账户并成为管理员

使用您在默认设置为管理员账户的电子邮件地址之一,注册一个新的管理员账户。

(如果您无法注册您的管理员账户,请检查 /var/discourse/shared/standalone/log/rails/production.log 中的日志,并查看我们的电子邮件故障排除清单。)

注册您的管理员账户后,设置向导将启动并引导您完成 Discourse 的基本配置。

完成设置向导后,一个弹出窗口将欢迎您来到您的新网站。它还将链接到管理员指南,我们强烈建议您立即查看并经常参考。该指南提供了设置清单、关于如何成功启动您的社区的重要指导以及故障排除技巧。

10. 安装后维护

  • 我们强烈建议您为您的操作系统启用自动安全更新。在 Ubuntu 中,使用 dpkg-reconfigure -plow unattended-upgrades 命令。在 CentOS/RHEL 中,使用 yum-cron 包。
  • 如果您使用的是密码而不是 SSH 密钥,请务必强制使用强 root 密码。在 Ubuntu 中,使用 apt install libpam-cracklib 包。我们还建议使用 fail2ban,它会阻止任何尝试超过 3 次密码重试的 IP 地址 10 分钟。
    • Ubuntu: apt install fail2ban
    • CentOS/RHEL: sudo dnf install fail2ban
  • 如果您需要或想要默认防火墙,请为 Ubuntu 启用 ufw,或者为 CentOS/RHEL 使用 firewalld

:bulb: 当新版本的 Discourse 发布时,Discourse 将向您发送电子邮件通知。请保持更新以获取最新的功能和安全修复。

将 Discourse 升级到最新版本,请在您的浏览器中访问 https://discourse.example.com/admin/upgrade 并单击“升级”按钮。
前端直接可以升级系统。
或者,您可以 ssh 进入您的服务器并使用以下命令重建:

cd /var/discourse
git pull
./launcher rebuild app

/var/discourse 文件夹中的 launcher 命令可用于各种维护:

Usage: launcher COMMAND CONFIG [--skip-prereqs] [--docker-args STRING]
Commands:
    start:      Start/initialize a container
    stop:       Stop a running container
    restart:    Restart a container
    destroy:    Stop and remove a container
    enter:      Use nsenter to get a shell into a container
    logs:       View the Docker logs for a container
    bootstrap:  Bootstrap a container for the config based on a template
    rebuild:    Rebuild a container (destroy old, bootstrap, start new)
    cleanup:    Remove all containers that have stopped for > 24 hours

Options:
    --skip-prereqs             Don't check launcher prerequisites
    --docker-args              Extra arguments to pass when running docker

11. (可选) 添加更多 Discourse 功能

您是否想要…

帮助我们改进本指南!请随时在 meta.discourse.org 上提问,或者更好的是,提交一个 pull request。

中国大陆服务器的特殊设置

你的服务器在境内,你按照上面的步骤安装,会发现,哦?怎么安装都报错,是的,无论你想了一万种办法,它都会报错,这不是你的错。我自己第一次用国内的服务器,重建了30多次容器才成功。

1、设置git 镜像

输入以下代码

cd/var/discourse/containers
nano app.yml

把里面所有涉及gitHub.com的地方,全部替换成 github.site

2、配置国内镜像加速(1panel默认配置了)

nano/etc/docker/daemon.json
https://docker.mirrors.ustc.edu.cn 替换为https://docker.1panel.live
如:

    bash
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": https://docker.1panel.live
    }
    EOF

重启docker
sudo systemctl restart docker

  • 验证是否生效:重启 Docker 服务后,可以尝试 docker pull ubuntu 来验证是否生效,如果下载速度显著提升,则说明加速已生效。
  • 使用多个镜像加速器:可以将多个镜像加速器地址放入 registry-mirrors 数组中,Docker 会依次尝试这些加速器。
{
    "registry-mirrors": [
      "https://docker.mirrors.ustc.edu.cn",
      "https://<你的加速器地址>.mirror.aliyuncs.com"
    ],
    "insecure-registries": []
  }

3、开启discourse社区对国内的特别设置

cd/var/discourse/containers
nano app.yml

templates:
  - "templates/postgres.template.yml"
  - "templates/redis.template.yml"
  - "templates/web.template.yml"
  - "templates/web.china.template.yml" # 添加这一行,配置包管理器的镜像

这样一切就大功告成。

服务器卡死???

在Linux系统中设置交换内存

  1. 创建交换文件:

    sudo fallocate -l 2G /swapfile  # 创建一个2GB的交换文件,可以根据需要调整大小
    
    • sudo:以管理员权限执行命令
    • fallocate -l 2G: 创建一个大小为2GB的文件
    • /swapfile:交换文件的路径和名称
  2. 设置文件权限:

    sudo chmod 600 /swapfile  # 只有root用户可以读取和写入
    
  3. 将文件标记为交换文件:

    sudo mkswap /swapfile
    
  4. 启用交换文件:

    sudo swapon /swapfile
    
  5. 永久启用交换:
    为了在系统重启后仍然使用交换文件,需要将它添加到 /etc/fstab 文件中。

    sudo echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
    

6.设置 swappiness 参数: swappiness 参数控制着系统使用交换空间的倾向,取值范围为 0 到 100, 0 代表尽量不使用交换空间,100代表尽量使用交换空间。 国内服务器默认值通常为 0。你可以通过以下命令查看当前值:

cat /proc/sys/vm/swappiness

nano /etc/sysctl.conf

设置
vm.swappiness=10

验证交换内存是否启用

您可以使用以下命令来验证交换内存是否已启用:

sudo swapon -s

或者

free -h
  • 交换空间大小: 一般建议交换空间的大小为物理内存的 1-2 倍。例如,如果您的服务器有 4GB 的 RAM,你可以设置 4-8GB 的交换空间。
  • 调整 swappiness 参数,以减少操作系统使用交换空间的频率(通常在默认情况下,系统会尽量使用内存,而不是硬盘。

之后你要干什么?

针对discourse社区设置 cloudflare
后续有什么不懂的地方,可以私信本人交流。