Objective is to end up with a reusable template of a VM that can be cloned for testing ruby on rails app deployment, upgrades etc. This post builds Ubuntu 14.04 LTS, nginx as the web server, passenger as the app server and mysql for the database. This is more of a memo for me, hence not explaining every single command.
Linux
Download server image from Ubuntu, http://www.ubuntu.com/download/server and set up your VM according to cloud providers process, or local vmware / fusion etc.
Get yourself ssh access https://help.ubuntu.com/community/SSH/OpenSSH/Configuring to install the ssh daemon, then copy your public key file to authorized_keys on the new server. Much easier than console access only from this point!
sudo apt-get update sudo apt-get install openssh-server sudo apt-get install avahi-daemon avahi-discover
Name Services
If you’re using Fusion or some such local virtual machine technology that allocates IP addresses using DHCP, get some naming going to make it easier to connect to your VM. Enable bonjour protocols using sudo apt-get install avahi-daemon avahi-discover. Change the hostname, sudo vi /etc/hostname and sudo vi /etc/hosts to change 127.0.1.1 entry then reboot, eg use a single hostname such as labweb1
BROKEN – cannot set up multiple local virtual hosts using avahi, mimicking how this is done in regular DNS to allow nginx server blocks (or apache virtual hosts) to work. Airtonix has scripts but couldn’t get them to work
SSH Access
With name services now set up, Get yourself ssh access – copy your public key file to .ssh/authorized_keys on the new server. Much easier than console access only from this point!
Web Server
Install nginx https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-14-04-lts
sudo apt-get install nginx
Ruby
sudo apt-get install git git clone https://github.com/sstephenson/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bash_profile echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc
restart your shell
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build sudo apt-get install build-essential libssl-dev rbenv install 2.1.3 rbenv global 2.1.3 echo "gem: --no-document" >> ~/.gemrc
App Server
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7 sudo apt-get install apt-transport-https ca-certificates
create file /etc/apt/sources.list.d/passenger.list and insert deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main
sudo chown root /etc/apt/sources.list.d/passenger.list sudo chmod 600 /etc/apt/sources.list.d/passenger.list sudo apt-get update sudo apt-get install nginx-extras passenger
Edit /etc/nginx/nginx.conf and uncomment passenger_root and passenger_ruby
Note this will mean passenger launches with the default ruby installed by ubuntu in /usr/bin/ruby – this is OK!
Edit /etc/nginx/nginx.conf to look similar to this:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/<INSERT YOUR RAILS APP NAME>/current/public; passenger_ruby /Users/alanwhite/.rbenv/versions/2.1.3/bin/ruby passenger_enabled on; passenger_set_cgi_param SECRET_KEY_BASE "blah whatever it was in your ruby on rails app"; }
This will ensure passenger uses your installed ruby version and not the system installed one. There’s still a flaw here in that to change ruby version you have to edit this file as root.
Database
Going to implement a mysql database here too, it’s really a web server, but one of the application use cases I need to experiment with relies on a mysql database local to the web server, rather than on a separate (virtual or otherwise) machine.
sudo apt-get install mysql-server
I use the password labsql1 usually for the mysql root user. This is a transient lab that gets torn down.
sudo mysql_install_db
Might be a transient lab but let’s get rid of the defaults.
sudo mysql_secure_installation
Now we have nginx, passenger and mysql running but haven’t deployed an app to it. Probably a good time to stop and use your virtualisation managers clone or snapshot facility, whatever works best, to create a known starting point for testing apps.
One of the areas I want to investigate further – needing all the dev libraries to install gems – seems wrong
sudo apt-get install libmysqlclient-dev
Prepare For Deployment
I’d like to deploy my test app to /var/www/my_app_name. Unfortunately Ubuntu 14 LTS doesn’t allow package deployers to point anything there so the installed nginx points to /usr/share/nginx/html
sudo mkdir /var/www sudo chgrp www-data /var/www sudo chmod 775 /var/www
Don’t forget to put you git/bitbucket credentials for deploy on the new server if you’re using a capistrano recipe that pulls down from one of those repo hosts.