• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略

Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了linux系统网卡配置,linux系统dns配置,linux系统怎么配置ip,linux系统安装配置,linux系统配置ip等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

安装 RVM

通常使用 RVM 或 rbenv 来安装 Ruby,这里选用 RVM。

$ curl -sSL https://get.rvm.io | bash -s stable

</div>

载入 RVM :

$ source /home/libuchao/.rvm/scripts/rvm
$ rvm -v
rvm 1.25.12 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com> ......

</div>

再执行以下命令:

$ type rvm
rvm is a function
......

</div>

说明 RVM 安装正确。
安装 Ruby

用 RVM 安装 Ruby

$ rvm install 2.1.0
$ rvm use 2.1.0 --default
$ ruby -v
ruby 2.1.0p0

</div>

国内服务器推荐替换 RubyGems 的到淘宝镜像

$ gem sources -r https://rubygems.org/
$ gem sources -a http://ruby.taobao.org/

</div>

否则安装 Gem 可能会非常非常慢。
安装 Rails

其实 Rails 也是一个 Gem

$ gem install rails --no-ri --no-rdoc -V
......
$ rails -v
Rails 4.0.2

</div>

至此,Rails 环境已经安装完成。
安装 MySQL

安装 Mysql 及相应的库文件:

$ sudo apt-get install mysql-server libmysqlclient-dev

</div>

然后进行一些安装方面的设置:

$ /usr/bin/mysql_secure_installation

</div>

创建相应的数据库,并为它新建一个权限小一些的用户:

mysql> CREATE DATABASE blix_production;
mysql> GRANT ALL PRIVILEGES ON blix_production.* TO blix@localhost IDENTIFIED BY "123456";
mysql> flush privileges;
mysql> exit

</div>

导入数据:

$ mysql -u blix -p blix_production < database.sql

</div>

安装 Nginx

Nginx 专门处理静态请求,并作为 Unicorn 的反向代理

编辑 /etc/apt/sources.list,末尾处添加以下两行

deb http://nginx.org/packages/ubuntu/ precise nginx
deb-src http://nginx.org/packages/ubuntu/ precise nginx

</div>

添加 Nginx 签名

$ wget http://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key

</div>

安装 Nginx

$ sudo apt-get update
$ sudo apt-get install nginx

</div>

安装完成后可以在浏览器中输入 http://server-ipaddress 查看是否安装正确。
配置 Unicorn

首先编译一下静态文件:

$ RAILS_ENV=production rake assets:clean
$ RAILS_ENV=production rake assets:precompile

</div>

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"

</div>

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

</div>

将这个 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

</div>

启动 Unicorn:

$ service unicorn start

</div>

在浏览器中输入 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;
  }
}

</div>

此时应该可以通过域名直接访问了。

</div>

您可能想查找下面的文章:

  • Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略

相关文章

  • 设计模式中的模板方法模式在Ruby中的应用实例两则
  • 升级到mac 10.10之后使用pod出现问题的解决方法
  • 简要说明Ruby中的迭代器
  • Ruby中类变量和实例变量的比较
  • 在Ruby中利用Net::SMTP类发送电子邮件的教程
  • 在Ruby on Rails中使用Rails Active Resource的教程
  • Ruby类继承、抽象类、类拓展混入、代理类实例
  • Ruby中的block、proc、lambda区别总结
  • 详解Ruby当中的算数运算
  • Windows下ruby语言安装教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 简单对比分析Ruby on Rails 和 Laravel
    • ruby实现修改ubuntu下的hosts
    • 在Ruby on Rails中优化ActiveRecord的方法
    • 优化Ruby代码使程序运行速度提高的例子
    • Ruby数组(Array)学习笔记
    • ruby 变量
    • ruby+nokogori抓取糗事百科前10页并存储进数据库示例
    • Ruby一行代码实现的快速排序
    • Ruby 字符串处理
    • Ruby中的方法(函数)学习总结

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有