apache伺服器安裝以及使用passenger外掛部署rails應用
小例子可以部署在rails自帶的WEBrick上,逐漸往後走還得上Apache。
安裝apache伺服器
命令是sudo apt-get install apache2
安裝passenger外掛
安裝完畢還不能立刻用,因為想執行rails應用的話,還要為apache伺服器安裝外掛passenger。
passenger是一個gem包,安裝命令是gem install passenger
passenger整合進Apache
執行命令passenger-install-apache2-module,進入安裝程式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[email protected]:~$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v3.0.18.
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. |
回車確定之後,會進行依賴關係的檢查,有部分內容不通過沒有關係,回車之後會提示如何解決。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Checking for required software...
* GNU C++ compiler... found at /usr/bin/g++
* Curl development headers with SSL support... found
* OpenSSL development headers... found
* Zlib development headers... found
* Ruby development headers... found
* OpenSSL support for Ruby... found
* RubyGems... found
* Rake... found at /home/abbuggy/.rvm/wrappers/ruby-1.9.2-p320/rake
* rack... found
* Apache 2... found at /usr/sbin/apache2
* Apache 2 development headers... not found
* Apache Portable Runtime (APR) development headers... not found
* Apache Portable Runtime Utility (APU) development headers... not found
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.
|
按照提示將缺少的依賴包,比如我缺少apache2-perfork-dev,libapr1-dev,libaprutil1-dev這三個包。
1 2 3 4 5 6 7 8 9 10 11 12 |
--------------------------------------------
Installation instructions for required software
* To install Apache 2 development headers:
Please run apt-get install apache2-prefork-dev as root.
* To install Apache Portable Runtime (APR) development headers:
Please run apt-get install libapr1-dev as root.
* To install Apache Portable Runtime Utility (APU) development headers:
Please run apt-get install libaprutil1-dev as root.
|
程式也給出瞭解決方法。雖然是分別給出的,但我們可以一併執行。你的環境可能和我不一樣,按照上面的提示來九成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[email protected]:~$ sudo apt-get install apache2-prefork-dev libapr1-dev libaprutil1-dev
正在讀取軟體包列表... 完成
正在分析軟體包的依賴關係樹
正在讀取狀態資訊... 完成
下列【新】軟體包將被安裝:
apache2-prefork-dev libapr1-dev libaprutil1-dev
升級了 0 個軟體包,新安裝了 3 個軟體包,要解除安裝 0 個軟體包,有 0 個軟體包未被升級。
需要下載 0 B/1,718 kB 的軟體包。
解壓縮後會消耗掉 11.3 MB 的額外空間。
Selecting previously unselected package libapr1-dev.
(正在讀取資料庫 ... 系統當前共安裝有 230693 個檔案和目錄。)
正在解壓縮 libapr1-dev (從 .../libapr1-dev_1.4.6-1_i386.deb) ...
Selecting previously unselected package libaprutil1-dev.
正在解壓縮 libaprutil1-dev (從 .../libaprutil1-dev_1.3.12+dfsg-3_i386.deb) ...
Selecting previously unselected package apache2-prefork-dev.
正在解壓縮 apache2-prefork-dev (從 .../apache2-prefork-dev_2.2.22-1ubuntu1.2_i386.deb) ...
正在處理用於 man-db 的觸發器...
正在設定 libapr1-dev (1.4.6-1) ...
正在設定 libaprutil1-dev (1.3.12+dfsg-3) ...
正在設定 apache2-prefork-dev (2.2.22-1ubuntu1.2) ...
|
再次執行passenger-install-apache2-module安裝,編譯完畢之後,給出了成功提示。還要求將以下內容放到apache的配置檔案中。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /home/abbuggy/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.18/ext/apache2/mod_passenger.so
PassengerRoot /home/abbuggy/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.18
PassengerRuby /home/abbuggy/.rvm/wrappers/ruby-1.9.2-p320/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的配置檔案位於/etc/apache2/httpd.conf,編輯並新增之。到目前為止,httpd.conf是這樣的。
1 2 3 |
LoadModule passenger_module /home/abbuggy/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.18/ext/apache2/mod_passenger.so
PassengerRoot /home/abbuggy/.rvm/gems/ruby-1.9.2-p320/gems/passenger-3.0.18
PassengerRuby /home/abbuggy/.rvm/wrappers/ruby-1.9.2-p320/ruby
|
我想通過類似於“localhost/simple-cms”的地址訪問我的例子站點,後續更多的站點都可以按照形如
localhost/site1
localhost/site2
的格式。
首先得配置一個基準目錄也就是apache的根目錄。
向httpd.conf中新增如下的配置,將/var/www設定為web的根目錄,回頭要往這個目錄中放置各個站點。
1 2 3 4 5 6 7 |
< VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www
< Directory /var/www>
Allow from all
</ Directory >
</ VirtualHost >
|
接下來把一個具體的站點掛在web根目錄下面,比如這個simple-cms。還是向/etc/apache2/httpd.conf配置,後面的四行是新加的。/simple-cms是相對於上面localhost的訪問地址
1 2 3 4 5 6 7 8 9 10 11 12 |
< VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www
< Directory /var/www>
Allow from all
</ Directory >
RailsBaseURI /simple-cms
< Directory /var/www/simple_cms>
Options -MultiViews
</ Directory >
</ VirtualHost >
|
/var/www/test_site是一個軟連線,指向開發環境中的public目錄。
1 |
sudo ln -s /home/abbuggy/workspace/simple_cms/public /var/www/simple-cms
|
不過我自己配置的時候,死活配不對,總是提示"We're sorry,but something went wrong!"臨近崩潰邊緣時終於找到解決方案。”The defaultRAILS_ENV environment in which deployed Rails applicationsare run, is “production”. You can change this by changing theRailsEnv configuration option.“ --來自於Phusion Passenger users guide
即預設生效的是production環境,如果在除錯時使用development環境的話需要在apache配置檔案中增加一行RailsEnv development,所以正確的配置是。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
< VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www
< Directory /var/www>
Allow from all
</ Directory >
RailsBaseURI /simple-cms
RailsEnv development
< Directory /var/www/simple_cms>
Options -MultiViews
</ Directory >
</ VirtualHost >
|
重啟apache,sudo service apache2 restart,訪問localhost/simple-cms成功。