This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20210811202726-deploy-on-aws.html
219 lines (219 loc) · 9.03 KB
/
20210811202726-deploy-on-aws.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
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
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="description" content=""><!-- Bing --><meta name="msvalidate.01" content="45CBBE1BD8265A2217DFDA630EB8F84A" /><title>Tiny Brain Fans - 20210811202726-deploy-on-aws</title><link rel="stylesheet" href="tinystyle.css"></head><body>
<main id="main"><article id="content"><h1 id="title">20210811202726-deploy-on-aws</h1><h2>MONGO</h2>
<p><a href="https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2" target="_blank">https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2</a></p>
<ul>
<li>
<p>Start up EC2 ^^</p>
</li>
<li>
<p>Log in</p>
</li>
<li>
<p>Install Node</p>
<ul>
<li>
<p>Download and install nvm: <code>curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash</code></p>
<ul>
<li><a href="https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html" target="_blank">https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html</a></li>
</ul>
</li>
<li>
<p>log out of the instance</p>
</li>
<li>
<p>log back in</p>
</li>
<li>
<p>Install Node:</p>
<pre><code># install node
nvm install node
# initiate nvm with new node version
. ~/.nvm/nvm.sh
# ensure desired version of node is running
node -e "console.log('Running Node.js ' + process.version)"
</code></pre>
</li>
</ul>
</li>
<li>
<p>Install Mongo</p>
<ul>
<li>Import the public key used by the package management system: <code>wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -</code></li>
<li>Create a list file for MongoDB: <code>echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list</code></li>
<li>Reload local package database: <code>sudo apt-get update</code></li>
<li>Install MongoDB packages: <code>sudo apt-get install -y mongodb-org</code></li>
</ul>
</li>
<li>
<p>Start up Mongo </p>
<ul>
<li><code>sudo systemctl start mongod</code></li>
<li>Verify it is running: <code>sudo systemctl status mongod</code></li>
<li>Stop/restart: <code>sudo systemctl stop mongod</code> / <code>sudo systemctl restart mongod</code></li>
<li>Use the shell: <code>mongosh</code></li>
</ul>
</li>
<li>
<p>Copy db data to instance</p>
<ul>
<li>
<p>you can use scp, or something like filezilla</p>
<ul>
<li>
<p>filezilla: <a href="https://ganevniko.github.io/using_filezilla_to_upload_files_into_an_aws_ec2_instance" target="_blank">https://ganevniko.github.io/using_filezilla_to_upload_files_into_an_aws_ec2_instance</a></p>
</li>
<li>
<p>scp: <a href="https://dearsikandarkhan.medium.com/files-copying-between-aws-ec2-and-local-d07ed205eefa" target="_blank">https://dearsikandarkhan.medium.com/files-copying-between-aws-ec2-and-local-d07ed205eefa</a></p>
<pre><code class="language-bash">$ scp -i /directory/to/abc.pem user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:path/to/file /your/local/directory/files/to/download
</code></pre>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2>SERVER</h2>
<ul>
<li>
<p>start an ec2 instance</p>
<ul>
<li>Amazon Linux</li>
<li>t2 micro</li>
<li>security, enable http on 80 and ssh on 22</li>
<li>use same pem as above</li>
</ul>
</li>
<li>
<p>install repo</p>
<ul>
<li>ssh in <code>ssh -i pemfile ec2-user@blahblah</code></li>
<li><code>sudo yum install git</code></li>
<li>clone repo in <code>git clone https://github.com/...</code></li>
</ul>
</li>
<li>
<p>install node</p>
<ul>
<li><code>curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash</code></li>
<li>restart instance</li>
<li><code>nvm install node</code></li>
<li><code>. ~/.nvm/nvm.sh</code></li>
<li><code>node -e "console.log('Running Node.js ' + process.version)"</code></li>
<li><code>npm install -g npx</code></li>
</ul>
</li>
<li>
<p>install nginx</p>
<ul>
<li>
<p><code>sudo amazon-linux-extras install nginx1</code></p>
</li>
<li>
<p><code>sudo service nginx start</code></p>
</li>
<li>
<p>set up nginx config</p>
<ul>
<li>
<p><code>sudo vim /etc/nginx/nginx.conf</code></p>
</li>
<li>
<p>Before the other location blocks within <code>server {</code>, insert:</p>
<pre><code>location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:3000;
}
</code></pre>
</li>
<li>
<p>Also ensure that the port your server is listening for has been set within the <code>server</code> block</p>
</li>
</ul>
</li>
<li>
<p>restart nginx</p>
</li>
</ul>
</li>
<li>
<p>make nginx persistent</p>
<ul>
<li><code>sudo systemctl start nginx</code></li>
<li><code>sudo systemctl enable nginx</code></li>
</ul>
</li>
<li>
<p>npm install everything in your repo</p>
</li>
<li>
<p>Initiate your server</p>
</li>
<li>
<p>install redis</p>
<ul>
<li>
<p><a href="https://medium.com/@andrewcbass/install-redis-v3-2-on-aws-ec2-instance-93259d40a3ce" target="_blank">https://medium.com/@andrewcbass/install-redis-v3-2-on-aws-ec2-instance-93259d40a3ce</a></p>
<pre><code>sudo yum -y install gcc make # install GCC compiler
cd /usr/local/src
sudo wget http://download.redis.io/redis-stable.tar.gz
sudo tar xvzf redis-stable.tar.gz
sudo rm -f redis-stable.tar.gz
cd redis-stable
sudo yum groupinstall "Development Tools"
sudo make distclean
sudo make
sudo yum install -y tcl
sudo make test
sudo cp src/redis-server /usr/local/bin/
sudo cp src/redis-cli /usr/local/bin/
</code></pre>
</li>
<li>
<p>test by running <code>redis-server &</code> and check ports with <code>sudo lsof -i -P -n | grep LISTEN</code>. Should see port open with <code>redis-server</code>. Kill by running <code>kill {PID}</code>, pid being the number in <code>lsof</code>.</p>
</li>
</ul>
</li>
<li>
<p>If you want your server to persist, use <code>pm2</code></p>
<ul>
<li><code>npm i -g pm2</code></li>
<li>run this <code>pm2 startup</code> and copy its output</li>
<li>run that output? I think</li>
<li><code>pm2 save</code></li>
<li><code>pm2 start nodemon path/to/server.js</code></li>
<li>This will allow the server to persist on logout and on server restart</li>
</ul>
</li>
</ul>
<h2>Run It All</h2>
<h3>Mongo</h3>
<ol>
<li><code>sudo systemctl start mongod</code></li>
</ol>
<h3>Server</h3>
<ol>
<li><code>sudo service nginx start</code></li>
<li><code>redis-server & npm start</code></li>
<li>on close: <code>sudo lsof -i -P -n | grep LISTEN</code> and <code>kill {PID}</code> for redis</li>
</ol>
<h2>Other AWS stuff</h2>
<p>Each Linux instance launches with a default Linux system user account. The default user name is determined by the AMI that was specified when you launched the instance.</p>
<ul>
<li>For <strong>Amazon Linux 2</strong> or the Amazon Linux AMI, the user name is <strong>ec2-user</strong>.</li>
<li>For a <strong>CentOS</strong> AMI, the user name is <strong>centos</strong>.</li>
<li>For a <strong>Debian</strong> AMI, the user name is <strong>admin</strong>.</li>
<li>For a <strong>Fedora</strong> AMI, the user name is <strong>ec2-user</strong> or <strong>fedora</strong>.</li>
<li>For a <strong>RHEL</strong> AMI, the user name is <strong>ec2-user</strong> or <strong>root</strong>.</li>
<li>For a <strong>SUSE</strong> AMI, the user name is <strong>ec2-user</strong> or <strong>root</strong>.</li>
<li>For an <strong>Ubuntu</strong> AMI, the user name is <strong>ubuntu</strong>.</li>
<li>Otherwise, if ec2-user and root don't work, check with the AMI provider.</li>
</ul>
<h2>References</h2>
<ul>
<li><a href="https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2" target="_blank">https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2</a></li>
<li><a href="https://jasonwatmore.com/post/2018/09/26/setup-nodejs-mongodb-production-server-on-ubuntu-1804" target="_blank">https://jasonwatmore.com/post/2018/09/26/setup-nodejs-mongodb-production-server-on-ubuntu-1804</a></li>
<li><a href="https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/" target="_blank">https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/</a></li>
<li><a href="https://stackoverflow.com/questions/33991816/ec2-ssh-permission-denied-publickey-gssapi-keyex-gssapi-with-mic" target="_blank">https://stackoverflow.com/questions/33991816/ec2-ssh-permission-denied-publickey-gssapi-keyex-gssapi-with-mic</a></li>
</ul>
<p class="last-modified">Last modified: 202206101419</p></article></main><footer><nav><a href="index.html">Sitemap</a></nav><div class="social"><p>Built using <a href="http://codeberg.org/milofultz/swiki" target="_blank" rel="noopener noreferrer">{{SWIKI}}</a></p><p><a href="http://codeberg.org/milofultz/" target="_blank" rel="noopener noreferrer">Codeberg</a></p><p><a href="http://milofultz.com/" target="_blank" rel="noopener noreferrer">milofultz.com</a></p><p><a href="https://merveilles.town/@milofultz" target="_blank" rel="me noopener noreferrer">Mastodon</a></p></div></footer></body></html>