搭建 CentOS 6 服务器(5) - Ruby/Rails环境

(1)安装Rails

1)安装需要的Package

# yum -y install gcc
# yum -y install gcc-c++
# yum -y install zlib zlib-devel
# yum -y install readline readline-devel
# yum -y install openssl openssl-devel


最好是 #yum install gcc gcc-c++ openssl* readline* ncurses* zlib* libxml* libjpeg* libpng* libxslt* libtool*

2)安装Ruby

# cd /usr/local/src
# wget ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
# tar zxvf ruby-2.0.0-p247.tar.gz
# cd ruby-2.0.0-p247
# ./configure
# make
# make install
# ruby -v


3)更新Gem

# gem update --system
# gem -v


4)安装Rails

# gem install rdoc
# gem install rails
# rails -v


5)卸载Ruby

# cd /usr/local/src/ruby-2.0.0-p247
# cat .installed.list | xargs rm


常见问题

1)问题1(NoMethodError:openssl)
引用
# gem update --system
ERROR:  Loading command: update (LoadError)
        cannot load such file -- openssl
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

# cd /usr/local/src/ruby-2.0.0-p247/ext/openssl/
# ruby extconf.rb
# make
# make install


2)问题2(NoMethodError:zlib)
引用
# gem update --system
ERROR: Loading command: update (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NoMethodError)
undefined method `invoke_with_build_args' for nil:NilClass

# cd /usr/local/src/ruby-2.0.0-p247/ext/zlib/
# ruby extconf.rb
# make
# make install


(2)Apache+Passenger

1)安装Passenger

# gem install passenger

2)安装Passenger的Apache模块

# passenger-install-apache2-module


缺什么装什么
# yum install gcc-c++
# yum install curl-devel
# yum install httpd-devel
# yum install apr-devel
# yum install apr-util-devel


如果是通过源码安装的Apache,会提示以下错误:
引用
* 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

引用
export APXS2=/usr/local/apache2/bin/apxs
export PATH=/usr/local/apache2/bin:$PATH


再次执行
# passenger-install-apache2-module


3)设置Apache

/etc/httpd/conf/httpd.conf

引用
#Passenger
LoadModule passenger_module /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7
PassengerDefaultRuby /usr/local/bin/ruby


/etc/httpd/conf.d/rails.conf

引用
<VirtualHost *:80>
   ServerName localhost:80
   DocumentRoot /var/www/rails/testapp/public
   <Directory /var/www/rails/testapp/public>
      AllowOverride all
      Options -MultiViews
   </Directory>
</VirtualHost>


4)创建Rails工程

# cd /var/www/rails
# rails new testapp
# bundle install


# yum install sqlite-devel
# cd testapp/
# bundle install


5)创建Scaffold

# rails g scaffold friend name:string address:string
# rake db:create RAILS_ENV=production
# rake db:migrate RAILS_ENV=production
# rake assets:precompile RAILS_ENV=production


6)启动服务

# setenforce 0
# /etc/init.d/httpd restart


http://localhost/friends


(3)Nginx+Unicorn

1)安装Nginx

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
# yum install nginx

# /etc/init.d/nginx start



2)安装unicorn

# gem install unicorn

3)创建测试工程

省略,和以上步骤相同

4)设置Unicorn

# cd /var/www/rails/testapp/config/
# wget -O unicorn.rb https://raw.github.com/defunkt/unicorn/master/examples/unicorn.conf.rb

修改模板文件,并创建相应的文件夹
config/unicorn.rb


5)启动服务

# unicorn_rails -c config/unicorn.rb -E production -p 5000 -D
# /etc/init.d/nginx restart


http://localhost:5000/friends


6)设置Ngnix

/etc/nginx/nginx.conf


http://localhost/friends


常见问题
1)问题1(Could not find a JavaScript runtime.)

将CoffeeScript编译成JavaScript需要JavaScript的runtime,
不像Windows或Mac OS X 默认已经有Javascript引擎,Linux下需要安装。
# gem install execjs
# gem install therubyracer

实际是libv8,也可以安装nodejs。JRuby用户更多的使用therubyrhino

2)问题排查2(PassengerWatchdog: Permission denied.)
开启SELinux
# setenforce 0

3)问题排查3(SQLite3::ReadOnlyException:attempt to write a readonly database)
SQLite的数据库文件权限不足
# chmod 777 /db
# chmod 777 databasefilename
# chown user.user databasefilename

猜你喜欢

转载自rensanning.iteye.com/blog/1927921