0、假设你RoR环境装好了
1、安装Passenger
$ sudo gem install passenger
出现
Done installing documentation for passenger after 48 seconds
1 gem installed
1 gem installed
说明安装好了
2、安装Nginx
由于nginx不支持动态的模块载入,所以要使用passenger来进行编译安装由passenger修改过的nginx
$ passenger-install-nginx-module
全选
⬢ Ruby
⬢ Python
⬢ Node.js
⬢ Meteor
⬢ 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/
* 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
$ 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
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]:
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;
}
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
gem 'dotenv-rails'
然后运行
bundle install
在项目根目录下建立一个.env文件
然后运行
rake secret
然后运行
rake secret
这个命令会随机生成一个安全密钥,将这个密钥复制下来,然后在.env中添加
SECRET_KEY_BASE = 你的密钥
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"] %>
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
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
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