流れ・予定
- 素のVirtualHostを設定
- Gemで本体のインストール
- apache用のモジュールをインストール
- apacheモジュールの読み込み設定
- テスト用のバーチャルホストの設定
- RackAppで起動テスト
環境
MacOS OSX El Capitan 10.11.6
Ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Gem -v 2.6.6
apachectl -v
Server version: Apache/2.4.23 (Unix)
Server built: Aug 8 2016 16:31:34
素のVirtualHostを設定
問題があったときに、Passengerの問題なのか、ApacheのVirtualHostが問題なのかを切り分けるため、先に素のVirtualHostを設定しておく。
/etc/apache2/httpd.confの以下のコメントを外す(449行目付近)
bash
Include /private/etc/apache2/extra/httpd-vhosts.conf
前置き
気付かなかったが、MarvericksあたりからWeb共有やユーザーごとのSitesディレクトリは削除されていたようだ。
関連して、一度もApacheを立ち上げたことがなければ、自動ではApacheは動いていないようだ。
ただ、一度 sudo apachectrl start とすると、次回からは勝手に起動してくれるようだ。
httpd.confの編集
/etc/apache2/httpd.confの499行目付近にあるVirtualHostの読み込み設定のコメントを外してincludeを有効にして、Apacheを再起動させる。
bash
Include /private/etc/apache2/extra/httpd-vhosts.conf
localhostの表示
VirtualHostが有効になると、localhostのがDocumentRootが
/etc/apache2/extra/httpd-vhosts.confによって書き換えられてしまうので、これをもともとのIt works!が表示されるように変更してみる。
spirytus:apache2 saima$ cd /etc/apache2/
spirytus:apache2 saima$ mv extra/httpd-vhosts.conf extra/httpd-vhosts.conf.dev
spirytus:apache2 saima$ vi extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
ServerAlias www.localhost
ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
VirtualHost
local.test.comというホストで動かしてみるため、以下を追加
Apacheの2.2系と2.4系ではアクセス制御の書き方がガラッと変わっているので注意
<VirtualHost *:80>
DocumentRoot "/Users/saima/Develop/vhost"
ServerName local.test.com
ErrorLog "/private/var/log/apache2/local.test.com-error_log"
CustomLog "/private/var/log/apache2/local.test.com-access_log" common
<Directory "/Users/saima/Develop/vhost">
Require all granted
</Directory>
</VirtualHost>
Gemで本体のインストール
Gemだけで普通に入るらしい
spirytus:~ saima$ sudo gem install passenger
Fetching: passenger-5.0.30.gem (100%)
Building native extensions. This could take a while...
Successfully installed passenger-5.0.30
Parsing documentation for passenger-5.0.30
Installing ri documentation for passenger-5.0.30
Done installing documentation for passenger after 108 seconds
1 gem installed
OSX Sierra
MarvericsまではBash'spirytus:~ saima$ passenger-install-apache2-module'
で普通にインストールできたと記憶しているが、Sierraにアプデしたから、xcode-select --installをして、APRとAPUを入れろと言われるが、Sierraからxcode-selectにAPRとAPUが含まれなくなったようだ。
...(略)
Location: /usr/local/include/ruby-2.3.0/ruby.h
* Checking for rack...
Found: yes
* Checking for Apache Portable Runtime (APR) development headers...
Found: no
* Checking for Apache Portable Runtime Utility (APU) development headers...
Found: no
Some required software is not installed.
But don't worry, this installer will tell you how to install them.
Press Enter to continue, or Ctrl-C to abort.
--------------------------------------------
Installation instructions for required software
* To install Apache Portable Runtime (APR) development headers:
Please install the Xcode command line tools: sudo xcode-select --install
* To install Apache Portable Runtime Utility (APU) development headers:
Please install the Xcode command line tools: sudo xcode-select --install
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/apache/
https://www.phusionpassenger.com/library/admin/apache/troubleshooting/
そこで、ARPとAPUのソースをApacheのHPからいただいて来てインストールすることにした。
ARPとAPUは基本的に./configure,make,make installできるが、APUのconfigureにはAPRのパスを教えてやらなければならないので、./configure --with-apr=/usr/local/apr
としてインストールできた。
これだけではpassenger-install-apache2-moduleはARPとAPUを認識してくれないようなので、ここを参考にAPRとAPUのリンクを作成した。
# 現在のディレクトリを確認
spirytus:~ saima$ ls /Applications/Xcode.app/Contents/Developer/Toolchains/
Swift_2.3.xctoolchain XcodeDefault.xctoolchain
# ディレクトリの作成
spirytus:~ saima$ sudo mkdir -p /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/
# apr-1-confのリンク
spirytus:~ saima$ sudo ln -s /usr/local/apr/bin/apr-1-config /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/
# apu-1-confのリンク
spirytus:~ saima$ sudo ln -s /usr/local/apr/bin/apu-1-config /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.12.xctoolchain/usr/local/bin/
apache用のモジュールをインストール
本体とは別にApacheにモジュールをインストールしなければならい。
bash'$sudo passenger-install-apache2-module
(Nogixとかでも使えるので、インストールするモジュールを選択しなければいけないため)
spirytus:~ saima$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v5.0.30.
This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.
Here's what you can expect from the installation process:
1. The Apache 2 module will be installed for you.
2. You'll learn how to configure Apache.
3. You'll learn how to deploy a Ruby on Rails application.
Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.
Press Enter to continue, or Ctrl-C to abort.
.
.
.
インストールの途中でApache.confへ書き込むサンプルが表示されるのでメモっておく。
.
.
.
--------------------------------------------
Almost there!
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.30/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.30
PassengerDefaultRuby /usr/local/bin/ruby
</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.
.
.
.
Warningが一つ出ているが、インストールできたようだ。
.
.
.
--------------------------------------------
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... (!)
You did not specify 'LoadModule passenger_module' in any of your Apache
configuration files. Please paste the configuration snippet that this
installer printed earlier, into one of your Apache configuration files, such
as /private/etc/apache2/httpd.conf.
Detected 0 error(s), 1 warning(s).
Press ENTER to continue.
--------------------------------------------
Deploying a web application
To learn how to deploy a web app on Passenger, please follow the deployment
guide:
https://www.phusionpassenger.com/library/deploy/apache/deploy/
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
https://www.phusionpassenger.com
Phusion Passenger is a registered trademark of Hongli Lai & Ninh Bui.
apacheモジュールの読み込み設定
モジュールのインストールで表示された記述を/etc/apache2/httpd.confに追記する。
記述場所は解りやすいようにVirtualHostの下の行とした。
# Passenger
LoadModule passenger_module /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.30/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.30
PassengerDefaultRuby /usr/local/bin/ruby
</IfModule>
Apacheを再起動して、passenger-statusでロードされているかを確認
spirytus:~ saima$ sudo apachectl restart
Password:
spirytus:~ saima$ passenger-status
Version : 5.0.30
Date : 2016-09-05 12:33:12 +0900
Instance: kFHYy3Tk (Apache/2.4.18 (Unix) Phusion_Passenger/5.0.30)
Phusion Passenger is currently not serving any applications.
Rackupで起動確認
Rackファイルの準備
下記のようにディレクトリを作成
spirytus:~ saima$ cd Develop/
spirytus:Develop saima$ mkdir rackapp
spirytus:Develop saima$ mkdir rackapp/public
spirytus:Develop saima$ mkdir rackapp/tmp
/etc/hostsに127.0.0.1 www.rackexample.com
を追記
rackapp内にconfig.ruを下記のように作成
app = proc do |env|
[200, { "Content-Type" => "text/html" }, ["hello <b>Rack world</b>"]]
end
run app
httpd-vhosts.confに下記を追記
<VirtualHost *:80>
DocumentRoot "/Users/saima/Develop/rackapp/public"
ServerName www.rackexample.com
ErrorLog "/private/var/log/apache2/rackexample.com-error_log"
CustomLog "/private/var/log/apache2/rackexample.com-access_log" common
<Directory "/Users/saima/Develop/rackapp/public">
Allow from all
Options -MultiViews
Require all granted
</Directory>
</VirtualHost>
apacheを再起動して、ブラウザでwww.rackexample.comにアクセスして、hello Rack wordが表示されているかを確認。