如何直接从Discourse源代码裸建论坛网站?

发布于 2021-03-30 02:26

Discourse是介于Ruby On Rails框架开发的一个现代论坛系统,是在github上排名最高的开源论坛系统,星数达到32.9k+,是ruby领域排名第三的互联网应用平台,紧随rails和jekyll之后。尽管如此受欢迎,但由于它的结构组成特别复杂,横跨很多系统,所以使用者很难直接从ruby源代码直接在自己的服务器直接构建全裸的运行系统,往往都直接使用Discourse官方提供的docker容器镜像来构建论坛系统,这样就能便捷地构建一个论坛网站。但在享受docker镜像方案所带来的便利的同时,也导致必须受限于docker镜像模式的不利之处。问题主要在于,当根据特定需要改动docker下的Disourse的ruby源代码后,一旦升级docker镜像,就会导致此前更改的源代码全部被覆盖丢失,只能在新的docker内,再次重新改写源代码。除此之外,也不能实时跟进github源的最新发布版本。
因此,为了支持大家摆脱docker镜像方案限制,在服务器上从零开始,从源代码开始自建一个Discourse论坛,把我的成功实现方法在此分享。

步骤: 

一.操作系统CentOS Linux 8
   1.去CentOS网站下载最新镜像文件, 制作ISO启动U盘镜像, 通过U盘安装,在此不再赘述。
   2.安装运行discourse所依赖的运行库
     [root@~]# dnf install optipng pngquant jhead jpegoptim gifsicle
   3.创建系统运行账户
     [root@~]# groupadd -g 10000 www
     [root@~]# useradd -g www discourse

二.安装数据库PostgresQL 12
  1.在浏览器中打开网页https://www.postgresql.org/download/linux/redhat
    根据页面配置提示, 生成rpm安装资源
    1.Select version:
    12 
    2.Select platform: 
    RedHat Enterprise, CentOS, Scientific or Oracle version 8
    3.Select architecture: 
    x86_64
    4.Install the repository RPM:
    dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    5.Disable the built-in PostgreSQL module
    dnf -qy module disable postgresql
    6.Install the client packages:
    dnf install postgresql12
    7.Optionally install the server packages:
    dnf install postgresql12-server
    8.Optionally initialize the database and enable automatic start:
    /usr/pgsql-12/bin/postgresql-12-setup initdb
    systemctl enable postgresql-12
    systemctl start postgresql-12
    在Linux终端中依次复制再粘贴执行上述从4到8的指令
  2.配置数据库环境变量
    [root@~]# vim /etc/profile
    PATH=/usr/pgsql-12/bin:$PATH
    :wq
  3.使环境变量生效
    [root@~]# source /etc/profile
  4.创建即将用于discourse的数据库和数据库账户
    [root@~]# su - postgres
    [postgres@~]$ mkdir /var/lib/pgsql/12/elysium_space
    [postgres@~]$ psql
    CREATE ROLE elysium_user LOGIN PASSWORD 'sdfj&532@#$%75%^' VALID UNTIL 'infinity';
    CREATE TABLESPACE elysium_space LOCATION '/var/lib/pgsql/12/elysium_space';
    GRANT ALL PRIVILEGES ON TABLESPACE elysium_space TO elysium_user;
    CREATE DATABASE elysium_db WITH ENCODING='UTF8' TABLESPACE=elysium_space CONNECTION LIMIT=-1;
    GRANT ALL PRIVILEGES ON DATABASE elysium_db TO elysium_user;
    \c elysium_db;
    CREATE EXTENSION hstore;
    CREATE EXTENSION pg_trgm;
    \q
    [postgres@~]$ exit
  5.配置数据库账户认证方式,在配置文件pg_hba.conf中将所有主机的网络访问本地配置行注释掉, 再加入一行有关用户elysium_user的访问认证为md5
    [root@~]# vim /var/lib/pgsql/12/data/pg_hba.conf
# IPv4 local connections:
    #host    all             all             127.0.0.1/32            ident
    # IPv6 local connections:
    #host    all             all             ::1/128                 ident

    host    elysium_db            elysium_user  127.0.0.1/32    md5
    :wq
  6.重启数据库服务
    [root@~]# systemctl restart postgresql-12

三.安装Redis
  1.在浏览器中打开网页https://redis.io/download,找到最新版下载链接
    [root@~]# wget http://download.redis.io/releases/redis-5.0.8.tar.gz
    [root@~]# tar zxvf redis-5.0.8.tar.gz
    [root@~]# cd redis-5.0.8/src/deps
    [root@~]# make hiredis jemalloc linenoise lua
    [root@~]# cd ..
    [root@~]# mkdir -p /opt/redis/5.0.8
    [root@~]# make distclean && make && make PREFIX=/opt/redis/5.0.8 install
    [root@~]# ./utils/install_server.sh 
    Welcome to the redis service installer
    This script will help you easily set up a running redis server

    Please select the redis port for this instance: [6379] 
    Selecting default: 6379
    Please select the redis config file name [/etc/redis/6379.conf] 
    Selected default - /etc/redis/6379.conf
    Please select the redis log file name [/var/log/redis_6379.log] 
    Selected default - /var/log/redis_6379.log
    Please select the data directory for this instance [/var/lib/redis/6379] 
    Selected default - /var/lib/redis/6379
    Please select the redis executable path [] /opt/redis/5.0.8/bin/redis-server
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /opt/redis/5.0.8/bin/redis-server
    Cli Executable : /opt/redis/5.0.8/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/6379.conf => /etc/init.d/redis_6379
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server...
    Installation successful!

四.安装Nginx
  1.在浏览器中打开网页http://nginx.org/en/linux_packages.html#RHEL-CentOS,按照网页中的说明进行安装
    [root@~]# yum install yum-utils
    [root@~]# vim /etc/yum.repos.d/nginx.repo
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true

    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    :wq
       
    [root@~]# yum-config-manager --enable nginx-mainline
    [root@~]# yum install nginx
  2.启动服务
    [root@~]# systemctl enable nginx
    [root@~]# systemctl restart nginx

五.安装RBENV
  1.从github上clone并安装
    [root@~]# cd /opt
    [root@~]# git clone https://github.com/rbenv/rbenv.git
    [root@~]# cd rbenv
    [root@~]# mkdir plugins
    [root@~]# cd plugins
    [root@~]# git clone https://github.com/rbenv/ruby-build.git
[root@~]# cd ruby-build
    [root@~]# PREFIX=/opt/rbenv ./install.sh
  2.设置全局环境变量
    [root@~]# vim /etc/profile.d/rbenv.sh
    export RBENV_ROOT=/opt/rbenv
    export PATH=/opt/rbenv/bin:$PATH
    eval "$(rbenv init -)"
    :wq
  3.使环境变量生效
    [root@~]# source /etc/profile

六.安装ruby
  1.查看ruby资源, 安装最新ruby稳定版
    [root@~]# rbenv install --list
    [root@~]# rbenv install 2.7.0
  2.设置ruby运行时版本
    [root@~]# rbenv versions
    2.7.0
    [root@~]# rbenv global
    system
    [root@~]# rbenv global 2.7.0
    [root@~]# rbenv global
    2.7.0 
    [root@~]# rbenv rehash

七.安装部署Discourse
  1.安装discourse
    [root@~]# mkdir /var/www
    [root@~]# cd /var/www
    [root@~]# git clone https://github.com/discourse/discourse.git
    [root@~]# chown -R discourse.www discourse 
    [root@~]# su discourse 
    [root@~]# cd discourse
    [discourse@~]$ bundle config set deployment 'true'
    [discourse@~]$ bundle config set path 'vendor/bundle'
    [discourse@~]$ bundle install
  2.配置discourse
    [discourse@~]$ config
    [discourse@~]$ cp discourse_defaults.conf discourse.conf
    [discourse@~]$ vim discourse.conf
    # discourse default config

    db_socket = "/var/run/postgresql"
    db_host = "127.0.0.1"
    db_port = 5432
    db_name = "elysium_db"
    db_username = "elysium_user"
    db_password = "sdfj&532@#$%75%^"
    db_prepared_statements = false
    hostname = "discourse.hsdk668899.com"
    smtp_address = "smtp.exmail.qq.com"
    smtp_port = 587
    smtp_domain = "hsdk668899.com"
    smtp_user_name = "notify@hsdk668899.com"
    smtp_password = "ovpadi*&8syf4"
    smtp_authentication = plain
    smtp_enable_start_tls = true
    :wq

  3.初始化数据库
    [discourse@~]$ cd /var/www/discourse
    [discourse@~]$ RAILS_ENV=production bundle exec rake db:migrate 
  4.预编译资源
    先调整不支持的参数
    [discourse@~]$ cp lib/tasks/assets.rake lib/tasks/assets.rake.bak
    [discourse@~]$ vim lib/tasks/assets.rake 
    注释掉 brotli(path, max_compress), 禁用Brotili压缩, 因为Gzip和Brotili同时使用时,压缩JavaScript文件时发生错误 
    :wq
    [discourse@~]$ RAILS_ENV=production bundle exec rake assets:precompile
    压缩完成后恢复文件
    [discourse@~]$ cp lib/tasks/assets.rake.bak lib/tasks/assets.rake
    注释掉lib/letter_avatar.rb
    # Digest::MD5.hexdigest(`convert --version` << `convert -list font`)
  5.自定义puma服务文件
    [discourse@~]$ vim puma.rb
    将APP_ROOT = '/home/discourse/discourse' 替换为 APP_ROOT = '/var/www/discourse'
    :wq
  6.创建puma生成参数对应的目录
    [discourse@~]$ mkdir tmp/sockets
    [discourse@~]$ mkdir tmp/pids
  8.创建管理员账户
    [discourse@~]$ RAILS_ENV=production bundle exec rake admin:create
    按照命令提示填写邮箱和密码
    [discourse@~]$ exit
  9.配置nginx
    [root@~]# mkdir -p /var/nginx/cache
    [root@~]# cp config/nginx.sample.conf /etc/nginx/conf.d/discourse.conf
    [root@~]# vim /etc/nginx/conf.d/discourse.conf
    注释掉参数
    #upstream discourse {
    #  server unix:/var/www/discourse/tmp/sockets/thin.0.sock;
    #  server unix:/var/www/discourse/tmp/sockets/thin.1.sock;
    #  server unix:/var/www/discourse/tmp/sockets/thin.2.sock;
    #  server unix:/var/www/discourse/tmp/sockets/thin.3.sock;
    #}

    注释掉参数
    #proxy_cache_path /var/nginx/cache inactive=1440m levels=1:2 keys_zone=one:10m max_size=600m;
    增加一行
    proxy_cache_path /var/cache/nginx inactive=1440m levels=1:2 keys_zone=one:10m max_size=600m;
    
    # 注释掉参数
    # brotli_static on;

    取消注释
    upstream discourse {
      server unix:/var/www/discourse/tmp/sockets/puma.sock;
    }
    设置主机名
    server_name elysium.hsdk668899.cn

    :wq
    [root@~]# systemctl restart nginx

  10.配置sidekiq后台进程服务
    [root@~]# cd /var/www/discourse/config && cp -p sidekiq.yml sidekiq_new.yml
    [root@~]# vim /var/www/discourse/config/sidekiq_new.yml
    production:
      :concurrency: 2
      :queues:
        - [critical, 4]
        - [default, 2]
        - [low, 2]
        - [ultra_low]

    :wq
    在.gitignore行尾中加入排除自定义文件的设置参数
    [root@~]# vim .gitignore
    /config/sidekiq_new.rb
    :wq
    [root@~]# vim /usr/lib/systemd/system/discourse-sidekiq.service
    [Unit]
    Description=Discourse sidekiq background processing service
    After=multi-user.target

    [Service]
    Type=simple
    User=discourse
    Group=wwwe
    PIDFile=/var/www/discourse/tmp/pids/sidekiq.pid
    WorkingDirectory=/var/www/discourse
    Environment=RAILS_ENV=production
    ExecStart=/opt/rbenv/shims/bundle exec sidekiq -C /var/www/discourse/config/sidekiq_new.yml -e production 
    Restart=on-failure
    RestartSec=5

    [Install]
    WantedBy=multi-user.target

    :wq

    [root@~]# cd /var/www/discourse
    [root@~]# systemctl daemon-reload
    [root@~]# systemctl enable discourse-sidekiq
    [root@~]# systemctl start discourse-sidekiq
  11.配置Discourse后台进程服务
    [root@~]# cd /var/www/discourse
    [root@~]# vim /usr/lib/systemd/system/discourse.service
    [Unit]
    Description=Discourse sidekiq background processing service
    After=multi-user.target

    [Service]
    Type=simple
    User=discourse
    Group=www
    PIDFile=/var/www/discourse/tmp/pids/puma.pid
    WorkingDirectory=/var/www/discourse
    Environment=RAILS_ENV=production
    ExecStart=/opt/rbenv/shims/bundle exec puma puma.rb -e production
    Restart=on-failure
    RestartSec=5
    [Install]
    WantedBy=multi-user.target

    :wq

    [root@~]# systemctl daemon-reload
    [root@~]# systemctl enable discourse
    [root@~]# systemctl start discourse

  12.打开浏览器, 输入网站地址url, 以第8步骤创建的系统管理员账户登录, 进行网站管理

  以上是对构建过程的每个步骤的详细说明,可以参照动手尝试,若有此方面的建设需求,请来信勾兑,相互扶持。

本文来自网络或网友投稿,如有侵犯您的权益,请发邮件至:aisoutu@outlook.com 我们将第一时间删除。

相关素材