Ubuntu 20.04: 配置passenger + apache2 + rails 記錄

前言

用了一天多時間完成。最初 passenger-install-apache2-module 好幾次都沒有到編譯那一步,提示還有依賴項沒有安裝好,其中一個似乎是找不到源,後用"使用passenger 代替puma/thin等作爲rails的WEB服務器"的方法解決。

 

使用passenger 代替puma/thin等作爲rails的WEB服務器

Install

Gemfile 中增加

Gemfile.local中不行,WHY?

gem "passenger", ">= 5.3.2", require: "phusion_passenger/rack_handler"

shell:

$ bundle install


# samxiao @ rails in ~/exp-rails/depot-exp-rails5-agile on git:master x [15:38:26] C:130
$ passenger -v
Phusion Passenger(R) 6.0.8

# samxiao @ rails in ~/exp-rails/depot-exp-rails5-agile on git:master x [15:38:32]
$ bundle exec passenger -v
Phusion Passenger(R) 6.0.8

運行

# samxiao @ rails in ~/exp-rails/depot-exp-rails5-agile on git:master x [15:38:39]
$ rails s passenger -b 0.0.0.0
Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`.
=> Booting Passenger application server
=> Rails 5.2.5 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
/var/lib/gems/2.7.0/gems/actionpack-5.2.5/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/var/lib/gems/2.7.0/gems/actionpack-5.2.5/lib/action_dispatch/middleware/static.rb:111: warning: The called method `initialize' is defined here
=============== Phusion Passenger(R) Standalone web server started ===============
PID file: /home/samxiao/exp-rails/depot-exp-rails5-agile/tmp/pids/passenger.3000.pid
Log file: /home/samxiao/exp-rails/depot-exp-rails5-agile/log/passenger.3000.log
Environment: development
Accessible via: http://0.0.0.0:3000/

You can stop Phusion Passenger(R) Standalone by pressing Ctrl-C.
Problems? Check https://www.phusionpassenger.com/library/admin/standalone/troubleshooting/
===============================================================================

 

執行$ sudo passenger-install-apache2-module   有錯按提示操作,直到成功

--------------------------------------------
Almost there!

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /var/lib/gems/2.7.0/gems/passenger-6.0.8/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /var/lib/gems/2.7.0/gems/passenger-6.0.8
     PassengerDefaultRuby /usr/bin/ruby2.7
   </IfModule>

After you restart Apache, you are ready to deploy any number of web
applications on Apache, with a minimum amount of configuration!

Press ENTER when you are done editing.


--------------------------------------------

Validating installation...

 * Checking whether this Passenger install is in PATH... ✓
 * Checking whether there are no other Passenger installations... ✓
 * Checking whether Apache is installed... ✓
 * Checking whether the Passenger module is correctly configured in Apache... ✓

Everything looks good.

注意這一步的以下內容:

LoadModule passenger_module /var/lib/gems/2.7.0/gems/passenger-6.0.8/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /var/lib/gems/2.7.0/gems/passenger-6.0.8
     PassengerDefaultRuby /usr/bin/ruby2.7
   </IfModule>

是放在2個文件(passenger.load, passenger.conf)中,見後

 

服務器端執行

$ sudo a2ensite rails36597.conf
$ sudo a2enmod passenger
$ sudo systemctl restart apache2 

配置文件如下:

apache2虛擬主機/etc/apache2/sites-available/rails-exp36597.conf

# Listen 8051
<VirtualHost *:80>
  ServerAdmin admin@localhost
  DocumentRoot /var/www/html/rails597/public
# ServerName  baidu.com
#  ServerName  localhost
#  ServerName  192.168.175.89
  ServerName  10.82.99.89
  ServerAlias rails36597.qz www.rails36597.qz 36597.qz
  SetEnv SECRET_KEY_BASE "0123456789abcdef"
#  RailsBaseURI /redmine
  # RailsEnv production
  RailsEnv development
  <Directory /var/www/html/rails597/public>
    AllowOverride all
    Options -MultiViews
    Require all granted
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

apache2 passenger配置

# samxiao @ rails in /etc/apache2/sites-available [21:03:28]
$ cat /etc/apache2/mods-available/passenger.load
# LoadModule passenger_module /usr/lib/apache2/modules/mod_passenger.so
LoadModule passenger_module /var/lib/gems/2.7.0/gems/passenger-6.0.8/buildout/apache2/mod_passenger.so


# samxiao @ rails in /etc/apache2/sites-available [21:03:39]
$ cat /etc/apache2/mods-available/passenger.conf
# LoadModule passenger_module /var/lib/gems/2.7.0/gems/passenger-6.0.8/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /var/lib/gems/2.7.0/gems/passenger-6.0.8
  PassengerDefaultRuby /usr/bin/ruby2.7
</IfModule>

/var/www/html/rails597

# samxiao @ rails in /var/www/html [21:04:30]
$ l
total 28K
drwxr-xr-x 2 root root 4.0K May  6 18:11 .
drwxr-xr-x 3 root root 4.0K May  5 18:18 ..
-rw-r--r-- 1 root root 2.2K Feb 17 14:34 db.php
-rw-r--r-- 1 root root  11K Feb 14 18:06 index.html
-rw-r--r-- 1 root root   20 Feb 17 09:40 info.php
lrwxrwxrwx 1 root root   21 Feb 17 08:53 phpmyadmin -> /usr/share/phpmyadmin
lrwxrwxrwx 1 root root   47 May  6 18:11 rails597 -> /home/samxiao/exp-rails/depot-exp-rails5-agile/
lrwxrwxrwx 1 root root   26 May  5 08:30 xsz-1 -> /home/samxiao/www-xsz-exp1

rails應用路徑

# samxiao @ rails in ~ [21:05:58]
$ l /home/samxiao/exp-rails/depot-exp-rails5-agile
total 96K
drwxrwxr-x 14 samxiao samxiao 4.0K May  5 17:21 .
drwxrwxr-x  4 samxiao samxiao 4.0K May  5 08:29 ..
drwxrwxr-x 10 samxiao samxiao 4.0K May  2 09:04 app
drwxr-xr-x  2 samxiao samxiao 4.0K May  2 09:07 bin
drwxrwxr-x  5 samxiao samxiao 4.0K May  4 21:32 config
-rw-rw-r--  1 samxiao samxiao  130 May  2 09:04 config.ru
drwxrwxr-x  3 samxiao samxiao 4.0K May  5 20:55 db
-rw-rw-r--  1 samxiao samxiao 2.3K May  5 15:46 Gemfile
-rw-rw-r--  1 samxiao samxiao 5.3K May  5 15:34 Gemfile.lock
drwxrwxr-x  8 samxiao samxiao 4.0K May  4 21:32 .git
-rw-rw-r--  1 samxiao samxiao  695 May  2 09:04 .gitignore
-rw-rw-r--  1 samxiao samxiao  193 May  5 14:52 index.html.bak
drwxrwxr-x  4 samxiao samxiao 4.0K May  2 09:04 lib
drwxrwxr-x  2 samxiao samxiao 4.0K May  5 15:35 log
-rw-rw-r--  1 samxiao samxiao   74 May  2 09:04 package.json
drwxrwxrwx  2 samxiao samxiao 4.0K May  6 18:10 public
-rw-rw-r--  1 samxiao samxiao  227 May  2 09:04 Rakefile
-rw-rw-r--  1 samxiao samxiao  374 May  2 09:04 README.md
-rw-rw-r--  1 samxiao samxiao   10 May  2 09:04 .ruby-version
drwxrwxr-x  2 samxiao samxiao 4.0K May  2 09:04 storage
drwxrwxr-x  9 samxiao samxiao 4.0K May  2 09:04 test
drwxrwxr-x  6 samxiao samxiao 4.0K May  2 09:41 tmp
drwxrwxr-x  2 samxiao samxiao 4.0K May  2 09:04 vendor

