add nginx, forever and clone app from github

This commit is contained in:
Sergio Crisostomo 2015-12-07 22:26:22 +01:00
parent cd92653f6c
commit 5379b3b577
4 changed files with 151 additions and 4 deletions

View File

@ -7,7 +7,10 @@ Works for any recent version of Ubuntu or OS X.
```bash ```bash
# Specify the version of node to install # Specify the version of node to install
echo "v0.11.14" > /tmp/NODE_VER echo "v4.2.3" > /tmp/NODE_VER
# Specify the Github url of your app
echo "https://github.com/<user>/<project>" > /tmp/APP_URL
# And install away! # And install away!
curl -fsSL bit.ly/easy-install-node | bash curl -fsSL bit.ly/easy-install-node | bash
@ -29,6 +32,8 @@ This is what gets installed:
* xcode / brew / build-essential / pkg-config / gcc * xcode / brew / build-essential / pkg-config / gcc
* node * node
* jshint * jshint
* nginx
* forever
Screencast Screencast
========== ==========

View File

@ -0,0 +1,80 @@
#!/bin/bash
#
# description: DevShelf service
# processname: node
# pidfile: /var/run/devshelf.pid
# logfile: /var/log/devshelf.log
#
# Based on https://gist.github.com/jinze/3748766
#
# To use it as service on Ubuntu:
# sudo cp devshelf.sh /etc/init.d/devshelf
# sudo chmod a+x /etc/init.d/devshelf
# sudo update-rc.d devshelf defaults
#
# Then use commands:
# service devshelf <command (start|stop|etc)>
NAME=devshelf # Unique name for the application
SOUREC_DIR=/home/node-app # Location of the application source
COMMAND=node # Command to run
SOURCE_NAME=index.js # Name os the applcation entry point script
USER=root # User for process running
NODE_ENVIROMENT=production # Node environment
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever=forever
start() {
export NODE_ENV=$NODE_ENVIROMENT
echo "Starting $NAME node instance : "
touch $logfile
chown $USER $logfile
touch $pidfile
chown $USER $pidfile
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo -H -u $USER $forever start --pidFile $pidfile -l $logfile -a --sourceDir $SOUREC_DIR -c $COMMAND $SOUREC_DIR/$SOURCE_NAME
RETVAL=$?
}
restart() {
echo -n "Restarting $NAME node instance : "
sudo -H -u $USER $forever restart $SOUREC_DIR/$SOURCE_NAME
RETVAL=$?
}
status() {
echo "Status for $NAME:"
sudo -H -u $USER $forever list
RETVAL=$?
}
stop() {
echo -n "Shutting down $NAME node instance : "
sudo -H -u $USER $forever stop $SOUREC_DIR/$SOURCE_NAME
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL

31
config-files/nginx-config Normal file
View File

@ -0,0 +1,31 @@
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
upstream project {
server localhost:3000;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
proxy_pass http://project;
}
}

View File

@ -3,23 +3,34 @@
# curl -fsSL https://example.com/setup.bash | bash # curl -fsSL https://example.com/setup.bash | bash
# wget -nv https://example.com/setup.bash -O - | bash # wget -nv https://example.com/setup.bash -O - | bash
echo ""
echo "Script starting..."
BASE_URL="https://raw.githubusercontent.com/coolaj86/node-install-script/master" BASE_URL="https://raw.githubusercontent.com/coolaj86/node-install-script/master"
if [ -n "$(which node 2>/dev/null || false)" ]; then if [ -n "$(which node 2>/dev/null || false)" ]; then
echo "" echo ""
echo "HEY, LISTEN:" echo "HEY, LISTEN:"
echo "node is already install as $(node -v | grep v)" echo "node is already installed as $(node -v | grep v)"
echo "" echo ""
echo "to reinstall please first run: rm $(which node)" echo "to reinstall please first run: rm $(which node)"
echo "" echo ""
fi fi
if [ -f "/tmp/NODE_VER" ]; then if [ -f "/tmp/NODE_VER" ]; then
NODE_VER=$(cat /tmp/NODE_VER | grep v) NODE_VER=$(cat /tmp/NODE_VER)
fi fi
if [ -f "/tmp/APP_URL" ]; then
APP_URL=$(cat /tmp/APP_URL)
fi
echo "app url: $APP_URL"
echo "node version: $NODE_VER"
if [ -z "$NODE_VER" ]; then if [ -z "$NODE_VER" ]; then
NODE_VER="v0.11.14" NODE_VER="v4.2.3"
fi fi
OS="unsupported" OS="unsupported"
ARCH="" ARCH=""
@ -146,4 +157,24 @@ else
echo "jshint already installed" echo "jshint already installed"
fi fi
# clone app
if [[ $APP_URL ]]; then
sudo bash -c "cd /home/ && git clone ${APP_URL} node-app && cd node-app && npm install"
fi
# forever
sudo bash -c "npm -g install forever"
wget --quiet "${BASE_URL}/config-files/forever-node-init.sh" -O /etc/init.d/forever || echo 'error downloading forever config script'
sudo bash -c "chmod a+x /etc/init.d/forever"
sudo bash -c "update-rc.d forever defaults"
if [[ $APP_URL ]]; then
sudo bash -c "forever start /home/node-app/index.js"
fi
# nginx
echo "installing nginx..."
sudo bash -c "apt-get install -qq -y nginx < /dev/null" > /dev/null
wget --quiet "${BASE_URL}/config-files/nginx-config" -O /etc/nginx/sites-enabled/default || echo 'error configuring nginx'
sudo bash -c "service nginx reload"
echo "" echo ""