Passenger + Nginx 部署Rails

0、假设你RoR环境装好了

1、安装Passenger

$ sudo gem install passenger
出现
Done installing documentation for passenger after 48 seconds
1 gem installed

说明安装好了

2、安装Nginx

由于nginx不支持动态的模块载入,所以要使用passenger来进行编译安装由passenger修改过的nginx
$ passenger-install-nginx-module
全选
   ⬢  Ruby
   ⬢  Python
   ⬢  Node.js
   ⬢  Meteor
出现
Installation instructions for required software


 * To install C++ compiler:
   Please install it with apt-get install build-essential


 * To install Curl development headers with SSL support:
   Please run apt-get install libcurl4-openssl-dev or libcurl4-gnutls-dev, whichever you prefer.


 * To install OpenSSL development headers:
   Please install it with apt-get install libssl-dev


If the aforementioned instructions didn't solve your problem, then please take
a look at our documentation for troubleshooting tips:


  https://www.phusionpassenger.com/library/install/nginx/
  https://www.phusionpassenger.com/library/admin/nginx/troubleshooting/


$ sudo apt-get update
$ sudo apt-get install build-essential libcurl4-openssl-dev libssl-dev
$ passenger-install-nginx-module


Do you want this installer to download, compile and install Nginx for you?


 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.12.1 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.


 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.



当遇到这个选择时,建议选择1,1代表自动完整安装并配置nginx,2是代表根据自己需求定制nginx

Where do you want to install Nginx to?


Please specify a prefix directory [/opt/nginx]: 


这个步骤是选择Nginx的安装目录,可以直接选择默认/opt/nginx。记住此处的安装目录。

This installer has already modified the configuration file for you! The
following configuration snippet was inserted:


  http {
      ...
      passenger_root /var/lib/gems/2.2.0/gems/passenger-5.1.6;
      passenger_ruby /usr/bin/ruby2.2;
      ...
  }


After you start Nginx, you are ready to deploy any number of Ruby on Rails
applications on Nginx.

安装成功

3、Nginx项目配置


$ vim /opt/nginx/conf/nginx.conf
http {
    passenger_root /var/lib/gems/2.2.0/gems/passenger-5.1.6;
    passenger_ruby /usr/bin/ruby2.2;


    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log  logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    server {
        listen       3333;
        server_name  127.0.0.1;
        root         /home/thinking/Documents/test_app/public;
        passenger_enabled on;
        rails_env production; 
    }

4、为项目配置安全密钥

在rails的Gemfile中加入
gem 'dotenv-rails'
然后运行
bundle install

在项目根目录下建立一个.env文件
然后运行
rake secret 
这个命令会随机生成一个安全密钥,将这个密钥复制下来,然后在.env中添加
SECRET_KEY_BASE = 你的密钥

最后修改项目根目录下的config/secrets.yml
development:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
test:
   secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

重启nginx就能成功访问到rails项目了

如果提示数据库找不到,运行
rails db:migrate RAILS_ENV=production

5、运行和停止


$ sudo /opt/nginx/sbin/nginx

$ ps -ef|grep nginx
root      42926   1976  0 23:40 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
$ sudo kill -QUIT 42926
OR
$ sudo /opt/nginx/sbin/nginx -s quit

6、出现Permission denied (errno=13)错误

问题:Cannot stat 'your_ror_proj_path/config.ru': Permission denied (errno=13); This error means that the Nginx worker process (PID 49188, running as UID 99) does not have permission to access this file. Please read this page to learn how to fix this problem: https://www.phusionpassenger.com/library/admin/nginx/troubleshooting/?a=upon-accessing-the-web-app-nginx-reports-a-permission-denied-error

解决:

参照提示https://www.phusionpassenger.com/library/admin/nginx/troubleshooting/?a=upon-accessing-the-web-app-nginx-reports-a-permission-denied-error

执行sudo chmod -R g+x,o+x your_ror_proj_path



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Phusion Passenger是一种基于Nginx或Apache的应用服务器,它可以帮助我们快速、稳定地部署Rails应用。下面以Redmine为例,介绍在CentOS下如何使用Phusion Passenger方式部署Rails应用。 1. 安装必要的软件 首先需要安装Ruby、RailsNginxPassenger等软件,可以使用以下命令: ``` sudo yum install epel-release sudo yum install ruby ruby-devel rubygems gcc gcc-c++ make sqlite-devel sudo gem install rails sudo gem install passenger ``` 2. 安装NginxPassenger 使用以下命令安装NginxPassenger: ``` sudo yum install nginx sudo passenger-install-nginx-module ``` 在安装过程中,需要选择“1. Yes: download, compile and install Nginx for me”选项,让Passenger自动编译和安装Nginx。 3. 配置NginxPassenger 默认情况下,Passenger会自动添加Nginx的配置文件并启动Nginx服务,但是我们需要手动修改Nginx配置文件。 找到Nginx的配置文件,一般是/etc/nginx/nginx.conf,添加以下内容: ``` http { # ... server { listen 80; server_name yourserver.com; root /path/to/your/redmine/public; passenger_enabled on; passenger_ruby /usr/bin/ruby; passenger_app_env production; } } ``` 其中,yourserver.com是你的服务器域名或IP地址,/path/to/your/redmine是你的Redmine应用所在路径。 4. 启动Nginx服务 启动Nginx服务: ``` sudo systemctl start nginx ``` 5. 配置数据库 修改/config/database.yml文件,配置Redmine连接数据库的相关信息。 6. 初始化Redmine 在Redmine目录下运行以下命令初始化数据库: ``` RAILS_ENV=production bundle exec rake db:create RAILS_ENV=production bundle exec rake db:migrate RAILS_ENV=production bundle exec rake redmine:load_default_data ``` 7. 启动Redmine 在Redmine目录下运行以下命令启动Redmine: ``` RAILS_ENV=production bundle exec rails server -e production ``` 现在你就可以在浏览器中输入yourserver.com访问Redmine了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值