Skip to content

Instantly share code, notes, and snippets.

@lukemartin
Last active December 17, 2015 05:49
Show Gist options
  • Save lukemartin/5560855 to your computer and use it in GitHub Desktop.
Save lukemartin/5560855 to your computer and use it in GitHub Desktop.
Simple shell script to generate an nginx virtual host. http://luke.is/blogging/nginx-virtual-host-generator/
#!/bin/bash
if [ "$#" -le "1" ]; then
echo "Usage: nvhgen [hostname] [location]"
exit 1
fi
NGINXSA="/etc/nginx/sites-available/"
NGINXSE="/etc/nginx/sites-enabled/"
FILENAME="$1"
HOSTNAME="$1"
WORKDIR="$2"
STR="server {\n\t
listen 80;\n\t
server_name $HOSTNAME;\n\t
root $WORKDIR;\n\t
index index.html;\n\n\t
location / {\n\t\t
try_files \$uri \$uri/ =404;\n\t
}\n\n\t
location ~ \.php$ {\n\t\t
try_files \$uri =404;\n\t\t
fastcgi_split_path_info ^(.+\.php)(/.+)$;\n\t\t
fastcgi_pass 127.0.0.1:9000;\n\t\t
fastcgi_index index.php;\n\t\t
include fastcgi_params;\n\t
}\n\n\t
location ~ /\. { deny all; }\n
}"
if [ -f $NGINXSA$FILENAME ];
then
read -p "$FILENAME already exists. Overwrite? (y/n): " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]];
then
echo -e "\nOk, exiting."
exit 1
fi
echo -e "\n"
fi
echo -e $STR > $NGINXSA$FILENAME
echo -e "Created $FILENAME\n"
ln -s $NGINXSA$FILENAME $NGINXSE$FILENAME
echo -e "Added symlink for $FILENAME\n"
echo -e "Done\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment