Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Last active December 20, 2018 16:01
Show Gist options
  • Save shotahorii/8a948ea5b502317d6364b4dcf62401d9 to your computer and use it in GitHub Desktop.
Save shotahorii/8a948ea5b502317d6364b4dcf62401d9 to your computer and use it in GitHub Desktop.
Deploy Node.js app on EC2

Deploy Node.js app on EC2

Initialisation

1. Create an EC2 instance

Create an instance via EC2 console.

2. Build your node.js app on the EC2 instance

git clone https://github.com/creationix/nvm.git ~/.nvm
source ~/.nvm/nvm.sh
nvm install 11.5 

(11.5 is the latest version at the time of this writing)

npm install express -global
npm install express-generator -global

Details on initialising app: https://qiita.com/tsuyoshioshima/items/7a553c25f222333b9503 (in JP)

Editing/Uploading files remotely:

scp -i path/to/pemfile path/to/local/file/to/copy [email protected]:/home/ec2-user/path/to/file/to/save

where XXX is your EC2 IP

3. Daemonize the app

The app terminates when we logs out from the terminal, if not daemonized.
To make app run without termination, use forever.js.

npm install forever -global

Details on how to start the app with forever: https://mi-rai.co.jp/blog/880 (in JP)

4. Install web server (nginx) and set port:3000 as default

Details on initialisation: https://qiita.com/yujiro0102/items/0bc30ab0a73de62c4e2d
How to start/stop server: https://qiita.com/ume1126/items/7cef8cb2c19959d25cb5

5. Get a domain

I've got a domain on https://www.onamae.com/ (in JP).

6. Associate the domain to EC2

Details here: https://qiita.com/nadonado/items/a7c32c94fef87b7db0d5 (in JP)

Running

Start

cd  
sudo service nginx start
cd path/to/your/nodejs/app
forever start ./bin/www 

Stop

cd path/to/your/nodejs/app
forever stop ./bin/www
cd  
sudo nginx -s stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment