• CentOS Install Passenger for ROR


    简介

    本篇文章涉及到的内容都是基于阿里云主机上面操作的,具体系统和软件版本如下:

    • CentOS
      • LSB Version: :core-4.1-amd64:core-4.1-noarch
      • Distributor ID: CentOS
      • Description: CentOS Linux release 7.9.2009 (Core)
      • Release: 7.9.2009
      • Codename: Core
    • Ruby:2.7.4
    • Rails:7.0.4
    • Nginx:1.20.1,云服务已经默认安装了
    • Passenger:Phusion Passenger® 6.0.15

    本篇文章主要目的:

    • 如何在 CentOS 上安装 Passenger
    • 如何通过 Passenger 运行、访问 ruby on rails 应用

    安装 ruby

    如果直接使用 yum install -y ruby 安装 ruby,其版本是 2.0.x,而我需要安装 2.7.x 版本。

    成功安装 ruby 是接下来一切操作的基础,使用 root 账号登录云主机,按照下面步骤操作即可。如果使用非 root 账号安装,需要在下面命令前加 sudo。

    # 安装源
    yum install centos-release-scl-rh
    
    # 安装 ruby-devel
    # 这里一定要和 ruby 版本对上,否则后面安装 gems 会报错
    yum install rh-ruby27-ruby-devel
    
    yum install ruby
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    检查 ruby 版本,如果不是 ruby-2.7,可以重新执行如下命令

    yum install -y rh-ruby27
    
    • 1

    安装完成后,执行

    scl enable rh-ruby27
    
    • 1

    配置开机自动切换到 ruby27,新建并编辑文件

    vim /etc/profile.d/rh-ruby27.sh

    source /opt/rh/rh-ruby27/enable
    export X_SCLS="`scl enable rh-ruby27 'echo $X_SCLS'`"
    
    • 1
    • 2

    安装后的 ruby 在 /opt/rh/rh-ruby27/root/usr/bin/ruby 下。

    安装 rails

    安装 ruby 稍微麻烦一点,接下来我们安装 rails

    gem install rails
    
    gem install bundler
    
    • 1
    • 2
    • 3

    我在安装 rails 遇到了一个问题(报错)

    gem install rails
    Building native extensions. This could take a while…
    ERROR: Error installing rails:
    ERROR: Failed to build gem native extension.

    current directory: /opt/rh/rh-ruby27/root/usr/local/share/gems/gems/racc-1.6.0/ext/racc/cparse
    /opt/rh/rh-ruby27/root/usr/bin/ruby -I /opt/rh/rh-ruby27/root/usr/share/rubygems -r ./siteconf20221029-17728-1uvh5rb.rb extconf.rb
    mkmf.rb can’t find header files for ruby at /opt/rh/rh-ruby27/root/usr/share/include/ruby.h

    You might have to install separate package for the ruby development
    environment, ruby-dev or ruby-devel for example.

    extconf failed, exit code 1

    Gem files will remain installed in /opt/rh/rh-ruby27/root/usr/local/share/gems/gems/racc-1.6.0 for inspection.
    Results logged to /opt/rh/rh-ruby27/root/usr/local/lib64/gems/ruby/racc-1.6.0/gem_make.out

    卸载已经安装的 ruby-devel,一般都是其版本和 ruby 版本不一致导致的,重新安装对应版本。

    # 安装源
    yum install centos-release-scl-rh
    
    # 安装 ruby-devel
    # 这里一定要和 ruby 版本对上,否则后面安装 gems 会报错
    yum install rh-ruby27-ruby-devel
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    安装 Passenger

    安装 Passenger 比较简单,如果你不是使用 root 账号登录,下面命令需要加上 sudo

    1、enable EPEL

    EPEL:Extra Packages for Enterprise Linux

    yum install -y yum-utils
    yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-$(< /etc/redhat-release tr -dc '0-9.'|cut -d \. -f1).noarch.rpm
    yum-config-manager --enable epel
    yum clean all && sudo yum update -y
    
    • 1
    • 2
    • 3
    • 4

    2、repair potential system issues

    避免在安装 Passenger 时,由于一些系统限制导致安装失败

    # Ensure curl and nss/openssl are sufficiently up-to-date to talk to the repo
    yum update -y
    
    date
    # if the output of date is wrong, please follow these instructions to install ntp
    # 这里要注意,检查系统时间是否正确,如果不正确需要先调整好再执行下面步骤
    
    yum install -y ntp
    chkconfig ntpd on
    ntpdate pool.ntp.org
    service ntpd start
    
    yum -y install chrony
    systemctl enable chronyd
    firewall-cmd --permanent --add-service=ntp
    firewall-cmd --reload
    systemctl restart chronyd
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3、install Passenger packages

    # Install various prerequisites
    yum install -y  curl
    
    # Add our el9 YUM repository
    curl --fail -sSLo /etc/yum.repos.d/passenger.repo https://oss-binaries.phusionpassenger.com/yum/definitions/el-passenger.repo
    
    # Install Passenger dynamic Nginx module
    yum install -y nginx-mod-http-passenger || { yum-config-manager --enable cr && yum install -y nginx-mod-http-passenger ; }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、restart Nginx

    重启 nginx

    systemctl restart nginx
    
    • 1

    5、check installation

    检查是否安装成功

    sudo /usr/bin/passenger-config validate-install
    
    • 1

    显示如下信息表示成功

    * Checking whether this Phusion Passenger install is in PATH... ✓
    * Checking whether there are no other Phusion Passenger installations... ✓
    
    • 1
    • 2

    使用如下命令看下运行状态

    sudo /usr/sbin/passenger-memory-stats
    
    • 1

    显示类似如下状态表示正常

    Version: 6.0.15
    
    --------- Nginx processes ---------
    PID   PPID  VMSize   Private  Name
    -----------------------------------
    5672  1     44.1 MB  0.8 MB   nginx: master process nginx
    8713  5672  44.3 MB  1.0 MB   nginx: worker process
    8714  5672  44.3 MB  1.0 MB   nginx: worker process
    
    ----- Passenger processes -----
    PID   VMSize     Private  Name
    -------------------------------
    8699  355.5 MB   2.2 MB   Passenger watchdog
    8703  1117.0 MB  4.3 MB   Passenger core
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    至此,Passenger 安装完成。

    创建 ROR 应用

    通过 rails 创建应用比较简单,在这一步我是用非 root 账号创建的即普通账号(ptyh) 创建的应用。

    您可以去了解一下如何在 CentOS 中添加用户(添加用户adduser ptyh、设置密码passwd ptyh)。

    创建 articles 应用,如下

    rails new articles
    
    • 1

    创建成功后的工程 articles 在目录 /home/ptyh/rails_projs 下。

    我们可以启动一下这个工程,确保它本身是正常的。

    rails s -b 0.0.0.0 -p 8080
    
    • 1

    我在阿里云上面是开放了 8080 端口的,这样你可以通过 云主机公网IP:8080 访问到,看到如下界面表示成功。
    在这里插入图片描述
    确保您的工程运行和访问没有问题后,停止该服务的运行(ctrl+c)。

    我们继续往下看~

    配置 Nginx + Passenger

    这一步至关重要,关系到是否能通过 passenger 成功运行和访问 ROR 应用。

    我们去检查一下 passenger 配置,该配置文件在 /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;
    
    • 1
    • 2
    • 3

    可以看出 ruby 路径不对,修改如下

    passenger_root /usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini;
    passenger_ruby /opt/rh/rh-ruby27/root/usr/bin/ruby;
    passenger_instance_registry_dir /var/run/passenger-instreg;
    
    • 1
    • 2
    • 3

    如果不做修改(ruby 版本不一致),后续在通过 passenger 启动的 ror 应用程序会报类似 cannot load such file -- bundler/setup (LoadError) 的错误。

    接下来,我们配置一下 nginx,其配置文件在 /etc/nginx/nginx.conf

    #user nginx;
    # 修改为 nobody
    user nobody;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        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;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 4096;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        # 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;
    
        server {
            listen       80;
            listen       [::]:80;
            server_name  _;
            #root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
    	   # 新增1:指定环境和开启 passenger
            passenger_app_env development;
            passenger_enabled on;
    
            # 新增2:指定 rails 工程的 public 目录
            root /home/ptyh/rails_projs/articles/public;
           
            error_page 404 /404.html;
            location = /404.html {
            }
    
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    修改的地方在文件中都有注释,修改完成后保存,记得重新 reload 该配置文件

    nginx -s reload
    
    • 1

    访问 ROR Web 应用

    到这里,基本搞定了,还剩最后一公里,冲。。。

    之前说过,修改了 nginx 配置后需要重新 reload,另外我们不需要单独去启动 articles 这个项目。

    如果我的云主机公网IP是 12x.x12.13.10x,我们在浏览器上打开 http://12x.x12.13.10x:80,大概率是无法正常显示应用的首页的。

    查看 nginx error log 可以发现这样的错误:

    Error opening ‘/home/ptyh/rails_projs/articles/Passengerfile.json’ for reading: Permission denied (errno=13);
    This error means that the Nginx worker process (PID 7991, 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;

    我们修改一下工程目录权限,命令如下:

    sudo chmod -R g+x,o+x /home/ptyh/rails_projects/articles
    sudo chmod -R g+x,o+x /home/ptyh/rails_projects
    
    • 1
    • 2

    再次访问 http://12x.x12.13.10x:80 即可。

    关键文件

    我在这里给大家罗列一下用到的配置文件及其路径。

    nginx 配置文件:/etc/nginx/nginx.conf

    nginx 日志文件:/var/log/nginx

    ruby 可执行文件: /opt/rh/rh-ruby27/root/usr/bin/ruby

    passenger 配置文件: /etc/nginx/conf.d/passenger.conf

    通过 nginx -t 可以找到 nginx 配置文件路径,在其配置文件中可以看到日志文件路径。

    参考资料

  • 相关阅读:
    俺把所有粉丝显示在地图上啦~【详细教程+完整源码】
    JSP如何使用request获取当前访问者的真实IP呢?
    性能测试 —— Jmeter事务控制器
    VBA -[知识点]: 字典
    数据流图:四大要素全解析!秘密就在这里
    【并发编程】为什么Hashtable和ConcurrentHashMap 是不允许键或值为 null 的,HashMap 的键值则都可以为 null?
    Shell之基础概念和变量
    如何基于Github Pages + Hexo 的方式,免费搭建个人博客?
    【学习】软件压力测试对软件产品的作用
    @Lazy注解的原理
  • 原文地址:https://blog.csdn.net/AndroidBluetooth/article/details/127714360