20210811202726-deploy-on-aws
MONGO
-
Start up EC2 ^^
-
Log in
-
Install Node
-
Download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
-
log out of the instance
-
log back in
-
Install Node:
# 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)"
-
-
Install Mongo
- Import the public key used by the package management system:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
- Create a list file for MongoDB:
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
- Reload local package database:
sudo apt-get update
- Install MongoDB packages:
sudo apt-get install -y mongodb-org
- Import the public key used by the package management system:
-
Start up Mongo
sudo systemctl start mongod
- Verify it is running:
sudo systemctl status mongod
- Stop/restart:
sudo systemctl stop mongod
/sudo systemctl restart mongod
- Use the shell:
mongosh
-
Copy db data to instance
-
you can use scp, or something like filezilla
-
filezilla: https://ganevniko.github.io/using_filezilla_to_upload_files_into_an_aws_ec2_instance
-
scp: https://dearsikandarkhan.medium.com/files-copying-between-aws-ec2-and-local-d07ed205eefa
$ 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
-
-
SERVER
-
start an ec2 instance
- Amazon Linux
- t2 micro
- security, enable http on 80 and ssh on 22
- use same pem as above
-
install repo
- ssh in
ssh -i pemfile ec2-user@blahblah
sudo yum install git
- clone repo in
git clone https://github.com/...
- ssh in
-
install node
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
- restart instance
nvm install node
. ~/.nvm/nvm.sh
node -e "console.log('Running Node.js ' + process.version)"
npm install -g npx
-
install nginx
-
sudo amazon-linux-extras install nginx1
-
sudo service nginx start
-
set up nginx config
-
sudo vim /etc/nginx/nginx.conf
-
Before the other location blocks within
server {
, insert:location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:3000; }
-
Also ensure that the port your server is listening for has been set within the
server
block
-
-
restart nginx
-
-
make nginx persistent
sudo systemctl start nginx
sudo systemctl enable nginx
-
npm install everything in your repo
-
Initiate your server
-
install redis
-
https://medium.com/@andrewcbass/install-redis-v3-2-on-aws-ec2-instance-93259d40a3ce
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/
-
test by running
redis-server &
and check ports withsudo lsof -i -P -n | grep LISTEN
. Should see port open withredis-server
. Kill by runningkill {PID}
, pid being the number inlsof
.
-
-
If you want your server to persist, use
pm2
npm i -g pm2
- run this
pm2 startup
and copy its output - run that output? I think
pm2 save
pm2 start nodemon path/to/server.js
- This will allow the server to persist on logout and on server restart
Run It All
Mongo
sudo systemctl start mongod
Server
sudo service nginx start
redis-server & npm start
- on close:
sudo lsof -i -P -n | grep LISTEN
andkill {PID}
for redis
Other AWS stuff
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.
- For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.
- For a CentOS AMI, the user name is centos.
- For a Debian AMI, the user name is admin.
- For a Fedora AMI, the user name is ec2-user or fedora.
- For a RHEL AMI, the user name is ec2-user or root.
- For a SUSE AMI, the user name is ec2-user or root.
- For an Ubuntu AMI, the user name is ubuntu.
- Otherwise, if ec2-user and root don't work, check with the AMI provider.
References
- https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2
- https://jasonwatmore.com/post/2018/09/26/setup-nodejs-mongodb-production-server-on-ubuntu-1804
- https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
- https://stackoverflow.com/questions/33991816/ec2-ssh-permission-denied-publickey-gssapi-keyex-gssapi-with-mic
Last modified: 202108270637