Skip to content

Instantly share code, notes, and snippets.

@cowgp
Created February 24, 2015 19:03
Show Gist options
  • Select an option

  • Save cowgp/6965a79848f12033ed0c to your computer and use it in GitHub Desktop.

Select an option

Save cowgp/6965a79848f12033ed0c to your computer and use it in GitHub Desktop.
Script for automating creation of subdomains on an EC2 instance. Assumes NGINX and Supervisord are driving the web/node.js sites.
#!/bin/bash
#
# launch script with: sudo ./newsite.sh
#
echo -e "What's a short name for your site? (ex. sbapi):"
read nickname
echo -e "What's the full domain for the site? (ex. sbapi.uncorkedstudios.com):"
read siteurl
echo "great, you told me $nickname will be available at $siteurl"
cd /var/www
mkdir $siteurl
cd $siteurl
mkdir logs
chown galapagos:team logs
chmod 2775 logs
isNode=NO
echo "Will this be for a node app?"
select yn in "Yes" "No"; do
case $yn in
Yes ) isNode=YES; break;;
No ) isNode=NO; break;;
esac
done
if [[ "$isNode" == YES ]]; then
echo -e "Provide a preferred port number to listen at:"
read port
echo -e "filename of the javascript file to start your server? (ex. app.js)"
read jsFile
mkdir app
chown galapagos:team app
chmod 2775 app
cat <<EOT >> server.sh
#!/bin/bash
cd /var/www/$siteurl/app
PORT=$port /usr/bin/nodejs $jsFile
EOT
chmod +x server.sh
chown galapagos:team server.sh
cd /etc/nginx/sites-available
cat <<EOT >> $nickname
server {
listen 80;
server_name $siteurl;
access_log /var/www/$siteurl/logs/access.log;
error_log /var/www/$siteurl/logs/error.log;
root /var/www/$siteurl/app/;
location / {
add_header Access-Control-Allow-Headers "Content-Type, Authorization";
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
proxy_http_version 1.1;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:$port;
proxy_redirect off;
}
}
EOT
cd ../sites-enabled
ln -s ../sites-available/$nickname ./$nickname
service nginx restart
# configure supervisor
cd /etc/supervisor/conf.d/
cat <<EOT >> $nickname.conf
[program:$nickname]
command=/var/www/$siteurl/server.sh
autostart=true
autorestart=true
startretries=3
stopasgroup=true
killasgroup=true
stdout_logfile=/var/www/$siteurl/logs/server.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stderr_logfile=/var/www/$siteurl/logs/sup_error.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
EOT
supervisorctl reread
supervisorctl update
echo "Success!"
echo "Upload your node app to /var/www/$siteurl/app/"
echo "after uplaoding do npm install to install any needed node modules"
echo "lastly, restart the supervisord process with:"
echo "sudo supervisorctl restart $nickname"
else
mkdir public_html
chown galapagos:team public_html
chmod 2775 public_html
cd /etc/nginx/sites-available
cat <<EOT >> $nickname
server {
listen 80;
server_name $siteurl;
access_log /var/www/$siteurl/logs/access.log;
error_log /var/www/$siteurl/logs/error.log;
root /var/www/$siteurl/public_html/;
index index.html;
error_page 404 /404.html;
}
EOT
cd ../sites-enabled
ln -s ../sites-available/$nickname ./$nickname
service nginx restart
echo "Success!"
echo "Please upload your files to /var/www/$siteurl/public_html/"
fi
echo "DONE!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment