Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sets up a new domain with Nginx and initialises a Git respository
#! /bin/sh
domain="$1"
# Check a domain is provided
if [ -z "$domain" ]
then
echo "Domain required"
exit
fi
gitDir="/home/git"
wwwDir="/var/www"
printf "Use PHP (y/N)?: "
read usePhp
if [ "$usePhp" = "y" ]; then
# Code to include if using PHP
inc="index index.php index.html;
location ~ \.php\$ {
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}"
else
# Code to include if not using PHP
inc="index index.html;"
fi
echo "Setting up Nginx..."
# Add Nginx configuration
echo "server {
listen 80;
server_name www.$domain;
return 301 \$scheme://$domain\$request_uri;
}
server {
listen 80;
root $wwwDir/$domain/public;
server_name $domain;
include sites.conf;
$inc
}" > "/etc/nginx/sites-available/$domain"
# Add to sites enabled
ln -s "/etc/nginx/sites-available/$domain" "/etc/nginx/sites-enabled/$domain"
# Reload Nginx
nginx -s reload
echo "Adding Git repositories..."
# Create hub Git
cd "$gitDir"
git init --bare "$domain.git"
# Create live Git
cd "$wwwDir"
git init "$domain"
# Create empty index.html file
cd "$wwwDir/$domain"
mkdir "public" && touch "public/index.html"
git add . && git commit -m "Initial commit"
git push "$gitDir/$domain.git" master
# Add hub Git to live
echo "[remote \"hub\"]
url = $gitDir/$domain.git
fetch = +refs/heads/*:refs/remotes/hub/*" >> "$wwwDir/$domain/.git/config"
# Add hub post-update hook
echo "#!/bin/sh
echo
echo \"**** Pulling changes into Live [Hub's post-update hook]\"
echo
cd $wwwDir/$domain || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info" > "$gitDir/$domain.git/hooks/post-update"
# Add live post-commit hook
echo "#!/bin/sh
echo
echo \"**** pushing changes to Hub [Live's post-commit hook]\"
echo
git push hub" > "$wwwDir/$domain/.git/hooks/post-commit"
# Fix hub permissions
chown -R git "$gitDir/$domain.git"
chgrp -R git "$gitDir/$domain.git"
# Fix live permissions
chown -R www-data "$wwwDir/$domain"
chgrp -R www-data "$wwwDir/$domain"
chmod -R g+w "$wwwDir/$domain"
# Make hooks executable
chmod +x "$gitDir/$domain.git/hooks/post-update"
chmod +x "$wwwDir/$domain/.git/hooks/post-commit"
# Done
echo
echo "Clone with git@smallhadroncollider.com:$domain.git"
echo
@smallhadroncollider

This comment has been minimized.

Copy link
Owner Author

commented Jul 23, 2014

Creates and enables a new Nginx domain with the given domain name. Also creates a bare Git repository to sync with from your local machine and one that syncs with this to keep the deployment up to date. Will automatically setup www. forwarders to the non-www version.

Usage:

  • sudo new-domain example.com

Setup:

  • You should update the final echo command with your server's address

Assumptions:

  • Nginx is setup to use user www-data and group www-data
  • git user is in group www-data

Based on:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.