Skip to content

Instantly share code, notes, and snippets.

@amolk
Created May 10, 2012 02:10
Show Gist options
  • Save amolk/2650498 to your computer and use it in GitHub Desktop.
Save amolk/2650498 to your computer and use it in GitHub Desktop.
First app server server for node.js on Ubuntu 10.x
echo "Set up folders" && \
sudo mkdir /var/www && \
sudo mkdir /var/www/testapp && \
sudo chown ubuntu /var/www/testapp && \
cd /var/www/testapp && \
echo "Create hello world application" && \
echo 'require("http").createServer(function(request, response) {
response.end("Hello World");
}).listen(3000);
console.log("Server running on localhost:3000");' > server.js && \
echo "Set up nginx" && \
sudo apt-get install nginx && \
sudo echo '
server {
listen 80;
# proxy to node
location / {
proxy_pass http://127.0.0.1:3000/;
}
}' | sudo tee /etc/nginx/sites-available/testapp > /dev/null && \
cd /etc/nginx/sites-enabled/ && \
sudo ln -s /etc/nginx/sites-available/testapp testapp && \
sudo rm default && \
sudo /etc/init.d/nginx restart && \
echo 'upstart' && \
sudo apt-get install upstart && \
echo ' #!upstart
description "node.js server"
author "amolk"
start on startup
stop on shutdown
script
export HOME="/root"
echo $$ > /var/run/testapp.pid
exec sudo -u ubuntu /home/ubuntu/local/bin/node /var/www/testapp/server.js >> /var/log/testapp.sys.log 2>&1
end script
pre-start script
# Date format same as (new Date()).toISOString() for consistency
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/testapp.sys.log
end script
pre-stop script
rm /var/run/testapp.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/testapp.sys.log
end script' | sudo tee /etc/init/testapp.conf > /dev/null && \
sudo start testapp && \
echo 'Set up monit monitoring' && \
sudo apt-get install monit && \
echo ' #!monit
set logfile /var/log/monit.log
check process nodejs with pidfile "/var/run/testapp.pid"
start program = "/sbin/start testapp"
stop program = "/sbin/stop testapp"
if failed port 3000 protocol HTTP
request /
with timeout 10 seconds
then restart
' | sudo tee /etc/monit/conf.d/testapp.conf > /dev/null && \
sudo monit -d 60 -c /etc/monit/monitrc && \
echo 'Install mongo' && \
cd ~ && \
curl http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.0.5.tgz > mongo.tgz && \
tar xzf mongo.tgz && \
cp mongodb-linux-x86_64-2.0.5/bin/* local/bin/ && \
sudo mkdir -p /data/db/ && \
sudo chown `id -u` /data/db && \
echo '# Ubuntu upstart file at /etc/init/mongodb.conf
pre-start script
mkdir -p /data/db
mkdir -p /data/log
end script
start on runlevel [2345]
stop on runlevel [06]
exec start-stop-daemon --start --quiet --chuid ubuntu --exec /home/ubuntu/local/bin/mongod
' | sudo tee /etc/init/mongodb.conf > /dev/null && \
sudo start mongodb && \
mongo --version && \
echo 'Install Express' && \
npm install express && \
echo 'export PATH=$HOME/node_modules/express/bin:$PATH' >> ~/.bashrc && \
source ~/.bashrc && \
express -v && \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment