部署 如何在 Ubuntu 14.03 Server 上安装 mina 的一键部署环境

lotusfire · 2016年02月12日 · 最后由 lotusfire 回复于 2016年02月12日 · 3529 次阅读

假期里把 AWS EC2 上的两台 Ubuntu 实例重新部署了一下,遇到不少坑,也从社区里面得到了不少帮助。

整理成文,希望能帮到Ruby China 社区里的其他小伙伴。相关 *.sh 均可以下载,根据自己的需要编辑使用。

转帖的时候请注明原文地址,谢谢。

1. 将自己的 Key 传到服务器上

ssh-agent bash
ssh-add ~/.ssh/ec2-instance-key.pem
ssh-copy-id -i ~/.ssh/id_rsa.pub <user_name>@<server_domain>

测试

ssh <user_name>@<server_domain>

2. 配置 Ubuntu 系统

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/init-ubuntu.sh | sh

3. 安装 rvm

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/rvm.sh | sh

4. 安装 Web 服务

4.1 Nginx + Passanger

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/nginx-passanger.sh | sh

现在修改 nginx 配置,编辑 /etc/nginx/nginx.conf,找到这两行注释:

# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;

去除 '#' 即在 nginx 中启用了 passanger

4.2 Nginx + PHP5 + PHP5-FPM

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/nginx-php5-php5fpm.sh | sh

5. 安装数据库

5.1 PostgreSQL

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/postgresql.sh | sh

登录数据库

sql sudo -u postgres psql postgres 

创建数据库和用户

sudo -u postgres psql postgres
create user "<user name>" with password '<password>';
/* 建议 <user name> 和 <database name> 相同 */
create database "<database name>" owner "<user name>"; 
grant all privileges on database <database name> to <user name>; 

5.2 MySQL

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/mysql.sh | sh

登录数据库

mysql -u root -p

创建数据库和用户

create database <database_name>;
create user <user_name>@localhost identified by '<password>';
/* 建议 <user name> 和 <database name> 相同 */
grant all privileges on <user_name>.* to <database_name>@localhost;
flush privileges;

6. 配置 mina 环境

gem install mina

在项目根目录下,执行

mina init

编辑 config/deploy.rb

6.1 Passanger 环境

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
require 'mina/rvm'    # for rvm support. (http://rvm.io)
require 'mina_sidekiq/tasks'
require 'mina/foreman'

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)
set :rails_env, 'production'

set :domain, 'Server domian'
set :unit_name, 'Git unit name'
set :app_name, 'Git app name'

# Optional settings:
set :user, 'Your username'    # Username in the server to SSH to.
set :port, '22'     # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.

set :deploy_to, "/home/#{user}/webapps/#{rails_env}/#{app_name}"

set :app_path,   "#{deploy_to}/#{current_path}"
set :repository, "git@github.com:#{unit_name}/#{app_name}.git"
set :branch, 'master'

# For system-wide RVM install.
set :rvm_path, '/usr/local/rvm/bin/rvm'

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/application.yml', 'config/database.yml','log', 'tmp']

# Keep the recent 3 release
set :keep_releases, '3'

# Foreman
set :foreman_sudo, true
set :foreman_app, "#{app_name}"
set :foreman_user, "#{user}"
set :foreman_log, "#{deploy_to}/shared/log"

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  invoke :'rvm:use[ruby-2.1.4@default]'
end

# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do  
  queue! %[mkdir -p "#{deploy_to}/shared/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]

  queue! %[mkdir -p "#{deploy_to}/shared/tmp"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp"]

  queue! %[mkdir -p "#{deploy_to}/shared/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]

  queue! %[touch "#{deploy_to}/shared/config/database.yml"]
  queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."]

  queue! %[touch "#{deploy_to}/shared/config/secrets.yml"]
  queue %[echo "-----> Be sure to edit 'shared/config/secrets.yml'."]

  # sidekiq needs a place to store its pid file and log file
  queue! %[mkdir -p "#{deploy_to}/shared/pids/"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/pids/"]
end

task :log do
  queue 'echo "Contents of the log file are as follows:"'
  queue "tail -f -n 100 /var/www/#{app_name}/log/error.log"
end

desc "Deploys the current version to the server."  
task :deploy => :environment do  
  deploy do
    invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'passenger:restart'
      invoke :'sidekiq:restart'
    end
  end
end

desc "Restarts the nginx server."  
task :restart do  
  invoke :'passenger:restart'
end

namespace :passenger do  
  task :restart do
    queue "touch #{deploy_to}/shared/tmp/restart.txt"
  end
end  

6.2 Jekyll 环境


require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
require 'mina/rvm'    # for rvm support. (http://rvm.io)

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)
set :rails_env, 'production'

set :domain, 'Server domian'
set :unit_name, 'Git unit name'
set :app_name, 'Git app name'

# Optional settings:
set :user, 'Your username'    # Username in the server to SSH to.
set :port, '22'     # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.

set :deploy_to, "/home/#{user}/webapps/#{rails_env}/#{app_name}"

set :app_path,   "#{deploy_to}/#{current_path}"
set :repository, "git@github.com:#{unit_name}/#{app_name}.git"
set :branch, 'master'

# For system-wide RVM install.
set :rvm_path, '/usr/local/rvm/bin/rvm'

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['log']

# Keep the recent 3 release
set :keep_releases, '3'

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  invoke :'rvm:use[ruby-2.1.4@default]'
end

# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do  
  queue! %[mkdir -p "#{deploy_to}/shared/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
end

task :log do
  queue 'echo "Contents of the log file are as follows:"'
  queue "tail -f -n 100 /var/www/#{app_name}/log/error.log"
end

desc "Deploys the current version to the server."  
task :deploy => :environment do  
  deploy do
    # invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'nginx:restart'
    end
  end
end

desc "Restarts the nginx server."  
task :restart do  
  invoke :'nginx:restart'
end

6.3 PHP 环境

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv'  # for rbenv support. (http://rbenv.org)
require 'mina/rvm'    # for rvm support. (http://rvm.io)
require 'mina_sidekiq/tasks'
require 'mina/foreman'

# Basic settings:
#   domain       - The hostname to SSH to.
#   deploy_to    - Path to deploy into.
#   repository   - Git repo to clone from. (needed by mina/git)
#   branch       - Branch name to deploy. (needed by mina/git)
set :rails_env, 'production'

set :domain, 'Server domian'
set :unit_name, 'Git unit name'
set :app_name, 'Git app name'

# Optional settings:
set :user, 'Your username'    # Username in the server to SSH to.
set :port, '22'     # SSH port number.
#   set :forward_agent, true     # SSH forward_agent.

set :deploy_to, "/home/#{user}/webapps/#{rails_env}/#{app_name}"

set :app_path,   "#{deploy_to}/#{current_path}"
set :repository, "git@github.com:#{unit_name}/#{app_name}.git"
set :branch, 'master'

# For system-wide RVM install.
set :rvm_path, '/usr/local/rvm/bin/rvm'

# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['wp-content/cache','wp-content/uploads', 'log', 'tmp']

# Keep the recent 3 release
set :keep_releases, '3'

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  # If you're using rbenv, use this to load the rbenv environment.
  # Be sure to commit your .ruby-version or .rbenv-version to your repository.
  # invoke :'rbenv:load'

  # For those using RVM, use this to load an RVM version@gemset.
  invoke :'rvm:use[ruby-2.1.4@default]'
end

# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do  
  queue! %[mkdir -p "#{deploy_to}/shared/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]

  queue! %[mkdir -p "#{deploy_to}/shared/tmp"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp"]

  queue! %[mkdir -p "#{deploy_to}/shared/wp-content/uploads"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/wp-content/uploads"]

  queue! %[mkdir -p "#{deploy_to}/shared/wp-content/cache"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/wp-content/cache"]
end

task :log do
  queue 'echo "Contents of the log file are as follows:"'
  queue "tail -f -n 100 /var/www/#{app_name}/log/error.log"
end

desc "Deploys the current version to the server."  
task :deploy => :environment do  
  deploy do
    # invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'nginx:restart'
    end
  end
end

desc "Restarts the nginx server."  
task :restart do  
  invoke :'nginx:restart'
end

Mina 部署项目

mina setup
mina deploy

7. 建立 Nginx 配置文件

sudo mkdir -p /var/www
cd /var/www
sudo ln -s /home/<user_name>/webapps/production/<project-dir>/current <project-dir>

建立 nginx 的配置文件

cd /etc/nginx/sites-available
sudo nano <your domain>

7.1 Passanger 的 nginx 文件


server {
    listen 80;
    server_name <your domian>;
    passenger_enabled on;
    root /var/www/<project-dir>/public;

    access_log off;
    error_log /var/www/<project-dir>/log/error.log;

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

7.2 Jekyll 的 nginx 文件

server {
    listen 80;
    # listen [::]:80 ipv6only=on;

    root /var/www/<project-dir>/_site;
    index index.html;

    server_name <your domian>;

    access_log off;
    error_log /var/www/<project-dir>/log/error.log;  


    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

7.3 PHP 的 nginx 文件

server {
    listen 80;
    # listen [::]:80 ipv6only=on;

    root /var/www/<project-dir>;
    index index.php index.html index.htm;

    server_name <your domian>;

    location / {
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    access_log off;
    error_log /var/www/<project-dir>/log/error.log;  


    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

8. 链接目录并重启 Nginx

链接目录

cd /etc/nginx//sites-enabled/
sudo ln -s ../sites-available/<your domian> <your domian>

重启 nginx 服务

sudo service nginx restart

如果看到

  • Restarting nginx nginx [ OK ]

说明配置成功。

9. 一键部署脚本

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/deploy.sh | sh

All done!

执行未知来源的脚本会有很大风险。

#1 楼 @rei 所以给大家下载后自己改了用,不放心的可以用

sudo wget -O- https://raw.githubusercontent.com/lookforIO/www-zhengweiyu-com/master/installer/init-ubuntu.sh | cat

看看。:)

懒人就用我的咯...

这是部署了一套 PHP 和 Rails 混合的应用么?

#3 楼 @lgn21st 有时会用到 Wordpress,也用 mina 的一键部署。

昨不用 ansible

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册