RailsãApacheä¸ã§åããããã®ã¢ã¸ã¥ã¼ã«Phusion Passenger
Rails(3.0.10)ãPhusion Passengerä¸ã§åãããã¨ãã®ã¡ã¢ããµã¼ãã¼ã¯ãããã¤ã³ã¿ã¼ãããã®VPSãOSã¯CentOSã
Phusion Passengerã¯ãRailsãApacheï¼WEBãµã¼ãã¼ï¼ä¸ã§åããããã®Apacheã¢ã¸ã¥ã¼ã«ãRailsãåããããã®WEBãµã¼ãã¼ã¯ãéçºç°å¢ã§ã¯WEBrickãæ¬çªç°å¢ã§ã¯Mongrelã使ããããã¨ãå¤ãã£ããããããã¯ç°¡åã«ä½¿ããåé¢ãåä½é度ãã¡ã¢ãªä½¿ç¨éã§ã¯ã¤ãã¤ãã ã£ããã¾ããApacheãå ¥ã£ã¦ããã®ã«ãRailså°ç¨ã®WEBãµã¼ãã¼ã¢ããªã±ã¼ã·ã§ã³ãå¥åã«ã¤ã³ã¹ãã¼ã«ããªãã¨ãããªãã®ãã¤ãã¤ãã ã£ãï¼ã¨ããã®ããPhusion Passengerãéçºãããèæ¯ãªãã ã¨æããï¼WEBãæ¸ç±ãæãèªã¿ãããï¼
ä»ã®WEBãµã¼ãã¼ã¨æ¯ã¹ã¦åä½é度ãã¡ã¢ãªä½¿ç¨éã®éãã¯ä¸ç®çç¶ã
Advantages of using Passenger + Apache over Webrick - stackoverflow
公式ページã§ããã³ããã¼ã¯ãã¬ãããªãã£ã¦ããã
æé ã¯ä»¥ä¸ã®éãã
- Passengerãã¤ã³ã¹ãã¼ã«ãã
- Apacheã®è¨å®ãã¡ã¤ã«ï¼httpd.confï¼ãç·¨éãã
- Railsã®ãµã³ãã«ã¢ããªãä½æãã
1. Passengerãã¤ã³ã¹ãã¼ã«ãã
以ä¸ã®ã³ãã³ããå®è¡ããã
$ gem list passenger # Passengerãå ¥ã£ã¦ããªããã¨ãç¢ºèª *** LOCAL GEMS *** # ä½ã表示ãããªãã $ sudo gem install passenger # Passengerã®gemãã¤ã³ã¹ãã¼ã« ... $ sudo passenger-install-apache2-module # Apacheã®ã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ããã³ãã³ã ... Installation instructions for required software * To install Curl development headers with SSL support: Please run yum install curl-devel as root. * To install Apache 2 development headers: Please run yum install httpd-devel as root. * To install Apache Portable Runtime (APR) development headers: Please run yum install apr-devel as root. * To install Apache Portable Runtime Utility (APU) development headers: Please run yum install apr-util-devel as root. If the aforementioned instructions didn't solve your problem, then please take a look at the Users Guide: /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.9/doc/Users guide Apache.html
ã¤ã³ã¹ãã¼ã«ã«ã¯ãã£ã¨ã½ããã¦ã§ã¢ãå¿ è¦ã¨è¨ããã¦ã¤ã³ã¹ãã¼ã«ãä¸æãããã®ã§ã足ããªããã®ãã¤ã³ã¹ãã¼ã«ãã¦åå®è¡ãåã®å ´åã¯ä¸è¨ã®ã©ã¤ãã©ãªã
# 足ããªãã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã« $ sudo yum install httpd-devel $ sudo yum install apr-devel $ sudo yum install apr-util-devel # å度passenger-install-apache2-moduleãå®è¡ $ sudo passenger-install-apache2-module ... The Apache 2 module was successfully installed. # Apacheã®è¨å®ãã¡ã¤ã«ã¸è¿½è¨ããããã®ï¼ Please edit your Apache configuration file, and add these lines: LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.9/ext/apache2/mod_passenger.so PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.9 PassengerRuby /usr/local/bin/ruby After you restart Apache, you are ready to deploy any number of Ruby on Rails applications on Apache, without any further Ruby on Rails-specific configuration! Press ENTER to continue. # Apacheã®è¨å®ãã¡ã¤ã«ã«è¿½è¨ããããã®ï¼ -------------------------------------------- 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 DocumentRoot /somewhere/public # <-- be sure to point to 'public'! <Directory /somewhere/public> AllowOverride all # <-- relax Apache security settings Options -MultiViews # <-- MultiViews must be turned off </Directory> </VirtualHost>
2. Apacheã®è¨å®ãã¡ã¤ã«ï¼httpd.confï¼ãç·¨éãã
ã¤ã³ã¹ãã¼ã«ãçµäºããã¨ãApacheã®è¨å®ãã¡ã¤ã«ï¼httpd.confï¼ã«ä½ãæ¸ãã°ããããããããããæ示ãã¦ãããï¼ä¸è¨ã®ãã°ï¼ããã£ãããã®éãã«ç·¨éãã¦ã¿ãã
$ sudo vi /etc/httpd/conf/httpd.conf # httpd.confã®ç·¨é ... # Dynamic Shared Object (DSO) Support ... # Passenger LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.9/ext/apache2/mod_passenger.so # è¿½è¨ PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-3.0.9 # è¿½è¨ PassengerRuby /usr/local/bin/ruby # è¿½è¨ ... # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. <VirtualHost *:80> ServerName wwwXXXXXX.sakura.ne.jp DocumentRoot /var/www/demo/public # Railsã¢ããªã±ã¼ã·ã§ã³ç´ä¸ã®publicãã©ã«ããæå® <Directory /var/www/demo/public> AllowOverride all # ãã£ã¬ã¯ããªãã¨ã®è¨å®ï¼.htaccessãã¡ã¤ã«ï¼ã§ã®ä¸æ¸ãã許å¯ãã Options -MultiViews # MultiViewsãæå¹ã«ãã </Directory> </VirtualHost> $ sudo /etc/init.d/httpd restart # Apacheãåèµ·å
VirtualHostã®ã¨ããã®è¨å®ãä½ãæå³ãã¦ãããããããªãã£ãã®ã§èª¿ã¹ã¦ã¿ãã
ããããVirtualHostã¨ã¯ãã²ã¨ã¤ã®ãµã¼ãã¼ã§è¤æ°ã®ãã¡ã¤ã³åã®WEBãµã¤ããéç¨ããããã®ä»çµã¿ããã¨ãã°ãwww.myapp1.comã¨www.myapp2.comã®ãã¡ã¤ã³ãåããµã¼ãã¼ã§éç¨ãããã¨ãã§ããã詳ããã¯バーチャルホストの例 Apacheãåç §ã®ãã¨ã
Directoryã®ãAllowOverride allãã¯ãhttpd.confã®è¨å®ãã¦ã¼ã¶ã¼ã«ããè¨å®ã§ä¸æ¸ãã§ãããã¨ãããã¨ãã¦ã¼ã¶ã¼ã«ããè¨å®ã¯ããã£ã¬ã¯ããªãã¨ã«.htaccessãã¡ã¤ã«ãä½ã£ã¦è¨å®å 容ãæ¸ãè¾¼ããã¨ã§ã§ããã.htaccessãã¡ã¤ã«ã®è©³ç´°ã¯ãバーチャルホストの例 Apacheãåç §ã®ãã¨ã
åããDirectoryã®ãOptions -MultiViewsãã¯ãHTTPã®ãªã¯ã¨ã¹ããããã«ãã£ã¦é©åãªã³ã³ãã³ããè¿ãè¨å®ãæå¹ã«ããã¨ããæå³ããã¨ãã°ãAccept-Languageãããå¤ã«ãenããåªå ããã¦ããã°è±èªçã®ãã¡ã¤ã«ããjaããåªå ããã¦ããã°æ¥æ¬èªçã®ãã¡ã¤ã«ãéãåºããããªãã¨ãã§ããããã ãITmedia エンタープライズ : Linux Tips「ApacheのMultiViews機能ってなに?」
DocumentRootã«ã¯ãRailsã¢ããªã®ãã£ã¬ã¯ããªãæå®ããã
3. Railsã®ãµã³ãã«ã¢ããªãä½æãã
ååãææ°ï¼3.1.1ï¼ã®Railsãã¤ã³ã¹ãã¼ã«ããããæå ã®æ¸ç±ã®Railsã®ãã¼ã¸ã§ã³ã¯3.0.xã ã£ãã®ã§ãã¾ãã¯ãã¦ã³ã°ã¬ã¼ãããã
$ rails -v Rails 3.1.1 $ sudo gem install rails --version 3.0.10 --no-rdoc $ sudo gem uninstall rails --version 3.1.1 $ rails -v Rails 3.0.10
ããã¦ãã¢ããªã±ã¼ã·ã§ã³ãæ°è¦ã«ä½æããã
$ cd /var/www/html $ sudo rails new demo -d mysql # DBãæå®ãã¦ã¢ããªã±ã¼ã·ã§ã³ãæ°è¦ä½æ $ cd demo $ sudo bundle install # 足ããªãã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã« $ sudo rails generate scaffold Post name:string title:string content:text # Postã¨ããååã®ã¢ãã«ããã¥ã¼ãã³ã³ããã¼ã©ã¼ã®ä¸å¼ãä½æ $ sudo rake db:create RAILS_ENV=production # DBãæ°è¦ä½æ $ sudo rake db:migrate RAILS_ENV=production # ãã¼ãã«ãæ°è¦ä½æ
ãã©ã¦ã¶ã§http://wwwXXXXXX.sakura.ne.jp:3000/postsãéãã¦ãdemoã¢ããªã±ã¼ã·ã§ã³ã®ç»é¢ã表示ãããã®ã§ããã§OKã
ã¨æããããä¸è¨ã®ããæ¹ã ã¨ãã¢ããªã±ã¼ã·ã§ã³ã®ãã£ã¬ã¯ããªããã¡ã¤ã«ã®ãªã¼ãã¼ã¯rootã«ãªã£ã¦ãããrootã ã¨ãã°ãã¡ã¤ã«ã«ãã°ãæ¸ãè¾¼ããªããªã©ã®åé¡ãåºããããã§ãlogã¨tmpãã£ã¬ã¯ããªã®ãªã¼ãã¼ã¨ãã¼ãã·ã§ã³ãå¤æ´ããã
$ ls -l /var/www/html/demo/log -rw-rw-rw- 1 root root 0 Nov 14 23:11 development.log -rw-rw-rw- 1 root root 0 Nov 14 23:39 production.log -rw-rw-rw- 1 root root 0 Nov 14 23:11 server.log -rw-rw-rw- 1 root root 0 Nov 14 23:11 test.log
ãã®ç¶æ ã§URLã«ã¢ã¯ã»ã¹ãã¦ããproduction.logã®ãã¡ã¤ã«ãµã¤ãºã¯0ã®ã¾ã¾ã以ä¸ã®ããã«ãã£ã¬ã¯ããªã®ãªã¼ãã¼ã¨ãã¼ãã·ã§ã³ãå¤æ´ããã
$ cd /var/www/html/demo $ sudo chown -R montecut:users log tmp # ãªã¼ãã¼ãmontecutã«å¤æ´ $ sudo chmod 755 -R log tmp # ã¢ã¯ã»ã¹æ¨©ãè¨å® $ sudo service httpd restart # Apacheãåèµ·å
è¤æ°äººã§ãµã¼ãã¼ã使ããã¨ãèããã¨ãæ¬å½ã¯ãªã¼ãã¼ã¯å人ã®ãã°ã¤ã³ã¢ã«ã¦ã³ãã§ãªãæ¹ããããããã¨ãããããããã§URLï¼http://wwwXXXXXX.sakura.ne.jp:3000/postsï¼ãéãããã®å¾ããã°ãã¡ã¤ã«ã確èªããã¨ããã¡ã¤ã«ãµã¤ãºãå¢ãã¦ãããã¨ã確èªã§ããã
$ ls -l /var/www/html/demo/log -rwxr-xr-x 1 montecut users 0 Nov 14 23:11 development.log -rwxr-xr-x 1 montecut users 251 Nov 14 23:39 production.log -rwxr-xr-x 1 montecut users 0 Nov 14 23:11 server.log -rwxr-xr-x 1 montecut users 0 Nov 14 23:11 test.log