# samxiao @ rails in ~ [21:06:08]
$ l /home/samxiao/exp-rails/depot-exp-rails5-agile/public
total 24K
drwxrwxrwx  2 samxiao samxiao 4.0K May  6 18:10 .
drwxrwxr-x 14 samxiao samxiao 4.0K May  5 17:21 ..
-rwxrwxrwx  1 samxiao samxiao 1.7K May  2 09:04 404.html
-rwxrwxrwx  1 samxiao samxiao 1.7K May  2 09:04 422.html
-rwxrwxrwx  1 samxiao samxiao 1.6K May  2 09:04 500.html
-rwxrwxrwx  1 samxiao samxiao    0 May  2 09:04 apple-touch-icon.png
-rwxrwxrwx  1 samxiao samxiao    0 May  2 09:04 apple-touch-icon-precomposed.png
-rwxrwxrwx  1 samxiao samxiao    0 May  2 09:04 favicon.ico
-rwxrwxrwx  1 samxiao samxiao   98 May  2 09:04 robots.txt

Windows 2019 c:\windows\system32\drivers\etc\hosts 文件增加以下內容

192.168.175.89  36597.qz
192.168.175.89  rails36597.qz
192.168.175.89  www.rails36597.qz

chrome: 以下URL都可以訪問

  • http://www.rails36597.qz/
  • http://36597.qz/
  • http://rails36597.qz/

以下不能訪問:

 Sam XIAO 更新於 37 分鐘 之前

$ systemctl status apache2

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-05-06 21:15:43 CST; 1min 3s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 226221 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
      Tasks: 45 (limit: 2247)
     Memory: 183.4M
     CGroup: /system.slice/apache2.service
             ├─226283 /usr/sbin/apache2 -k start
             ├─226286 Passenger watchdog
             ├─226293 Passenger core
             ├─226307 /usr/sbin/apache2 -k start
             ├─226308 /usr/sbin/apache2 -k start
             ├─226309 /usr/sbin/apache2 -k start
             ├─226310 /usr/sbin/apache2 -k start
             ├─226311 /usr/sbin/apache2 -k start
             ├─226482 /usr/sbin/apache2 -k start
             ├─226866 Passenger AppPreloader: /var/www/html/rails597
             ├─226929 Passenger RubyApp: /var/www/html/rails597 (development)
             └─226976 /usr/sbin/apache2 -k start
  • 配置不正確時至少沒有下面2行:
├─226866 Passenger AppPreloader: /var/www/html/rails597
             ├─226929 Passenger RubyApp: /var/www/html/rails597 (development)

 

重要注意事項

  • 軟連接很重要: rails597 -> /home/samxiao/exp-rails/depot-exp-rails5-agile/不能寫成 rails597 -> /home/samxiao/exp-rails/depot-exp-rails5-agile/public, 然後虛擬主機配置文件rails-exp36597.conf中的相關2行改爲
DocumentRoot /var/www/html/rails597/
.
.
.
  <Directory /var/www/html/rails597/>

這種改法是不等效的。

原因應該是:

passenger要使用DocumentRoot /var/www/html/rails597/public的父目錄作爲passenger與rails的“通信”操作

  • rails597 -> /home/samxiao/exp-rails/depot-exp-rails5-agile/時, 即:
    /var/www/html/rails597  /home/samxiao/exp-rails/depot-exp-rails5-agile/的軟連接。
    DocumentRoot /var/www/html/rails597/public的父目錄爲/var/www/html/rails597/, 這是一個軟連接,即:/home/samxiao/exp-rails/depot-exp-rails5-agile/

  • rails597 -> /home/samxiao/exp-rails/depot-exp-rails5-agile/public時, 即:
    /var/www/html/rails597 是/home/samxiao/exp-rails/depot-exp-rails5-agile/public的軟連接。
    此時
    DocumentRoot /var/www/html/rails597/的父目錄爲/var/www/html/而不是期望的/home/samxiao/exp-rails/depot-exp-rails5-agile/`

  環境信息

  • Ubuntu 20.04.2LTS server
  • Apache/2.4.41 (Ubuntu)  Server built:   2020-08-12T19:46:17
  • Phusion Passenger(R) 6.0.8
  • ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]
  • Rails 5.2.5

遇到過的坑記錄

參考

apache2 虛擬主機VirtualHost配置,自定義域名訪問

apache服務器安裝以及使用passenger插件部署rails應用,基於ubuntu 12.04 LTS

Working with the Apache configuration file

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章