SPARQLthon17/TogoStanzaDeploy
提供:TogoWiki
目次 |
公開構成
StanzaのフレームワークはRubyで実装されています。
TogoStanzaのチュートリアルの手順ではローカルの9292ポートでRubyのWebアプリケーションを立ち上げますが、これをApache等のWebサーバ経由で公開する設定を紹介します。
※注意!! Rubyアプリケーションを起動した9292ポートに対して、Webサーバ側から単純にプロキシ設定しても、正しく動作しません。ブラウザで表示しても「Not found」と表示されます。
Rubyのアプリケーションを本番環境で動作させる代表的な構成は以下の組み合わせになります。
- Apache HTTP Server + Phusion Passenger
- Nginx + Unicorn
Apaceh + Phusion Passenger での公開手順
成功した環境
Ubuntu Apache 2.2 Phusion Passenger 4.0 Ruby 2.0
CentOS Apache 2.2 Phusion Passenger 4.0 Ruby 2.0
Phusion Passengerのダウンロード
gemで取得します
$ sudo gem install passenger Fetching: daemon_controller-1.1.8.gem (100%) Successfully installed daemon_controller-1.1.8 Fetching: passenger-4.0.33.gem (100%) Building native extensions. This could take a while... Successfully installed passenger-4.0.33 Parsing documentation for daemon_controller-1.1.8 Installing ri documentation for daemon_controller-1.1.8 Parsing documentation for passenger-4.0.33 Installing ri documentation for passenger-4.0.33 2 gems installed //成功
Phusion Passengerのインストール
インストール用のコマンドを実行します。
何度かEnterを押せと言われるので、気にせず押します。
※インストールが成功した場合に出力されるメッセージには、後で設定に使うものがあります。
$ sudo passenger-install-apache2-module Welcome to the Phusion Passenger Apache 2 module installer, v4.0.33. 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. //Enterを押す ------------------------------------//開発言語を聞かれることがあるので、Rubyを選択 Which languages are you interested in? Use <space> to select. ? ? Ruby ? Python ? Node.js ? Meteor ------------------------------------//依存ツールが揃っているかチェックが始まる Checking for required software... * Checking for C compiler... Found: yes Location: /usr/bin/gcc * Checking for C++ compiler... Found: yes Location: /usr/bin/g++ * Checking for Curl development headers with SSL support... Found: yes curl-config location: /usr/bin/curl-config Header location (中略) * Checking for Apache 2 development headers... Found: no * 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 2 development headers: Please install it with apt-get install apache2-threaded-dev * To install Apache Portable Runtime (APR) development headers: Please install it with apt-get install libapr1-dev * To install Apache Portable Runtime Utility (APU) development headers: Please install it with apt-get install libaprutil1-dev If the aforementioned instructions didn't solve your problem, then please take a look at the Users Guide: /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.23/doc/Users guide Apache.html http://www.modrails.com/documentation/Users%20guide%20Apache.html
メッセージをよく読むとOS環境に合わせたインストールコマンドが記載されているので、実行します。
$ sudo apt-get install apache2-threaded-dev $ sudo apt-get install libapr1-dev $ sudo apt-get install libaprutil1-dev
必要なパッケージがインストールできたらもう一度インストールコマンドを実行します。
$ sudo passenger-install-apache2-module (省略) -------------------------------------------- Sanity checking Apache installation... -------------------------------------------- Compiling and installing Apache 2 module... cd /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.33 /usr/local/bin/ruby /usr/local/bin/rake apache2:clean apache2 RELEASE=yes # /usr/local/bin/ruby /usr/local/bin/rake apache2:clean apache2 RELEASE=yes (中略) linking shared-object passenger_native_support.so -------------------------------------------- //passengerの設定ファイル用のテキストが出力される The Apache 2 module was successfully installed. Please edit your Apache configuration file, and add these lines: LoadModule passenger_module /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.33/buildout/apache2/mod_passenger.so PassengerRoot /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.33 PassengerDefaultRuby /usr/local/bin/ruby After you restart Apache, you are ready to deploy any number of web applications on Apache, with a minimum amount of configuration! Press ENTER to continue. //何故か何度も続行の意思を問われるがEnterを押す -------------------------------------------- //Aapacheのバーチャルホストの設定例が出力される Deploying a Ruby on Rails application: an example Suppose you have a Rails application in /somewhere. Add a virtual host to your Apache configuration file and set its DocumentRoot to /somewhere/public: <VirtualHost *:80> ServerName www.yourhost.com # !!! Be sure to point DocumentRoot to 'public'! DocumentRoot /somewhere/public <Directory /somewhere/public> # This relaxes Apache security settings. AllowOverride all # MultiViews must be turned off. Options -MultiViews </Directory> </VirtualHost> And that's it! You may also want to check the Users Guide for security and optimization tips, troubleshooting and other useful information: /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.23/doc/Users guide Apache.html http://www.modrails.com/documentation/Users%20guide%20Apache.html Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-) https://www.phusionpassenger.com Phusion Passenger is a trademark of Hongli Lai & Ninh Bui. //最後にこのメッセージが出ればインストール完了
Phusion PassengerをApacheに組み込む
Apacheの設定ディレクトリに、新規でpassenger.confを作成します。
パスの例) /etc/httpd/conf.d/passenger.conf
インストール時のメッセージで出力された以下のような3行をコピペします。
LoadModule passenger_module /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.33/buildout/apache2/mod_passenger.so PassengerRoot /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.33 PassengerDefaultRuby /usr/local/bin/ruby
Apacheのバーチャルホストを設定する
Rubyアプリケーションをバーチャルホストに追加します。
バーチャルホストは色々な構成で設定方法も変わりますが、あるWebサーバのサブディレクトリ"stanza"をスタンザサーバとして動かす設定を紹介します。(例:http://hogehoge.com/stanza にアクセスすればstanzaリストが表示される状態)
セキュリティ設定等はそれぞれのポリシーに従ってください。
デフォルトバーチャルホストの設定
VirtualHostを有効にするにはhttpd.conf(設定ファイルが細分化されている異なる場合もある)の以下一行を有効にします。コメントアウトされていればはずします。
NameVirtualHost *:80
Apacheのバーチャルホスト設定箇所はApacheの設定ディレクトリ構成によって異なります。
- 設定ディレクトリにsites-availableがある場合
例)/etc/apache2/sites-available/default(既にある場合は修正、なければ新規作成)
- 無い場合
例)/etc/httpd/conf/httpd.conf(VirtulHost欄に追記)
または
例)/etc/httpd/conf.d/000-virtualhost-default.conf(新規作成)
プロキシ設定で/stanzaにアクセスされた場合に、http://stanza_server.com/stanzaディレクトリに飛ばすように設定しています。
この"stanza_server.com"の部分は後でStanza用に設定する仮想のホスト名です。サーバーの稼働するホストにおいて問題なく名前が引ける限り、任意の値で構いません。
従って、稼働するホスト名、例えばlocalhostでも問題無いでしょう。
<VirtualHost *:80> DocumentRoot /var/www/html #デフォルトのドキュメントディレクトリ #プロキシ設定。/stanzaでアクセスされたらhttp://stanza_server.com/stanzaに飛ばす ProxyPass /stanza http://stanza_server.com/stanza ProxyPassReverse /stanza http://stanza_server.com/stanza <Directory /var/www/html> AllowOverride all Options None Order allow,deny Allow from all </Directory> </VirtualHost>
/etc/apache2/sites-available/の設定ファイルに記載した場合には、同じ階層のsite-enabled/にシンボリックリンクを貼ります。既にリンク貼られている場合は不要です。
$ sudo ln -s /etc/apache2/sites-available/default /etc/apache2/sites-enabled/000-default
Stanza用バーチャルホストの設定
次に、stanza_server.com/stanzaで飛ばされてきた場合にStanzaプロバイダーと連携をさせるバーチャルホストを設定します。
基本的にはPhusion Passengerインストール時のメッセージで出力されたバーチャルホストの設定例を元に、設定を書きます。
- 設定ディレクトリにsites-availableがある場合
例)/etc/apache2/sites-available/stanza_server(新規作成)
- 無い場合
例)/etc/httpd/conf/httpd.conf(VirtulHost欄に追記)
または
例)/etc/httpd/conf.d/001-virtualhost-stanza.conf(新規作成)
ここでは/var/www/html/stanza_server/をStanzaプロバイダのルートのパスとして設定していますが、実際のStanzaプロバイダの置き場所を指定してください。
<VirtualHost *:80> #以下は任意の値。先ほどのプロキシ設定と揃える。 Servername stanza_server.com #スタンザプロバイダのパスを指定。"public"は実体のディレクトリがなくても構わないが必須 DocumentRoot /var/www/html/stanza_server/public #同上 (Order allow,denay と Allow from all は、指定ディレクトリ /var/www/html/stanza_server/public が含まれるディレクトリに対して他の場所で指定していれば不要) <Directory /var/www/html/stanza_server/public> AllowOverride all Options -MultiViews Order allow,deny Allow from all </Directory> </VirtualHost>
sites-available/の設定ファイルに記載した場合には、同じ階層のsite-enabled/にシンボリックリンクを貼ります。Ubuntuでも必要です。
$ sudo ln -s /etc/apache2/sites-available/stanza_server /etc/apache2/sites-enabled/001-stanza_server
ローカルDNS設定
VirtualHostで設定したstanza_server.comのサーバ名を解決するために、ローカル内でDNSを設定します。 /etc/hostsに以下の一行を追記します。
127.0.0.1 stanza_server.com
再起動と確認
Apacheを再起動するとVirtualHostが有効になります。
この状態でhttp://hogehoge.com/stanza にアクセスしてスタンザリストが見えると設定成功です。