Ruby on Rails网站布署

前言:

        安装环境:Linux
        数据库:development:sqlite;production:mysql
        全自动代理方式:Nginx + Passenger
一,安装 Ruby, Rails 及相关

二,安装 MySQL :
       1.判断系统是否已经安装mysql:

sudo  netstat  -tap  grep  mysql
        2.若已安装mysql但不能正常使用,则先卸载:
sudo apt-get autoremove --purge mysql-server-5.0
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common  //非常重要
      上面的其实有一些是多余的,建议还是按照顺序执行一遍
      清理残留数据:
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
      3.安装mysql:
sudo apt-get install mysql-server mysql-client   //默认用户是root,一定要输入密码
      4.如果安装出错则可能是 安装rails环境的时候没安装依赖的mysql开发库和头文件
sudo apt-get install libmysql-ruby libmysqlclient-dev
 三,让你的项目在production下能跑起来(因为是 production状态,要注意加“ RAILS_ENV='production' )(之前要能够用 rails s 跑起来       1.copy你的项目到服务器上,如果是在github上:
git clone  你的github网址
      2.进入项目中,bundle
cd project
bundle install
      3.用“ yaml_db”把sqlite数据库转换为mysql数据库:
gem 'yaml_db'     //在Gemfile中添加
bundle install
rake db:dump
      4.修改config/database.yml 文件
production:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: name
  pool: 5
  username: root
  password: password
       5如果还没有创建数据库的话,创建一个数据库        6.加载数据库:
rake db:load   RAILS_ENV='production'
      7.让工程在服务器上跑起来
rails s -e production
 四,安装Nginx + Passenger      1.由于 Passenger 需要重新编译 Nginx,如果你之前有安装 Nginx 需要卸载掉!
nginx -v   //确定是否有
sudo apt-get remove nginx-common nginx-full nginx
    2. 用 Passenger 安装 Nginx:
 sudo passenger-install-nginx-module
    3.安装 Nginx 的启动脚本,以及配置开机自动启动
 cd ~/
git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git
sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
      配置 Nginx 开机自启动
sudo update-rc.d nginx defaults
 五,配置 Nginx 与网站
sudo vim /opt/nginx/conf/nginx.conf    //用passenger安装的nginx目录默认在这里
//-----------------------------打开后可看到------------------
user jason;      //修改成你的系统帐号名
worker_processes 8;       //修改成和你 CPU 核数一样
pid /var/run/nginx.pid;
http {
  include       mime.types;
  default_type  application/octet-stream;
  client_max_body_size 50m;
  sendfile        on;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  gzip on;
  gzip_disable "msie6";
  //---------------------- ------------ 重点修改内容 --------
  server {    
    //此处用于防止其他的域名绑定到你的网站上面
    listen 80 default;
    return 403;
  }
  server {
    listen       80;
    server_name  you.host.name;   //请替换成你网站的域名
    rails_env    production;
    root         /home/jason/www/gitlab/public;
    passenger_enabled on;
    location ~ ^(/assets) {
      access_log        off;
      //设置 assets 下面的浏览器缓存时间为最大值(由于 Rails Assets Pipline 的文件名是根据文件修改产生的 MD5 digest 文件名,所以此处可以放心开启)
      expires           max; 
    }
  }
}
     重启 Nginx
 sudo /etc/init.d/nginx start   //还可以用restart
 参考: nginx:http://blog.csdn.net/tuoxz/article/details/20370083

 

数据库:http://stackoverflow.com/questions/1670154/convert-a-ruby-on-rails-app-from-sqlite-to-mysql

猜你喜欢

转载自stu-zhaoli.iteye.com/blog/2068830