201

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 that comes with Nginx, but it did happen 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.

Does anybody know what I'm talking about?

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

HXH
  • 3,185

10 Answers10

290

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.

After linking the files, remember to run sudo service nginx reload/ service nginx reload

Drifter104
  • 3,883
pkhamre
  • 6,400
98

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 &quot;$NGINX_SITES_AVAILABLE/$SELECTED_SITE&quot; ]] &amp;&amp; 
    ngx_error &quot;Site does not appear to exist.&quot;
[[ -e &quot;$NGINX_SITES_ENABLED/$SELECTED_SITE&quot; ]] &amp;&amp;
    ngx_error &quot;Site appears to already be enabled&quot;

ln -sf &quot;$NGINX_SITES_AVAILABLE/$SELECTED_SITE&quot; -T &quot;$NGINX_SITES_ENABLED/$SELECTED_SITE&quot;
ngx_reload

}

ngx_disable_site() { [[ ! "$SELECTED_SITE" ]] && ngx_select_site "is_enabled"

[[ ! -e &quot;$NGINX_SITES_AVAILABLE/$SELECTED_SITE&quot; ]] &amp;&amp;
    ngx_error &quot;Site does not appear to be \'available\'. - Not Removing&quot;
[[ ! -e &quot;$NGINX_SITES_ENABLED/$SELECTED_SITE&quot; ]] &amp;&amp;
    ngx_error &quot;Site does not appear to be enabled.&quot;

rm -f &quot;$NGINX_SITES_ENABLED/$SELECTED_SITE&quot;
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 &quot;$1&quot; in
    not_enabled) sites=$(comm -13 &lt;(printf &quot;%s\n&quot; $se) &lt;(printf &quot;%s\n&quot; $sa));;
    is_enabled) sites=$(comm -12 &lt;(printf &quot;%s\n&quot; $se) &lt;(printf &quot;%s\n&quot; $sa));;
esac

ngx_prompt &quot;$sites&quot;

}

ngx_prompt() { sites=($1) i=0

echo &quot;SELECT A WEBSITE:&quot;
for site in ${sites[@]}; do
    echo -e &quot;$i:\t${sites[$i]}&quot;
    ((i++))
done

read -p &quot;Enter number for website: &quot; i
SELECTED_SITE=&quot;${sites[$i]}&quot;

}

ngx_sites() { case "$1" in available) dir="$NGINX_SITES_AVAILABLE";; enabled) dir="$NGINX_SITES_ENABLED";; esac

for file in $dir/*; do
    echo -e &quot;\t${file#*$dir/}&quot;
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
HXH
  • 3,185
37

There's third-party nginx_ensite and nginx_dissite available.

Can be installed as quick as

git clone https://github.com/perusio/nginx_ensite.git
cd nginx_ensite
sudo make install

(see the repo, though)

Example usage: nginx_ensite example.org (see more at online man page).

YakovL
  • 117
Michael Hampton
  • 252,907
5

Link with full path:

ln -s /etc/nginx/sites-available/site_1.conf /etc/nginx/sites-enabled/
service nginx reload
5

NGINX

If you're using one of the official upstream packages of nginx from http://nginx.org/packages/, the best way is to navigate to the /etc/nginx/conf.d directory, and rename the affected file from having a .conf suffix to having a different one to disable the site:

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

Or the opposite to enable it:

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

This is because the default /etc/nginx/nginx.conf has the following include directive:

http {
    …
    include /etc/nginx/conf.d/*.conf;
}

Debian/Ubuntu

However, if you're using a Debian/Ubuntu derivative, then in addition to conf.d, you may also have the evil non-standard sites-available and sites-enabled directories, some files under which may be sloppily included without regard to their extension:

http {
    …
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

As such, in Debian/Ubuntu, you might first have to figure out where the site config is located.

  • You could use the following command to get a list of all available sites by running find(1) to find all regular files matching the given mask:

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

  • You could use the following command to get a list of all enabled sites:

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

Then to disable/enable sites on Debian/Ubuntu:

  • To disable a site: if the config is in conf.d, just rename the file to no longer have a .conf suffix; or if in sites-enabled, move it out of sites-enabled.

  • To enable a site, the best way would be to move it to /etc/nginx/conf.d, and rename to have a .conf suffix.

P.S. Why do I think Debian's include /etc/nginx/sites-enabled/*; is evil? Try editing a couple of files in that directory, and have your emacs create the backup files (with the ~ suffix), then ask me again.

cnst
  • 14,646
2

Compact ngensite/ngdisite shell scripts

After reading the replies here while setting up a new Debian server, then going off to do some research, I made a couple of readable shell scripts to help me enable/disable sites on a server with at least some security (root disabled, non-default ports, etc.). Once the files are executable with chmod +x anyone with root access can call these scripts from anywhere as /usr/local/bin/ is in the Debian PATH by default.

These work (the way I've used for years) by creating and deleting aliases in sites_enabled so don't touch the contents of virtual hosts files in sites_available.

Enable site

in: /usr/local/bin/ngensite:

#!/bin/sh
read -p "Website to enable: " site;
ln -s /etc/nginx/sites-available/"$site" /etc/nginx/sites-enabled/
echo "$site enabled. Now run 'service nginx reload'"

Then from the command-line: sudo ngensite

(The prompt will need the exact nginx virtualhosts config file).

Disable site

in: /usr/local/bin/ngdissite:

#!/bin/sh
read -p "Website to disable: " site;
rm /etc/nginx/sites-enabled/"$site"
echo "$site disabled. Now run 'service nginx reload'"

Then from the command-line: sudo ngdissite

(The prompt requires the exact name of the nginx virtualhosts config file).


If you spot any issues in these (they're very simple but do the job for me) please comment.

1

I know it's not technically correct, but I just mv the config in sites-available to sites-enabled. It works fine, don't live a complicated life.

Jonathan
  • 322
0

To enable:

ln -s /etc/nginx/sites-available/site /etc/nginx/sites-enabled/

To disable:

unlink /etc/nginx/sites-enabled/conf
assayag.org
  • 119
  • 1
0

I want to submit my script written in Bash to accommodate this feature. It's called nginxsitehttps://github.com/L1so/nginxsite/. For more info check the github link.


Enabling

To activate a site, replace (YOUR SITE) with your actual site domain (located in /etc/nginx/sites-available/).

sudo ngxensite (YOUR SITE)

Disabling

To deactivate a site, replace (YOUR SITE) with your actual site domain (located in /etc/nginx/sites-available/).

sudo ngxdissite (YOUR SITE)

Create server block

To create a site, replace (YOUR DOMAIN) with your actual domain.

sudo ngxcreate (YOUR DOMAIN)

Running ngxcreate without any argument will give you a prompt to enter desired site name, if you don't include tld, the script will give you .com domain.

$ sudo ngxcreate
Please Enter a Domain Name (default TLD is .com):
examplesite

Will save a new file named /etc/nginx/sites-available/examplesite.com

Delete server block

To delete a site, replace (YOUR DOMAIN) with your actual site domain.

sudo ngxdelete (YOUR DOMAIN)

Example given below.

root@mutiny:~# ngxdelete bar.co 
Removing --> /etc/nginx/sites-available/bar.co
Liso
  • 65
0

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.

Brandon
  • 128