Nginx 启用站点命令

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

  • 那么我不确定你真正要求的是什么。 (31认同)
  • @pkhamre:使用 Apache 时有两个脚本:a2ensite 和 a2dissite。它们只是创建和删除您描述的符号链接,因此它们是启用和禁用的更快方法。 (19认同)
  • 感谢您对这个旧答案的持续投票。如果 OP 接受这个答案,那将是史诗般的:) (9认同)
  • 是的,我知道如何使用命令行,谢谢 (7认同)
  • 记得使用以下命令重新加载 nginx 服务器: sudo service nginx reload (4认同)
  • 确实这将是史诗般的,但 OP 永远不会接受您的答案,只需看看他对问题的最后一次编辑!事实上,我在下面提供了一个完整的非 Debian 特定的答案(因为“启用站点”是特定于 Debian/Ubuntu 的),结果却收到了来自 OP 的几乎立即的反对票,以及不知何故他们大规模的评论- 没有任何第三方脚本比单个 `mv` 或 `ln` 调用更容易使用。看起来他们对除他们自己以外的每一个答案都投了反对票!如果我们其他人对我们的反对票也能如此慷慨就好了! (2认同)

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)

  • 一个非常大的脚本来包装一些标准的单行命令。 (21认同)
  • @GhassenTelmoudi 因为您一直提到的脚本是第三方脚本,它甚至没有被创建者 (ubuntu) 打包到 nginx 包中,您的评论建议在(一行)命令行替代方案上使用第三方脚本。这就是创建安全漏洞和不必要的复杂依赖树的方式 (10认同)
  • @tobltobs 优秀的程序员编写代码,优秀的程序员窃取代码 :) 这很好地添加到我的服务器映像脚本集合中。 (2认同)

Mic*_*ton 36

你指的是nginx_ensitenginx_dissite吗?

  • 您回答了两个命令和一个 url,甚至以问题的形式回答。作为我经验不足的人,你的回答会让我在谷歌上搜索。也许我会在 2 分钟内找到一个有用的指南/教程/演示,也许我会环顾一个小时仍然感到困惑。当时对我有帮助的是:“有这些工具 nginx_ensite 和 nginx_dissite,这是一个 3rd 方脚本,从这里下载,它们以这种方式工作,例如,例子”。Ghassen 的回答更详尽、更具介绍性、更有帮助。我希望你明白我的意思:) (29认同)
  • 这几乎不是一个答案,是吗?这些命令在我的 nginx 安装中不存在,在使用 apt-get 安装的 Ubuntu 上。它似乎只是一个 3rd 方脚本:https://github.com/perusio/nginx_ensite (21认同)
  • @MadsSkjern 好吧,你可以点击链接。:) (10认同)
  • 首先,感谢您的回答 :) 抱歉我的评论,这可能听起来令人反感,当我实际上只想指出它对我(当时)不是很有用时,因为它假设了太多来自读者。 (3认同)

cns*_*nst 5

NGINX

\n\n

如果您使用的是http://nginx.org/packages/中的nginx 官方上游包之一,最好的方法是导航到该目录,并将受影响的文件从具有后缀的后缀重命名为具有不同的后缀禁用该站点:/etc/nginx/conf.d.conf

\n\n

sudo mv -i /etc/nginx/conf.d/default.conf{,.off}

\n\n

或者相反的方式来启用它:

\n\n

sudo mv -i /etc/nginx/conf.d/example.com.conf{.disabled,}

\n\n

这是因为默认/etc/nginx/nginx.conf有以下include指令:

\n\n
http {\n    \xe2\x80\xa6\n    include /etc/nginx/conf.d/*.conf;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

Debian/Ubuntu

\n\n

但是,如果您使用的是 Debian/Ubuntu 衍生版本,那么除了 之外conf.d,您还可能拥有邪恶的非标准sites-availablesites-enabled目录,其中的某些文件可能会被随意包含,而不管其扩展名:

\n\n
http {\n    \xe2\x80\xa6\n    include /etc/nginx/conf.d/*.conf;\n    include /etc/nginx/sites-enabled/*;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,在 Debian/Ubuntu 中,您可能首先必须弄清楚站点配置所在的位置。

\n\n
    \n
  • 您可以使用以下命令通过运行查找与给定掩码匹配的所有常规文件来获取所有可用站点的列表:find(1)

    \n\n

    find /etc/nginx -maxdepth 2 -type f \\( -path "*/conf.d/*.conf" -or -path "*/sites-*/*" \\)

  • \n
  • 您可以使用以下命令获取所有已启用站点的列表:

    \n\n

    find /etc/nginx -maxdepth 2 \\( -path "*/conf.d/*.conf" -or -path "*/sites-enabled/*" \\)

  • \n
\n\n

然后在 Debian/Ubuntu 上禁用/启用站点:

\n\n
    \n
  • 禁用站点:如果配置在 中,只需重命名该文件conf.d即可不再有.conf后缀;或者如果在sites-enabled,则将其移出sites-enabled

  • \n
  • 启用站点,最好的方法是将其移动到/etc/nginx/conf.d,并重命名为具有.conf后缀。

  • \n
\n\n

PS 为什么我认为 Debian 是include /etc/nginx/sites-enabled/*;邪恶的?尝试编辑该目录中的几个文件,并让您emacs创建备份文件(带有后缀~),然后再次询问我。

\n

  • 我想指出这个答案的问题在于关于 Debian 及其衍生产品的两个错误假设: 1) `conf.d` 目录的目的是服务器范围的配置,例如模块、插件、fastcgi 处理程序等的配置明确**不**存储主机/虚拟主机配置,2)一**不**编辑`sites-enabled`中的任何文件 https://serverfault.com/a/825297/86189 (9认同)
  • ..我会给你这个(这方面的文档非常缺乏)。然而,您是第一个在 Debian 上运行 Web 服务器的人,我对此感到困惑。只需在 Apache 或 Nginx 中执行一个简单的“ls -al site-enabled”即可显示目录中的现有文件是来自“-available”的符号链接,Apache 下的模块也是如此,并提供了“a2enmod”/“a2dismod”脚本。 (3认同)
  • 你提到的问题与此完全没有关系。我对 `conf.d` 的看法是错误的,可能是 Nginx 的 Debian 维护者(或者保留它是为了与上游兼容)。关于不编辑“sites-enabled”中的文件,这不是一厢情愿的想法,而是他们试图在 Nginx 上模拟的 Apache 下的假设工作流程。在 apache 中,由于存在“a2ensite”和“a2dissite”脚本,这一点非常明显。不幸的是,Nginx 没有提供任何此类内容,这表明该软件包在 Debian 上的维护质量有多低。两者都缺乏文档,确实如此。 (2认同)