测试,测试数据分离,开发,PaaS 云服务,这里我要用 docker 搭建一个 rails 项目
切记一定要去看 docker,此贴只简单描述,此贴只是一种思路,不一定是正确的流程,仅供参考及讨论
友情链接 Docker 官网 Docker 安装参考链接
+--------------------+--------------------+
| ubuntu | mysql | #docker
+--------------------+--------------------+
| rvm | mysql | #包含
| ruby | |
| rails | |
| nginx | |
+--------------------+--------------------+
一。构建所需要的 Dockerfile (ubuntu)
FROM ubuntu:16.04
#捆绑套件
RUN apt-get update && apt-get install -y openssh-server
RUN apt-get update && apt-get install gnupg2 -y && apt-get install vim -y && apt-get install curl -y && apt-get install git -y && apt-get install nodejs -y
#创建ssh
RUN mkdir -p /var/run/sshd
RUN mkdir /root/.ssh
#创建账号 以供操作和部署等
RUN echo 'root:root2020' | chpasswd
RUN useradd -ms /bin/bash deploy
RUN echo 'deploy:deploy2020' | chpasswd
#当容器启动时运行的脚本
CMD ["/usr/sbin/sshd", "-D"]
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
#开放端口
EXPOSE 22 80 8080
#创建镜像
$ docker build .
#查看镜像
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> a58c0f537c60 23 seconds ago 251MB #新创建docker
$ docker tag a58c0f537c60 ubuntu:base # TAG
#创建我所需要的容器
$ docker run -idt \
-p 126:22 -p 8081:80 \ #映射端口
--name rails_app \ #名
ubuntu:base #镜像
$ docker ps #查看运行中容器
$ ssh [email protected] -p 126
#改个时区
#查看时间
$ date
$ apt install tzdata
$ dpkg-reconfigure tzdata
6 Asia 70 Shangehai #设置时区
二。装 rvm,ruby 等环境 安装 rvm
$ gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ \curl -sSL https://get.rvm.io | bash -s stable
$ source /etc/profile.d/rvm.sh
#查看版本
$ rvm list known
#安装版本
$ echo "export rvm_max_time_flag=20" >> ~/.rvmrc
$ rvm install 2.6.1 --disable-binary
#安装成功
$ ruby -v
$ ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-linux]
#手动装个bundler,Gemfile用起来更香
$ gem install bundler
三。创建 rails 项目,capistrano 配置 参考
# 本地
# 如果你已经有开发好的项目,这一步请直接略过。
# 新建一个目录,存放rails项目
$ mkdir -p ~/Developer/Rails
# 新建一个Rails项目,默认使用MySQL数据库
$ rails new web_app -d mysql
#Gemfile
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'pry-rails' # binding.pry
# capistrano family gem
gem 'capistrano', '~> 3.6'
gem 'capistrano-rails', '~> 1.1'
gem 'capistrano-rvm'
# gem 'capistrano-passenger'
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
# 修改后bundle
$ bundle install
# 本地运行
$ cap install
#编辑 Capfile
require 'capistrano/rails'
require "capistrano/rvm"
require "capistrano/bundler"
require 'capistrano/puma'
lock "~> 3.14.0"
# 项目名称
set :application, "web_app"
# git仓库地址 我这里用的coding
set :repo_url, "[email protected]:xxxx/web_app.git"
set :rails_env, 'production'
# 去掉注释,服务器需要有这些配置
append :linked_files, "config/database.yml","config/secrets.yml"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public"
#参数设置
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
# set :ssh_options, {forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub)}
#puma参数可参考capistrano3-puma
set :puma_threads, [4, 16]
set :puma_workers, 0
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true
#以下我个人设置,可按照自己喜好配置
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`
puts "WARNING: HEAD is not the same as origin/#{fetch(:branch)}"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
# after :finishing, :compile_assets
# after :finishing, :cleanup
# after :finishing, :restart
end
set :stage, :production # 部署的环境
set :branch, 'master' # 部署的代码分支
set :rails_env, :production
#我这里仅供测试,需要根据实际配置来
server '127.0.0.1', user: 'root', password: 'root2020', roles: %w{web app db} , port: '126'
set :deploy_to, "/var/www/web_app" # 部署到服务器的位置
set :enable_ssl, false
四.capistrano 部署相关配置
#run一个mysql docker
$ docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=336699 -d mysql:5.7
$ docker exec -it CONTAINER_ID bash #进入docker
cat /etc/hosts #获取ip 我这里获取的是 172.17.0.3
#创建个库
mysql -u root -p
CREATE DATABASE web_app_pro CHARACTER SET utf8mb4;
#退出 mysql console
$ exit
#退出 mysql docker
$ exit
$ ssh [email protected] -p 126
#准备工作
$ mkdir -p /var/www/web_app #项目存放路径
$ mkdir -p /var/www/web_app/shared/config #项目配置
$ cd /var/www/web_app/shared/config
$ vim database.yml
#创建所需的文件
# database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: 20
username: root #<%= ENV["DATAUSER"] %> #推荐使用<%= ENV["DATAUSER"] %> 这样不会直接暴露账号密码,我这里图方便就用了root(不建议使用)
password: 336699 #<%= ENV["DATAPASSWD"] %> #来源docker mysql的密码
host: 172.17.0.3 #<%= ENV['DATABASE_HOST'] %> #来源docker mysql的地址
port: 3306
reconnect: true
wait_timeout: 30
production:
<<: *default
database: web_app_pro #<%= ENV["DATABASE"] %>
#此处省略secrets。yml创建
五.capistrano 部署代码 开始爆破
#本地执行
$ cap production deploy:check
#好了执行下面的
$ ccap production deploy
六。配置 nginx
$ apt-get install nginx
$ cd /etc/nginx/sites-enabled
$ vim web_app.conf
#web_app.conf配置
upstream web_app {
server unix:///var/www/web_app/shared/tmp/sockets/web_app-puma.sock;
}
server {
listen 80;
root /var/www/web_app/current/public;
server_name localhost;
try_files $uri/index.html $uri @police_iot;
location @police_iot {
proxy_pass http://web_app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
$ nginx -s reload 重新加载nginx配置
七.end 真香 谢谢收看