Skip to content

Instantly share code, notes, and snippets.

@erotte
Created July 26, 2011 20:04
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save erotte/1107861 to your computer and use it in GitHub Desktop.
Save erotte/1107861 to your computer and use it in GitHub Desktop.
Server Setup Ubuntu 10.4/rvm/nginx/passenger Hosteurope VM

Server Setup

Unsere Setup-Routine für eine Rails-App mit Nginx, Passenger, rvm auf einen Host Europe-VPS mit Ubuntu 10.4.

Installation der benötigten Pakete

apt-get update
apt-get install -y build-essential bison openssl libreadline5 libreadline5-dev curl \
git-core zlib1g zlib1g-dev libopenssl-ruby libcurl4-openssl-dev libssl-dev \
libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libmysqlclient-dev \
mysql-client mysql-server

Die Option -y beantwortet alle Confirm-Dialoge mit "yes". Nginx wird später mit dem Passenger-Gem installiert.

RVM Installation

RVM wird als root systemweit installiert:

bash < <(curl -s -B https://rvm.beginrescueend.com/install/rvm)

Folgendes muss anschließend noch an das Ende der .bashrc angefügt werden:

[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"

Jetzt noch das Script sourcen, um mit RVM direkt arbeiten zu können:

source /usr/local/rvm/scripts/rvm

Folgender Aufruf sollte nun "rvm is a function" zurückgeben:

type rvm | head -n1

Nun das gewünschte Ruby installieren und anschließend als default markieren:

rvm install 1.9.2
rvm use 1.9.2 --default

Keine Docs für gems:

echo "gem: --no-rdoc --no-ri" >> /etc/gemrc

Passenger Installation

gem install passenger
passenger-install-nginx-module

Nginx kann (sofern noch nicht vorhanden) gleich mit installiert werden, Passenger fragt danach.

Anschließend noch den RVM Wrapper für Passenger setzen:

rvm wrapper 1.9.2 passenger

Weitere Informationen finden sich auf der RVM Passenger Website

Nginx Setup

Die Konfiguration für die Webapp erstellen:

server {
  listen 80;
  server_name yourapp.com;
  root /var/www/yourapp/public;
  passenger_enabled on;
}

Apache stoppen:

/etc/init.d/apache2 stop

...und Autostart deaktivieren

update-rc.d -f apache2 remove

Es muss noch ein Init-Script angelegt werden:

cd &&
git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git &&
mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx &&
chown root:root /etc/init.d/nginx &&
/etc/init.d/nginx restart

Autostart aktivieren

    update-rc.d nginx defaults

Ggf rcconf installieren und Autostarts konfigurieren

apt-get install rcconf && rcconf

Plesk

Plesk mit allen Diensten kann man per /etc/init.d/psa stopall bzw. /etc/init.d/psa start stoppen und starten.

Root-User einrichten

Login als root um ein .ssh-Verzeichnis anzulegen:

mkdir ~/.ssh

Jetzt ausloggen und den SSH-Key für den Benutzer hochladen:

cat ~/.ssh/id_rsa.pub | ssh root@remotehost "cat >> .ssh/authorized_keys2"

Login als root um Berechtigungen für den Key zu setzen:

chmod 0600 ~/.ssh/authorized_keys2

Deploy User

Zunächst wird ein User zum deployen der Applikation(en) hinzugefügt:

useradd -m -s /bin/bash deploy
su deploy
mkdir ~/.ssh
# wieder als root (CTRL-d)
cp ~/.ssh/authorized_keys2 /home/deploy/.ssh/
chown deploy:deploy /home/deploy/.ssh/authorized_keys2 
chmod 0600 /home/deploy/.ssh/authorized_keys2

Der rvm-Pfad muss an das Ende der .bashrc angefügt werden:

[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"
# so z.B.:
echo '[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"' >> ~/.bashrc

.rvmrc global akzeptieren erspart einen Shell-Dialog, der das Setup unterbricht:

echo 'rvm_trust_rvmrcs_flag=1' >> ~/.rvmrc

Bundler

Bundler-Gem installieren

gem install bundler

Deployment mit vlad und SSH-Forward-Agent

Die (noch) lokale App benötigt die Gems vlad, vlad-git und vlad-extras

Gemfile

# deployment tools
group :rake do
  gem 'vlad'
  gem 'vlad-git'
  gem 'vlad-extras'
end

Beispiel für eine vlad config:

set :user, "deploy"
set :application, " repo"
set :domain, "178.77.76.127"
set :deploy_to, "/var/www/#{application}"
set :repository, "git@github.com:erotte/#{application}.git"
# Revision defaults to master
set :revision, "origin/rails3"
set :bundle_cmd, "/usr/local/rvm/gems/ree-1.8.7-2011.03/bin/bundle"
set :web_command, "/etc/init.d/nginx"
set :symlinks, { 'config/database.yml' => 'config/database.yml' }

role :app, domain
role :web, domain
role :db,  domain, :primary => true

require 'bundler/vlad'

namespace :vlad do
  desc "Full deployment cycle: Update, install bundle, migrate, restart, cleanup"
  remote_task :deploy, :roles => :app do
    %w(update symlink bundle:install migrate start_app cleanup).each do |task|
      Rake::Task["vlad:#{task}"].invoke
    end
  end
end

ssh Forward-Agent

Damit ssh-forwarding für das Deployment reibungslos funktioniert, muss eine ssh-config angelegt werden, deren Hostname genauso lautet wie die konfigurierte Domain in vlad (set :domain) . Beispiele:

# ~/.ssh/config
Host dein-hostname.tld
  User deploy
  IdentityFile ~/.ssh/id_rsa
  ForwardAgent yes

# config/deploy.rb
set :domain, "es-ist-dein-werk.de"      
set :user, "deploy"   

Falls noch keine Domain geschaltet ist, muss die IP als Alias verwendet werden

# ~/.ssh/config
Host 178.77.23...
  HostName 178.77.23...
  User deploy
  IdentityFile ~/.ssh/id_rsa
  ForwardAgent yes

# config/deploy.rb
set :domain, "178.77.23..."      
set :user, "deploy"

Ausserdem muss die SSH-Umgebung auf dem Server angepasst werden:

# /etc/ssh/sshd_config
PermitUserEnvironment yes

Pfad in ~/.ssh/environment setzen, z.B. so:

PATH=/usr/local/rvm/gems/ree-1.8.7-2011.03/bin:/usr/local/rvm/gems/ree-1.8.7-2011.03@global/bin....
GEM_HOME=/usr/local/rvm/gems/ree-1.8.7-2011.03

echo $PATH und echo $GEM_HOME als Deploy-User solten die entsprechenden Infos liefern. Mehr dazu in den Kommentaren in der config/deploy.rb

Das Vlad-Setup müsste nun funktionieren. Ggf. das App-Verzeichnis händisch anlegen und Rechte anpassen, falls es hier Probleme gibt.

rake vlad:setup

Damit der Deploy-User auschecken kann, muss der Host Key von Github manuell akzeptiert werden. Einfach initial ein clone mit den deploy-user durchführen.

git clone git@github.com:erotte/ repo.git
Initialized empty Git repository in /home/deploy/repo/.git/
The authenticity of host 'github.com (207.97.xxx.xxx)' can't be established.
RSA key fingerprint is 16:27:ac:a5:7......
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,207.97...' (RSA) to the list of known hosts.
#!/usr/bin/env bash
# login as root and run this script via bash & curl:
# bash < <(curl -s -B https://raw.github.com/gist/1107861/setup.sh)
apt-get update
apt-get install -y build-essential bison openssl libreadline5 libreadline5-dev curl \
git-core zlib1g zlib1g-dev libopenssl-ruby libcurl4-openssl-dev libssl-dev \
libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libmysqlclient-dev \
mysql-client mysql-server
bash < <(curl -s -B https://rvm.beginrescueend.com/install/rvm)
cd && echo "[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"" >> .bashrc
source /usr/local/rvm/scripts/rvm
type rvm | head -n1
rvm install 1.9.2
rvm use 1.9.2 --default
echo "gem: --no-rdoc --no-ri" >> /etc/gemrc
gem install passenger
passenger-install-nginx-module --auto --auto-download
rvm wrapper 1.9.2 passenger
/etc/init.d/apache2 stop && update-rc.d -f apache2 remove
cd &&
git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git &&
mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx &&
chown root:root /etc/init.d/nginx
update-rc.d nginx defaults
echo "Nginx successfully installed!"
echo "add this section (or similar) to your nginx.conf and edit:"
echo
echo "server {"
echo " listen 80;"
echo " server_name <yourapp.com>;"
echo " root /var/www/<yourapp>/public;"
echo " passenger_enabled on;"
echo "}"
echo
#!/usr/bin/env bash
# login as root and run this script via bash & curl:
# bash < <(curl -s -B https://raw.github.com/gist/1107861/user.sh)
# Root-User einrichten
# Login als root um ein .ssh-Verzeichnis anzulegen:
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys2
chmod 0600 ~/.ssh/authorized_keys2
# Deploy User
useradd -m -s /bin/bash deploy
su deploy
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys2
chmod 0600 ~/.ssh/authorized_keys2
echo '[[ -s "/usr/local/rvm/scripts/rvm" ]] && . "/usr/local/rvm/scripts/rvm"' >> ~/.bashrc
echo 'rvm_trust_rvmrcs_flag=1' >> ~/.rvmrc
# log off
exit && exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment