Ready EC2 Instance server for a MERN project
I assume that you have already an EC2 Instance server. If you don’t know, how to make EC2 Instance then please read my “Create an EC2 Instance in AWS” article before starting this article.
Step 1:- Connect Ec2 Instance to local terminal
Before establishing the connection you should have a key and permission.
$ chmod 400 key.pem
Now, After change permission, we will connect the server with the terminal via ssh command.
$ ssh -i key.pem ubuntu@ec2-17-XXX-87-XXX.ap-south-1.compute.amazonaws.com
After clicking enter, you will see this warning, don’t worry that is the warning, not an error. It will ask for security yes or no after yes you will be in the server terminal.
Warning: Identity file Live_Key/key.pem not accessible: No such file or directory.The authenticity of host 'ec2-31-XXX-89-XXX.ap-south-1.compute.amazonaws.com (13.233.89.162)' can't be established.ECDSA key fingerprint is SHA256:1JXXxxJLq7XjqIpEsgorNuxEA06F2J0vXI.Are you sure you want to continue connecting (yes/no)? yes
Step 2:- Install NVM & Node
Can we manage and use different versions of Node.js on the same machine? Using NVM, yes we can!
Node Version Manager (NVM) is a tool that helps us to use different versions of Node.js on the same machine, each version running locally in its isolated environment. We can also switch between different versions without hampering the working of our system as a whole.
$ sudo apt-get update
$ sudo apt-get install build-essential libssl-devOutput:-Do you want to continue? [Y/n]
Next, run this curl command to install nvm
$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh -o install_nvm.sh
And inspect the installation script with nano
$ nano install_nvm.sh
You will see this script.
#!/usr/bin/env bash{ # this ensures the entire script is downloaded #nvm_has() {
type "$1" > /dev/null 2>&1
}nvm_install_dir() {
printf %s "${NVM_DIR:-"$HOME/.nvm"}"
}nvm_latest_version() {
echo "v0.33.8"
}nvm_profile_is_bash_or_zsh() {
local TEST_PROFILE
TEST_PROFILE="${1-}"
case "${TEST_PROFILE-}" in
*"/.bashrc" | *"/.bash_profile" | *"/.zshrc")
return
;;
*)
return 1
;;
esac
}
Save then Exit.
Run the script with bash.
$ bash install_nvm.sh
After Run
export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
see profile
$ source ~/.profile
Now, you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can run this command:
$ nvm ls-remoteOutput :-v12.10.0v12.11.0v12.11.1v12.12.0v12.13.0 (LTS: Erbium)v12.13.1 (LTS: Erbium)v12.14.0 (Latest LTS: Erbium)v13.0.0v13.0.1v13.1.0v13.2.0v13.3.0v13.4.0v13.5.0
As you can see, the newest LTS version at the time of this writing is v10.18.0. You can install it using below command:
$ nvm install 10.18.0Output:-Downloading and installing node v10.18.0...Downloading https://nodejs.org/dist/v10.18.0/node-v10.18.0-linux-x64.tar.xz...######################################################################## 100.0%Computing checksum with sha256sumChecksums matched!manpath: can't set the locale; make sure $LC_* and $LANG are correctNow using node v10.18.0 (npm v6.13.4)Creating default alias: default -> 10.18.0 (-> v10.18.0)
Check installed version
$ node -vOutPut:-v10.18.0
Now, we will set the default version.
$ nvm lsOutPut:--> v10.18.0default -> 10.18.0 (-> v10.18.0)node -> stable (-> v10.18.0) (default)stable -> 10.18 (-> v10.18.0) (default)iojs -> N/A (default)lts/* -> lts/erbium (-> N/A)lts/argon -> v4.9.1 (-> N/A)lts/boron -> v6.17.1 (-> N/A)lts/carbon -> v8.17.0 (-> N/A)lts/dubnium -> v10.18.0lts/erbium -> v12.14.0 (-> N/A)
If you wish to install default version then you can run this command:
$ nvm alias default 10.18.0Output :-default -> 10.18.0 (-> v10.18.0)
To use default nvm run this command.
$ nvm use defaultOtPut :-manpath: can't set the locale; make sure $LC_* and $LANG are correctNow using node v10.18.0 (npm v6.13.4)
Step3:- Install MongoDB
Adding MongoDB repository
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
After successfully importing the key, you will see:
Output:-Ok
To create a list file for MongoDB.
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
After adding the repository details, we need to update the packages list.
$ sudo apt-get update
Installing and Verify MongoDB
Now we can install the MongoDB package itself.
// Latest version
$ sudo apt-get install -y mongodb-orgOR// Specific version
$ sudo apt-get install -y mongodb-org=4.2.8 mongodb-org-server=4.2.8 mongodb-org-shell=4.2.8 mongodb-org-mongos=4.2.8 mongodb-org-tools=4.2.8
Next, start MongoDB with systemctl
or service
and check status.
$ sudo service mongodb start
Check status
$ sudo service mongodb statusOutPut:-● mongodb.service - An object/document-oriented databaseLoaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2019-12-30 07:55:41 UTC; 9s agoDocs: man:mongod(1)Main PID: 14202 (mongod)Tasks: 10Memory: 43.2MCPU: 51msCGroup: /system.slice/mongodb.service└─14202 /usr/bin/mongod --config /etc/mongodb.conf
Step4:- Install Git
We will have to install Git to take projects on the server. There are multiple ways like “SCP command” and “FileZilla” etc. But we use Git.
First check, Git version if you find any version then don’t install if you don’t find any version then install Git. In my case, Git is already installed because maximum os like Linux and Mac provide Git.
$ git --versionOutPut:-git version 2.17.2
Now, we will set a git’s default username for this machine.
$ git config --global user.name "Your name here"
$ git config --global user.email "your_email@example.com"
Check username and email
$ git config — listOutPut:-user.name=XXXXXXXuser.email=XXXXXX@mail.com
Git has been installed.
Now we will clone all projects on the server.
$ git clone https://github.com/z2p-today/sme_form.git
Step5:- Install Pm2
PM2 is a Production Process Manager for Node.js applications with a built-in Load Balancer and a daemon process manager that will help you to manage and keep your application online. Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via NPM.
Installation
$ npm install pm2@latest -g
Check, how many projects are running?
$ pm2 list
You will find this empty list. Because yet, no project is running.
Enter the project's root folder and enter the command to run the project.
ubuntu@ip-172-31-22-183:~/sme_form_backend$ pm2 start bin/www NODE_ENV=productio --name sme_form_backendubuntu@ip-172-31-22-183:~/sme_form$ pm2 start 'npm run start:production' --name sme_form
Again check running projects list.
$ pm2 list
Now, you will see two projects are running.
Enjoy your server.🤓
Feel free to ask any questions or queries in the comment section or you can ping me on Facebook.