Setting up Amazon EC2 instance for PawJS
Prepare Amazon EC2 Instance
Log into your AWS account
Log into the AWS Management Console.
Launch Amazon EC2 Instance
In the Amazon EC2 Dashboard, choose "Launch Instance" to create and configure your virtual machine.
Configure the Instance
In this wizard, you have the option to configure your instance features. Below are some guidelines on setting up your first instance.
- Choose an Amazon Machine Image (AMI): In step 1 of the wizard, I recommend **Latest Ubuntu Server LTS**. - Choose an instance type: In step 2 of the wizard, I recommend **t2.micro** for testing. - Launch instance: In step 7, review your instance configuration and choose "Launch". - Create a key pair: Select "Create a new key pair" and assign a name. The key pair file (.pem) will download automatically - save this in a safe place as we will later use this file to log in to the instance. Finally, choose "Launch Instances" to complete the set up.Connect to the Instance
- In a command-line shell, change directories to the location of the private key file that you created when you launched the instance.
- Use the following command to set the permissions of your private key file so that only you can read it.
$ chmod 400 /path/my-key-pair.pem
- Use the ssh command to connect to the instance. You specify the private key (.pem) file and ubuntu@public_dns_name.
$ ssh -i /path/my-key-pair.pem [email protected]
A more detailed guide of Getting Started with Amazon EC2 can be found here.
Install NodeJS
To install NodeJS, you can follow the guide here. I recommend you to use Latest NodeJS version.
For Ubuntu, the steps are as follows:
- Node.js is available from the NodeSource Debian and Ubuntu binary distributions repository
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - $ sudo apt-get install -y nodejs
- Optional: install build tools
$ sudo apt-get install -y build-essential
Install and Setup Nginx
You can install Nginx by following the guide here.
For Ubuntu, Nginx is available in Ubuntu's default repositories, so the installation is rather straight forward.
Update the Debian repository information:
$ sudo apt-get update
Install the NGINX Open Source package:
$ sudo apt-get install nginx
Verify the installation:
$ sudo nginx -v
To redirect all request to Node app, open and edit Nginx configuration file.
$ sudo nano /etc/nginx/sites-available/my-site.conf
Add the following config to the file
server { server_name mysite.com www.mysite.com; # location ~* \.(eot|otf|ttf|woff|woff2)$ { add_header Access-Control-Allow-Origin *; # } location / { proxy_set_header HOST $host; proxy_set_header X-Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:port_of_your_app; proxy_connect_timeout 600; proxy_send_timeout 600; proxy_read_timeout 600; send_timeout 600; } }
Save it by press CTRL+O then exit the Nano editor by press CTRL+X.
Create a Symbolic Link of the file to
sites-enabled
:$ ln -s /etc/nginx/sites-available/my-site.conf /etc/nginx/sites-enabled/
To test Nginx configuration, type this command.
$ sudo nginx -t
If the status is
OK
continue further.To restart Nginx type this command.
$ sudo service nginx restart
Install Process Manager
The most popular process managers for Express and other Node.js applications are as follows:
For more information, see Process managers for Express apps
- PM2 is a tool for managing Node.js process. To install it, type this command.
$ sudo npm install -g pm2
- Run the Node app using PM2 as follows:
$ cd /path/your-project && pm2 start server.js --name="my-site" -i 3 --watch