Skip to content

Instantly share code, notes, and snippets.

@bitsandbooks
Last active June 22, 2017 01:45
Show Gist options
  • Save bitsandbooks/5509955 to your computer and use it in GitHub Desktop.
Save bitsandbooks/5509955 to your computer and use it in GitHub Desktop.
My custom script for adding a vhost to Nginx.
#!/usr/bin/env bash
#
# Nginx - new server block
# http://rosehosting.com
# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
working() { echo -ne '\e[1;33m'$1'\e[m'; } # Yellow
die() { echo -e '\e[1;31m'$1'\e[m'; exit 1; }
# Variables
NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available'
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
WEB_USER='www-data'
WEB_GROUP='developers'
GIT_USER='git'
REPO_DIR=`eval echo '/home/$GIT_USER/repos'`
LIVE_BRANCH='master'
STAGING_BRANCH='dev'
# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
[ $# != "1" ] && die "Usage: $(basename $0) domainName"
# Create nginx config file for live site
working "Creating nginx config file for live site... "
cat > $NGINX_AVAILABLE_VHOSTS/$1 <<EOF
server {
server_name $1;
listen 80;
root $WEB_DIR/$1/public_html;
access_log $WEB_DIR/$1/logs/access.log;
error_log $WEB_DIR/$1/logs/error.log;
index index.html index.php;
location / {
try_files \$uri \$uri/ =404;
}
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
}
}
# Redirect www.$1 to $1.
server {
server_name www.$1;
rewrite ^(.*) http://$1\$1 permanent;
}
EOF
ok "done."
# Create nginx config file for staging server
working "Creating nginx config file for staging server... "
cat > $NGINX_AVAILABLE_VHOSTS/staging.$1 <<EOF
server {
server_name staging.$1;
listen 80;
root $WEB_DIR/staging.$1/public_html;
access_log $WEB_DIR/staging.$1/logs/access.log;
error_log $WEB_DIR/staging.$1/logs/error.log;
index index.html index.php;
location / {
try_files \$uri \$uri/ =404;
}
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ /\.ht {
deny all;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
}
}
# Redirect www.staging.$1 to staging.$1.
server {
server_name www.staging.$1;
rewrite ^(.*) http://staging.$1\$1 permanent;
}
EOF
ok "done."
# Creating {public,log} directories
working "Creating folders for live and staging sites... "
mkdir -p $WEB_DIR/$1/{public_html,logs}
mkdir -p $WEB_DIR/staging.$1/{public_html,logs}
ok "done."
# Creating index.html file in live site folder
working "Creating placeholder index.html files for live and staging sites... "
cat > $WEB_DIR/$1/public_html/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<title>$1</title>
<meta charset="utf-8" />
</head>
<body>
<h1>$1<h1>
<p>Hello World</p>
</body>
</html>
EOF
# Creating index.html file in staging site folder
cat > $WEB_DIR/staging.$1/public_html/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<title>staging.$1</title>
<meta charset="utf-8" />
</head>
<body>
<h1>staging.$1<h1>
<p>Hello World</p>
</body>
</html>
EOF
ok "done."
# Changing permissions
working "Setting permissions on folders... "
chown -R $WEB_USER:$WEB_GROUP $WEB_DIR/$1
chown -R $WEB_USER:$WEB_GROUP $WEB_DIR/staging.$1
chmod -R g+w $WEB_DIR/$1
chmod -R g+w $WEB_DIR/staging.$1
ok "done."
# Create git repo.
working "Creating Git repository... "
mkdir -p $REPO_DIR/$1.git
cd $REPO_DIR/$1.git
git init --bare
git config core.bare false
git config receive.denycurrentbranch ignore
chown -R $GIT_USER:$WEB_GROUP $REPO_DIR/$1.git
chgrp -R $WEB_GROUP $REPO_DIR/$1.git
chmod -R g+rsw $REPO_DIR/$1.git
# Create post-receive hook to push to push branches to different web folders.
cat > $REPO_DIR/$1.git/hooks/post-receive <<EOF
#!/bin/sh
# A hook script that fires after receiving a push.
# by Rob D. (@stray)
# Based upon info from:
# http://blog.ekynoxe.com/2011/10/22/git-post-receive-for-multiple-branches-and-work-trees/
while read oldrev newrev refname
do
# branch=\`echo $ref | cut -d/ -f3\`
branch=\$(git rev-parse --symbolic --abbrev-ref \$refname)
if [ "\$branch" = "$LIVE_BRANCH" ]; then
git --work-tree=$WEB_DIR/$1/public_html/ checkout -f $LIVE_BRANCH
echo "Changes on \$branch branch deployed to live server."
fi
if [ "\$branch" = "$STAGING_BRANCH" ]; then
git --work-tree=$WEB_DIR/staging.$1/public_html/ checkout -f $STAGING_BRANCH
echo "Changes on \$branch branch deployed to staging server."
fi
done
EOF
chmod u+x,g+x $REPO_DIR/$1.git/hooks/post-receive
ok "done."
# Enable site by creating symbolic link
working "Enabling live and staging sites and creating symlinks in your home folder... "
ln -s $NGINX_AVAILABLE_VHOSTS/$1 $NGINX_ENABLED_VHOSTS/$1
ln -s $NGINX_AVAILABLE_VHOSTS/staging.$1 $NGINX_ENABLED_VHOSTS/staging.$1
# Create symlinks to files in user's home folder.
mkdir -p $HOME/sites
ln -s $REPO_DIR/$1.git $HOME/sites/$1.git
ln -s $WEB_DIR/$1/public_html $HOME/sites/$1
ln -s $WEB_DIR/$1/logs/access.log $HOME/sites/$1.access.log
ln -s $WEB_DIR/$1/logs/error.log $HOME/sites/$1.error.log
ln -s $WEB_DIR/staging.$1/public_html $HOME/sites/staging.$1
ln -s $WEB_DIR/staging.$1/logs/access.log $HOME/sites/staging.$1.access.log
ln -s $WEB_DIR/staging.$1/logs/error.log $HOME/sites/staging.$1.error.log
chown -R `who am i | awk '{print $1}'`:`who am i | awk '{print $1}'` $HOME/sites
ok "done."
# Restart
echo "Do you wish to restart nginx?"
select yn in "Yes" "No"; do
case $yn in
Yes ) /etc/init.d/nginx restart ; break;;
No ) exit;;
esac
done
ok "\nDone! Live and staging sites created for $1."
echo "Thanks to the magic of Git hooks, your $LIVE_BRANCH and $STAGING_BRANCH branches will be automatically deployed to their respectable site folders when you push them to the repo. \nYou also now have symlinks pointing to the various files and folders for the site in ~/sites."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment