部署 Ubuntu + Passenger + Apache 部署 Ruby on Rails

liukai · June 25, 2015 · Last by yanbin92 replied at November 02, 2016 · 4980 hits

创建账号

假设你已经用 root 帐号通过 SSH 登陆服务器。 出于安全考虑,不要使用 root 帐号运行 web 应用。这里新建一个专门用于部署的用户,例如 deploy 或者其它你喜欢的名字。运行以下命令创建用户:

useradd -m -s /bin/bash deploy

将用户加入 sudo 群组,以便使用 sudo 命令:

adduser deploy sudo

为 deploy 用户设置密码:

passwd deploy

退出当前 SSH 链接,用 deploy 帐号重新登陆。

安装 RVM 和 Ruby

更新 apt,并安装 curl:

sudo apt-get update
sudo apt-get install curl

然后安装 RVM:

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

RVM 安装完毕后,重新登陆 SSH,让 RVM 配置生效。然后安装 Ruby 2.0.0:

$ rvm use --install --default 2.0.0
$ rvm use 2.1.2@your_gemset --create --default

Ruby 安装过程会请求 apt-get update 的权限,并自动安装系统依赖。安装完毕后,确认目前的 Ruby 版本:

$ ruby -v

安装 Passenger

Passenger 是一个 app server,支持基于 Rack 框架的 Ruby app(包括 Rails)。Passenger 的特点是需要作为模块编译到 Nginx 中,优点是配置简单,不需要自己写启动脚本。 安装 Passenger 最简单的方法是通过 apt 安装,首先导入 Passenger 的密钥(官方文档):

$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

安装 apt 插件以支持 https 传输:

sudo apt-get install apt-transport-https ca-certificates

添加 apt 源(对应 Ubuntu 12.04 LTS):

$ sudo bash -c 'echo "deb https://oss-binaries.phusionpassenger.com/apt/passenger precise main" > /etc/apt/sources.list.d/passenger.list'
$ sudo apt-get update

安装 Passenger 的包:

$ sudo apt-get install nginx-extras passenger    **这一步,我换了apache,所以这一步,搭建apache的朋友可以绕过,根据这个链接继续:https://github.com/ruby-china/ruby-china/wiki/Ubuntu-12.04-%E4%B8%8A%E4%BD%BF%E7%94%A8-Nginx-Passenger-%E9%83%A8%E7%BD%B2-Ruby-on-Rails**

sudo gem install passenger
sudo  passenger-install-apache2-module  

如果没有满足不要的包依赖关系,第二条命令会提示你需要安装些什么。

例如:

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

  1. The Apache 2 module will be installed for you.
  2. You'll learn how to configure Apache.
  3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

按照提示一步一步进行,我缺少 apache2-perfork-dev,libapr1-dev,libaprutil1-dev 这几个包。 这个 sudo passenger-install-apache2-module 命令多执行几次,直到配置完成。 你也可以执行命令:sudo apt-get install apache2-prefork-dev libapr1-dev libaprutil1-dev 来完成安装。 最后会提示你配置的模块内容: Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /home/liukai/.rvm/gems/ruby-2.0.0-p645@your_gemset/gems/passenger-5.0.10/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /home/liukai/.rvm/gems/ruby-2.0.0-p645@your_gemset/gems/passenger-5.0.10
  PassengerDefaultRuby /home/liukai/.rvm/gems/ruby-2.0.0-p645@your_gemset/wrappers/ruby
</IfModule>

After you restart Apache, you are ready to deploy any number of web applications on Apache, with a minimum amount of configuration! 将中间一段配置到 apache 的配置文件中。(这是我的配置文件位置:/etc/apache2/apache2.conf)

部署 rails 应用

配置 apache 的 VirtualHost 信息:

<VirtualHost *:80>
     ServerName www.yourhost.com
     # !!! Be sure to point DocumentRoot to 'public'!
     DocumentRoot /somewhere/public
     RailsEnv development  #这句很重要,否则会报错We're sorry,but something went wrong!"
     <Directory /somewhere/public>
        # This relaxes Apache security settings.
        AllowOverride all
        # MultiViews must be turned off.
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        #Require all granted
     </Directory>
  </VirtualHost>

修改网站根目录: 1、在终端窗口中输入"sudo vi /etc/apache2/apache2.conf"-->回车-->找到""的位置-->更改" /somewhere/public"为新的根目录就可以了。 2、在终端窗口中输入"sudo vi /etc/apache2/sites-available/000-default.conf"-->回车-->找到"DocumentRoot /var/www/html"的位置-->更改" /somewhere/public"为新的根目录。

编辑本地的 hosts 文件: ubuntu:/etc/hosts windowns: c:\windows\system32\drivers\etc\ 加上一句:

127.0.0.1 www.yourhost.com

重新 apache:

sudo service apache2 restart

安装 mysql 服务器

apt-get install mysql-server
apt-get install mysql-client
apt-get install libmysqlclient15-dev

启动本地的 mysql 服务器

service mysqld start

设置 gem source(国内还是设置一下吧)

gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l

参考资料

https://github.com/ruby-china/ruby-china/wiki/Ubuntu-12.04-%E4%B8%8A%E4%BD%BF%E7%94%A8-Nginx-Passenger-%E9%83%A8%E7%BD%B2-Ruby-on-RailsUbuntu(12.04 上使用 Nginx Passenger 部署 Ruby on Rails) http://blog.csdn.net/hetuo198881/article/details/8689007 http://www.linuxidc.com/Linux/2012-12/75230p2.htm https://github.com/ruby-china/ruby-china/wiki/%E5%A6%82%E4%BD%95%E5%AE%89%E8%A3%85-Rails-%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83

development #这句很重要,否则会报错 We're sorry,but something went wrong!"

这行报错 Syntax error on line 65 of /private/etc/apache2/extra/httpd-vhosts.conf: Invalid command 'development', perhaps misspelled or defined by a module not included in the server configuration

#1 楼 希望回复下

在你给的链接中找到全的了 是 RailsEnv development

You need to Sign in before reply, if you don't have an account, please Sign up first.