nginx site enable
2024.09.07 23:38
Just create this script /usr/bin/nginx_modsite
and make it executable with chmod 700 /usr/bin/nginx_modsite
.
#!/bin/bash
##
# File:
# nginx_modsite
# Description:
# Provides a basic script to automate enabling and disabling websites found
# in the default configuration directories:
# /etc/nginx/sites-available and /etc/nginx/sites-enabled
# For easy access to this script, copy it into the directory:
# /usr/local/sbin
# Run this script without any arguments or with -h or --help to see a basic
# help dialog displaying all options.
##
# Copyright (C) 2010 Michael Lustfield <mtecknology@ubuntu.com>
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
##
# Default Settings
##
NGINX_CONF_FILE="$(awk -F= -v RS=' ' '/conf-path/ {print $2}' <<< $(nginx -V 2>&1))"
NGINX_CONF_DIR="${NGINX_CONF_FILE%/*}"
NGINX_SITES_AVAILABLE="$NGINX_CONF_DIR/sites-available"
NGINX_SITES_ENABLED="$NGINX_CONF_DIR/sites-enabled"
SELECTED_SITE="$2"
##
# Script Functions
##
ngx_enable_site() {
[[ ! "$SELECTED_SITE" ]] &&
ngx_select_site "not_enabled"
[[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
ngx_error "Site does not appear to exist."
[[ -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
ngx_error "Site appears to already be enabled"
ln -sf "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" -T "$NGINX_SITES_ENABLED/$SELECTED_SITE"
ngx_reload
}
ngx_disable_site() {
[[ ! "$SELECTED_SITE" ]] &&
ngx_select_site "is_enabled"
[[ ! -e "$NGINX_SITES_AVAILABLE/$SELECTED_SITE" ]] &&
ngx_error "Site does not appear to be \'available\'. - Not Removing"
[[ ! -e "$NGINX_SITES_ENABLED/$SELECTED_SITE" ]] &&
ngx_error "Site does not appear to be enabled."
rm -f "$NGINX_SITES_ENABLED/$SELECTED_SITE"
ngx_reload
}
ngx_list_site() {
echo "Available sites:"
ngx_sites "available"
echo "Enabled Sites"
ngx_sites "enabled"
}
##
# Helper Functions
##
ngx_select_site() {
sites_avail=($NGINX_SITES_AVAILABLE/*)
sa="${sites_avail[@]##*/}"
sites_en=($NGINX_SITES_ENABLED/*)
se="${sites_en[@]##*/}"
case "$1" in
not_enabled) sites=$(comm -13 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
is_enabled) sites=$(comm -12 <(printf "%s\n" $se) <(printf "%s\n" $sa));;
esac
ngx_prompt "$sites"
}
ngx_prompt() {
sites=($1)
i=0
echo "SELECT A WEBSITE:"
for site in ${sites[@]}; do
echo -e "$i:\t${sites[$i]}"
((i++))
done
read -p "Enter number for website: " i
SELECTED_SITE="${sites[$i]}"
}
ngx_sites() {
case "$1" in
available) dir="$NGINX_SITES_AVAILABLE";;
enabled) dir="$NGINX_SITES_ENABLED";;
esac
for file in $dir/*; do
echo -e "\t${file#*$dir/}"
done
}
ngx_reload() {
read -p "Would you like to reload the Nginx configuration now? (Y/n) " reload
[[ "$reload" != "n" && "$reload" != "N" ]] && invoke-rc.d nginx reload
}
ngx_error() {
echo -e "${0##*/}: ERROR: $1"
[[ "$2" ]] && ngx_help
exit 1
}
ngx_help() {
echo "Usage: ${0##*/} [options]"
echo "Options:"
echo -e "\t<-e|--enable> <site>\tEnable site"
echo -e "\t<-d|--disable> <site>\tDisable site"
echo -e "\t<-l|--list>\t\tList sites"
echo -e "\t<-h|--help>\t\tDisplay help"
echo -e "\n\tIf <site> is left out a selection of options will be presented."
echo -e "\tIt is assumed you are using the default sites-enabled and"
echo -e "\tsites-disabled located at $NGINX_CONF_DIR."
}
##
# Core Piece
##
case "$1" in
-e|--enable) ngx_enable_site;;
-d|--disable) ngx_disable_site;;
-l|--list) ngx_list_site;;
-h|--help) ngx_help;;
*) ngx_error "No Options Selected" 1; ngx_help;;
esac
How it works:
To list all the sites
$ sudo nginx_modsite -l
To enable site "test_website"
$ sudo nginx_modsite -e test_website
To disable site "test_website"
$ sudo nginx_modsite -d test_website
출처: https://serverfault.com/a/562210
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
» | nginx site enable | 꿈돌이 | 2024.09.07 | 69 |
20 | slim routeplaceholder | 꿈돌이 | 2020.02.27 | 4285 |
19 | slim 4 , Intelephense | 꿈돌이 | 2020.01.17 | 361 |
18 | axios post로 데이터 안 넘어갈 때 체크할 거 | 꿈돌이 | 2018.10.24 | 661 |
17 |
윈도우에서 사설인증서로 https localhost 사용하기
![]() | 꿈돌이 | 2018.08.24 | 328 |
16 | php obfuscator | 꿈돌이 | 2018.01.31 | 266 |
15 | DB 순번 시퀀스 초기화 | 꿈돌이 | 2017.12.25 | 12969 |
14 | Vue.js td내 input 태그에서 v-for | 꿈돌이 | 2017.12.19 | 675 |
13 |
Vue Slim으로 카테고리 만들기
![]() | 꿈돌이 | 2017.11.10 | 297 |
12 | Doctrine DBAL 메모 | 꿈돌이 | 2017.10.27 | 300 |
11 |
Vue Tree view with Single file component
![]() | 꿈돌이 | 2017.09.29 | 433 |
10 | PHP 내장 웹서버 띄우기 | 꿈돌이 | 2017.09.04 | 601 |
9 |
vue-slim-medoo boilerplate
![]() | 꿈돌이 | 2017.06.04 | 326 |
8 |
Vue boilerplate
![]() | 꿈돌이 | 2017.06.02 | 472 |
7 |
Slim boilerplates
![]() | 꿈돌이 | 2017.06.02 | 254 |
6 | Windows에서 Composer로 slim 설치하기 | 꿈돌이 | 2017.06.01 | 616 |
5 | 코드이그나이터 3.1.3에서 HMVC 오류 대응 | 꿈돌이 | 2017.03.01 | 540 |
4 | CodeIgniter - Helper 폐기 항목 | 꿈돌이 | 2016.06.14 | 1260 |
3 | CodeIgniter - Class Libraries 폐기 항목 | 꿈돌이 | 2016.06.14 | 479 |
2 | Windows에서 Composer로 CodeIgniter 설치하기 | 꿈돌이 | 2016.06.06 | 1213 |