Skip to content

Instantly share code, notes, and snippets.

@moh-alsheikh
Created October 29, 2012 19:16
Show Gist options
  • Save moh-alsheikh/3975837 to your computer and use it in GitHub Desktop.
Save moh-alsheikh/3975837 to your computer and use it in GitHub Desktop.
Nginx and unicorn # Setup Deployment Enviroment Notes #
:::::::: Nginx and unicorn Deployment Enviroment ::::::::::
Nginx is a pure web server that's intended for serving up static content and/or redirecting the request to another socket to handle the request.
Unicorn is a Rack web server and only intended to host a 'Rack App' which is usually generating dynamic content. Rack apps can also serve up static content but it's less efficient than most other traditional web servers.
Most RoR setups use a combination of both traditional web servers and Rack servers to apply the best of both of their capabilities. Nginx is incredibly fast at request redirection through proxy balancing and serving up static content. Unicorn is quite capable of processing HTTP headers and balancing inbound requests to Ruby for processing.
http://nginx.org/en/docs/
neyard.com/blog/2010/everything-you-need-to-know-about-unicorn/
unicorn.bogomips.org
https://github.com/blog/517-unicorn
==========================================================================================================================================================================================================================================
:::::::: Installing Nginx and unicorn in linux ubunto ::::::::::
1- sudo apt-get install nginx
# To show commands for nginx server :
nginx -h
/etc/init.d/nginx -h
# To start the nginx server ::
sudo service nginx start
# Nginx configration file
/etc/nginx/nginx.conf
2- install unicorn
gem 'unicorn'
bundle install
3-
## Make a new configration file for nginx in our rails app by removing the default nginx file
from sites-enabled folder in /etc/nginx/sites-enabled
sudo rm default
# Create one inside our config foler with name nginx.conf and make simi link between them
sudo ln -s /home/mohammed/Desktop/railsproj/empapp/config/nginx.conf todo
# The new configrartion file will include the follwing ::
Now it will be linked with web-brik deafule rails wev-server
################################################################################################################
server {
listen 80 default;
root /home/mohammed/Desktop/railsproj/empapp/public;
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_pass http://localhost:3000;
}
# root /usr/share/nginx/www;
# server_name localhost;
}
#################################################################################################################
=====================================================================================================================
## if we would like to run the nginx with unicorn rack web server
1- modify the nginx configration file in config/nginx.conf with the follwing
=====================================================================================================================
nginx configration
################################################################################################################
upstream unicorn {
server unix:/tmp/unicorn.todo.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
#root /vagrant/public;
root /home/mohammed/Desktop/railsproj/empapp/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;
client_max_body_size 4G;
keepalive_timeout 10;
}
################################################################################################################
=====================================================================================================================
2- make a new configration file in config folder with name unicron.rb
and copy the follwing to this files
## Note :: We should include unicorn gem in our gem file
gem 'unicorn'
================================================================================
unicorn configration
################################################################################################################
working_directory "/home/mohammed/Desktop/railsproj/empapp"
pid "/home/mohammed/Desktop/railsproj/empapp/tmp/pids/unicorn.pid"
stderr_path "/home/mohammed/Desktop/railsproj/empapp/log/unicorn.log"
stdout_path "/home/mohammed/Desktop/railsproj/empapp/log/unicorn.log"
listen "/tmp/unicorn.todo.sock"
worker_processes 2
timeout 30
################################################################################################################
=====================================================================================================================
3- After creating the unicorn configration file we run the follwing command to check if our unicorn configration work
sudo service nginx stop
bundle exec unicorn -c config/unicorn.rb -D
sudo service nginx start
================================================================================
4- We can create service for unicorn by create a new shell file in config and call it unicorn_init.sh
the file will be the same with file in attachment ::
## now run the this from terminal ::
chmod +x config/unicorn_init.sh
## Make link for the file so we can start unicorn from any path
sudo ln -s /home/mohammed/Desktop/railsproj/empapp/config/unicorn_init.sh /etc/init.d/unicorn
## also we shoud run the follwing commanf from terminal to generate the bin file
bundle install --binstubs
## We can now test the nginx and unicorn by runing ::
sudo service nginx stop
sudo service unicorn start
sudo service nginx start
## open our web app
localhost:8080
================================================================================
Nginx & Unicorn commands
================================================================================
sudo service nginx start
sudo service nginx stop
sudo service unicorn start
sudo service unicorn stop
bundle exec unicorn -c config/unicorn.rb -D
#!/bin/sh
set -e
# 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/mohammed/Desktop/railsproj/empapp
PID=$APP_ROOT/tmp/pids/unicorn.pid
# CMD="$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
CMD="$APP_ROOT/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
# echo "$CMD"
action="$1"
set -u
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig () {
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig () {
test -s $old_pid && kill -$1 `cat $old_pid`
}
case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
su -c "$CMD" - mohammed
;;
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 '$CMD' instead"
su -c "$CMD" - mohammed
;;
upgrade)
if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(( $n - 1 ))
done
echo
if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit 1
fi
exit 0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su -c "$CMD" - mohammed
;;
reopen-logs)
sig USR1
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment