Rails on OpenBSD 4.3
June 7th, 2008
OpenBSD released version 4.3 of their OS on May first. This version is especially exciting because there has been considerable effort made by the ports contributors to get the Rails stack up to date. This makes setting up a server with OpenBSD considerably easier than my last set of posts regading deploying with OpenBSD. A few things have changed, but the idea is the same, so I will run through a full stack setup from a scratch install. If you need information on installing OpenBSD there is a great tutorial that will get you started.
Post Install
You will be greeted with a vanilla install and not a whole lot to work with. Let’s get some basic sytem tools installed. We will use OpenBSD’s pkg_add tool to get us up and running quickly. The stack we will setup will include:
- Helpful system tools
- Ruby / Rails
- Thin
- MySQL
- nginx
Helpful System Tools
# build the locate database
% /usr/libexec/locate.updatedb
# setup pkg_add path for easy installs
% export PKG_PATH=ftp://ftp.openbsd.org/pub/OpenBSD/4.3/packages/i386/
# install bash (optional)
% pkg_add -i bash
% chsh -s bash
# install other needed system tools. we use pkg_add with the -i
# flag for interactive mode. this way it will ask you what version
# of a specific software you want if there is more than one available.
% pkg_add -i curl nano subversion git
Ruby / Rails / Thin
% pkg_add -i ruby ruby-iconv ruby-gems
% gem update --system
% gem install rails thin
# if you want to use thin with sockets you will need to install
# eventmachine from source to get version 0.11
% gem install eventmachine --source http://code.macournoyer.com
MySQL
% pkg_add -i mysql-server
% mysql_install_db
# start mysql
% mysqld_safe &
Create the file /etc/rc.conf.local and place the following into it.
mysql="YES"
Now open up /etc/rc.local and add the following script just before the ending echo ”.”.
if [ X"${mysql}" == X"YES" -a -x /usr/local/bin/mysqld_safe ]; then
echo -n " mysqld"; /usr/local/bin/mysqld_safe --user=_mysql --log --open-files-limit=256 &
for i in 1 2 3 4 5 6; do
if [ -S /var/run/mysql/mysql.sock ]; then
break
else
sleep 1
echo -n "."
fi
done
fi
This will setup mysql to start when the system boots.
nginx
% pkg_add -i nginx
Then open up /etc/rc.local and add the following after your mysql entry.
if [ -x /usr/local/sbin/nginx ]; then
echo -n ' nginx'; /usr/local/sbin/nginx
fi
This will setup nginx to start when the system boots. I like to modify my nginx configs a little bit taking a little away from the debian linux apache2 and nginx configs.
% cd /etc/nginx
% mv nginx.conf nginx.conf.orig
% mkdir sites-available
% mkdir sites-enabled
% curl -O http://aaronbedra.com/nginx.conf
% cd sites-available
% curl -O http://aaronbedra.com/vhost
This will enable nginx to automagically load any vhost file that is in sites-enabled. We use the sites-available and sites-enabled directories and symlink from one to another. I prefer this, you don’t have to do this if you already have an nginx setup you like to use. Assuming you followed the above steps the only thing you have left to do is edit the vhost file and add your sites information. I would suggest renaming the vhost file to something a little more descriptive as well. Don’t forget to create the directory in /var/log/nginx/yoursite or nginx will fail to start. Once you have completed that you just need to symlink your new config into sites-enabled and restart nginx.
% ln -s /etc/nginx/sites-available/vhost /etc/nginx/sites-enabled/vhost
# start nginx
% nginx
# restart nginx
% kill -HUP `cat /var/run/nginx.pid`
Post Setup Notes
There is one small problem with eventmachine on OpenBSD. When thin intializes eventmachine at application boot time, it tries to use the wrong eventmachine library. You need to use the pure ruby version. An easy way to fix this is to add the following to your .profile
export EVENTMACHINE_LIBRARY="pure_ruby"
This will make eventmachine happy and thin able to run. The reason for choosing thin and nginx over the other implementations is because thin can connect to to nginx via sockets, and nginx is just plain easier to configure, not to mention faster. Obviously you can season this setup to taste if you prefer other stack implementations.
Surviving the Reboot
It’s good practice to have a thin config file that can start your application. Let’s take a look at how to configure thin for your app.
% mkdir /etc/thin
% thin config -C /etc/thin/your_config.yml -s2 -S /tmp/thin.sock
This will create a thin config file that will spawn to instances of thin (s2) and use sockets instead of ports. You can change the number of instances it spawns or where you want to put your sockets if you so desire. Now that you have a running system, you just need to add one more thing to make sure that your app starts if your server reboots. To make this work you need to do two things.
% ln -s /usr/local/bin/thin /usr/bin/thin
This will make sure thin is available at boot time. Next you need to add a line in etc/rc.local to start your app. Add this after the other startup scripts you added previously.
echo -n " thin"; thin start -C /etc/thin/your_config.yml
Happy OpenBSD-ing!
Rails on OpenBSD part 3 - Setting up Rails
July 29th, 2007
# pkg-add -i mysql-server
You will get a message when you have completed the install telling you to run the initial db creation so let's go ahead and do that now. First we have to start the server.
# /usr/local/bin/mysqld_safe &
# mysql_install_db
This will set up the initial tables for your database. Don't forget to set the root password!
# mysqladmin -u root password your_super_secret_password
Since we are going to be modifying some of our services at boot up, let's take a small detour and disable some services that aren't needed. You don't actually have to do this, but I would recommend it for the safety of the machine itself. Find and comment out the following lines in /etc/inetd.conf.
#ident stream tcp nowait _identd /usr/libexec/identd identd -el
#ident stream tcp6 nowait _identd /usr/libexec/identd identd -el
#daytime stream tcp nowait root internal
#daytime stream tcp6 nowait root internal
#time stream tcp nowait root internal
#time stream tcp6 nowait root internal
Now let's setup MySQL to start at bootup. First modify /etc/rc.conf
inetd=NO
Now add the following line into rc.conf.local
mysql=YES
Now enter the following in /etc/rc.local after the 'starting local daemons' and before the following echo '.'
if [ X"${mysql}" == X"YES" -a -x /usr/local/bin/mysqld_safe ]; then
echo -n " mysqld"; /usr/local/bin/mysqld_safe --user=_mysql --log --open-files-limit=256 &
for i in 1 2 3 4 5 6; do
if [ -S /var/run/mysql/mysql.sock ]; then
break
else
sleep 1
echo -n "."
fi
done
fi
Now that MySQL is installed, let's get the rest of the stack running. The next piece is going to be ruby.
# cd /usr/ports/lang/ruby
# make all install clean
# cd /usr/ports/devel/ruby-gems && make install
# pkg_add /usr/ports/packages/i386/all/ruby-iconv-1.8.4p0.tgz
This will complete your setup of Ruby and rubygems. We can now complete the stack by installing Rails and Mongrel. You can always install any other needed gems, but that part is up to you.
# gem install rails -y
# gem install mongrel -y
This completes the install of the your stack and will get you up and running on rails. There is still a little bit of post install setup and cleanup left to do and I will post one more article in this series on adding new users, securing ssh from root logins, and setting up the firewall with pf. Stay tuned for more.
Rails on OpenBSD part 2 - Updating
July 16th, 2007
PKG_PATH=ftp://ftp.openbsd.org/pub/OpenBSD/4.1/packages/i386/
export PKG_PATH
Once you have done this, simply reload your .profile by sourcing it, or by logging out and back in. Now you have the ability to use the pkg_add system in interactive mode.The first thing to do now is install a useable shell. Let's install bash and some other important tools that we will need later
# pkg_add -i bash
# pkg_add -i cvsup (when prompted choose the no-x11 version)
# pkg_add -i curl
Now let's grab the sys, src, and ports archives so that we can use them for updating later.
# curl -O ftp://ftp.openbsd.org/pub/OpenBSD/4.1/src.tar.gz (core system)
# curl -O ftp://ftp.openbsd.org/pub/OpenBSD/4.1/sys.tar.gz (kernel source)
# curl -O ftp://ftp.openbsd.org/pub/OpenBSD/4.1/ports.tar.gz (ports tree)
Now let's move these packages to the proper places and extract them.
# mv src.tar.gz /usr/src
# cd /usr/src
# tar -xvzf src.tar.gz
# mv sys.tar.gz /usr/src
# cd /usr/src
#tar -xvzf sys.tar.gz
# mv ports.tar.gz /usr
# cd /usr
# tar -xvzf ports.tar.gz
You now have the update framework in place for future updating of your system. Now let's create the cvsup files needed to run updates for your server. The first file you will create will update your src directories. Let's call it cvsup-src.
# Defaults that apply to all the collections
*default release=cvs
*default delete use-rel-suffix
*default umask=002
*default host=anoncvs3.usa.openbsd.org
*default base=/usr
*default prefix=/usr
*default tag=OPENBSD_4_1
# If your network link is a T1 or faster, comment out the following line.
# *default compress
OpenBSD-src
Now just copy the file you created and name it cvsup-ports. The only thing you need to change is the last line. Just change it from OpenBSD-src to OpenBSD-ports. Now that you have all of these things in place let's run cvsup and grab the latest versions of the source trees.
# cvsup -g -L 2 cvsup-src
# cvsup -g -L 2 cvsup-ports
After you have run this you will now have the latest version of the OpenBSD source. I would recommend that you follow the next couple steps for actually rebuilding your system but they are technically not required.
Rebuilding your world
After you have run the update scripts you can now start the rebuilding process. The first step is building your kernel. Here's how we do it.
# cp /bsd /bsd.old
# cd /usr/src/sys/arch/i386/conf/
# config GENERIC
# cd /usr/src/sys/arch/i386/compile/GENERIC/
# make clean && make depend && make && make install
This step will take a bit of time to complete. Once you have completed without errors just reboot to test out your new kernel. You're not quite done yet, you still have to complete this step with your ports tree as well as the system itself. Let's take a look at the ports tree.
# cd /usr/ports/infrastructure/build/
# ./out-of-date
This will give you a list of ports that are out of date and need to be recompiled. Since you don't have anything installed from ports yet it's not really a big deal. Now let's tackle the most time consuming part of our rebuild, the system.
# rm -rf /usr/obj/*
# cd /usr/src
# make obj
# cd /usr/src/etc && env DESTDIR=/ make distrib-dirs
# cd /usr/src
# make build
This will take a bit longer (usually somewhere around twice as long) than the kernel compile. Once you have completed successfully you can now reboot into your new running system.
That's all for this part of the tutorial. The next step will cover the ruby on rails stack install and basic application setup.

