Skip to content

Instantly share code, notes, and snippets.

@highelf
Last active December 21, 2015 08:14
Show Gist options
  • Save highelf/c26ce77cbbfda48c80e6 to your computer and use it in GitHub Desktop.
Save highelf/c26ce77cbbfda48c80e6 to your computer and use it in GitHub Desktop.
bash for create install and config nodejs nginx app server
#!/bin/bash
##[] install node
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs
##[] install forever
sudo npm install npm -g
sudo npm install forever -g
##[] install nginx
sudo apt-get install -y nginx
##[] extra dependencies
sudo apt-get install unzip
##[] config nginx
cat > /etc/nginx/conf.d/sgapi.conf <<EOL
upstream sgapi {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
server_name .amazonaws.com;
location / {
proxy_pass http://sgapi;
}
}
EOL
##[] start nginx
sudo service nginx restart
##[] test server
TESTFILE=test.js
TESTROUT=/var/www
TESTSERVER=$TESTROUT/$TESTFILE
sudo mkdir $TESTROUT
if [$? -eq 1]
then
echo cant create $TESTROUT
exit 1
fi
cat > ${TESTSERVER} <<EOF
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=3000;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
EOF
nohup node $TESTSERVER &
echo "done !"
@highelf
Copy link
Author

highelf commented Dec 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment