回到主页

Discourse部署连接外部PostgreSQL

整合Nginx并配置阿里云邮箱

· discourse,论坛建设,PostgreSQL,Nginx,Docker

Discourse的安装现在使用了Docker环境,在很大程度上方便了站长。

我们用的Ubuntu16.04 x64位服务器进行的搭建,该环境之前已经安装过Docker,在安装Discourse时被要求升级Docker版本,最低要求是17.02

Docker升级完成后会自动启动那些配置成自运行的容器,个别需要手动启动的容器也别忘记启动;之后排查了一下业务容器,都没什么问题,Docker生态确实友好强大。

app.yml

从sample目录下拷贝standalone模板至containers/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/sshd.template.yml"
  - "templates/web.template.yml"
  - "templates/web.ratelimited.template.yml"
  - "templates/web.socketed.template.yml"  # <-- Added
## Uncomment these two lines if you wish to add Lets Encrypt (https)
  #- "templates/web.ssl.template.yml"
  #- "templates/web.letsencrypt.ssl.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:

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: "256MB"

  ## 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:
  LANG: en_US.UTF-8
  #DISCOURSE_DEFAULT_LOCALE: zh_CN

  ## 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: 2

  DISCOURSE_DB_HOST: 127.0.0.1
  DISCOURSE_DB_PORT: 5432
  DISCOURSE_DB_USERNAME: postgres
  DISCOURSE_DB_PASSWORD: password
  DISCOURSE_DB_NAME: discourse

  ## TODO: The domain name this Discourse instance will respond to
  DISCOURSE_HOSTNAME: www.xxx.com

  ## 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 'user1@example.com,user2@example.com'
  DISCOURSE_DEVELOPER_EMAILS: 'xxx@amzport.com'

  ## TODO: The SMTP mail server used to validate new accounts and send notifications
  DISCOURSE_SMTP_ADDRESS: smtp.mxhichina.com
  DISCOURSE_SMTP_PORT: 80
  DISCOURSE_SMTP_USER_NAME: xxx@amzport.com
  DISCOURSE_SMTP_PASSWORD: "password"
  DISCOURSE_SMTP_AUTHENTICATION: login
  DISCOURSE_SMTP_OPENSSL_VERIFY_MODE: none
  DISCOURSE_SMTP_ENABLE_START_TLS: true           # (optional, default true)

  ## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
  #LETSENCRYPT_ACCOUNT_EMAIL: me@example.com

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

## 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

## 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='info@unconfigured.discourse.org'"
  - exec: echo "End of custom commands"

上面实现了阿里云企业邮箱的连接配置,本地Postgresql数据库的连接配置 和 Nginx的串联。

最开始的templates里注释了postgresql的模板,引入了socket模板,去掉了端口映射等

PostgreSQL配置

我们的环境有个特殊,PostgreSQL数据库也是docker的一个容器实例,为了安全起见只绑定了本地127.0.0.1地址。

在宿主机上安装postgresql-client包,并连接server端,创建一个空白的数据库实例,并安装需要的扩展。

rebuild app || discourse-setup

之前由于配置问题踩了很多坑,所以没有用discourse-setup一步到位,使用了rebuild app重新构建

./launcher rebuild app --docker-args --net=host --skip-mac-address

上面 --net=host 这些docker参数主要是因为我们的PostgreSQL配置在同一台机器的docker容器里,而且只允许127.0.0.1地址访问;而discourse也跑在容器里,默认情况下它的127.0.0.1是访问的容器里的独立网络环境。此处的--net=host参数使得discourse容器直接使用宿主机的网络环境,成功连接了PostgreSQL数据库

激活管理员帐号

即使阿里云企业邮箱的连接信息配置正确,最开始需要激活的管理员帐号却还是收不到激活邮件。app.yml里面开启了ssh控制就是为了应对这一情况。

简单概括就是用ssh进入discourse容器,去激活管理员帐号,并进入网页管理后台配置notification邮箱地址为同一个云邮箱。后续新用户的邮件收发都没有问题了。

其他的配置操作都可以用管理员帐号登录并配置完成了。