Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
安装RVM
通常使用RVM或rbenv来安装Ruby,这里选用RVM。
$curl-sSLhttps://get.rvm.io|bash-sstable
载入RVM:
$source/home/libuchao/.rvm/scripts/rvm $rvm-v rvm1.25.12(stable)byWayneE.Seguin<wayneeseguin@gmail.com>......
再执行以下命令:
$typervm rvmisafunction ......
说明RVM安装正确。
安装Ruby
用RVM安装Ruby
$rvminstall2.1.0 $rvmuse2.1.0--default $ruby-v ruby2.1.0p0
国内服务器推荐替换RubyGems的到淘宝镜像
$gemsources-rhttps://rubygems.org/ $gemsources-ahttp://ruby.taobao.org/
否则安装Gem可能会非常非常慢。
安装Rails
其实Rails也是一个Gem
$geminstallrails--no-ri--no-rdoc-V ...... $rails-v Rails4.0.2
至此,Rails环境已经安装完成。
安装MySQL
安装Mysql及相应的库文件:
$sudoapt-getinstallmysql-serverlibmysqlclient-dev
然后进行一些安装方面的设置:
$/usr/bin/mysql_secure_installation
创建相应的数据库,并为它新建一个权限小一些的用户:
mysql>CREATEDATABASEblix_production; mysql>GRANTALLPRIVILEGESONblix_production.*TOblix@localhostIDENTIFIEDBY"123456"; mysql>flushprivileges; mysql>exit
导入数据:
$mysql-ublix-pblix_production<database.sql
安装Nginx
Nginx专门处理静态请求,并作为Unicorn的反向代理
编辑/etc/apt/sources.list,末尾处添加以下两行
debhttp://nginx.org/packages/ubuntu/precisenginx deb-srchttp://nginx.org/packages/ubuntu/precisenginx
添加Nginx签名
$wgethttp://nginx.org/keys/nginx_signing.key $sudoapt-keyaddnginx_signing.key
安装Nginx
$sudoapt-getupdate $sudoapt-getinstallnginx
安装完成后可以在浏览器中输入http://server-ipaddress查看是否安装正确。
配置Unicorn
首先编译一下静态文件:
$RAILS_ENV=productionrakeassets:clean $RAILS_ENV=productionrakeassets:precompile
Unicorn配置参考:
worker_processes2
timeout30
APP_PATH=File.expand_path("../..",__FILE__)
working_directoryAPP_PATH
listen8080,:tcp_nopush=>true
listen"/tmp/unicorn.sock",:backlog=>64
stderr_pathAPP_PATH+"/log/unicorn.stderr.log"
stdout_pathAPP_PATH+"/log/unicorn.stdout.log"
pidAPP_PATH+"/tmp/pids/unicorn.pid"
Unicorn自启动脚本:
#!/bin/sh
set-e
#Exampleinitscript,thiscanbeusedwithnginx,too,
#sincenginxandunicornacceptthesamesignals
#Feelfreetochangeanyofthefollowingvariablesforyourapp:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/libuchao/blix
APP_USER=libuchao
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="unicorn_rails-D-Eproduction-c$APP_ROOT/config/unicorn.rb"
action="$1"
set-u
old_pid="$PID.oldbin"
cd$APP_ROOT||exit1
sig(){
test-s"$PID"&&kill-$1`cat$PID`
}
oldsig(){
test-s$old_pid&&kill-$1`cat$old_pid`
}
case$actionin
start)
sig0&&echo>&2"Alreadyrunning"&&exit0
su-c"$CMD"-$APP_USER
;;
stop)
sigQUIT&&exit0
echo>&2"Notrunning"
;;
force-stop)
sigTERM&&exit0
echo>&2"Notrunning"
;;
restart|reload)
sigHUP&&echoreloadedOK&&exit0
echo>&2"Couldn'treload,starting'$CMD'instead"
su-c"$CMD"-$APP_USER
;;
upgrade)
ifsigUSR2&&sleep2&&sig0&&oldsigQUIT
then
n=$TIMEOUT
whiletest-s$old_pid&&test$n-ge0
do
printf'.'&&sleep1&&n=$(($n-1))
done
echo
iftest$n-lt0&&test-s$old_pid
then
echo>&2"$old_pidstillexistsafter$TIMEOUTseconds"
exit1
fi
exit0
fi
echo>&2"Couldn'tupgrade,starting'$CMD'instead"
su-c"$CMD"-$APP_USER
;;
reopen-logs)
sigUSR1
;;
*)
echo>&2"Usage:$0<start|stop|restart|upgrade|force-stop|reopen-logs>"
exit1
;;
esac
将这个shell在/etc/init.d/下做一个软连接,并使其开机自启动:
$chmod+x/home/libuchao/blix/config/unicorn_init.sh $sudoln-s/home/libuchao/blix/config/unicorn_init.sh/etc/init.d/unicorn $sudoupdate-rc.dunicorndefaults
启动Unicorn:
$serviceunicornstart
在浏览器中输入http://server_ipaddress:8080查看效果。
配置Nginx
Nginx配置参考:
upstreamblix_backend{
serverunix:/tmp/unicorn.sockfail_timeout=0;
}
gzipon;
gzip_disable"msie6";
client_max_body_size150m;
server{
listen80default;
return403;
}
server{
listen80;
server_namelibuchao.comwww.libuchao.com;
root/home/libuchao/blix/public;
try_files$uri/index.html$uri.html$uri@httpapp;
location@httpapp{
proxy_redirectoff;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-Host$host;
proxy_set_headerX-Forwarded-Server$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_bufferingon;
proxy_passhttp://blix_backend;
}
location~^(/assets){
access_logoff;
expiresmax;
}
}
此时应该可以通过域名直接访问了。