实现同台服务器同时运行WORDPRESS与DISCOURSE

简思录站点就是通过一台服务器同时运行WordPress和Discourse项目的。今天是简思录搭建成功上线的第二天,刚好有时间,顺便整理一下如何部署这两个项目,同时也希望能帮到一些需要的站长们!要实现同一台服务器同时运行WordPress和Discourse有2种方式,一种是全新安装(即服务器上并没有运行WordPress和Discourse的项目),另一种则是已经有Discourse项目运行着了。

@keyboardstaff / 2017-10-28 / CC-BY-SA-3.0

这里就说说全新安装方式:

服务器配置清单

简思录站点服务器托管在Qcloud。购于2017年10月26号,当时看到双十一广告,一口气买了3年,花费1534.60元,还是挺划算的,需要购买的请猛戳该链接!

Linux Server [2 Core CPU, 4G Memory, 50G Disk, 64 位] - Debian Server 9.0 64 位

WordPress部署

WordPress运行需要LNMP环境(Linux + Nginx+ MySQL+ PHP),这里我们可以采用LNMP一键安装包,推荐OneinStack,官网链接:https://oneinstack.com/

OneinStack安装这里就不详细阐述了,如果不明白安装的可以去OneinStack官网查看安装教程,非常详细。那么下面的步骤我就权当你已经成功运行WordPress了。

Nginx配置

如果是通过oneinstack一键安装包的,Nginx的配置文件路径为:/usr/local/nginx/conf/vhost
进入该目录后,修改你绑定域名的配置文件,参考代码如下:

server {
  listen 80;
  listen 443 ssl http2;
  ssl_certificate /usr/local/nginx/conf/ssl/jiansilu.com.crt;  #修改为你证书的路径
  ssl_certificate_key /usr/local/nginx/conf/ssl/jiansilu.com.key;  #修改为你证书的路径
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name jiansilu.com www.jiansilu.com;  #修改为你的域名
  location / {
    access_log /data/wwwlogs/jiansilu.com_nginx.log combined;
    index index.html index.htm index.php;
    root /data/wwwroot/jiansilu.com;  #修改为你站点的目录
    if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
    if ($host != jiansilu.com) {  return 301 $scheme://jiansilu.com$request_uri;  }  #修改为你的域名
    include /usr/local/nginx/conf/rewrite/wordpress.conf;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
  
    location ~ [^/]\.php(/|$) {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
        expires 30d;
        access_log off;
    }
    location ~ .*\.(js|css)?$ {
        expires 7d;
        access_log off;
    }
    location ~ /\.ht {
        deny all;
    }
  }
  location /community/ { # /community/ 为站点子目录名称,例如:https://jiansilu.com/community
    proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
    proxy_set_header Host $http_host;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
  }
}

然后,执行如下命令:

root@localhost:~# sudo nginx -t # 测试配置文件
# 重要:如果 nginx -t 返回了错误,请一定要修正配置!
root@localhost:~# sudo service nginx reload #加载配置文件
root@localhost:~# sudo service nginx restart #重启nginx

好了,到这里WordPress和Nginx配置就可以了,接下来就可以部署Discourse了。

Discourse部署

运行命令安装docker

root@localhost:~# curl -sSL https://get.docker.com/ | sh

运行命令创建discourse目录与克隆项目,并进入discourse目录

root@localhost:~# sudo -s
root@localhost:~# mkdir /var/discourse
root@localhost:~# git clone https://github.com/discourse/discourse_docker.git /var/discourse
root@localhost:~# cd /var/discourse

创建app.yml配置文件,并上传至/var/discourse/containers ,参考代码如下:

## 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"
  - "templates/web.ratelimited.template.yml"
  - "templates/web.socketed.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"

## 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:
  #- "80:80"   # http
  #- "443: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"   # 根据服务器配置修改,如果内存是2g的话改为256MB,4G则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:
  LANG: 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    #根据服务器配置修改,如果CPU是1核的话改为2,2核则4

  ## TODO: The domain name this Discourse instance will respond to
  DISCOURSE_HOSTNAME: jiansilu.com     #修改成你的域名
  DISCOURSE_RELATIVE_URL_ROOT: /community     #修改成你discourse子目录名称

  ## 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]'   #管理员邮箱

  ## TODO: The SMTP mail server used to validate new accounts and send notifications
  DISCOURSE_SMTP_ADDRESS: smtp.exmail.qq.com    #修改成你SMTP邮箱的服务器
  DISCOURSE_SMTP_PORT: 587     #修改成你SMTP邮箱的服务器端口
  DISCOURSE_SMTP_USER_NAME: [email protected]     #修改成你的SMTP邮箱
  DISCOURSE_SMTP_PASSWORD: "jiansilu.com"    #修改成你的SMTP邮箱密码
  DISCOURSE_SMTP_ENABLE_START_TLS: true           # (optional, default true)
  DISCOURSE_SMTP_AUTHENTICATION: login
  DISCOURSE_SMTP_OPENSSL_VERIFY_MODE: none

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

  ## 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:
  ## Subfolder support
  - exec:
        cd: $home
        cmd:
          - mkdir -p public/community  #修改成你discourse子目录名称
          - cd public/community && ln -s ../uploads && ln -s ../backups  #修改成你discourse子目录名称
  - replace:
       global: true
       filename: /etc/nginx/conf.d/discourse.conf
       from: proxy_pass http://discourse;
       to: |
          rewrite ^/(.*)$ /community/$1 break;   #修改成你discourse子目录名称
          proxy_pass http://discourse;
  - replace:
       filename: /etc/nginx/conf.d/discourse.conf
       from: etag off;
       to: |
          etag off;
          location /community {    #修改成你discourse子目录名称
             rewrite ^/community/?(.*)$ /$1;     #修改成你discourse子目录名称
          }
  - replace:
         filename: /etc/nginx/conf.d/discourse.conf
         from: $proxy_add_x_forwarded_for
         to: $http_fastly_client_ip
         global: true
  - 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

root@localhost:/var/discourse# ./launcher bootstrap app

安装discourse完成后,运行命令启动discourse

root@localhost:/var/discourse# ./launcher start app

duan~~~,至此就完成WordPress和Discourse同时运行在一个服务器了。

文章来源:简思录 https://jiansilu.com/

你可能感兴趣的:(实现同台服务器同时运行WORDPRESS与DISCOURSE)