Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
程序员文章站
2024-01-07 13:45:04
安装 rvm
通常使用 rvm 或 rbenv 来安装 ruby,这里选用 rvm。
$ curl -ssl https://get.rvm.io | bash...
安装 rvm
通常使用 rvm 或 rbenv 来安装 ruby,这里选用 rvm。
$ curl -ssl https://get.rvm.io | bash -s stable
载入 rvm :
$ source /home/libuchao/.rvm/scripts/rvm $ rvm -v rvm 1.25.12 (stable) by wayne e. seguin <wayneeseguin@gmail.com> ......
再执行以下命令:
$ type rvm rvm is a function ......
说明 rvm 安装正确。
安装 ruby
用 rvm 安装 ruby
$ rvm install 2.1.0 $ rvm use 2.1.0 --default $ ruby -v ruby 2.1.0p0
国内服务器推荐替换 rubygems 的到淘宝镜像
$ gem sources -r https://rubygems.org/ $ gem sources -a http://ruby.taobao.org/
否则安装 gem 可能会非常非常慢。
安装 rails
其实 rails 也是一个 gem
$ gem install rails --no-ri --no-rdoc -v ...... $ rails -v rails 4.0.2
至此,rails 环境已经安装完成。
安装 mysql
安装 mysql 及相应的库文件:
$ sudo apt-get install mysql-server libmysqlclient-dev
然后进行一些安装方面的设置:
$ /usr/bin/mysql_secure_installation
创建相应的数据库,并为它新建一个权限小一些的用户:
mysql> create database blix_production; mysql> grant all privileges on blix_production.* to blix@localhost identified by "123456"; mysql> flush privileges; mysql> exit
导入数据:
$ mysql -u blix -p blix_production < database.sql
安装 nginx
nginx 专门处理静态请求,并作为 unicorn 的反向代理
编辑 /etc/apt/sources.list,末尾处添加以下两行
deb http://nginx.org/packages/ubuntu/ precise nginx deb-src http://nginx.org/packages/ubuntu/ precise nginx
添加 nginx 签名
$ wget http://nginx.org/keys/nginx_signing.key $ sudo apt-key add nginx_signing.key
安装 nginx
$ sudo apt-get update $ sudo apt-get install nginx
安装完成后可以在浏览器中输入 http://server-ipaddress 查看是否安装正确。
配置 unicorn
首先编译一下静态文件:
$ rails_env=production rake assets:clean $ rails_env=production rake assets:precompile
unicorn 配置参考:
worker_processes 2 timeout 30 app_path = file.expand_path("../..", __file__) working_directory app_path listen 8080, :tcp_nopush => true listen "/tmp/unicorn.sock", :backlog => 64 stderr_path app_path + "/log/unicorn.stderr.log" stdout_path app_path + "/log/unicorn.stdout.log" pid app_path + "/tmp/pids/unicorn.pid"
unicorn 自启动脚本:
#!/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/libuchao/blix app_user=libuchao pid=$app_root/tmp/pids/unicorn.pid cmd="unicorn_rails -d -e production -c $app_root/config/unicorn.rb" 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" - $app_user ;; 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" - $app_user ;; 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" - $app_user ;; reopen-logs) sig usr1 ;; *) echo >&2 "usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>" exit 1 ;; esac
将这个 shell 在/etc/init.d/下做一个软连接,并使其开机自启动:
$ chmod +x /home/libuchao/blix/config/unicorn_init.sh $ sudo ln -s /home/libuchao/blix/config/unicorn_init.sh /etc/init.d/unicorn $ sudo update-rc.d unicorn defaults
启动 unicorn:
$ service unicorn start
在浏览器中输入 http://server_ipaddress:8080 查看效果。
配置 nginx
nginx 配置参考:
upstream blix_backend { server unix:/tmp/unicorn.sock fail_timeout=0; } gzip on; gzip_disable "msie6"; client_max_body_size 150m; server { listen 80 default; return 403; } server { listen 80; server_name libuchao.com www.libuchao.com; root /home/libuchao/blix/public; try_files $uri/index.html $uri.html $uri @httpapp; location @httpapp { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-forwarded-host $host; proxy_set_header x-forwarded-server $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_buffering on; proxy_pass http://blix_backend; } location ~ ^(/assets) { access_log off; expires max; } }
此时应该可以通过域名直接访问了。