2016년 6월 21일 화요일

AWS Amazon Linux EC2에 Nginx와 pm2, nvm을 이용해서 Node.js 서버 구축 | Deploy Node.js server with Nginx, pm2 and nvm on AWS Amazon Linux EC2

deploy.conf - Configuration file for deploy scripts

#!/bin/bash
set -e

YUM_PACKAGES='gcc gcc-c++ git nginx'
NODEJS_VERSION='4.4.5'
PROJECT_NAME='aws-nodejs'
WEB_ROOT='/var/www'
PROJECT_DIR=$WEB_ROOT'/'$PROJECT_NAME
SSH_HOST='aws-nodejs'

_initialize - Deploy script to initialize EC2 and start Node.js application

set -e

source ./deploy.conf

sudo su -p <<HERE
set -e

# Set locale to C
echo 'LC_ALL=C' >> /etc/sysconfig/i18n
export LC_ALL=C

# yum packages
yum -y update
yum -y install $YUM_PACKAGES

# nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
source ~/.bashrc
nvm install $NODEJS_VERSION
nvm use $NODEJS_VERSION

# user www-data
mkdir -p $PROJECT_DIR
useradd www-data
usermod -a -G www-data ec2-user

# Extract project
tar -xzf /home/ec2-user/$PROJECT_NAME.tar.gz -C $PROJECT_DIR

# directory permission
chown -R www-data:www-data $WEB_ROOT
chmod 2775 $WEB_ROOT
find $WEB_ROOT -type d -exec sudo chmod 2775 {} +
find $WEB_ROOT -type f -exec sudo chmod 0664 {} +

# npm install
cd $PROJECT_DIR
npm install --save

# pm2
npm install -g pm2
pm2 start $PROJECT_DIR/server.js
pm2 save
pm2 startup amazon -u www-data
chmod u+x /etc/init.d/pm2-init.sh
chkconfig --add pm2-init.sh
chown -R www-data:www-data ~/

# nginx
yes | cp -rf $PROJECT_DIR/deploy/nginx.conf /etc/nginx
chkconfig nginx on

# service
service nginx start
HERE

_release - Deploy script to release Node.js application

#!/bin/bash
set -e

source ./deploy.conf

sudo su -p <<HERE
set -e
source ~/.bashrc

# Extract project
tar -xzf /home/ec2-user/$PROJECT_NAME.tar.gz -C $PROJECT_DIR

# directory permission
chown -R www-data:www-data $WEB_ROOT
chmod 2775 $WEB_ROOT
find $WEB_ROOT -type d -exec sudo chmod 2775 {} +
find $WEB_ROOT -type f -exec sudo chmod 0664 {} +

# nginx
yes | cp -rf $PROJECT_DIR/deploy/nginx.conf /etc/nginx

# service
service nginx restart
service pm2-init.sh restart
HERE

Read More

댓글 없음:

댓글 쓰기