apache服务器安装以及使用passenger插件部署rails应用,基于ubuntu 12.04 LTS

作者: 边晓宇
发布时间:2015-06-24 11:33:11

小例子可以部署在rails自带的WEBrick上,逐渐往后走还得上Apache

安装apache服务器

命令是sudo apt-get install apache2

安装passenger插件

安装完毕还不能立刻用,因为想运行rails应用的话,还要为apache服务器安装插件passenger

passenger是一个gem包,安装命令是gem install passenger

passenger集成进Apache

执行命令passenger-install-apache2-module,进入安装程序。

abbuggy@abbuggy-ubuntu:~$ 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.

回车确定之后,会进行依赖关系的检查,有部分内容不通过没有关系,回车之后会提示如何解决。

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这三个包。

--------------------------------------------    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.  

程序也给出了解决方法。虽然是分别给出的,但我们可以一并执行。你的环境可能和我不一样,按照上面的提示来九成。

abbuggy@abbuggy-ubuntu:~$ 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的配置文件中。

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是这样的。

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
部署Rails应用

我想通过类似于“localhost/simple-cms”的地址访问我的例子站点,后续更多的站点都可以按照形如

localhost/site1

localhost/site2

的格式。

首先得配置一个基准目录也就是apache的根目录。

向httpd.conf中添加如下的配置,将/var/www设置为web的根目录,回头要往这个目录中放置各个站点。

<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的访问地址

   <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目录。

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,所以正确的配置是。

   <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成功。

标签: Apache Rails Ubuntu
来源:http://www.cnblogs.com/abbuggy/archive/2012/12/10/2812060.ht

推荐: