安裝 RVM
通常使用 RVM 或 rbenv 來安裝 Ruby,這里選用 RVM。
1
|
$ curl -sSL https: //get .rvm.io | bash -s stable |
載入 RVM :
1
2
3
|
$ source /home/libuchao/ .rvm /scripts/rvm $ rvm - v rvm 1.25.12 (stable) by Wayne E. Seguin <[email protected]> ...... |
再執行以下命令:
1
2
3
|
$ type rvm rvm is a function ...... |
說明 RVM 安裝正確。
安裝 Ruby
用 RVM 安裝 Ruby
1
2
3
4
|
$ rvm install 2.1.0 $ rvm use 2.1.0 --default $ ruby - v ruby 2.1.0p0 |
國內服務器推薦替換 RubyGems 的到淘寶鏡像
1
2
|
$ gem sources -r https: //rubygems .org/ $ gem sources -a http: //ruby .taobao.org/ |
否則安裝 Gem 可能會非常非常慢。
安裝 Rails
其實 Rails 也是一個 Gem
1
2
3
4
|
$ gem install rails --no-ri --no-rdoc -V ...... $ rails - v Rails 4.0.2 |
至此,Rails 環境已經安裝完成。
安裝 MySQL
安裝 Mysql 及相應的庫文件:
1
|
$ sudo apt-get install mysql-server libmysqlclient-dev |
然后進行一些安裝方面的設置:
1
|
$ /usr/bin/mysql_secure_installation |
創建相應的數據庫,并為它新建一個權限小一些的用戶:
1
2
3
4
|
mysql> CREATE DATABASE blix_production; mysql> GRANT ALL PRIVILEGES ON blix_production.* TO blix@localhost IDENTIFIED BY "123456" ; mysql> flush privileges ; mysql> exit |
導入數據:
1
|
$ mysql -u blix -p blix_production < database.sql |
安裝 Nginx
Nginx 專門處理靜態請求,并作為 Unicorn 的反向代理
編輯 /etc/apt/sources.list,末尾處添加以下兩行
1
2
|
deb http://nginx.org/packages/ubuntu/ precise nginx deb-src http://nginx.org/packages/ubuntu/ precise nginx |
添加 Nginx 簽名
1
2
|
$ wget http: //nginx .org /keys/nginx_signing .key $ sudo apt-key add nginx_signing.key |
安裝 Nginx
1
2
|
$ sudo apt-get update $ sudo apt-get install nginx |
安裝完成后可以在瀏覽器中輸入 http://server-ipaddress 查看是否安裝正確。
配置 Unicorn
首先編譯一下靜態文件:
1
2
|
$ RAILS_ENV=production rake assets:clean $ RAILS_ENV=production rake assets:precompile |
Unicorn 配置參考:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
worker_processes 2 timeout 30 APP_PATH = File.expand_path("../..", __FILE__) working_directory APP_PATH listen 8080, :tcp_nopush => true listen "/tmp/unicorn.sock", :backlog => 64 stderr_path APP_PATH + "/log/unicorn.stderr.log" stdout_path APP_PATH + "/log/unicorn.stdout.log" pid APP_PATH + "/tmp/pids/unicorn.pid" |
Unicorn 自啟動腳本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#!/bin/sh set -e # Example init script, this can be used with nginx, too, # since nginx and unicorn accept the same signals # Feel free to change any of the following variables for your app: TIMEOUT=${TIMEOUT-60} APP_ROOT= /home/libuchao/blix APP_USER=libuchao PID=$APP_ROOT /tmp/pids/unicorn .pid CMD= "unicorn_rails -D -E production -c $APP_ROOT/config/unicorn.rb" action= "$1" set -u old_pid= "$PID.oldbin" cd $APP_ROOT || exit 1 sig () { test -s "$PID" && kill -$1 ` cat $PID` } oldsig () { test -s $old_pid && kill -$1 ` cat $old_pid` } case $action in start) sig 0 && echo >&2 "Already running" && exit 0 su -c "$CMD" - $APP_USER ;; stop) sig QUIT && exit 0 echo >&2 "Not running" ;; force-stop) sig TERM && exit 0 echo >&2 "Not running" ;; restart|reload) sig HUP && echo reloaded OK && exit 0 echo >&2 "Couldn't reload, starting '$CMD' instead" su -c "$CMD" - $APP_USER ;; upgrade) if sig USR2 && sleep 2 && sig 0 && oldsig QUIT then n=$TIMEOUT while test -s $old_pid && test $n - ge 0 do printf '.' && sleep 1 && n=$(( $n - 1 )) done echo if test $n -lt 0 && test -s $old_pid then echo >&2 "$old_pid still exists after $TIMEOUT seconds" exit 1 fi exit 0 fi echo >&2 "Couldn't upgrade, starting '$CMD' instead" su -c "$CMD" - $APP_USER ;; reopen-logs) sig USR1 ;; *) echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>" exit 1 ;; esac |
將這個 shell 在/etc/init.d/下做一個軟連接,并使其開機自啟動:
1
2
3
|
$ chmod +x /home/libuchao/blix/config/unicorn_init .sh $ sudo ln -s /home/libuchao/blix/config/unicorn_init .sh /etc/init .d /unicorn $ sudo update-rc.d unicorn defaults |
啟動 Unicorn:
1
|
$ service unicorn start |
在瀏覽器中輸入 http://server_ipaddress:8080 查看效果。
配置 Nginx
Nginx 配置參考:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
upstream blix_backend { server unix:/tmp/unicorn.sock fail_timeout=0; } gzip on; gzip_disable "msie6"; client_max_body_size 150m; server { listen 80 default; return 403; } server { listen 80; server_name libuchao.com www.libuchao.com; root /home/libuchao/blix/public; try_files $uri/index.html $uri.html $uri @httpapp; location @httpapp { 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://blix_backend; } location ~ ^(/assets) { access_log off; expires max; } } |
此時應該可以通過域名直接訪問了。