-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathold_node_deploy.html
119 lines (116 loc) · 7.73 KB
/
old_node_deploy.html
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
<div class="module_description active_lesson active_lesson_with_video ">
<h1>Node AWS Deployment</h1><h2>Part 1: Get prepared</h2><h3>Core Setup Requirements</h3><ol>
<li>An AWS account 2) A GitHub or bitbucket account 3) An internet connection 4) Git should be installed locally</li></ol><h3>Get going</h3><ol>
<li>Create a local, functional project 2) Keep that version, version controlled via git - good practice anyways.</li></ol><h3>Make a GitHub/bitbucket repository</h3><ol>
<li>Push your project to that GitHub/bitbucket repository</li></ol><hr>
<h2>Part 2: Set up AWS</h2><ol>
<li>Enter AWS, and click launch new instance. 2) Select ubuntu14.04 3) Select t2.micro 4) Set security settings:</li></ol><p>
<code>ssh 0.0.0.0, (Anywhere or myIP)</code></p><p>
<code>http 0.0.0.0 (Anywhere)</code></p><p>
<code>https 0.0.0.0 (Anywhere, or don't set it)</code> 5) Download a <code>.pem</code> key from AWS 6) Move the <code>.pem</code> file to an appropriate folder on your system 7) Change user permission on <code>.pem</code> <code>chmod 400 {{mypem}}.pem</code></p><h3>For PC, that command might require kitty or putty or bash terminal</h3><p>
We are now ready to enter the cloud server!</p><h2>Part 3: Enter cloud server</h2><p>
<strong>Navigate to the folder where your .pem file is!</strong></p><p>
(you can use the ‘connect’ button on Amazon AWS to get the next line of code)</p><h3>For PC, the command below might require kitty or putty or bash terminal</h3><ol>
<li><code>ssh -i {{mypem}}.pem ubuntu@{{yourAWS.ip}}</code> again you can get this exact line of code from <button> Connect </button> on AWS. (The command is found below <strong>example</strong>). 2) In the ubuntu terminal: These establish some basic dependencies for deployment and the Linux server.</li></ol>
<pre data-language="bash">sudo apt-get update
sudo apt-get install -y build-essential openssl libssl-dev pkg-config
</pre><ol start="3">
<li>In the ubuntu terminal, one at a time because they require confirmation: (these install basic node and npm)</li></ol>
<pre data-language="bash">sudo apt-get install node
sudo apt-get install npm
sudo npm cache clean -f
(The cache clean -f, forcibly cleans the cache. This will give an interesting comment:))
</pre><ol start="4">
<li>In the ubuntu terminal: These install the node package manager <strong>n</strong> and updated node.</li></ol>
<pre data-language="bash">sudo npm install -g n
sudo n stable (or whichever node version you want e.g. 5.9.0)
</pre><ol start="5">
<li><code>node -v</code> should give you the stable version of node, or the version that you just installed. </li> <li>Install NGINX and git:</li></ol>
<pre data-language="bash">sudo apt-get install nginx
sudo apt-get install git
</pre><ol start="7">
<li>Make your file folder:</li></ol>
<pre data-language="bash">sudo mkdir /var/www
</pre><ol start="8">
<li>Enter the folder:</li></ol>
<pre data-language="bash">cd /var/www
</pre><ol start="9">
<li>Clone your project:</li></ol>
<pre data-language="bash">sudo git clone {{your project file path on github/bitbucket}}
</pre><h4>At this point, you should be able to change directories into your project and run your server. It will most likely fail, because of not having mongod up and running, but running the project should be as simple as <code>node server.js</code> or a similar command like <code>npm start</code>.</h4><h2>Part 4: Set up NGINX</h2><ol>
<li>Go to nginx’s sites-available directory :</li></ol>
<pre data-language="bash">cd /etc/nginx/sites-available
</pre><ol start="2">
<li>Enter vim:</li></ol>
<pre data-language="bash">sudo vim {{project_name}}
</pre><h3>vim is a terminal-based text editor for more info see: <a href="http://vim-adventures.com/" target="_blank">vim-adventures.com/</a> or other vim learning tools. The key commands for us are <strong>i</strong> which allows us to type, <strong>esc</strong> which turns off insert and then after <strong>esc</strong> <strong>:wq</strong> which says write and quit.</h3><ol start="3">
<li>Paste and modify the following code into vim after hitting i:</li></ol>
<pre data-language="bash">server {
listen 80;
location / {
proxy_pass http://{{PRIVATE-IP}}:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
</pre><p>
This code says: have the reverse proxy server (nginx) listen at port 80. When going to root /, listen for http requests as though you were actually http:// your private ip and the port your server is listening e.g @8000 or @6789 etc.</p><p>
Learn more from nginx: <a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html" target="_blank">http://nginx.org/en/docs/http/ngx_http_proxy_module.html</a></p><ol start="4">
<li>Remove the defaults from <code>/etc/nginx/sites-available</code></li></ol>
<pre data-language="bash">sudo rm default
</pre><ol start="5">
<li>Create a symbolic link from sites-enabled to sites available:</li></ol>
<pre data-language="bash">sudo ln -s /etc/nginx/sites-available/{{project_name}} /etc/nginx/sites-enabled/{{project_name}}
</pre><p>
6 Remove the defaults from /etc/nginx/sites-enabled/</p>
<br>
<pre data-language="bash">cd /etc/nginx/sites-enabled/
sudo rm default
</pre><h2>Part 5: Project Dependencies and PM2</h2><ol>
<li>Install pm2 globally (<a href="https://www.npmjs.com/package/pm2.5" target="_blank">https://www.npmjs.com/package/pm2.5</a>) (<a href="https://www.npmjs.com/package/pm2" target="_blank">https://www.npmjs.com/package/pm2</a>). This is a production process manager that allows us to run node processes in the background.</li></ol>
<pre data-language="bash">sudo npm install pm2 -g
</pre><ol start="2">
<li>Try some stuff with pm2!</li></ol>
<pre data-language="bash">cd /var/www/{{project_name}}
pm2 start server.js
pm2 stop 0
pm2 restart 0
sudo service nginx reload && sudo service nginx restart
</pre><p>
<strong>Probably not quite working yet but close</strong> 3) You might have some components that you still need to install: (get your dependencies from npm (assuming your git project has a package.json))</p>
<pre data-language="bash">sudo npm install
</pre><p>
3b) IF USING BOWER (assuming you have a bower.json)</p>
<pre data-language="bash">sudo npm install bower -g
sudo bower install --allow-root
</pre><h2>Part 6: Mongod</h2><ol>
<li>The last thing, setting up mongod! 2) Set up a key</li></ol>
<pre>sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
#version 14.04
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
</pre><ol start="3">
<li>Setup mongodb in a source list</li></ol>
<pre data-language="bash">echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
#version 14.04
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
</pre><p>
That is all one line! </p><p>
4. Reupdate to integrate mongo</p>
<pre data-language="bash">sudo apt-get update
</pre><ol start="5">
<li>install mongo</li></ol>
<pre data-language="bash">sudo apt-get install -y mongodb-org
</pre><ol start="6">
<li>Start mongo (probably already started)</li></ol>
<pre data-language="bash">sudo service mongod start
</pre><ol start="7">
<li>Restart your pm2 project and make sure the nginx config’s are working:</li></ol>
<pre data-language="bash">pm2 stop 0
pm2 restart 0
sudo service nginx reload && sudo service nginx restart
</pre><ol start="8">
<li>At this point, the nginx commands should have shown 2 OKs and you should be off and running. Go to the AWS public IP and see your site live!</li></ol>
</div>