Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.

Commit b01f05f

Browse files
authored
Merge pull request #352 from Audarth/master
Added pm2 config generation
2 parents 28405ff + 4357b50 commit b01f05f

File tree

7 files changed

+640
-174
lines changed

7 files changed

+640
-174
lines changed

app/templates/INSTALL.mdown

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Deploying to a server
2+
3+
To deploy, make sure both your local machine and your remote server/deployment target have pm2 installed with:
4+
5+
`npm install pm2 -g`
6+
7+
Make sure you have the file `ecosystem.json` with any saved deployment targets inside, to create this file or add a new deployment target run:
8+
9+
`gulp add-deploy-target`
10+
11+
**Note:** This command may not work on older projects! If it's not available you will need to modify the `ecosystem.json` file manually.
12+
13+
`ecosystem.json` will include any config as well as any deployment config along with your credentials. If you need to put in any credentials or sensitive information that can't be in the repository then answer the final question from `gulp add-deploy-target` with a yes and it will put the deployment config into `local.ecosystem.json`.
14+
15+
Once you've got your TARGETNAME (answer to the first question in `gulp add-deploy-target`) you can initialize the target host with:
16+
17+
`pm2 deploy TARGETNAME setup`
18+
19+
Then deploy your latest code from your git repository and run any post-deploy commands with:
20+
21+
`pm2 deploy TARGETNAME`
22+
23+
**Hint**, pm2 automatically looks for `ecosystem.json` in your current directory, but you can use your `local.ecosystem.json` configuration with `pm2 deploy local.ecosystem.json TARGETNAME`
24+
25+
If you have files you haven't commited, pm2 will ask you to commit them before deploying, to ignore this use `--force`:
26+
27+
`pm2 deploy TARGETNAME --force`
28+
29+
You will now need to ssh into the server, once you've ssh'd in it's very **Important** that when deploying to a secure server that you do all pm2 commands as the pm2 user by using:
30+
31+
`sudo su pm2`
32+
33+
You can then navigate to the app's folder that you chose during `gulp add-deploy-target`.
34+
35+
Once you're in the folder you should have 3 folders: 'current', 'shared', and 'source'; go into the current folder:
36+
37+
`cd current`
38+
39+
You can now launch the app using pm2 with:
40+
41+
`pm2 startOrRestart ecosystem.json`
42+
43+
You can also launch the app using different environment configurations in ecosystem.json (env name prefixed with "env_"), for example:
44+
45+
`pm2 startOrRestart ecosystem.json --env prod`
46+
47+
You should then save your current pm2 deployments with:
48+
49+
`pm2 save`
50+
51+
Running `pm2 save` whenever you deploy a new app is very important as in the event of a server restart the previously saved apps will be resurrected.
52+
53+
Now that the app is started you will not have to stop or restart it when making updates. pm2 will automatically restart the app if any of it's files are changed so you can do `pm2 deploy TARGETNAME --force` from your local machine and the pm2 will automatically restart the app with any changes!
54+
55+
## Initializing httpd
56+
57+
Next to this, you likely want to enable the httpd daemon. Often only very limited ports are exposed on servers, and we usually deliberately configure the application outside that scope. Add a forwarding rule for the appropriate dns:
58+
59+
- sudo chkconfig --levels 2345 httpd on
60+
- sudo service httpd stop
61+
- sudo vi /etc/httpd/conf/httpd.conf, uncomment the line with:
62+
63+
NameVirtualHost *:80
64+
65+
- and append:
66+
67+
<VirtualHost *:80>
68+
ServerName @sample-app-name.demoserver.com
69+
RewriteEngine On
70+
RewriteRule ^(.*)$ http://localhost:@node-port$1 [P]
71+
</VirtualHost>
72+
73+
- sudo service httpd start
74+
75+
## Administration
76+
77+
To view all running pm2 applications use:
78+
79+
`pm2 list`
80+
81+
To view any logs use:
82+
83+
`pm2 logs`
84+
85+
To monitor ram or cpu usage use:
86+
87+
`pm2 monit`
88+
89+
**If** you've deployed applications previously but don't see them when you run pm2 list then it might be because you're not logged in as the pm2 user or you deployed them under a different user.
90+
91+
If you accidently launch pm2 under your own account then you can kill the process with:
92+
93+
`pm2 kill`
94+
95+
And if you accidently run that command when logged in as the pm2 user then you can start pm2 and resurrect all processes from the last `pm2 save` with:
96+
97+
`pm2 resurrect`
98+
99+
For more information use the help commands or consult the documentation [here](http://pm2.keymetrics.io/docs/usage/quick-start/):
100+
101+
`pm2 help`
102+
103+
`pm2 deploy help`
104+
105+
## Converting an existing project
106+
107+
If you've previously deployed an application using linux server scripts then you will need to remove the scripts and the old deployment.
108+
109+
To minimize application downtime, you should configure and deploy the project using pm2 before removing the old deployment.
110+
111+
When converting a project to pm2 you should create an `ecosystem.json` file with your desired deployment config and commit it into the project, follow the above instructions and deploy it to your target server. pm2 will deploy the project to `/space/projects/{appname}/source`. **Don't** run `pm2 startOrRestart ecosystem.json` yet though!
112+
113+
Now once you've almost finished deploying with pm2 you will need to stop it with the service scripts:
114+
115+
`service stop {appname}`
116+
117+
If you have a {appname}-watch script then stop that as well:
118+
119+
`service stop {appname}-watch`
120+
121+
You should now be able to run `pm2 startOrRestart ecosystem.json`
122+
123+
Now that the project is deployed and assuming it's running okay you will need to clean up the old deployment.
124+
125+
Firstly, remove the chkconfig
126+
127+
`chkconfig -del {appname}`
128+
129+
And also for the watch script if appropriate
130+
131+
`chkconfig -del {appname}-watch`
132+
133+
Then delete the scripts
134+
135+
`rm /etc/init.d/{appname}`
136+
137+
`rm /etc/{appname}`
138+
139+
And for the watch script:
140+
141+
`rm /etc/init.d/{appname}-watch`
142+
143+
Older projects were typically deployed using a .git .live folder structure, since we no longer need these you can delete them:
144+
145+
`rm -r /space/projects/{appname}.git`
146+
147+
`rm -r /space/projects/{appname}.live]`
148+
149+
Finally delete the git remote that pointed to the .git folder from your laptop:
150+
151+
cd to your project directory and run:
152+
153+
`git remote rm {DeploymentName}`
154+
155+
You can then finish off by deleting the old etc folder from the project folder as the scripts and documentation inside should no longer be needed:
156+
157+
`rm -r ./etc`
158+
159+
You should also copy THIS Install guide to your projects repository for the benefit of other users.
160+

app/templates/README.mdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Edit `./local.json` to set your desired ports
4848

4949
# Installation and deployment on server
5050

51-
See etc/INSTALL.md
51+
See INSTALL.md
5252

5353
# Data
5454

app/templates/_gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ coverage
44
ui/fonts
55
deploy/local.properties
66
local.json
7+
local.ecosystem.json
78
/dist
89
.DS_Store
910
*.pid

app/templates/ecosystem.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"apps": [{
3+
"name": "@sample-app-name",
4+
"script": "./node-server/node-app.js",
5+
"watch": true,
6+
"restart_delay": 4000,
7+
"env": {
8+
"NODE_ENV": "local"
9+
},
10+
"env_local": {
11+
"NODE_ENV": "local"
12+
},
13+
"env_dev": {
14+
"NODE_ENV": "dev"
15+
},
16+
"env_prod": {
17+
"NODE_ENV": "prod"
18+
}
19+
}],
20+
"deploy": {}
21+
}

0 commit comments

Comments
 (0)