Rails on Ubuntu 10.04: Passenger, Nginx, MySQL, Varnish

I setup a new VPS this weekend and decided to share my notes.

My notes are from being logged in as root. If you're logged in as another user, you'll be typing sudo a lot. :)

Configure your locale
 
  • locale -a
  • locale-gen en_US.UTF-8
  • update-locale LANG=en_US.UTF-8
  • update-locale LANGUAGE=en_US.UTF-8
  • exit
  • ssh zer1 #zer1 is my VPS added to ~/.ssh/config 
  • locale -a #should have en_US.UTF-8 listed 
  • update-locale #errors? 

Set your time zone

  • dpkg-reconfigure tzdata
  • ntpdate ntp.ubuntu.com

Update Ubuntu

  • apt-get update && apt-get upgrade

Install dependencies

  • apt-get install build-essential libmagickcore-dev imagemagick libxml2-dev libxslt1-dev git-core libapr1-dev libssl-dev libmysqlclient15-dev libcurl4-openssl-dev curl libncurses5-dev libreadline5-dev libopenssl-ruby1.9

Install MySQL

  • apt-get install mysql-server libmysqlclient-dev libmysqlclient15-dev libmysql-ruby
  • mysql_secure_installation

Install latest Ruby from source

Set default environment for Rails

  • nano /etc/environment
  • add the following line to it:
  • export RAILS_ENV=production

Install Nginx and Passenger

Configure Nginx

  • mkdir /opt/nginx/conf/sites-enabled
  • nano /opt/nginx/conf/nginx.conf
  • add this within the http { } block:
  • change default listen port to 8080
  • include /opt/nginx/conf/sites-enabled/*;
  • nano /opt/nginx/conf/sites-enabled/test

*note port 8080 for Varnish

server {
   listen 8080;
   server_name mywebsite.com;
   root /home/deploy/test/public;
   passenger_enabled on;
}


  • service nginx restart

Add deploy user

  • adduser deploy
  • su deploy #switches to deploy
  • ssh-keygen -t rsa -C “deploy@myserver”
  • cat ~/.ssh/id_rsa.pub
  • copy the output into your favorite Git hosting as an allowed key (Assembla, Github, etc)
  • on your development machine:
  • cat ~/.ssh/id_rsa.pub
  • on the server as deploy user:
  • nano ~/.ssh/authorized_keys
  • paste in your development machine’s public key
  • git clone git@git.assembla.com:myapp.git      #lets you add host as known_host
  • rm -rf myapp         

Create a MySQL user and database

  • mysql -u root -p
  • create database myapp_prod;
  • create user 'mydbuser'@'localhost' identified by 'secret';
  • grant all privileges on myapp_prod.* to 'mydbuser'@'localhost';

Install Varnish HTTP cache

  • apt-get install subversion autotools-dev automake1.9 libtool autoconf libncurses-dev xsltproc quilt debhelper
  • svn co http://varnish-cache.org/svn/tags/varnish-2.0.5
  • cd varnish-2.0.5/varnish-cache
  • dpkg-buildpackage
  • cd ..
  • dpkg -i libvarnish1_2.0.6-2_i386.deb
  • dpkg -i varnish_2.0.6-2_i386.deb
  • service varnish start

Configure Varnish

  • nano /etc/varnish/default.vcl
  • Leave this at defaults, but now you know where it is :)
  • nano /etc/default/varnish
  • Comment out DAEMON_OPTS use the following instead:
  • DAEMON_OPTS="-a :80 -f /etc/varnish/default.vcl -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"
  • service varnish restart

 

Posted