Ubuntu 11.10 com Ruby on Rails + Nginx + Mysql de forma simples

Andei procurando na internet por algum script para fazer uma instalação mais “automatizada” de um ambiente Ruby on Rails numa máquina virtual (VPS) rodando Ubuntu 11.10

Depois de pesquisar muito, só encontrei referências detalhadas  para ubuntu 10.* ou então alguns blogs sugerindo algumas linhas de comando apenas.

Como eu já estava terminando um projeto de um site e precisava de um ambiente de produção, resolvi fazer um script (em bash), onde eu tivesse o controle de tudo que iria acontecer de forma segura e eficaz.

O script está disponível no meu github, use como quiser =)

Lembrando que só testei no Ubuntu 11.10, a instalação instala e configura o seguinte:
– Ruby 1.9.2-p290
–  Nginx Upload Module 2.2.0
– Nginx 1.1.12 
– iptables para as portas principais de ssh(22), http(80), https(443), dns(53)
– Mysql (versão estável do apt-get do ubuntu)
– Postfix (versão estável do apt-get do ubuntu)
– Gem Bundler (última versão pelo rubygems)

Com isso, basta configurar o domínio da sua aplicação corretamente na configuração do nginx, fazer o deploy e ser feliz! =)

Link do projeto: https://github.com/rafaelbiriba/Ubuntu-VPS-Builder 

Abaixo segue uma cópia do código que eu coloquei no git assim você pode tirar suas dúvidas pelos comentários… Caso decida usar o script, baixe ele pelo link do github. Não use o código abaixo pois o mesmo pode já estar desatualizado!

Modo de usar no servidor:
$ chmod +x vps_builder.sh
$ ./vps_builder.sh seudominio.com 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# "Automated VPS Setup for Ubuntu 11.10 - Rails with Nginx"
#
# Created by: Rafael Biriba - www.rafaelbiriba.com - [email protected]
# https://github.com/rafaelbiriba/Ubuntu-VPS-Builder/
#
# USAGE:
#
# $ chmod +x vps_builder.sh
# $ ./vps_builder.sh yourdomain.com"
 
echo "Updating before all"
apt-get update && apt-get upgrade -y
 
echo "Set Hostname"
 
echo $1 > /etc/hostname
echo "127.0.0.1 $1" >> /etc/hosts
hostname -F /etc/hostname
 
echo "Set Timezone"
 
ln -sf /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime
 
echo "Install Essencials"
 
apt-get install build-essential zlib1g-dev libreadline6-dev libssl-dev wget git-core sudo -y
 
echo "Install Ruby 1.9.2"
 
mkdir ~/tmp && cd ~/tmp
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p290.tar.gz
tar xzvf ruby-1.9.2-p290.tar.gz
cd ruby-1.9.2-p290
./configure
make
make install
cd ~
rm -rf ~/tmp
 
echo "Install Passenger and Nginx"
 
mkdir ~/tmp && cd ~/tmp
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar xzvf pcre-8.21.tar.gz
cd pcre-8.21
./configure && make && make install
cd ~/tmp
 
wget http://nginx.org/download/nginx-1.1.12.tar.gz
wget http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz
tar xzvf nginx-1.1.12.tar.gz
tar xzvf nginx_upload_module-2.2.0.tar.gz
 
gem install passenger
apt-get install libcurl4-openssl-dev -y
passenger-install-nginx-module --prefix=/opt/nginx --nginx-source-dir=/root/tmp/nginx-1.1.12 --extra-configure-flags=--add-module='/root/tmp/nginx_upload_module-2.2.0'
#passenger-install-nginx-module --auto --auto-download
cd ~
rm -rf ~/tmp
 
cd ~
wget https://raw.github.com/rafaelbiriba/Ubuntu-VPS-Builder/master/ubuntu_11-10_desktop/nginx-init.sh
cp nginx-init.sh /etc/init.d/nginx
rm nginx-init.sh
chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults
/etc/init.d/nginx start
 
echo "Configure iptables"
 
apt-get install iptables
 
tee /etc/init.d/firewall <<ENDOFFILE
#!/bin/bash
 
start(){
# Accepting all connections made on the special lo - loopback - 127.0.0.1 - interface
iptables -A INPUT -p tcp -i lo -j ACCEPT
 
# Rule which allows established tcp connections to stay up
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# SSH:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
# DNS:
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
 
# HTTP e HTTPS:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
 
# Block others ports
iptables -A INPUT -p tcp --syn -j DROP
iptables -A INPUT -p udp --dport 0:1023 -j DROP
 
}
stop(){
iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
}
 
case "\$1" in
"start") start ;;
"stop") stop ;;
"restart") stop; start ;;
*) echo "start or stop params"
esac
ENDOFFILE
 
chmod +x /etc/init.d/firewall
update-rc.d firewall defaults 99
/etc/init.d/firewall start
 
echo "Install MySQL"
 
apt-get install mysql-server mysql-client libmysqlclient-dev -y
 
echo "Install postfix"
 
# Install type: Internet Site
# Default email domain name: example.com
apt-get install postfix mailutils telnet -y
/usr/sbin/update-rc.d postfix defaults
/etc/init.d/postfix start
 
echo "Install gem bundler"
 
gem install bundler
 
echo "VPS Setup Complete"

Qualquer dúvida, estamos aí para trocar ideias 😉

___________________________________________________

Atualização 1 – 07/fev/2012:

Agradeço ao leitor Laércio pelo contato via email, reportando um problema, onde se o ubuntu estiver muito desatualizado, o script não roda direito. Com isso adicionei lá no github e no exemplo acima os comandos apt-get update e apt-get upgrade para atualizar o ubuntu antes de fazer qualquer coisa. Obrigado mais uma vez Laércio !

Share