Last active
December 21, 2015 08:14
-
-
Save highelf/c26ce77cbbfda48c80e6 to your computer and use it in GitHub Desktop.
bash for create install and config nodejs nginx app server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 !" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for run it in ubuntu can use this command
curl -s https://gist.github.com/highelf/c26ce77cbbfda48c80e6/raw/8a3d390b3c67829cef914b43d9eea882015ac974/gistfile1.txt | sudo /bin/bash