Skip to content

Instantly share code, notes, and snippets.

@gravitano
Last active August 29, 2015 14:26
Show Gist options
  • Save gravitano/08ba25307fe1af2007fe to your computer and use it in GitHub Desktop.
Save gravitano/08ba25307fe1af2007fe to your computer and use it in GitHub Desktop.
Bash script for creating new virtual host in OS X (Homebrew, Nginx, PHP-FPM)

First, you need to follow this blog tutorial for installing Nginx, PHP-FPM and MySQL to your computer (OSX Only). Then, get the vhost.sh file to your PC and make it executable:

chmod +x /path/to/vhost.sh

You may also move the vhost.sh file to /usr/local/bin path for global use.

sudo mv /path/to/vhost.sh /usr/local/bin/vhost

And now you can use the vhost script to create new virtualhost.

sudo vhost domain.dev /path/to/document/root

sudo vhost foo.dev /Users/username/Code/Laravel/public
# Generate SSL Key for new Host
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=$1" -keyout /usr/local/etc/nginx/ssl/$1.key -out /usr/local/etc/nginx/ssl/$1.crt
block="server {
listen 80;
server_name $1;
root \"$2\";
index index.html index.htm index.php;
access_log /usr/local/etc/nginx/logs/default.access.log main;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
try_files \$uri \$uri/ /index.php?\$query_string;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
server {
listen 443;
server_name $1;
root \"$2\";
index index.html index.htm index.php;
access_log /usr/local/etc/nginx/logs/default-ssl.access.log main;
ssl on;
ssl_certificate ssl/$1.crt;
ssl_certificate_key ssl/$1.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
try_files \$uri \$uri/ /index.php?\$query_string;
}
error_page 404 /404.html;
error_page 403 /403.html;
}"
# Put the server block to available sites
echo "$block" > "/usr/local/etc/nginx/sites-available/$1"
# Create Symlink To Enabled Sites
ln -sfv "/usr/local/etc/nginx/sites-available/$1" "/usr/local/etc/nginx/sites-enabled/$1"
# Restart Nginx
launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
# Restart PHP FPM
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
# Add New Host
echo "127.0.0.1 $1" >> /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment