Last active
August 29, 2015 14:00
-
-
Save Ilyes512/11284225 to your computer and use it in GitHub Desktop.
Nginx scripts for enable and disabling a site. This will create or destroy a symlink between a real config file in /etc/nginx/sites-available and a symlink in /etc/nginx/sites-enabled.
This file contains 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 | |
# Show the usage for NGXCB | |
function show_usage { | |
cat <<EOF | |
NGXCB: | |
Create a new Nginx Server Block (Ubuntu Server). | |
Assumes /etc/nginx/sites-available and /etc/nginx/sites-enabled setup used. | |
-e Enable the Server Block right away with NGXEN - i.e -e (without any value) | |
-d DocumentRoot - i.e. -d /var/www/yoursite | |
-h Help - Show this menu. | |
-n The Server Block Config name - default: vagrant - i.e. -n /etc/nginx/sites-available/vagrant | |
-s ServerName - i.e. -s example.com or -s sub.example.com | |
EOF | |
exit 1 | |
} | |
if [ $EUID -ne 0 ]; then | |
echo "!!! Please use root: \"sudo NGXCB\"" | |
show_usage | |
fi | |
# Output Nginx Server Block Config | |
function create_server_block { | |
# Test if PHP is installed | |
php -v > /dev/null 2>&1 | |
PHP_IS_INSTALLED=$? | |
# Default empty PHP Config | |
PHP_NO_SSL="" | |
PHP_WITH_SSL="" | |
if [[ $PHP_IS_INSTALLED -eq 0 ]]; then | |
# Nginx Server Block config for PHP (without using SSL) | |
read -d '' PHP_NO_SSL <<EOF | |
# pass the PHP scripts to php5-fpm | |
# Note: \.php$ is susceptible to file upload attacks | |
# Consider using: "location ~ ^/(index|app|app_dev|config)\.php(/|$) {" | |
location ~ \.php$ { | |
try_files \$uri =404; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
# With php5-fpm: | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
include fastcgi_params; | |
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
fastcgi_param LARA_ENV local; # Environment variable for Laravel | |
fastcgi_param HTTPS off; | |
} | |
EOF | |
# Nginx Server Block config for PHP (with SSL) | |
read -d '' PHP_WITH_SSL <<EOF | |
# pass the PHP scripts to php5-fpm | |
# Note: \.php$ is susceptible to file upload attacks | |
# Consider using: "location ~ ^/(index|app|app_dev|config)\.php(/|$) {" | |
location ~ \.php$ { | |
try_files \$uri =404; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
# With php5-fpm: | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
include fastcgi_params; | |
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | |
fastcgi_param LARA_ENV local; # Environment variable for Laravel | |
fastcgi_param HTTPS on; | |
} | |
EOF | |
fi | |
# Main Nginx Server Block Config | |
cat <<EOF | |
server { | |
listen 80; | |
root $DocumentRoot; | |
index index.html index.htm index.php app.php app_dev.php; | |
# Make site accessible from http://set-ip-address.xip.io | |
server_name $ServerName; | |
access_log /var/log/nginx/vagrant.com-access.log; | |
error_log /var/log/nginx/vagrant.com-error.log error; | |
charset utf-8; | |
location / { | |
try_files \$uri \$uri/ /app.php?\$query_string /index.php?\$query_string; | |
} | |
location = /favicon.ico { log_not_found off; access_log off; } | |
location = /robots.txt { access_log off; log_not_found off; } | |
error_page 404 /index.php; | |
$PHP_NO_SSL | |
# Deny .htaccess file access | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
server { | |
listen 443; | |
ssl on; | |
ssl_certificate /etc/ssl/xip.io/xip.io.crt; | |
ssl_certificate_key /etc/ssl/xip.io/xip.io.key; | |
root $DocumentRoot; | |
index index.html index.htm index.php app.php app_dev.php; | |
# Make site accessible from http://set-ip-address.xip.io | |
server_name $ServerName.xip.io; | |
access_log /var/log/nginx/vagrant.com-access.log; | |
error_log /var/log/nginx/vagrant.com-error.log error; | |
charset utf-8; | |
location / { | |
try_files \$uri \$uri/ /app.php?\$query_string /index.php?\$query_string; | |
} | |
location = /favicon.ico { log_not_found off; access_log off; } | |
location = /robots.txt { access_log off; log_not_found off; } | |
error_page 404 /index.php; | |
$PHP_WITH_SSL | |
# Deny .htaccess file access | |
location ~ /\.ht { | |
deny all; | |
} | |
} | |
EOF | |
} | |
# Check if there are enough arguments provided (2 arguments and there 2 values) | |
if [[ $# -lt 4 ]]; then | |
echo "Not enough arguments. Please read the below for NGXCB useage:" | |
show_usage | |
fi | |
# The default for the optional argument's: | |
ServerBlockName="vagrant" | |
EnableServerBlock=0 | |
# Parse flags: | |
# - Run it in "silence"-mode by starting with a ":" | |
# - Single ":" after an argument means "requred" | |
# - Double ":" after an argument means "optional" | |
while getopts ":hd:s:n::e" OPTION; do | |
case $OPTION in | |
h) | |
show_usage | |
;; | |
d) | |
DocumentRoot=$OPTARG | |
;; | |
s) | |
ServerName=$OPTARG | |
;; | |
n) | |
ServerBlockName=$OPTARG | |
;; | |
e) | |
EnableServerBlock=1 | |
;; | |
*) | |
show_usage | |
;; | |
esac | |
done | |
if [[ ! -d $DocumentRoot ]]; then | |
mkdir -p $DocumentRoot | |
fi | |
if [[ -f "/etc/nginx/sites-available/$ServerBlockName" ]]; then | |
echo 'Nginx Server Block already exists. Aborting!' | |
show_usage | |
else | |
# Create the Server Block config | |
create_server_block > /etc/nginx/sites-available/${ServerBlockName} | |
# Enable the Server Block and reload Nginx | |
if [[ $EnableServerBlock -eq 1 ]]; then | |
# Enable Server Block | |
ngxen ${ServerBlockName} | |
# Reload Nginx | |
service nginx reload | |
fi | |
fi |
This file contains 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 ngxdis\"" | |
exit 1 | |
fi | |
# -z str: Returns True if the length of str is equal to zero. | |
if [ -z "$1" ]; then | |
echo "Please choose a site." | |
exit 1 | |
else | |
echo "Disabling site $1..." | |
# -h filename: True if file exists and is a symbolic link. | |
# -f filename: Returns True if file, filename is an ordinary file. | |
if [ ! -h "/etc/nginx/sites-enabled/$1" ] && [ ! -f "/etc/nginx/sites-enabled/$1" ]; then | |
echo "$1 is not enabled." | |
exit 1 | |
else | |
rm /etc/nginx/sites-enabled/$1 | |
echo "Disabled $1" | |
fi | |
fi |
This file contains 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 ngxen\"" | |
exit 1 | |
fi | |
# -z str: Returns True if the length of str is equal to zero. | |
if [ -z "$1" ]; then | |
echo "Please choose a site." | |
exit 1 | |
else | |
echo "Enabling site $1..." | |
# -h filename: True if file exists and is a symbolic link. | |
# -f filename: Returns True if file, filename is an ordinary file. | |
if [ -h "/etc/nginx/sites-enabled/$1" ] || [ -f "/etc/nginx/sites-enabled/$1" ]; then | |
echo "$1 is already enabled." | |
exit 1 | |
else | |
if [ ! -f "/etc/nginx/sites-available/$1" ]; then | |
echo "Site $1 does not exist in /etc/nginx/sites-available." | |
exit 1 | |
else | |
ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled/$1 | |
echo "Enabled $1" | |
exit 0 | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment