centos ruby on rails apache mysql 安装配置详解
客户的服务器,是Ubuntu + apache2 + ruby on rails,现在客户的服务器有的页面老是报500错误 ,ruby这门语言只是了解过,没有深入的研究。也从来没有搭建过ruby的服务器环境,还有就是客户为什么会用Ubuntu来做服务器,我感觉很奇怪。我在CentOS上面装完ruby on rails后,我明白了。
前言,yum安装后的种种问题
开始的时候,我是用yum来安装apache mysql ruby on rails等,装完后发现很多问题,用gem根本装不了rails
我的CentOS版本
[root@localhost script]# cat /etc/redhat-release
CentOS release 5.9 (Final)
[root@localhost ~]# gem install rails #安装过程中报以下错误
ERROR: Error installing rails:
i18n requires RubyGems version >= 1.3.5
rubyGems版本没达到要求,yum装的rubyGems是1.3.2,源里面没有更新的二进制的源码,这个问题无法解决。
[root@localhost ~]# gem update --system
Updating RubyGems
Updating rubygems-update
ERROR: Error installing rubygems-update:
rubygems-update requires Ruby version >= 1.8.7
更新时报ruby版本达不到要求,yum装的ruby版本是1.8.5。想想算了,还是手动安装吧。如果你要在CentOS上安装ruby on rails,先看看遇到的错误 ,下面标红的部分,可以少走一些弯路。
一,安装所要的软件
ruby: wget http://ftp.ruby-lang.org/pub/ruby/ruby-1.9-stable.tar.gz
rubygems: wget http://rubyforge.org/frs/download.php/76729/rubygems-1.8.25.tgz
nodejs: wget http://nodejs.org/dist/v0.8.18/node-v0.8.18.tar.gz
nodejs不建议下新的,因为要用python2.6,centos5.9下面更新了python还是2.4的。非要用最新的node,就要单独装一个python26
[root@localhost ~]# yum install httpd mysql server libyaml*
在这里要注意,先装libyaml的二个包,不然后面你装好了gem后,会报错的,报错后在装libyaml包,ruby还要重新安装,不然报错会一直存在,这个有点坑爹
三,安装ruby on rails
1,安装ruby
[root@localhost ~]# tar xzvf ruby-1.9-stable.tar.gz [root@localhost ~]# cd ruby-1.9.3-p374 [root@localhost ruby-1.9.3-p374]# mkdir /usr/local/ruby [root@localhost ruby-1.9.3-p374]# ./configure --prefix=/usr/local/ruby [root@localhost ruby-1.9.3-p374]# make && make install
2,加环境变量
[root@localhost ~]# export PATH=/usr/local/ruby/bin:$PATH
这样加环境变量,只是临时的。
3,安装rubygems
[root@localhost ~]# tar -xvzf rubygems-1.8.25.tgz [root@localhost ~]# cd rubygems-1.8.25 [root@localhost rubygems-1.8.25]# ruby setup.rb
如果报以下错误,说明libyaml相关包没有装。
[root@localhost rubygems-1.8.25]# gem -v
/usr/local/ruby/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
解决方法:
yum install libyaml* 重装ruby和rubygems
四,安装rails
1,换安装地址
因为是通过网络安装,所以我们要选择一个快的安装地址。
[root@localhost ~]# gem sources --remove http://gems.rubyforge.org/ http://gems.rubyforge.org/ removed from sources [root@localhost ~]# gem sources -a http://ruby.taobao.org/ http://ruby.taobao.org/ added to sources [root@localhost ~]# gem source -l *** CURRENT SOURCES *** http://ruby.taobao.org/
2,gem更新一下源
当用gem update时报以下错误
[root@localhost ruby-1.9.3-p374]# gem update
ERROR: Loading command: update (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NameError)
uninitialized constant Gem::Commands::UpdateCommand
解决办法:
[root@localhost ~]# cd /home/zhangy/download/ruby-1.9.3-p374/ext/zlib/ [root@localhost zlib]# ruby extconf.rb [root@localhost zlib]# make && make install
在这里有点坑爹,zlib的包,我用yum装过了,但是ruby根本不识别,没办法用,才用ruby安装包里的zlib
3,gem安装rails
[root@localhost zlib]# gem install rails -V
安装时报以下错误:
file 'lib' not found
解决方法:
[root@localhost zlib]# gem install rdoc -V
问题解决后在说bundle和passener,bundle创建rails项目要到用,passenger配置apache时要用到。
[root@localhost zlib]# gem install bundle -V [root@localhost zlib]# gem install passenger -V
小提示,安装时加上-V参数,这样你可以看到,如果不加,什么内容都不显示,感觉像死机了一样
4,passenger生成apache的配置部分
[root@localhost bin]# passenger-install-apache2-module
我装的时候,在这里,缺少了差不多5,6个包,总之缺少什么包,就用yum装什么包,openssl除外,openssl包要用ruby自带的,不清楚ruby为什么要这样。报错如下:
To install Openssl support for Ruby:
解决办法:
[root@localhost ~]# cd /home/zhangy/download/ruby-1.9.3-p374/ext/openssl/ [root@localhost openssl]# ruby extconf.rb [root@localhost openssl]# make && make install
装完后,在运行,passenger-install-apache2-module
,提示你修改apache的配置,这个对于初学者,还是很方便的。
我在httpd.conf添加的配置如下:
LoadModule passenger_module /usr/local/ruby/lib/ruby/gems/1.9.1/gems/passenger-3.0.19/ext/apache2/mod_passenger.so PassengerRoot /usr/local/ruby/lib/ruby/gems/1.9.1/gems/passenger-3.0.19 PassengerRuby /usr/local/ruby/bin/ruby listen 8080 NameVirtualHost localhost:8080 <VirtualHost localhost:8080> ServerName localhost:8080 DocumentRoot /var/www/public <Directory /var/www/public> AllowOverride all Options Indexes -MultiViews </Directory> </VirtualHost>
5,创建目录,并重启apache:
[root@localhost www]# mkdir /var/www/public [root@localhost www]# /etc/init.d/httpd restart
五,创建一个rails的项目,并启动server
1,创建rails
[root@localhost www]# rails new public
创建时报以下错误:
报以下错误 ,ruby默认用的是sqlite数据库,缺少了连接sqlite的模块
An error occurred while installing sqlite3 (1.3.7), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.3.7'` succeeds before bundling.
解决办法:
[root@localhost www]# yum install sqlite-devel [root@localhost www]# gem install sqlite3 -V
或者
[root@localhost public]# bundle install --without sqlite
在这里要注意rails server时要在你的web根目录下,如果单独bundle install的时候,也是要在web根目录下的。不然报错。
2,启动rails的server
[root@localhost public]# rails server
启动报错如下:
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs.rb:5:in `<module:ExecJS>'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/execjs-1.4.0/lib/execjs.rb:4:in `<top (required)>'
解决方法如下:
[root@localhost public]# gem install therubyracer -V [root@localhost public]# gem install execjs -V //有人说只要装上面二个就不会出现Could not find a JavaScript runtime错误了. 但是我试了不行了。非要装node [root@localhost ~]# tar xzvf node-v0.8.18.tar.gz [root@localhost ~]# cd node-v0.8.18 [root@localhost node-v0.8.18]# ./configure
./configure报错如下:
[root@localhost node-v0.8.18]# ./configure
File "./configure", line 360
1 if options.unsafe_optimizations else 0)
^
SyntaxError: invalid syntax
解决办法如下:
[root@localhost ~]# yum install python26 修改node的configure 文件第一行,python改成python26 修改node的Makefile 文件第四行,python改成python26
改好后在./configure ,make && make install
[root@localhost script]# rails server => Booting WEBrick => Rails 3.2.11 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2013-01-30 23:19:48] INFO WEBrick 1.3.1 [2013-01-30 23:19:48] INFO ruby 1.9.3 (2013-01-15) [i686-linux] [2013-01-30 23:19:48] INFO WEBrick::HTTPServer#start: pid=2784 port=3000
总算启动成功。
3,rails 生成代码
[root@localhost public]# rails generate controller welcome index
现在我们就可以通过url来访问了。
localhost:3000而不是apache中配置的8080,感觉根配置php和perl不一样,感觉很怪。
url请求的,server端会收到信息,我还不习惯这种方式。感觉怪。到这儿ruby on rails在apache上已经跑起来了。
六,安装ruby mysql的连接库
1,安装
[root@localhost www]# gem install mysql
如果报以下错误:
Building native extensions. This could take a while...
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.
/usr/local/ruby/bin/ruby extconf.rb
checking for mysql_ssl_set()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
解决方法:
root@localhost ~]# yum install mysql-devel
如果安装成功会提示以下信息:
[root@localhost www]# gem install mysql
Building native extensions. This could take a while...
Successfully installed mysql-2.9.0
1 gem installed
Installing ri documentation for mysql-2.9.0...
Installing RDoc documentation for mysql-2.9.0...
2,测试ruby连接mysql,创建一个test.rb文件
require "mysql" dbc=Mysql.real_connect('localhost','root','','test') query_parse=dbc.query('select * from user') while row=query_parse.fetch_row do puts "#{row[0]},#{row[1]}" end
运行结果如下:
第一次执行报错了,因为代码中多写一个-,改过后就正常了。到这儿配置结束了,总体感觉,挺麻烦的,不像在Ubuntu下面,ruby和rubygem的版本都很高并且node块,在源里面都有,不用手动编译安装。找资料的时候,整屏幕的都是在Ubuntu下安装ruby on rails的文章