最近一段时间都在搞 rails,也打算用它写一个 hacker blog,把之前学习的基本安装和部署总结下。 地址在 iteye blog http://flowerwrong.iteye.com/blog/2086358
一。安装 rvm
sudo apt-get install curl
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm -v
二。安装 ruby2.1.2(目前最新,也建议最新)
rvm install 2.1.2
rvm use --default 2.1.2
ruby -v
gem -v
三。替换 gem 原为 taobao
gem source -r https://rubygems.org/
gem source -a https://ruby.taobao.org
四。安装最新版 rails(默认最新,建议最新)
gem install rails
rails -v
五。安装 nodejs 最新版 via ppa
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
node -v
六。安装 mysql(我的 rails 项目使用 mysql)
netstat -tap |grep mysql#检查是否已经安装mysql
sudo apt-get install mysql-server mysql-client
sudo /etc/init.d/mysql start|stop|restart|reload|force-reload|status
七。安装 sublime text3(编辑器)或者 atom(编辑器)via ppa
#sublime text3
sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer
#remove sublime text3
sudo apt-get remove sublime-text-installer
#atom /opt/atom/atom
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom
八。安装 nginx+passenger
#install passenger
#参考:https://www.phusionpassenger.com/documentation/Users%20guide%20Nginx.html#install_on_debian_ubuntu
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7
sudo apt-get install apt-transport-https ca-certificates
cd /etc/apt/sources.list.d
sudo apt-get install vim
sudo vim passenger.list
### to_add
# Ubuntu 12.04
deb https://oss-binaries.phusionpassenger.com/apt/passenger precise main
###
sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
sudo apt-get update
sudo apt-get install nginx-extras passenger
九。配置 nginx+passenger
1.编辑/etc/nginx/nginx.conf,将
# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;
改为(取消注释,修改 passenger_ruby 路径)
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;#默认即可
passenger_ruby /home/yang/.rvm/wrappers/default/ruby;#注意yang为你的用户名
sudo rm /etc/nginx/sites-enabled/default
sudo touch /etc/nginx/sites-enabled/hackerblog.com.conf
#add to /etc/nginx/sites-enabled/hackerblog.com.conf
###
server {
listen 80 default;
server_name hackerblog.com; # 这里填写你真实域名
root /var/www/hackerblog/public;
passenger_enabled on;
}
###
sudo service nginx restart
十。建立 rails 项目
rails new hackerblog --skip-bundle -d mysql
#如果遇到------程序“rails”尚未安装
#执行bash --login
cd hackerblog
bundle install
#如果遇到:An error occurred while installing mysql2,执行sudo apt-get install libmysql-ruby libmysqlclient-dev
#rake db:create
#rake db:drop
#rake db:migrate
#rails s
#http://127.0.0.1:3000/
cp -R /home/yang/dev/ruby/rails/hackerblog /home/yang/www/hackerblog
cd /home/yang/www/hackerblog
RAILS_ENV=production rake db:create db:migrate
#注意部署环境mysql密码不能为空
rake assets:precompile
十一。修改 mysql root 密码(附加)
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
十三。注意事项
#visit http://127.0.0.1
#如果遇到502,就查看/var/log/nginx/error.log
#如果问题是secret
#则执行 rake secret,并复制secret到config目录下的secrets.yml里面的production位置。
貌似必须要设置 routes.rb 的 root 才能正常访问,否则会出错,待验证。
#config/ervironment/production.rb
config.serve_static_assets = true
rails generate controller welcome index
#routs.rb
root 'welcome#index'