Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

We all know how to enable a website using apache on linux. I'm pretty sure that we all agree on using the a2ensite command.

Unfortunately there is no default equivalent command comes with Nginx, but it did happens that I installed some package on ubuntu that allowed me to enable/disable sites and list them.

The problem is I don't remember the name of this package.

Anybody knows what I'm talking about?

Please tell me name of this package and the command name.

Update:

Go straight to answer the script is there just run it and everything is well explained.

share|improve this question
1  
The assertion about a2ensite isn't true for CentOS –  Iain Sep 5 '12 at 8:50

4 Answers 4

Are you referring to nginx_ensite and nginx_dissite?

share|improve this answer
2  
This is barely an answer, is it? These commands are not present on my installation of nginx, on Ubuntu installed with apt-get. It seems like it is just a 3rd party script: github.com/perusio/nginx_ensite –  Mads Skjern Aug 20 '14 at 19:53
    
@MadsSkjern If this is "barely an answer" then the accepted answer isn't much of one either! –  Michael Hampton Mar 23 at 5:46
    
First of all, thanks for answering :) And sorry for my comment, which perhaps sounds offensive, when I actually only wanted to point out that it was not very useful for me (at the time), because of it assuming too much from the reader. –  Mads Skjern Mar 23 at 9:32
    
You answered with two commands and a url, and even in the form of a question. As someone with my low level of experience, your answer would have sent me out there googling. Maybe I would find a helpful guide/tutorial/demo in 2 minutes, maybe I would be looking around for an hour and still be confused. What would have helped me back then was: "There are these tools nginx_ensite and nginx_dissite, it is a 3rd party script, download it from here, and they work this way, example, example". Ghassen's answer is more elaborate, more introductory, more helpful. I hope you understand what I mean :) –  Mads Skjern Mar 23 at 9:32
    
@MadsSkjern Well, you could have just clicked the link. :) –  Michael Hampton Mar 23 at 15:17

If you have installed the nginx package from the Ubuntu repositories, you will have two directories.

/etc/nginx/sites-enabled and /etc/nginx/sites-available.

In the main nginx configuration, /etc/nginx/nginx.conf, you have the following line:

include /etc/nginx/sites-enabled/*.conf;

So basically to list all available virtualhosts, you can run the following command:

ls /etc/nginx/sites-available

To activate one of them, run the following command:

ln -s /etc/nginx/sites-available/www.example.org.conf /etc/nginx/sites-enabled/

The scripts that comes with Apache is basically just simple shell wrappers that does something similar as above.

share|improve this answer
1  
Yeah I know how to that using the command line, thanks –  Ghassen Telmoudi Sep 5 '12 at 9:02
1  
Then I am unsure what you are really asking for. –  pkhamre Sep 5 '12 at 9:04
1  
remember to reload nginx server with: sudo service nginx reload –  Ricardo Martins Jan 23 '13 at 11:34
2  
@pkhamre: When using Apache there are two scripts: a2ensite and a2dissite. They simply create and delete the symbolic links you describe, so they are faster ways of enabling and disabling. –  Mads Skjern Aug 20 '14 at 19:59
up vote 17 down vote accepted

Just create this script /usr/bin/nginx_modsite and make it executable.

#!/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
share|improve this answer
    
in ngx_relaod function, I commented out the read and just make reload="y" since I run this via cron and didn't want the prompt at all. Thanks! –  radtek Nov 4 '14 at 19:16
    
yeah it make the perfect sense, can you tell me where did you make the change? –  Ghassen Telmoudi Nov 5 '14 at 20:25
    
search for ngx_reload() , comment out the 1st line of the function and replace with reload="y" –  radtek Nov 5 '14 at 20:53

Another method is just to rename the site's config file to something that ends without .conf

E.g. sudo mv mysite.conf mysite.conf.disabled

Then reload nginx, and that vhost will fall back to the default.

share|improve this answer
    
it's always nice to use the nginx_modsite command, you can list, disable, enabled site much easier and faster, than renaming the file every time @Pyrite –  Ghassen Telmoudi May 19 at 18:24
    
It doesn't appear that nginx_modsite is installed by default. Yet renaming files is a stock option. Besides, I prefixed my answer as an alternate method, not the best method @GhassenTelmoudi –  Pyrite May 25 at 11:07
    
@Pyrite On Ubuntu 14.04 the extension doesn't mater as nginx.conf includes sites-enabled as include /etc/nginx/sites-enabled/*; it only includes conf dir as *.conf –  Bojan Markovic Jul 5 at 17:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.