欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

centos6 安装ruby on rails(nginx+unicorn)(建立环境依赖)

.

1.第一步

su root ***

安装依赖包

yum -y install gcc
yum install flex autoconf zlib curl zlib-devel curl-devel bzip2 bzip2-devel ncurses-devel libjpeg-devel libpng-devel libtiff-devel freetype-devel pam-devel

gcc+ gcc-c++ libxml2 libxml2-devel libxslt libxslt-devel
(说明:以上包都得安装上,否则最后rails 环境会出错)

2.安装ruby

wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p0.tar.gz
tar -zxvf ruby-1.9.2-p0.tar.gz
cd ruby-1.9.2-p0
./configure –prefix=/usr/local/ruby
make
make test
make install
3.ruby 安装后 gem list 出现错误

ERROR: Loading command: list (LoadError)
no such file to load — zlib
ERROR: While executing gem … (NameError)
uninitialized constant Gem::Commands::ListCommand

解决办法:
sudo apt-get install zlib1g-dev
(2)然后到源码目录的ext/zlib下 (刚才解压的ruby1.9.2目录)
sudo ruby extconf.rb (需要管理员权限)
make
sudo make install

4. sudo: ruby: command not found

建立以下软链接:

sudo ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
sudo ln -s /usr/local/ruby/bin/gem /usr/bin/gem
5. gem install rails

6. 安装 mysql

三条命令实现安装 mysql

yum install mysql-server
yum install mysql
yum install mysql-devel

默认安装的mysql 密码是空 , 所以需要修改密码

mysql -u root -p 回车进入
mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD(‘new-password’) WHERE user=’root’;
mysql> FLUSH PRIVILEGES;
重启 mysql service
/etc/init.d/mysqld restart
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/ var/lib/mysql/mysql.sock’ (2)
sudo /etc/init.d/mysqld start

7. 安装git和部署项目

yum install git-core
ssh-keygen -t rsa

如果是从别的地方拷贝过来的,需 进行权限操作:

chmod 777 id_rsa id_rsa.pub

8.克隆服务器项目

(1).rails 项目为了避免麻烦的权限,一般部署在 ~/ 目录下

mkdir projects && cd ..
git clone git@192.168.8.201:entos.git
cd entos
切换到开发分支
git checkout develop
bundle install
mv config/database.yml.example database.yml

(2) 修改示例数据库配置文件

vi config/database.yml edit root# password
建库,数据迁移
bundle exec rake RAILS_ENV=production db:create:all
bundle exec rake RAILS_ENV=production db:migrate

(3) 启动服务并测试

rails s

本地测试rails 环境是否搭建成功

浏览器访问:localhost:3000 是否出现首页

(4) 再此期间遇到的问题:

a 如果出现:errors:libxml2 is missing. please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.

解决方法:sudo yum install -y libxml2 libxml2-devel libxslt libxslt-devel (此时要有 -y 参数,否则安装出错)

b 如果出现 :make: g++: Command not found

解决方法: yum -y install gcc+ gcc-c++

c 如果出现: no such file to load — openssl (LoadError)

解决办法: cd ~/Downloads/ruby-1.9.2-p180/ext/openssl (assuming you extracted the ruby tar to ~/Downloads)

$ ruby extconf.rb && make && sudo make install

9. nginx+unicorn 部署:

(1) install nginx

wget http://nginx.org/download/nginx-1.0.10.tar.gz
sudo ./configure –prefix=/usr/local/nginx
make && make install
启动 :sudo /usr/local/nginx/sbin/nginx
停止:sudo /usr/local/nginx/sbin/nginx -s stop
重启:sudo /usr/local/nginx/sbin/nginx -s reload

(2)安装遇到的问题

./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel解决问题

yum -y install pcre-devel

(3)安装Unicorn

gem install unicorn
获取 unicorn 配置文件
curl -o config/unicorn.rb https://raw.github.com/defunkt/unicorn/master/examples/unicorn.conf.rb

修改配置文件:

vi config/unicorn.rb

app_path=”/home/dong/projects/entos”
working_directory app_path # available in 0.94.0+
listen app_path+”/tmp/nginx/sockets/unicorn.sock”, :backlog => 64 (/tmp/nginx/sockets 需提前创建该文件目录,特别注意:unicorn.sock 不用创建,当unicorn_rails 启动后,会自动生成该文件*.sock)
listen 8080, :tcp_nopush => true
timeout 30
pid app_path+”/tmp/pids/unicorn.pid”
stderr_path app_path+”/log/unicorn.stderr.log”
stdout_path app_path+”/log/unicorn.stdout.log”

(须在项目log目录下创建 unicorn.stderr.log ,unicorn.stdout.log,两个文件)

touch log/unicorn.stderr.log

touch log/unicorn.stdout.log

启动 unicorn_rails

unicorn_rails config/unicorn.rb -E production -D

 
sudo gedit /usr/local/nginx/conf/nginx.conf

关键代码配置:

user root root;
http{
upstream unicorn_server {
server unix:/home/dong/projects/entos/tmp/nginx/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.entsea.com;
root /home/dong/projects/entos/public;
#charset koi8-r;

 
#access_log logs/host.access.log main;

 
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_pass http://unicorn_server; #关键部分,如果没此配置:则会出现 nginx 403 Forbidden ,即使放到最大权限也无济于事
}
}

 
}

如果是本地测试,需添加 链接

vim /etc/hosts

127.0.0.1 www.entsea.com

测试:浏览器输入 www.entsea.com 会看到首页

10.centos 安装中文输入法

yum install “@Chinese Support”

Preferences-Input method-最后选择添加…