部署 Node.js 到 AppFog


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# 登入帳號
af login

# 查看是否已登入
af user

# 查看目前已有的 Apps
af apps

# 查看可用的 runtimes
af runtimes

# 在 package.json 加入
"scripts": {
"start": "node app.js"
}

# 修改 app.js 的 listen port
app.listen(process.env.VCAP_APP_PORT || 3000, function (err) {
if (err) {
throw err;
}

console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});

# 切到專案目錄
cd ~/Deploy/[PROJECT]

# 安裝所有依賴套件
npm install

# 提交
af push

# 出來的結果大約是
Would you like to deploy from the current directory? [Yn]: y
Application Name: <<PROJECT_NAME>>
Detected a Node.js Application, is this correct? [Yn]: y
1: AWS US East - Virginia
2: AWS EU West - Ireland
3: AWS Asia SE - Singapore
4: Rackspace AZ 1 - Dallas
5: HP AZ 2 - Las Vegas
Select Infrastructure: 3
Application Deployed URL [<<PROJECT_NAME>>.ap01.aws.af.cm]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [64M]:
How many instances? [1]:
Bind existing services to '<<PROJECT_NAME>>'? [yN]:
Create services to bind to '<<PROJECT_NAME>>'? [yN]:
Would you like to save this configuration? [yN]:y
Creating Application: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (54K): OK
Push Status: OK
Staging Application '<<PROJECT_NAME>>': OK
Starting Application '<<PROJECT_NAME>>': OK

# 如果他不能識別出 Application 類型,可能會是
Would you like to deploy from the current directory? [Yn]: y
Application Name: <<PROJECT_NAME>>
Detected a Standalone Application, is this correct? [Yn]: n
1: Rails
2: Spring
3: Grails
4: Lift
5: JavaWeb
6: Standalone
7: Sinatra
8: Node
9: PHP
10: Erlang/OTP Rebar
11: WSGI
12: Django
13: Rack
14: Play
Select Application Type: 8
Selected Node.js Application
1: AWS US East - Virginia
2: AWS EU West - Ireland
3: AWS Asia SE - Singapore
4: Rackspace AZ 1 - Dallas
5: HP AZ 2 - Las Vegas
Select Infrastructure: 3
Application Deployed URL [<<PROJECT_NAME>>.ap01.aws.af.cm]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [64M]:
How many instances? [1]:
Bind existing services to '<<PROJECT_NAME>>'? [yN]:
Create services to bind to '<<PROJECT_NAME>>'? [yN]:
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (117K): OK
Push Status: OK
Staging Application '<<PROJECT_NAME>>': OK
Starting Application '<<PROJECT_NAME>>': OK

# 如果需要加入 MongoDB 或者 MySQL 的可以這樣 (MongoDB 為例子)
# 按之前的方法修改 app.js 的 Port
# 之後再加入 MongoDB 的連接
var env = JSON.parse(process.env.VCAP_SERVICES);
mongo = env['mongodb-1.8'][0]['credentials'];

var generate_mongo_url = function(obj){
obj.hostname = (obj.hostname || 'localhost');
obj.port = (obj.port || 27017);
obj.db = (obj.db || 'test');
if(obj.username && obj.password){
return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
}else{
return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
}

mongoose.connect(generate_mongo_url(mongo));

# 比較好的做法可參考
# Add MongoDB Configuration 的章節
https://docs.appfog.com/languages/node

# 之後提交到 appfog 大約就多了 Bind services
Would you like to deploy from the current directory? [Yn]: y
Application Name: <<PROJECT_NAME>>
Detected a Standalone Application, is this correct? [Yn]: n
1: Rails
2: Spring
3: Grails
4: Lift
5: JavaWeb
6: Standalone
7: Sinatra
8: Node
9: PHP
10: Erlang/OTP Rebar
11: WSGI
12: Django
13: Rack
14: Play
Select Application Type: 8
Selected Node.js Application
1: AWS US East - Virginia
2: AWS EU West - Ireland
3: AWS Asia SE - Singapore
4: Rackspace AZ 1 - Dallas
5: HP AZ 2 - Las Vegas
Select Infrastructure: 3
Application Deployed URL [<<PROJECT_NAME>>.ap01.aws.af.cm]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [64M]:
How many instances? [1]:
Bind existing services to '<<PROJECT_NAME>>'? [yN]: n
Create services to bind to '<<PROJECT_NAME>>'? [yN]: y
1: mongodb
2: mysql
3: postgresql
4: rabbitmq
5: redis
What kind of service?: 1
Specify the name of the service [mongodb-5c38a]: <<PROJECT_NAME>>
Create another? [yN]: n
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Creating Service [<<PROJECT_NAME>>]: OK
Binding Service [<<PROJECT_NAME>>]: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (145K): OK
Push Status: OK
Staging Application '<<PROJECT_NAME>>': OK
Starting Application '<<PROJECT_NAME>>': OK

# 關於 PHP 方面的提交
af push

Would you like to deploy from the current directory? [Yn]: y
Application Name: <<PROJECT_NAME>>
Detected a PHP Application, is this correct? [Yn]: y
1: AWS US East - Virginia
2: AWS EU West - Ireland
3: AWS Asia SE - Singapore
4: Rackspace AZ 1 - Dallas
5: HP AZ 2 - Las Vegas
Select Infrastructure: 3
Application Deployed URL [<<PROJECT_NAME>>.ap01.aws.af.cm]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [128M]:
How many instances? [1]:
Bind existing services to '<<PROJECT_NAME>>'? [yN]:
Create services to bind to '<<PROJECT_NAME>>'? [yN]:
Would you like to save this configuration? [yN]: y
Manifest written to manifest.yml.
Creating Application: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (11K): OK
Push Status: OK
Staging Application '<<PROJECT_NAME>>': OK
Starting Application '<<PROJECT_NAME>>': OK

# 提交更新
af update

Updating application '<<PROJECT_NAME>>'...
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (5K): OK
Push Status: OK
Stopping Application '<<PROJECT_NAME>>': OK
Staging Application '<<PROJECT_NAME>>': OK
Starting Application '<<PROJECT_NAME>>': OK