Building a Rails Server
Fellow Los Techie ****John Petersen** took the famous (or infamous) **Nerd Dinner** **application created by Scott Hanselman and ported it to the **Ruby on Rails platform. When John was developing this, I recommended we actually put it online and proceeded to purchase the domain name www.railsdinner.com
Owning the domain name was one thing, hosting the site was entirely another. Last week I decided to find a place to host Rails Dinner. That place conveniently, is the server closet in my home office.
I had a spare machine not really doing much so I decided to appropriate it as a Linux server capable of hosting Ruby on Rails applications.
The Tools
Hosting a Rails application requires a number of moving parts. The moving parts I installed and their purpose are as follows:
Software | Purpose |
Ubuntu 9.10 Server | Linux server. |
Ruby (including Gems) | The runtime for ruby |
Ruby on Rails | MVC based web application framework |
MySQL | Open source SQL database. |
Passenger | Runtime deployment tool for Rails applications. |
nginx | High performance web server |
The Steps
Around the 3rd or 4th time of trying to install this server I decided to keep a log. The following tasks are what you need to do to get a Rails Server up and running.
Task | Purpose |
Download and install Ubuntu Server | www.ubuntu.com |
sudo apt-get install ubuntu-desktop | Optional step to add GUI support to your Linux server. |
sudo apt-get install ruby-full build-essential | Install ruby and all necessary libraries. |
sudo apt-get install rubygems | Install ruby gems distribution tools |
sudo gem install rails | Install rails |
sudo apt-get install mysql-server | Install mySQL Server |
sudo apt-get install mysql-client libmysql-ruby libmysqlclient15-dev | Install all libraries used to talk to mySQL |
sudo gem install passenger | Download all libraries needed to install passenger |
sudo apt-get install libopenssl-ruby | Install library required to compile nginx web server |
sudo apt-get install libssl-dev | Install library required to compile nginx web server |
sudo apt-get install zlib1g-dev | Install library required to compile nginx web server |
cd /var/lib/gems/1.8/bin |
Change to directory where nginx build files are located. </p>
NOTE: You may want to add this folder to your systems PATH settings</td> </tr> |
sudo ./passenger-install-nginx-module |
Downloads code and compiles (yes compiles!) custom web server with passenger built in. </p>
NOTE: This command is finicky and took me a few tries to get it to run properly. NOTE: nginx is a high performance web server that is used by a lot of major web sites. It does not have a module/plug-in architecture so passenger is compiled directly into the server code. Web Server ConfigurationAfter your web server compiles you need do add a script to your system to facilitate the stopping/starting/restarting of the nginx web server. I found the script (and instructions) below at the following site: http://library.linode.com/development/frameworks/ruby/ruby-on-rails/nginx-ubuntu-9.10-karmic #! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile
/opt/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile
/opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >;&2
exit 1
;;
esac
exit 0
</div> </div> Copy this file to a file called “nginx” in your /etc/init.d folder Run the following code to make the script runnable and start your nginx web server when Ubuntu launches.
chmod +x /etc/init.d/nginx
usr/sbin/update-rc.d -f nginx defaults </div> </div> Now you can start your server with the following command /etc/init.d/nginx start The last step is to drop your code into a folder on your server and configure nginx to use it. When you create the folder where you plan on installing your rails application make sure to use the sudo command. The sudo command insures that nginx and passenger can run your code. The following two lines create a directory for your application code.
cd /home
sudo mkdir www.railsdinner.com </div> </div> Now copy your Rails source code into this folder. Finally you need to add a configuration setting to your nginx configuration file (cd /opt/nginx/conf/nginx.conf) . Add the following code inside the http{} brackets in your .conf file. server {
listen 80;
server_name railsdinner.com www.railsdinner.com;
root /home/www.railsdinner.com/public; # <--- be sure to point to 'public'!
passenger_enabled on;
} </div> </div>
Finally Rails Dinner!Now simply restart your nginx server and you are off to the races. /etc/init.d/nginx restart Go check it out for yourself www.railsdinner.comThanks!Thanks to John Petersen for creating this application. It was a great learning experience and it took his work to inspire this endeavor. |