Skip to content

Latest commit

 

History

History
130 lines (109 loc) · 3.72 KB

Instructions_for_EC2.md

File metadata and controls

130 lines (109 loc) · 3.72 KB

Instructions for setting up a webserver in an EC2

SSH Connection

Refer https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

ssh -i "~/Downloads/aws_webserver.pem" ubuntu@ec2-3-83-163-243.compute-1.amazonaws.com

Install NodeJS

The easiest way is to install NodeJS via the snap package manager sudo snap install node --classic . However this kind of installation might cause some issues due to running with lesser privileges.

A better way to do this is via NVM.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
nvm install 16.3.0 # for a specific version
nvm use 16.3.0

Transferring files

In the event that git is not installed. Use wormhole. sudo snap install wormhole

Important wormhole is named magic-wormhole in mac. You have to install in your local machine to upload the files

wormhole receive <received code>

Install packages

cd react-express-project
npm i
cd client
npm i
npm run build

Set up Nginx.

sudo apt install nginx
sudo service nginx start
sudo service nginx status

sudo nano /etc/nginx/sites-available/default
sudo systemctl restart nginx

Nginx server configuration

Refer https://www.nginx.com/resources/wiki/start/topics/examples/full/ /etc/nginx/sites-available/default file

server {

       index index.html index.htm index.nginx-debian.html;
       server_name example.com www.example.com;

       location / {
   			proxy_pass http://localhost:5000;
               proxy_buffering         on;

       }

}

Run Server

Refer https://www.dev2qa.com/how-to-run-node-js-server-in-background/

nohup node server.js  > output.log & disown

A better way is to use PM2

npm install pm2 -g
pm2 start server.js

Installing MongoDB

Refer https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
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
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl status mongod

Setup authentication (optional)

Refer

mongosh

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "myPassword", // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)
db.adminCommand( { shutdown: 1 } )

Exit mongosh with Crtl+ C

sudo nano /etc/mongod.conf
# add the following lines ###########
security
  authentication: enabled
#####################################
sudo systemctl restart mongod

# how to connect after enabling auth
mongosh --port 27017 --authenticationDatabase "admin" -u "myUserAdmin" -p "myPassword"
# change mongodb URL in the server config too

In case of errors try the following

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo service mongod restart
sudo systemctl status mongod

Additional Resources