Skip to content

Instantly share code, notes, and snippets.

@Linnk
Created October 27, 2014 22:55
Show Gist options
  • Save Linnk/ded52d192463fb163a3a to your computer and use it in GitHub Desktop.
Save Linnk/ded52d192463fb163a3a to your computer and use it in GitHub Desktop.
Script to add virtualhost for CakePHP 2.x
#!/bin/bash
if [ -z $1 ]
then
read -e -p "Please enter your local domain name: " DOMAIN
if [ -z $DOMAIN ]
then
echo "You must specify a local domain name (example: name.dev)."
exit 1
fi
else
DOMAIN=$1
fi
if [ -z $2 ]
then
read -e -p "Enter the path for the virtualhost [$PWD]: " DIR
if [ -z $DIR ]
then
DIR=$PWD
fi
else
DIR=$2
fi
DOEXIST=`cat /etc/hosts | grep "$DOMAIN"`
if [ "$DOEXIST" != "" ]
then
echo ""
echo "This domain looks like it's already in /etc/hosts."
echo "Better check this manually. I'm a coward."
echo ""
else
# INSERT ENTRY IN /ETC/HOSTS
sudo bash -c "echo \"127.0.0.1 $DOMAIN\" >> /etc/hosts"
echo "Added local redirection to \""$DOMAIN"\" in /etc/hosts"
fi
# ADD VIRTUALHOST TO NGINX AS AVAILABLE SITE
echo "server {
listen 80;
server_name $DOMAIN;
root $DIR;
index index.php;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~ \.php\$ {
try_files \$uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param HTTPS \$fastcgi_https;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}" | sudo tee /opt/local/etc/nginx/sites-available/$DOMAIN.conf
echo "Added virtual host for $DOMAIN ($DIR) to /opt/local/etc/nginx/sites-available/"
# ADD VIRTUALHOST TO NGINX AS ENABLED SITE
sudo ln -s ../sites-available/$DOMAIN.conf /opt/local/etc/nginx/sites-enabled/$DOMAIN.conf
echo "Added symbolic link $DOMAIN.conf to /opt/local/etc/nginx/sites-enabled/"
# RESTART NGINX
sudo nginx -s reload
echo "Nginx reloading..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment