5 months ago

deploy process for a new ubuntu 16.04 64bit

part I setup linux machine for secure reason

verify root account

$ ssh root@remote
$ exit

ssh to remote with local pub key

$ ssh-copy-id root@remote

login again without password

add new user doudouappadmin

$ adduser doudouappadmin

$ adduser doudouappadmin sudo

$ exit

verify with doudouappadmin

$ ssh doudouappadmin@remote
$ exit

login with pub key without password

$ ssh-copy-id doudouappadmin@remote

login with doudouappadmin to disable root login

$ sudo vim /etc/ssh/sshd_config

edit

PermitRootLogin no

make change effect

$ sudo systemctl restart sshd
$ exit

PartII update system and basic lib

$ ssh doudouappadmin@remote

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo dpkg-reconfigure tzdata

Geographic aera - Asia
Time zone - Shanghai

support multilanguage

$ sudo locale-gen zh_TW zh_TW.UTF-8 zh_CN.UTF-8 en_US.UTF-8

用vi编辑器打开

$vim ~/.bashrc

在空白处新增行

export LC_ALL="en_US.UTF-8"

执行

$ source ~/.bashrc

install basic application and lib

$ sudo apt-get install build-essential git-core curl libssl-dev libreadline5 libreadline-gplv2-dev zlib1g zlib1g-dev libcurl4-openssl-dev libxslt-dev libxml2-dev libffi-dev git vim

install RVM

$ \curl -sSL https://get.rvm.io | bash

then run

$ source ~/.rvm/scripts/rvm

install Ruby

$ rvm install 2.3.1

confirm ruby

$ rvm list

setup ruby china mirror

$ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
$ gem sources -l
*** CURRENT SOURCES ***

https://gems.ruby-china.org
## confirm there is only one gems.ruby-china.org
$ gem install bundler

install RMagick

$ sudo apt-get install imagemagick
$ sudo apt-get install libmagickwand-dev
$ gem install rmagick

install Passenger

install Passenger

$ gem install passenger

install Nginx and merge Passenger

$ rvmsudo passenger-install-nginx-module

选择Ruby & NodeJS

再选择选项1. Yes: download, complie and install Nginx for me. (recommended)

选择安装目录, 可以直接按回车,使用默认安装目录[/opt/niginx]

start nginx

$ git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git
$ sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
$ sudo chown root:root /etc/init.d/nginx

edit nginx server 档(只有 Ubuntu 16+ 需要)

$ sudo vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

restart Nginx

$ sudo /etc/init.d/nginx restart

Nginx

browser http://xx.xx.xx.xx to verify nginx

install rails 5.0.3

$ gem install rails -v 5.0.3

install nodejs

$ sudo apt-get install npm

install PostgreSQL

$ sudo apt-get install postgresql postgresql-contrib libpq-dev
$ gem install pg

update postgres password
login psql

$ sudo -u postgres psql

update postgres password

\password postgres

leave by

\q

let Rails know to connect db with the account

# sudo vim /etc/postgresql/9.5/main/pg_hba.conf

update all md5 to peer and save and run

$ sudo /etc/init.d/postgresql reload

镜像文件到此为止

让远端机器可以去 github 拉档案

$ ssh doudouappadmin@remote

生成ssh key

$ key ssh-keygen

查看pub key

$ more ~/.ssh/id_rsa.pub

去Github的仓库里,项目里面贴到 Settins -> Deploy keys
去远端机测试

$ ssh -T git@github.com

deploy cap if there is no cap 当前已经使用cap了。没有cap的进行如下

使用 Capistrano 部署项目

修改 Gemfile,加入以下内容

group :development do
gem "capistrano", "~> 3.4"
gem "capistrano-rvm"
gem "capistrano-rails"
end
$ bundle install

然后执行

$ cap install

修改 config/deploy.rb

lock '3.6.1'  # 每个人按自己的默认版本,不一定要是"3.6.1"

set :application, 'vidopk'

set :repo_url, 'git@github.com:jiezhon/videopk.git'  # 这里填的是每个人自己的repo地址



set :deploy_to, "/home/doudouappadmin/videopk"

注: 这里示例项目为videopk,也是Gitup的仓库名

如果有图片,视频需要存在其他的云存储服务器,关于云存储的密钥文件application.yml用linkded_files实现从shared目录下读取

set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/application.yml')

修改 Capfile

require "capistrano/rvm"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/passenger"

修改config/deploy/production.rb
加上一行,配置server和user

server "1.1.1.1", user: "doudouappadmin", roles: %w{app db web}, my_property: :my_value

在远端生成档案
执行

$ cap production deploy:check

远端服务器配置

在远端机的shared目录下的设定档

$ vim database.yml
production:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
database: videopk_production
username: postgres
password: YOURPASSWORD
$ vim secrets.yml

在本机上执行

$ rake secret

产生密钥
然后贴到远端机的

$ vim shared/config/secrets.yml
production:

  secret_key_base: 上面产生的那串密钥

配置upyun存储的相关密钥

$vim application.yml
production:
  UPYUN_OPERATOR_ID: "11"
  UPYUN_OPERATOR_PASSWORD: "11"
  UPYUN_BUCKET: "11"
  UPYUN_HOST: "http://11.b0.upaiyun.com"

正式 deploy

执行

$ cap production deploy

第一次会卡在 bundle install 比较久,并且第一次deploy会失败,因为 production db 还没创建
出错信息为 “FATAL: database "videopk_production" dose not exist”
创建DB

到最新的release目录下面

$ cd videopk/released/xxxxxxxx
RAILS_ENV="production" bundle exec rake db:create

再跑一次 cap production deploy

设定 Nginx

$ sudo vi /opt/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  dd.doudouapp.com;

        root /home/doudouappadmin/videopk/current/public;
        passenger_enabled on;
        client_max_body_size 30M;
    }

重开Nginx

$ sudo /etc/init.d/nginx restart

迁移数据盘

$ sudo disk -l

Device     Boot Start      End  Sectors Size Id Type
/dev/vdb1        2048 41943039 41940992  20G 83 Linux
$ sudo mkdir /data
$ sudo mount /dev/vdb1 /data
$ sudo chown -R postgres:postgres /data/pkdata
#### only if you want to init a disk
$ sudo su - postgres 
$ /usr/lib/postgresql/9.5/bin/initdb -D /data/pkdata
$ exit
### 
$ sudo /etc/init.d/postgresql stop
$ sudo su - postgres
$ /usr/lib/postgresql/9.5/bin/pg_ctl -D /data/pkdata -l logfile start
$ exit
← add JSON API to devise gem in rails setup your own server →