This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
With the app deployed, our next step is to start the web server. The Guestbook app is set up to run using Nginx as a reverse proxy, and Unicorn as its web server. We need to tweak our Nginx and Unicorn configurations before we can start them up.
Edit config/nginx.conf
:
upstream unicorn {
server unix:/tmp/unicorn.guestbook.sock fail_timeout=0;
}
server {
client_max_body_size 4G;
listen 80 default;
root /home/deploy/guestbook/current/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
}
Edit config/unicorn.rb
:
pid "/home/deploy/guestbook/current/tmp/pids/unicorn.pid"
stderr_path "/home/deploy/guestbook/current/unicorn/unicorn.log"
stdout_path "/home/deploy/guestbook/current/unicorn/unicorn.log"
listen "/tmp/unicorn.guestbook.sock"
worker_processes 2
timeout 30
Create config/unicorn_init.sh
(based on the Unicorn team's example script):
#!/bin/sh
set -e
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop unicorn Rack app server
### END INIT INFO
# Example init script, this can be used with nginx, too,
# since nginx and unicorn accept the same signals.
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deploy/guestbook/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT && bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
INIT_CONF=$APP_ROOT/config/init.conf
UPGRADE_DELAY=${UPGRADE_DELAY-2}
action="$1"
set -u
test -f "$INIT_CONF" && . $INIT_CONF
OLD="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 $(cat $PID)
}
oldsig () {
test -s "$OLD" && kill -$1 $(cat $OLD)
}
case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
su -c "$CMD" - deploy
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting 'su -c "$CMD" - deploy' instead"
su -c "$CMD" - deploy
;;
upgrade)
if oldsig 0
then
echo >&2 "Old upgraded process still running with $OLD"
exit 1
fi
cur_pid=
if test -s "$PID"
then
cur_pid=$(cat $PID)
fi
if test -n "$cur_pid" &&
kill -USR2 "$cur_pid" &&
sleep $UPGRADE_DELAY &&
new_pid=$(cat $PID) &&
test x"$new_pid" != x"$cur_pid" &&
kill -0 "$new_pid" &&
kill -QUIT "$cur_pid"
then
n=$TIMEOUT
while kill -0 "$cur_pid" 2>/dev/null && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && kill -0 "$cur_pid" 2>/dev/null
then
echo >&2 "$cur_pid still running after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting 'su -c "$CMD" - deploy' instead"
su -c "$CMD" - deploy
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
Connect to your server via ssh
and run:
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /home/deploy/guestbook/current/config/nginx.conf /etc/nginx/sites-enabled/guestbook
sudo ln -s /home/deploy/guestbook/current/config/unicorn_init.sh /etc/init.d/unicorn_guestbook
sudo update-rc.d unicorn_guestbook defaults
sudo service nginx restart
echo 'SECRET_KEY_BASE=...' >> ~/guestbook/shared/.rbenv-vars
On your development machine, add to config/deploy.rb
:
append :linked_files, ".rbenv-vars"
From your development machine's terminal, run:
bundle exec cap production deploy
Connect to your server via ssh
and run:
sudo /home/deploy/guestbook/current/config/unicorn_init.sh restart
Your Rails app should now be running on your server.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up