在ubuntu server 12.04上部署Discourse

独自空忆成欢 提交于 2019-12-30 12:28:31

本教程参考http://davidcel.is/blog/2013/05/02/deploying-discourse-with-capistrano/

 因为网站马上要在阿里云上面运行,所以针对阿里云进行一些特殊操作:比如挂载数据盘,将数据库存放在/data目录下,等等。

 

一、初始化服务器

1.创建一个Ubuntu12.04的ECS,用root登录。

2.挂载数据盘

根据http://help.aliyun.com/origin?spm=0.0.0.0.8QIYDn&helpId=271的步骤,挂载数据盘到/data目录。

3.创建普通用户并以普通用户登录

adduser sjyyt

usermod -a -G sudo sjyyt

logout

ssh sjyyt@123.456.78.90#将/data目录授予sjyytsudo chown sjyyt /data

4.修改主机名称

echo sjyyt.com | sudo tee /etc/hostname#上面修改主机名需重启才能生效,先修改下sudo hostname sjyyt.com

同时sudo vi /etc/hosts

127.0.0.1 localhost sjyyt.com

5.创建交换区

下面的count = 容量 * 1024(容量以mb为单位),以2GB为例

sudo dd if=/dev/zero of=/swapfile bs=1024 count=2048k
sudo mkswap /swapfile
sudo swapon /swapfile

sudo vi /etc/fstab,在最后添加:

/swapfile       none    swap    sw      0       0
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

6.为保证服务器安全,配置并启用防火墙

ubuntu使用的是ufw防火墙,使用方法参见:http://www.cnblogs.com/sjyyt/articles/3161928.html,这里只列举初始配置:

# 默认禁止连接
sudo ufw default deny

# 打开SSH和web端口
sudo ufw allow ssh
sudo ufw allow 80

#启用防火墙
sudo ufw enable

7.安装agent,用于阿里云监控

#下载
wget http://imgs-storage.cdn.aliyuncs.com/jiankongagent.x86_64.rpm?spm=0.0.0.0.WDzl6E&file=jiankongagent.x86_64.rpm

#修改下载文件名
mv jiankongagent.x86_64.rpm\?spm\=0.0.0.0.WDzl6E jiankongagent.x86_64.rpm

#安装alien
sudo apt-get install alien

#安装agent
sudo alien --scripts -k -i jiankongagent.x86_64.rpm

#修改agent-id
sudo  /usr/local/cms/config/token.cfg

#启动agent
sudo cmsd start

二、安装软件

1.安装基本组件

sudo apt-get updatesudo apt-get install build-essential openssl libreadline6 libreadline6-dev  curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev libgdbm-dev  ncurses-dev automake libtool bison subversion pkg-config libffi-dev

2.安装ruby/rvm

参考连接:https://www.digitalocean.com/community/articles/how-to-install-ruby-on-rails-on-ubuntu-12-04-lts-precise-pangolin-with-rvm

# 安装rvm
\curl -L https://get.rvm.io | bash -s stable

#加载lvm
source ~/.rvm/scripts/rvm

#检测依赖(依赖在前面已经安装完毕了,一般不需要安装)
rvm requirements
#如果确实检测到依赖未安装,可以安装

#安装ruby2.0
rvm install 2.0.0

#将2.0.0设为默认
rvm use 2.0.0 --default

#安装rubygems
rvm rubygems current#安装bundlergem install bundler --no-rdoc --no-ri

 * 注意修改rubygems源:http://www.cnblogs.com/sjyyt/p/3161927.html

gem sources --remove https://rubygems.org/
gem sources -a http://ruby.taobao.org/

3.安装nginx

# Run these commands as your normal login (e.g. "michael")
# Remove any existing versions of nginx
sudo apt-get remove '^nginx.*$'

# Add nginx repo to sources.list
cat <<'EOF' | sudo tee -a /etc/apt/sources.list

deb http://nginx.org/packages/ubuntu/ precise nginx
deb-src http://nginx.org/packages/ubuntu/ precise nginx
EOF

# Add nginx key
curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add -

# install nginx
sudo apt-get update && sudo apt-get -y install nginx

4.安装postgresql、redis等

sudo apt-get install postgresql-9.1 postgresql-contrib-9.1 redis-server libxml2-dev libxslt-dev libpq-dev make g++

配置postgresql:

#创建数据库目录sudo mkdir /data/dbsudo chown postgres /data/db#初始化这个目录sudo passwd postgres #修改postgres密码su postgres/usr/lib/postgresql/9.1/bin/initdb /data/db
#修改配置 sudo vi /etc/postgresql/9.1/main/postgresql.conf,将data_directory改为/data/db,同时将ssl设置为false(后面这个原因不明)#重启数据库service postgresql restartsudo -u postgres createuser sjyyt -s -P

createdb -U sjyyt discourse_production

5.安装postfix

这部分内容参见:http://www.cnblogs.com/sjyyt/articles/3161653.html

 

三、部署Discourse

1.准备代码

首先,我们fork一份discourse代码,然后用github客户端下载到本地。下载之后,就可以对这个代码进行开发了,使用vagrant up + vagrant ssh,配合本地编辑器,进行开发。

开发过程中,我们需要原项目的最新进展,所以需要对远程仓库进行配置,也就是添加源项目的仓库地址:

git remote add upstream git@github.com:discourse/discourse.git

我们的操作模式就是对本地进行修改,然后推送到自己的远程仓库中,然后定期从源仓库中pull下来最新代码,进行合并之后再推送,而服务器部署的代码则是从我们的远程仓库直接下载下来,也就是:

git fetch upstream
git merge upstream/master
git push origin master

2.为部署准备配置文件

这些文件均是由本地上传到服务器上,因此,可以在目录中新建:config_upload

  1)config_upload/database.yml

production:
  adapter: postgresql
  database: discourse_production
  username: sjyyt
  password: sjyyt123
  pool: 5
  timeout: 5000
  host_names:
    - sjyyt.com

  2)config_upload/redis.yml

无需改变

3)config_upload/production.rb

无需改变

4)config_upload/thin.yml

---
chdir: /data/web/discourse/current
environment: production
address: 0.0.0.0
port: 3000
timeout: 30
log: /data/web/discourse/shared/log/thin.log
pid: /data/web/discourse/shared/pids/thin.pid
socket: /data/web/discourse/tmp/thin.sock
max_conns: 1024
max_persistent_conns: 100
require: []
wait: 30
servers: 4
daemonize: trueonebyone: true

5)config_upload/nginx.conf

upstream discourse {
  server unix:/data/web/discourse/tmp/thin.0.sock max_fails=1 fail_timeout=15s;
  server unix:/data/web/discourse/tmp/thin.1.sock max_fails=1 fail_timeout=15s;
  server unix:/data/web/discourse/tmp/thin.2.sock max_fails=1 fail_timeout=15s;
  server unix:/data/web/discourse/tmp/thin.3.sock max_fails=1 fail_timeout=15s;
}

server {
    listen 80;
    server_name www.sjyyt.com;
    return 301 $scheme://sjyyt.com$request_uri;
}

server {

  listen 80;
  gzip on;
  gzip_min_length 1000;
  gzip_types application/json text/css application/x-javascript;

  server_name sjyyt.com;

  sendfile on;

  keepalive_timeout 65;
  client_max_body_size 2m;

  location / {
    root /data/web/discourse/current/public;

    ## optional upload anti-hotlinking rules
    #location ~ ^/uploads/ {
    #  valid_referers none blocked mysite.com *.mysite.com;
    #  if ($invalid_referer) {
    #    return 403;
    #  }
    #}

    location ~ ^/t\/[0-9]+\/[0-9]+\/avatar {
      expires 1d;
      add_header Cache-Control public;
      add_header ETag "";
    }

    location ~ ^/(assets|uploads)/ {
      expires 1y;
      add_header Cache-Control public;
      add_header ETag "";
      break;
    }

    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    proxy_set_header  Host $http_host;


    # If the file exists as a static file serve it directly without
    # running all the other rewrite tests on it
    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      proxy_pass http://discourse;
      break;
    }

  }

}

11.添加secret_token

bundle exec rake secret

将生成的结果写入:config_upload/secret_token.rb

12.配置cap

在Gemfile文件中写入(最好是:development)

gem 'capistrano', require: falsegem 'rvm-capistrano', require: false

运行bundle install

在目录中添加Capfile(直接从sample文件复制)

在config目录中添加deploy.rb(直接从sample文件复制,然后稍作修改即可)

运行bundle install,添加cap功能

13.初次部署

# Set up the server's directory structure for Capistrano
cap deploy:setup

# Do the first deploy! No server is running yet, so do a cold deployment
cap deploy:cold
# Seed the database you created earlier
cap db:seed

14.定期部署

cap deploy

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!