5月 10, 2012

在Amazon ec2上用ubuntu11 建立 rails server(apache2+passenger)

在Amazon ec2上用ubuntu11 建立 rails server(apache2+passenger)

改root密碼

sudo su passwd

換預設編輯器

/usr/bin/select-editor

換port

vim /etc/ssh/sshd_config

1.Port 7533 2. PasswordAuthentication yes

/etc/init.d/ssh restart

調整server時間

dpkg-reconfigure tzdata # 將時區設回台灣 ntpdate time.stdtime.gov.tw

安裝系統套件

sudo apt-get update sudo apt-get upgrade sudo apt-get install git sudo apt-get install mysql-server libmysqlclient15-dev sudo apt-get install curl sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline6-dev sudo apt-get install libbuilder-ruby

安裝ruby

sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev libv8-dev autoconf libc6-dev xclip ncurses-dev automake

wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p194.tar.gz

tar xvf ruby-1.9.3-p194.tar.gz ./configure make make install

安裝rails

gem install rails --no-rdoc --no-ri gem install mysql --no-rdoc --no-ri gem install passenger --no-rdoc --no-ri sudo apt-get install nodejs Install CURL with SSL sudo apt-get install libcurl4-openssl-dev

Install Apache

sudo apt-get install apache2 sudo apt-get install apache2-prefork-dev

Install Passenger on Apache

passenger-install-apache2-module 把passenger掛到Apache裡 sudo vim /etc/apache2/mods-enabled/mod_rails.conf 貼上Passenger輸出的路徑

example: LoadModule passengermodule /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.12/ext/apache2/modpassenger.so PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.12 PassengerRuby /usr/local/bin/ruby

設定visualhost vim /etc/apache2/sites-enabled/release.conf

<VirtualHost *:80> ServerName 114.34.169.207 DocumentRoot /home/joehwang/project/appwed/public <Directory /home/joehwang/project/appwed/public > AllowOverride all Options -MultiViews </Directory> </VirtualHost>

安裝Imagemagick

sudo apt-get remove imagemagick sudo apt-get install libperl-dev gcc libjpeg62-dev libbz2-dev libtiff4-dev libwmf-dev libz-dev libpng12-dev libx11-dev libxt-dev libxext-dev libxml2-dev libfreetype6-dev liblcms1-dev libexif-dev perl libjasper-dev libltdl3-dev graphviz gs-gpl pkg-config

下載編譯 wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz tar zxvf ImageMagick.tar.gz cd ImageMagick-* ./configure make sudo make install gem install rmagick

安裝memcache

sudo apt-get install memcached libmemcache-dev libsasl2-dev gem install memcached gem install memcache-client

讓alpha server和production server可以使用key互相登入

alpha -> production免key 在alpha server中

ssh-keygen -t rsa

如此便產生公鑰和私鑰 把公鑰 xxx.pub 傳到 production server

scp -P 7533-i /.key/登入ec2的金鑰.pem /home/joehwang/.ssh/id_rsa.pub 帳號@遠端ip:

在production server中

把xxx.pub移到~/.ssh資料夾

mv xxx.pub ~/.ssh/

並把xxx.pub內容附加到authorized_keys

cat ~/.ssh/xxx.pub >> ~/.ssh/authorized_keys

如此使用ssh://帳號@ip -p 埠號,便可直接登入 其他帳號也要直接連入,一樣把alpha的pub檔內容,附加到該帳號的authorized_keys就行了

git .ignore使用摘要

git .ignore使用摘要

在專案目錄下建立.gitignore的檔案

內容為要排除的檔案

bin-debug/ (排除bin-debug這個資料夾) main.as (排除main.as這個檔案) *.log (排除副檔名為.log的檔案)

如果想要做ignore的檔案已經被commit進Repository

git rm -f 相關檔案

git commit

設定好.gitignore的檔案

git status看一下,確認沒有被列在Untracked files

收工!

rake入門筆記

rake入門筆記

rake是rails裡的自動化工具,可以批次完成工作

像是rake db:create、rake db:migrate等...

輸入rake -T可以得到該專案所有rake task

入門文件: http://jasonseifer.com/2010/04/06/rake-tutorial#rails_models

http://allenyoung.iteye.com/blog/126309

自已開發的rake要放在"專案/libs/task/xxx.rake"

以下為簡單的範例

namespace :web do #name space
   desc 'compile web assests and restart rails server'
     task :compile =:environment do
         RAILS_ENV=production #指定執行環境
         Rake::Task['db:migrate'].invoke #執行rake db:migrate
         Rake::Task['assets:precompile'].invoke #執行assest:precompile
         @works = Film.find_all_by_uid(102); #rake存取專案中的model           
      puts "touch tmp/restart.txt" #重啟rails app
    end
end