-
-
Save jasonmccallister/9728984 to your computer and use it in GitHub Desktop.
My custom ngxhost.sh for vagrant. Forked from https://gist.github.com/fideloper/9063376
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ $EUID -ne 0 ]; then | |
echo "You must be root: \"sudo ngxvhost\"" | |
exit 1 | |
fi | |
# May need to run this as sudo! | |
# I have it in /usr/local/bin and run command 'ngxvhost' from anywhere, using sudo. | |
# | |
# Show Usage, Output to STDERR | |
# | |
function show_usage { | |
cat <<- _EOF_ | |
Create a new nginx vHost in Ubuntu Server | |
Assumes /etc/nginx/sites-available and /etc/nginx/sites-enabled setup used | |
-d DocumentRoot - i.e. /var/www/yoursite | |
-h Help - Show this menu. | |
-s ServerName - i.e. example.com or sub.example.com | |
_EOF_ | |
exit 1 | |
} | |
# | |
# Output vHost skeleton, fill with userinput | |
# To be outputted into new file | |
# | |
function create_vhost { | |
cat <<- _EOF_ | |
server { | |
listen 80; | |
server_name $ServerName; | |
root $DocumentRoot/public; | |
access_log /var/log/nginx/$ServerName-access.log; | |
error_log /var/log/nginx/$ServerName-error.log error; | |
autoindex on; | |
index index.php; | |
charset utf-8; | |
# Cache static files for a long time | |
include global/static-asset-caching.conf; | |
# Pull in some good PHP defaults | |
include global/php-restrictions.conf; | |
location / { | |
# First try and load files from the public folder, if they don't exist | |
# then send the request through to index.php | |
try_files $uri $uri/ /index.php; | |
# Forward requests on to PHP-FPM | |
location = /index.php { | |
include /etc/nginx/fastcgi_params; | |
fastcgi_index index.php; | |
fastcgi_intercept_errors on; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
fastcgi_pass unix:/var/run/php5-fpm.sock; | |
} | |
} | |
# If someone explicitly tries to load a PHP file return a 404 error, | |
# always use url rewrites and never have the .php extension in the url | |
location ~ \.php$ { | |
return 404; | |
} | |
# Deny .htaccess file access | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
_EOF_ | |
} | |
#Sanity Check - are there two arguments with 2 values? | |
if [ $# -ne 4 ]; then | |
show_usage | |
fi | |
#Parse flags | |
while getopts "hd:s:" OPTION; do | |
case $OPTION in | |
h) | |
show_usage | |
;; | |
d) | |
DocumentRoot=$OPTARG | |
;; | |
s) | |
ServerName=$OPTARG | |
;; | |
*) | |
show_usage | |
;; | |
esac | |
done | |
if [ ! -d $DocumentRoot ]; then | |
mkdir -p $DocumentRoot | |
#chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ? | |
fi | |
if [ -f "/etc/nginx/sites-available/$ServerName.conf" ]; then | |
echo 'vHost already exists. Aborting' | |
show_usage | |
else | |
create_vhost > /etc/nginx/sites-available/${ServerName}.conf | |
cd /etc/nginx/sites-available/ && ngxen ${ServerName}.conf #Enable site | |
service nginx reload #Optional implementation | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment