Skip to content

Instantly share code, notes, and snippets.

@gildotdev
Created November 7, 2010 18:01
Show Gist options
  • Save gildotdev/666284 to your computer and use it in GitHub Desktop.
Save gildotdev/666284 to your computer and use it in GitHub Desktop.
Bash script to add new domain to slicehost and create nginx config file for it
#!/bin/sh
#Setup a new domain on slicehost using layout outlined here:
#http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts
#and creating an nginx config file based on this article:
#http://www.dailycupoftech.com/2009/02/24/basic-slicehost-slice-setup-part-7-create-the-virtual-host-website/
# ----------------------------------------------------------------
#check to see if arguments are set
E_BADARGS=65
if [ ! -n "$1" ]
then
echo "Usage: `basename $0` domain.com"
exit $E_BADARGS
fi
while true; do
read -p "Are you sure you want to add $1 as a domain on this host? Y or N" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer Y or N.";;
esac
done
#move to proper directory and create directory structure
cd ~
userhome=`pwd`
# check to see if public_html directory exists and create it if it doesn't
if [ ! -e $userhome/public_html ]
then
echo "$userhome/public_html directory does not exist, creating it"
mkdir $userhome/public_html
continue # On to next.
fi
#creating {public,private,log,backup} directories
mkdir -p $userhome/public_html/$1/public
mkdir -p $userhome/public_html/$1/private
mkdir -p $userhome/public_html/$1/log
mkdir -p $userhome/public_html/$1/backup
#create dummy index.php
echo "<html>" > $userhome/public_html/$1/public/index.php
echo " <head>" >> $userhome/public_html/$1/public/index.php
echo " <title>$1</title>" >> $userhome/public_html/$1/public/index.php
echo " </head>" >> $userhome/public_html/$1/public/index.php
echo " <body>" >> $userhome/public_html/$1/public/index.php
echo " <h1>$1</h1>" >> $userhome/public_html/$1/public/index.php
echo " </body>" >> $userhome/public_html/$1/public/index.php
echo "</html>" >> $userhome/public_html/$1/public/index.php
#check nginx install type
if [ -d "/usr/local/nginx" ]
then # installed from source
nginxbase="/usr/local/nginx"
elif [ -d "/etc/nginx" ]
then # installed from package manager
nginxbase="/etc/nginx"
else
echo "Cannot determine where nginx is installed"
exit #stopping nginx setup
fi
#create nginx config file
touch $userhome/$1
echo "server {" >> $userhome/$1
echo " listen 80 default;" >> $userhome/$1
echo " server_name $1;\n" >> $userhome/$1
echo " access_log /home/fw/public_html/$1/log/access.log;" >> $userhome/$1
echo " error_log /home/fw/public_html/$1/log/error.log;\n\n" >> $userhome/$1
echo " root /home/fw/public_html/$1/public/;" >> $userhome/$1
echo " index index.php;\n" >> $userhome/$1
echo " if (!-e \$request_filename) {" >> $userhome/$1
echo " rewrite ^/(.*)\$ /index.php?q=\$1 last;" >> $userhome/$1
echo " }\n" >> $userhome/$1
echo " error_page 404 index.php;\n" >> $userhome/$1
echo " # hide protected files" >> $userhome/$1
echo " location ~* \.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)\$|^(code-style\.pl|Entries.*|Repository|Root|Tag|Template)\$ {" >> $userhome/$1
echo " deny all;" >> $userhome/$1
echo " }\n" >> $userhome/$1
echo " # hide backup_migrate files" >> $userhome/$1
echo " location ~* ^/files/backup_migrate {" >> $userhome/$1
echo " deny all;" >> $userhome/$1
echo " }\n" >> $userhome/$1
echo " # serve static files directly" >> $userhome/$1
echo " location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico)\$ {" >> $userhome/$1
echo " access_log off;" >> $userhome/$1
echo " expires 30d;" >> $userhome/$1
echo " }\n" >> $userhome/$1
echo " location ~ \.php\$ {" >> $userhome/$1
echo " fastcgi_pass 127.0.0.1:10005;" >> $userhome/$1
echo " fastcgi_index index.php;" >> $userhome/$1
echo " fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;" >> $userhome/$1
echo " fastcgi_param QUERY_STRING \$query_string;" >> $userhome/$1
echo " fastcgi_param REQUEST_METHOD \$request_method;" >> $userhome/$1
echo " fastcgi_param CONTENT_TYPE \$content_type;" >> $userhome/$1
echo " fastcgi_param CONTENT_LENGTH \$content_length;" >> $userhome/$1
echo " }" >> $userhome/$1
echo "}" >> $userhome/$1
sudo chown root:root $userhome/$1
sudo mv $userhome/$1 $nginxbase/sites-available/$1
#enable site by creating symbolic link
sudo ln -s $nginxbase/sites-available/$1 $nginxbase/sites-enabled/$1
#restart nginx
while true; do
read -p "Is nginx able to be restarted at this time? Y or N" yn
case $yn in
[Yy]* ) sudo /etc/init.d/nginx stop; sudo /etc/init.d/nginx start; exit;;
[Nn]* ) exit;;
* ) echo "Please answer Y or N.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment