Last active
April 29, 2017 07:07
-
-
Save acdesouza/3555100 to your computer and use it in GitHub Desktop.
Scripts para criação de um servidor Ruby on Rails. Exemplo de config do Capistrano que usa esses scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/capistrano' | |
set :application, "<nome_projeto>" | |
set :scm, :git | |
set :repository, 'git://<url_do_projeto>' | |
set :user, '<user_allowed_to_connect_with_ssh_on_servers>' | |
set :domain, '<URL_do_servidor>' | |
# Using Vagrant? See also: https://gist.github.com/3490513 | |
role :web, domain # Your HTTP server, Apache/etc | |
role :app, domain # This may be the same as your `Web` server | |
role :db, domain, :primary => true # This is where Rails migrations will run | |
#role :db, "your slave db-server here" | |
set :deploy_to, "/var/www/#{application}" | |
set :use_sudo, false | |
# if you want to clean up old releases on each deploy uncomment this: | |
set :keep_releases, 5 | |
after 'deploy:restart', 'deploy:cleanup' | |
# if you're still using the script/reaper helper you will need | |
# these http://github.com/rails/irs_process_scripts | |
# If you are using Passenger mod_rails uncomment this: | |
namespace :deploy do | |
task :start do ; end | |
task :stop do ; end | |
task :restart, :roles => :app, :except => { :no_release => true } do | |
run "#{try_sudo} touch #{current_release}/tmp/restart.txt" | |
end | |
namespace :db do | |
task :create, :roles => :db, :except => { :no_release => true } do | |
stream "cd #{current_path}; bundle exec rake db:setup RAILS_ENV=#{rails_env}" | |
end | |
end | |
task :'setup-amazon' do | |
set :amazon_scripts_dir, 'amazon-instance-config.tar.gz' | |
run "wget https://gist.github.com/3555100/download -O #{amazon_scripts_dir}" | |
run "tar xzvf #{amazon_scripts_dir}" | |
run "mv gist* amazon-script" | |
stream "cd amazon-script; sh amazon-instance-config.sh -d #{domain} -a #{application} -u #{user}" | |
run "rm -rf amazon-*" | |
end | |
end | |
before 'deploy:setup', 'deploy:setup-amazon' | |
after 'deploy:setup-amazon', 'deploy:db:create' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# TODO Criar a máquina na Amazon, com um Volume EBS | |
sudo sh mount-ebs-volume.sh | |
sudo apt-get -y update | |
sudo sh install-database.sh | |
sudo sh install-ruby.sh | |
while getopts u:a:d: option; do | |
case "${option}" in | |
a) | |
APPLICATION=${OPTARG} | |
;; | |
d) | |
DOMAIN=${OPTARG} | |
;; | |
u) | |
DEPLOY_USER=${OPTARG} | |
;; | |
:) | |
echo "Invalid option: -$OPTARG. Inform a domain name, using -d." >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [ -n "$APPLICATION" ] && [ -n "$DOMAIN" ] && [ -n "$DEPLOY_USER" ]; then | |
sudo sh install-web.sh $DOMAIN $APPLICATION $DEPLOY_USER | |
fi | |
# [Git] | |
sudo apt-get install -y git | |
# Aceitar o fingerprint RSA do github | |
ssh git@github.com -o StrictHostKeyChecking=no -o'HostKeyAlgorithms=ssh-rsa' 2>&1 > /dev/null | |
# Criar uns alias para facilitar a manutenção | |
echo 'alias tail_log_production="tail -f /var/www/$APPLICATION/current/log/production.log"' >> ~/.bash_profile | |
# Upstart e Monit | |
# TODO: http://www.akitaonrails.com/2013/05/05/por-que-bluepill-ou-god-se-temos-upstart-e-monit#.UYfW2itARCQ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# [MySQL] Instalação | |
# Não perguntar a senha de root: | |
# http://stackoverflow.com/questions/7739645/install-mysql-on-ubuntu-without-password-prompt | |
DEBIAN_FRONTEND=noninteractive apt-get -q -y install mysql-server | |
# [MySQL] Criar links para o MySQL | |
# https://help.ubuntu.com/12.04/serverguide/mysql.html | |
service mysql stop | |
# Mover o diretório data para o EBS | |
mkdir -p /mnt/ebs/var/lib | |
mv /var/lib/mysql/ /mnt/ebs/var/lib/ | |
ln -s /mnt/ebs/var/lib/mysql /var/lib/mysql | |
# [MySQL + Apparmor] Permitir o uso da pasta data em outro lugar | |
# FIXME Olhar como fazer isso sem essa excrotidão do: bash -c <string> | |
cat >> /etc/apparmor.d/local/usr.sbin.mysqld <<EOF | |
/mnt/ebs/var/lib/mysql/ r, | |
/mnt/ebs/var/lib/mysql/** rwk, | |
EOF | |
service apparmor restart | |
service mysql start | |
# TODO Criar o database: http://www.grahambrooks.com/blog/create-mysql-database-with-capistrano/ | |
# TODO Criar uma senha para o banco: http://stackoverflow.com/questions/9609985/please-install-mysql-adapter-gem-install-activerecord-mysql-adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# [Ruby] Instalar as dependências para compilar o Ruby | |
# https://gist.github.com/2917836 | |
apt-get -y install build-essential libyaml-dev zlib1g-dev libssl-dev libreadline-dev | |
# Nokogiri deps | |
apt-get -y install libxml2-dev libxslt1-dev | |
# Ir para o diretório de download | |
DOWNLOAD_DIR=/tmp | |
cd $DOWNLOAD_DIR | |
## YAML | |
YAML_SRC="yaml-0.1.4.tar.gz" | |
if [ ! -f $YAML_SRC ]; then | |
wget "http://pyyaml.org/download/libyaml/$YAML_SRC" | |
fi | |
tar xzvf $YAML_SRC | |
cd yaml-0.1.4 | |
./configure --prefix=/usr/local | |
make | |
make install | |
## [Ruby] Baixar o ruby | |
cd $DOWNLOAD_DIR | |
RUBY_VERSION="1.9.3-p362" | |
RUBY_SRC="ruby-$RUBY_VERSION.tar.gz" | |
if [ ! -f $RUBY_SRC ]; then | |
wget "http://ftp.ruby-lang.org/pub/ruby/1.9/$RUBY_SRC" | |
fi | |
tar xzvf $RUBY_SRC | |
RUBY_SRC_DIR="$DOWNLOAD_DIR/ruby-$RUBY_VERSION" | |
# [Ruby] Entrar no diretório do código-fonte | |
cd $RUBY_SRC_DIR | |
# [Ruby] Configurar as dependências | |
cat >> $RUBY_SRC_DIR/ext/Setup <<EOF | |
# ----------------------------------- | |
# Also install the dependencies | |
# ----------------------------------- | |
openssl | |
readline | |
zlib | |
EOF | |
# [Ruby] Compilar e instalar | |
./configure | |
make | |
make install | |
# [Bundler] Instalação | |
gem install bundler | |
# [node.js] Instalação | |
# Necessário para a compilação dos assets | |
apt-get -y install nodejs | |
gem install execjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Revisar com base nesse tutorial: | |
# http://www.codebeerstartups.com/2012/10/complete-guide-to-setup-a-rails-server/?goback=%2Egde_22413_member_207280950 | |
# [Apache] Criar os diretórios de deploy da aplicação, com permissão de escrita para o usuário do apache. | |
mkdir -p /mnt/ebs/var/www | |
chown -R www-data: /mnt/ebs/var/www | |
ln -s /mnt/ebs/var/www /var/www | |
# [Apache] Configurar diretórios sem intervenção manual | |
DOMAIN=$1 | |
APPLICATION=$2 | |
DEPLOY_USER=$3 | |
APPLICATION_DIR="/var/www/$APPLICATION" | |
mkdir -p $APPLICATION_DIR/releases $APPLICATION_DIR/shared/system $APPLICATION_DIR/shared/log $APPLICATION_DIR/shared/pids | |
# [Permissões] Acertar as permissões para o Capistrano. Espero poder remover isso! Um dia... | |
# Criar a árvore de diretórios para o Capistrano não cagar minhas permissões | |
# http://stackoverflow.com/questions/6310086/capistrano-git-deployment-could-not-create-work-tree-dir-permission-denied | |
chown -hR $DEPLOY_USER $APPLICATION_DIR | |
# [Apache] Instalação | |
apt-get -y install apache2 | |
# [Apache] Desabilitar a listagem de diretórios | |
a2dismod autoindex | |
# [Apache] Habilitar cache dos assets | |
a2enmod headers | |
a2enmod expires | |
# [Passenger] Instalar Dependências | |
# * Curl development headers with SSL support | |
# * Apache 2 development headers | |
# * Apache Portable Runtime (APR) development headers | |
# * Apache Portable Runtime Utility (APU) development headers | |
apt-get -y install libcurl4-openssl-dev apache2-prefork-dev libapr1-dev libaprutil1-dev | |
# [Passenger] Instalar | |
# http://www.modrails.com/documentation/Users%20guide%20Apache.html | |
# https://groups.google.com/forum/?fromgroups=#!topic/phusion-passenger/BvlrYsh9LXc | |
# https://groups.google.com/forum/?fromgroups=#!topic/phusion-passenger/fvd4KDJtru4 | |
gem install passenger | |
yes | passenger-install-apache2-module | |
# [Passenger] Habilitar o módulo do apache | |
# https://github.com/teambox/teambox/wiki/Installing-on-Ubuntu | |
PASSENGER_VERSION=`gem list passenger | sed 's/[^0-9.]*//g'` | |
PASSENGER_MODULE_FILE="/etc/apache2/mods-available/passenger" | |
PASSENGER_MODULE_LOAD_FILE="$PASSENGER_MODULE_FILE.load" | |
touch $PASSENGER_MODULE_LOAD_FILE | |
cat >> $PASSENGER_MODULE_LOAD_FILE <<EOF | |
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-$PASSENGER_VERSION/ext/apache2/mod_passenger.so | |
EOF | |
PASSENGER_MODULE_CONF_FILE="$PASSENGER_MODULE_FILE.conf" | |
touch $PASSENGER_MODULE_CONF_FILE | |
cat >> $PASSENGER_MODULE_CONF_FILE <<EOF | |
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-$PASSENGER_VERSION | |
PassengerRuby /usr/local/bin/ruby | |
EOF | |
a2enmod passenger | |
# [Apache] Configurar o VirtualHost | |
APPLICATION_CONFIG_FILE="/etc/apache2/sites-available/$APPLICATION" | |
touch $APPLICATION_CONFIG_FILE | |
cat >> $APPLICATION_CONFIG_FILE <<EOF | |
# ----------------------------------------------------- | |
# Apache Httpd | |
# https://help.ubuntu.com/12.04/serverguide/httpd.html | |
# ----------------------------------------------------- | |
<VirtualHost *:80> | |
ServerName $DOMAIN | |
# !!! Be sure to point DocumentRoot to 'public'! | |
DocumentRoot $APPLICATION_DIR/current/public | |
<Directory $APPLICATION_DIR/current/public> | |
# This relaxes Apache security settings. | |
AllowOverride all | |
# MultiViews must be turned off. | |
Options -MultiViews | |
</Directory> | |
</VirtualHost> | |
<LocationMatch "^/assets/.*$"> | |
Header unset ETag | |
FileETag None | |
# RFC says only cache for 1 year | |
ExpiresActive On | |
ExpiresDefault "access plus 1 year" | |
</LocationMatch> | |
EOF | |
a2ensite $APPLICATION | |
# [Apache] Reiniciar o Apache | |
# service apache2 restart | |
echo 'Run cap deploy:cold to start apache' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# [EBS] Preparar o volume EBS para ser usado como base para o banco de dados e o deploy da aplicação | |
# http://danbarnett.net/how-to-mount-amazon-ec2-ebs-volume-on-an-instances-file-system-76.html | |
# Ao criar o volume, garantir que o volume EBS seja configurado para Device: /dev/sd*b* | |
mke2fs -F -j /dev/xvdb | |
mkdir -p /mnt/ebs/var | |
# [EBS] Adicionar o ponto de montagem no fstab | |
# https://help.ubuntu.com/community/Fstab | |
cat >> /etc/fstab <<EOF | |
/dev/xvdb /mnt/ebs auto defaults 1 2 | |
EOF | |
mount /dev/xvdb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment