HXH*_*HXH 170 website nginx command-line-interface command
我们都知道如何在 Linux 上使用 apache 启用网站。我很确定我们都同意使用 a2ensite 命令。
不幸的是,Nginx 没有默认的等效命令,但确实发生了我在 ubuntu 上安装了一些允许我启用/禁用站点并列出它们的包。
问题是我不记得这个包的名字。
有人知道我在说什么吗?
请告诉我这个包的名称和命令名称。
pkh*_*mre 231
如果您已经nginx从 Ubuntu 存储库安装了该软件包,您将拥有两个目录。
/etc/nginx/sites-enabled和/etc/nginx/sites-available。
在主要的 nginx 配置中/etc/nginx/nginx.conf,您有以下行:
include /etc/nginx/sites-enabled/*.conf;
Run Code Online (Sandbox Code Playgroud)
所以基本上要列出所有可用的虚拟主机,您可以运行以下命令:
ls /etc/nginx/sites-available
Run Code Online (Sandbox Code Playgroud)
要激活其中之一,请运行以下命令:
ln -s /etc/nginx/sites-available/www.example.org.conf /etc/nginx/sites-enabled/
Run Code Online (Sandbox Code Playgroud)
Apache 附带的脚本基本上只是简单的 shell 包装器,可以执行与上述类似的操作。
链接文件后,记得运行sudo service nginx reload/service nginx reload
HXH*_*HXH 83
只需创建此脚本/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
Run Code Online (Sandbox Code Playgroud)
这个怎么运作:
列出所有站点
$ sudo nginx_modsite -l
Run Code Online (Sandbox Code Playgroud)
启用站点“test_website”
$ sudo nginx_modsite -e test_website
Run Code Online (Sandbox Code Playgroud)
禁用站点“test_website”
$ sudo nginx_modsite -d test_website
Run Code Online (Sandbox Code Playgroud)
Mic*_*ton 36
你指的是nginx_ensite和nginx_dissite吗?
如果您使用的是http://nginx.org/packages/中的nginx 官方上游包之一,最好的方法是导航到该目录,并将受影响的文件从具有后缀的后缀重命名为具有不同的后缀禁用该站点:/etc/nginx/conf.d.conf
sudo mv -i /etc/nginx/conf.d/default.conf{,.off}
或者相反的方式来启用它:
\n\nsudo mv -i /etc/nginx/conf.d/example.com.conf{.disabled,}
这是因为默认/etc/nginx/nginx.conf有以下include指令:
http {\n \xe2\x80\xa6\n include /etc/nginx/conf.d/*.conf;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但是,如果您使用的是 Debian/Ubuntu 衍生版本,那么除了 之外conf.d,您还可能拥有邪恶的非标准sites-available和sites-enabled目录,其中的某些文件可能会被随意包含,而不管其扩展名:
http {\n \xe2\x80\xa6\n include /etc/nginx/conf.d/*.conf;\n include /etc/nginx/sites-enabled/*;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n因此,在 Debian/Ubuntu 中,您可能首先必须弄清楚站点配置所在的位置。
\n\n您可以使用以下命令通过运行查找与给定掩码匹配的所有常规文件来获取所有可用站点的列表:find(1)
find /etc/nginx -maxdepth 2 -type f \\( -path "*/conf.d/*.conf" -or -path "*/sites-*/*" \\)
您可以使用以下命令获取所有已启用站点的列表:
\n\nfind /etc/nginx -maxdepth 2 \\( -path "*/conf.d/*.conf" -or -path "*/sites-enabled/*" \\)
然后在 Debian/Ubuntu 上禁用/启用站点:
\n\n禁用站点:如果配置在 中,只需重命名该文件conf.d即可不再有.conf后缀;或者如果在sites-enabled,则将其移出sites-enabled。
要启用站点,最好的方法是将其移动到/etc/nginx/conf.d,并重命名为具有.conf后缀。
PS 为什么我认为 Debian 是include /etc/nginx/sites-enabled/*;邪恶的?尝试编辑该目录中的几个文件,并让您emacs创建备份文件(带有后缀~),然后再次询问我。