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.
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:So basically to list all available virtualhosts, you can run the following command:
To activate one of them, run the following command:
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
Just create this script
/usr/bin/nginx_modsite
and make it executable.How it works:
To list all the sites
To enable site "test_website"
To disable site "test_website"
There's third-party
nginx_ensite
andnginx_dissite
available.Can be installed as quick as
(see the repo, though)
Example usage:
nginx_ensite example.org
(see more at online man page).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 followinginclude
directive:Debian/Ubuntu
However, if you're using a Debian/Ubuntu derivative, then in addition to
conf.d
, you may also have the evil non-standardsites-available
andsites-enabled
directories, some files under which may be sloppily included without regard to their extension: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 insites-enabled
, move it out ofsites-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 youremacs
create the backup files (with the~
suffix), then ask me again.Link with full path:
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 insites_available
.Enable site
in:
/usr/local/bin/ngensite
:Then from the command-line:
sudo ngensite
(The prompt will need the exact nginx virtualhosts config file).
Disable site
in:
/usr/local/bin/ngdissite
: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.
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.
I know it's not technically correct, but I just
mv
the config insites-available
tosites-enabled
. It works fine, don't live a complicated life.To enable:
To disable:
I want to submit my script written in Bash to accommodate this feature. It's called
nginxsite
→ https://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/
).Disabling
To deactivate a site, replace
(YOUR SITE)
with your actual site domain (located in/etc/nginx/sites-available/
).Create server block
To create a site, replace
(YOUR DOMAIN)
with your actual 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.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.Example given below.