使用CentOS将nginx和passenger引入,并将其与Rails应用程序进行集成
首先
最近,当将Rails应用部署到GKE(Google容器引擎)时,我使用了Passenger,并发现它能够方便地实现nginx和rails的整合。因此,我希望进一步了解Passenger。
参考文献:在 Qiita 上部署 GKE 上的 Rails + nginx(+Phusion Passenger ) + CloudSQL 应用
我想在这篇投稿中将CentOS7中安装nginx和passenger并运行Rails应用的步骤记录下来,作为备忘录保存。
有关Phusion Passenger的信息
Phusion Passenger是用于Apache HTTP Server和nginx的免费模块。
就我理解来说,通常情况下需要将nginx与rails(puma, unicorn)进行配合。
-
- nginxを起動して、リクエストをport:80で受け付ける
-
- rails(puma, unicorn)を起動して、リクエストをport:3000で受け付ける
- リクエストをnginx(port:80)からrails(port:3000)にリパースプロキシさせる
假设
如果使用Passenger,则Rails可以在nginx上运行,无需反向代理。
“Rails指南”
在 Rails 导引中的《使用3.15.1版本的Passenger》章节中,介绍了使用Passenger可以轻松在子目录中运行应用程序。
引导的步骤
在Passenger的官方网站上,对安装步骤有详细的解释。
这次
-
- Select infrastructure: Generic Linux/Unix deployment tutorial
-
- Pick integration mode : nginx
-
- Open source vs Enterprise : Passenger open Source
- Operationg system : Red Hat 7 / CentOS 7
我根据所选的步骤进行了构建。
参考:使用Passenger将Ruby应用部署到生产环境 – Passenger图书馆
1. 引入Ruby和Rails
正如乘客教程中解释的那样,我参考了自己过去的帖子来引入Ruby和Rails。
在CentOS上安装rbenv和Ruby – Qiita
在CentOS上安装Rails – Qiita
2. 引入Nginx和Passenger
安装EPEL
$ sudo yum install -y epel-release yum-utils
$ sudo yum-config-manager --enable epel
$ sudo yum clean all && sudo yum update -y
安装Nginx和Passenger
按照教程安装Nginx和Passenger。
# 事前準備のためにpygpgme,curlをインストール
$ sudo yum install -y pygpgme curl
$ date
# => Sat Sep 16 04:56:58 UTC 2017
# 時刻が出力されない場合は、ntpをインストール
$ sudo yum install -y ntp
$ sudo chkconfig ntpd on
$ sudo ntpdate pool.ntp.org
$sudo service ntpd start
# リポジトリを追加する
$ sudo curl --fail -sSLo /etc/yum.repos.d/passenger.repo https://oss-binaries.phusionpassenger.com/yum/definitions/el-passenger.repo
# nginx,passengerをインストール
$ sudo yum install -y nginx passenger || sudo yum-config-manager --enable cr && sudo yum install -y nginx passenger
激活乘客Nginx模块。
确认passenger.conf的初始值。
$ cat /etc/nginx/conf.d/passenger.conf
#passenger_root /usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini;
#passenger_ruby /usr/bin/ruby;
#passenger_instance_registry_dir /var/run/passenger-instreg;
请确认指定在 passenger.conf 中的路径。
# passenger_root
$ passenger-config --root
/usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini
$ passenger-config about ruby-command
...
To use in Nginx : passenger_ruby /home/xxxxx/.rbenv/versions/2.4.2/bin/ruby
...
修改passenger.conf
$ sudo vi /etc/nginx/conf.d/passenger.conf
# ↓コメントアウトを外す
passenger_root /usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini;
# ↓コメントアウトを外す&rbenvでRubyを導入した場合はパスを書き換える
passenger_ruby /home/xxxxx/.rbenv/versions/2.4.2/bin/ruby;
# ↓コメントアウトを外す
passenger_instance_registry_dir /var/run/passenger-instreg;
验证配置文件
# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动nginx
$ sudo service nginx start
Redirecting to /bin/systemctl reload nginx.service
确认访问
通过浏览器访问服务器的IP地址,确认nginx的欢迎页面显示出来
http://xxx.xxx.xxx.xxx
3. 部署Rails应用程序
根据乘客的教程,将应用程序放置在/var/www/myapp/code中。
# ディレクトリの作成
$ sudo mkdir -p /var/www/myapp
# ディレクトリ所有者の変更
$ sudo chown [USERNAME]: /var/www/myapp
# カレントディレクトリの変更
$ cd /var/www/myapp
# アプリの配置
# GitからCloneする場合
$ sudo -u [USERNAME] -H git clone git://github.com/username/myapp.git code
# 新たにアプリを作成する場合
$ rails new code
4. 部署Rails应用
修改nginx.conf
设置使得当在浏览器中访问服务器的IP地址时,Rails应用程序能够显示出来。
# nginx.confに変更を加える前にバックアップしておく
$ sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bk
# nginx.confを修正(server{...}を削除)
$ sudo vi /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
index index.html index.htm;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
增加服务器设置
$ sudo vi /etc/nginx/conf.d/myapp.conf
在教程中,如下所示:
server {
listen 80;
server_name yourserver.com;
# Tell Nginx and Passenger where your app's 'public' directory is
root /var/www/myapp/code/public;
# Turn on Passenger
passenger_enabled on;
passenger_ruby /path-to-ruby;
}
参考了这个存储库并进行了一些修改。
server {
listen 80;
server_name localhost;
root /var/www/myapp/code/public;
passenger_enabled on;
passenger_app_env development;
client_max_body_size 100m;
location ^~ /assets/ {
access_log off;
gzip_static on;
expires 0;
add_header Cache-Control public;
add_header ETag "";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Request-Method *;
}
}
# 設定ファイルを検証
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 設定ファイルを再読込
$ sudo service nginx reload
Redirecting to /bin/systemctl reload nginx.service
确认访问
使用浏览器访问服务器的IP地址:http://xxx.xxx.xxx.xxx
添加:改变动作环境
通过更改passenger_app_env,可以切换操作环境。
# 開発環境
passenger_app_env development;
# 本番環境
passenger_app_env production;
# 設定ファイルを検証
$ sudo nginx -t
# 設定ファイルを再読込
$ sudo service nginx reload
補充:訪問時出现错误
五百错误
错误日志: (Error log)
nginx用户无法访问config.ru是问题的根源。
$ sudo tail /var/log/nginx/error.log
2017/09/16 06:02:56 [alert] 22400#0: *3 Cannot stat '/var/www/myapp/code/config.ru': Permission denied (errno=13);
This error means that the Nginx worker process (PID 22400, running as UID 996) does not have permission to access t
his file. Please read this page to learn how to fix this problem: https://www.phusionpassenger.com/library/admin/ng
inx/troubleshooting/?a=upon-accessing-the-web-app-nginx-reports-a-permission-denied-error; Extra info, client: 111.
216.58.83, server: localhost, request: "GET / HTTP/1.1", host: "xx.xxx.xxx.xxx"
解决方法:
禁用 SELinux
# SELinux無効化
$ sudo setenforce 0
# 永続化する
$ sudo vi /etc/selinux/config
SELINUX=disabled
堆栈跟踪
在设置开发时,由于堆栈跟踪显示的界面与常见的界面不同,可能会有些困惑,但是只要查看详细信息,就很容易确定原因。
补充:乘客相关命令
验证Passenger的安装
$ sudo /usr/bin/passenger-config validate-install
What would you like to validate?
Use <space> to select.
If the menu doesn't display correctly, press '!'
‣ ⬢ Passenger itself
⬡ Apache
-------------------------------------------------------------------------
* Checking whether this Passenger install is in PATH... ✓
* Checking whether there are no other Passenger installations... ✓
Everything looks good. :-)
确认乘客的状态。
$ sudo /usr/sbin/passenger-memory-stats
Version: 5.1.8
Date : 2017-09-16 05:21:08 +0000
------------- Apache processes -------------
*** WARNING: The Apache executable cannot be found.
Please set the APXS2 environment variable to your 'apxs2' executable's filename, or set the HTTPD environment varia
ble to your 'httpd' or 'apache2' executable's filename.
---------- Nginx processes -----------
PID PPID VMSize Private Name
--------------------------------------
19213 1 110.8 MB 0.3 MB nginx: master process /usr/sbin/nginx
19214 19213 111.2 MB 0.7 MB nginx: worker process
### Processes: 2
### Total private dirty RSS: 1.04 MB
----- Passenger processes -----
PID VMSize Private Name
-------------------------------
19186 417.8 MB 1.0 MB Passenger watchdog
19189 857.5 MB 3.4 MB Passenger core
19197 425.9 MB 1.0 MB Passenger ust-router
### Processes: 3