Giter Club home page Giter Club logo

nginx_ensite's Introduction

nginx_ensite and nginx_dissite for quick virtual host enabling and disabling

Description

This is a shell (Bash) script that replicates for nginx the Debian a2ensite and a2dissite for enabling and disabling sites as virtual hosts in Apache 2.2/2.4.

The original a2ensite and a2dissite is written in Perl. a2dissite is a symbolic link to a2ensite. Here I followed the same approach, i.e., nginx_dissite is a symbolic link to nginx_ensite.

The script allows for arbitrary paths for the nginx configuration directories. This is particularly useful not only to those on Mac, but also in a microservice approach where each service has it's own vhost configuration, for example.

Th deafult startup program is service. Feel free to pass another program. For example using nginx to send a SIGHUP to reload the configuration you just pass the option -s nginx to the script.

Installation

Automatic

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

Manual

Just drop the script and the symbolic link in /usr/sbin or other location appropriate for your system. Meaning: cp nginx_* /usr/sbin. That's it you're done.

Note that the script assumes a specific file system topology for your nginx configuration. Here's the rundown:

  1. All virtual hosts configuration files are by default under /etc/nginx/sites-available. For example the virtual host foobar is configured through a file in /etc/nginx/sites-available.

  2. After running the script with foobar as argument: nginx_ensite foobar. A symbolic link /etc/nginx/sites-enabled/foobar -> /etc/nginx/sites-available/foobar is created. Note that if the /etc/nginx/sites-enabled directory doesn't exist the script creates it.

  3. The script invokes nginx -t to test if the configuration is correct. If the test fails no symbolic link is created and an error is signaled.

  4. If everything is correct now just reload nginx, in Debian based systems that means invoking service nginx reload (default startup program name is service).

  5. Now point the browser to the newly configured host and everything should work properly assuming your configuration is sensible.

  6. To disable the site just run nginx_dissite foobar. Reload nginx to update the running environment.

Requirements

The script is written in Bash and uses what I believe to be some Bash specific idioms. I never tested it in other shells. You're welcomed to try it in any other shell. Please do tell me how it went.

It requires also awk. The original awk (usually called BWK awk) from Bell Labs will do if you don't have gawk (Gnu awk). IN OS X and *BSD the former is the default awk. The script should work in *BSD, OS X and GNU/Linux.

Command Completion

There's a Bash script for automatic completion of sites to be enabled and disabled located in the bash_completion.d directory.

To make use of it you should:

  1. Source the script to Bash by issuing either source nginx-ensite or . nginx-ensite.

  2. Now when you invoke nginx_ensite the sites under /etc/nginx/sites-available will appear as hypothesis for completion. For nginx_dissite you get all the sites listed in /etc/nginx/sites-enabled as possible completions.

  3. To get the completion script to be sourced upon login please copy it to /etc/bash_completion.d/ or whatever location your shell environment uses to place all the completion scripts. /etc/bash_completion.d/ is the location in Debian (hence also in Ubuntu) of Bash completion scripts.

Manual pages

Two UNIX manual pages are included in the man directory. They should be copied to a proper directory in your system. Something along the lines of /usr/share/man/man8 or /usr/local/share/man/man8.

Here's an online version of the manpage.

Security & Trust

The script is signed with my GPG key. Just do gpg --keyserver keys.gnupg.net --recv-keys 4D722217. Verify by issuing gpg --verify nginx_ensite.sig.

Acknowledgments

Thanks to the many people that have contributed to this script. You're the stuff Free Software is made of.

nginx_ensite's People

Contributors

aseques avatar cdzombak avatar dingens avatar jc00ke avatar luislobo avatar mprenditore avatar mtreilly avatar nothing4you avatar perusio avatar resessh avatar samatary avatar sunknudsen avatar trulycool avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nginx_ensite's Issues

Use -follow on "find" at bash_completion script to complete for nginx_dissite.

Your bash completion script has a bug. It does not suggest any completion for nginx_dissite because "find -type f" shows only regular files, but sites_enabled has only symlinks.

Use -follow as bugfix:

--- nginx-ensite 2013-03-20 13:58:39.000000000 +0000
+++ /etc/bash_completion.d/nginx-ensite 2013-03-20 13:58:11.000000000 +0000
@@ -32,7 +32,7 @@

Handling of both enabled and available sites.

_nginx_sites() {
# Get the available or enabled sites for nginx.

  • COMPREPLY=( $( compgen -W '$( command find /etc/nginx/$1 -type f -printf "%P " 2>/dev/null \
  • COMPREPLY=( $( compgen -W '$( command find /etc/nginx/$1 -type f -follow -printf "%P " 2>/dev/null
    | sed 's/[.]conf$//' )' -- $cur ) )
    }

P.S. Thanks for useful scripts.

nginx_dissite not calling ensite correctly.

Hello,

I have installed these helpers on my Ubuntu 12.04 box. I have nginx_ensite and the bash completion working, but when I call the following:

nginx_dissite example.com

I receiving this message:

nginx_ensite <site name>

It seems as though the dissite script is not passing the arguments to the new ensite script.

Regards,
Andrew

[mispost] [Nginx] repo update to reflect Debian 8 (Jessie)?

[ Please excuse that I've posted this in an incorrect thread. If there is a better channel to raise questions on your repos, please advise. ]

Hello Perusio -

Will your repo, specifically the one for your custom nginx build, be updated to reflect how to install on Jessie? I'd like to experiment w/ Ansible & know I'll be needing your deb. Mission critical goodness, if you will. ;-)

TIA,
AMS

List sites by default

If site not specified
nginx_ensite must list sites at sites-available and
nginx_dissite must list sites at sites-enabled

ZSH completion

I think it might be a good idea to enhance the bash completion with some ZSH completion file.

Here is what have been already done in oh-my-zsh : https://github.com/miklos-martin/oh-my-zsh-plugins/blob/master/nginx/

I'm a fervent user of nginx_{en,dis}site, I'd appreciate ! (If I have time and if I learn some ZSH-scripts basics, I'll try to translate your script, but I don't feel confident about it.)

[OpenBSD] expr: syntax error

nginx_dissite example.com

expr: syntax error
Site example.com doesn't exist.
+ SCRIPTNAME=nginx_dissite
++ id -u
+ '[' 0 -eq 0 ']'
+ IS_ROOT=1
++ command -v nginx
+ NGINX=/usr/local/sbin/nginx
+ NGINX_CONF_DIR=/etc/nginx
++ get_startup_program_name
++ local value=
++ [[ '' =~ [[:alnum:]/-]]+nginx$ ]]
++ '[' -z '' ']'
++ value='service nginx reload'
++ echo 'service nginx reload'
+ STARTUP_PROGRAM_NAME='service nginx reload'
++ echo nginx_dissite
++ awk '$0 ~ /dissite/ {print "DISABLE"} $0 ~ /ensite/ {print "ENABLE"} $0 !~ /(dis|en)site/ {print "UNKNOWN"}'
+ ACTION=DISABLE
+ '[' DISABLE == UNKNOWN ']'
+ '[' 1 -lt 1 -o 1 -gt 5 ']'
+ getopts c:s: OPT
+ shift 0
+ OPTIND=1
+ AVAILABLE_SITES_PATH=/etc/nginx/sites-available
+ ENABLED_SITES_PATH=/etc/nginx/sites-enabled
+ '[' 1 -ne 1 ']'
++ make_relative_path example.com sites-available
++++ expr length ''
expr: syntax error
+++ eval echo '{0..}'
++++ echo '{0..}'
++ printf ../%.0s%s/%s '{0..}' sites-available example.com
+ SITE_AVAILABLE=../sites-available/example.com
+ '[' example.com == default ']'
+ SITE_ENABLED=/etc/nginx/sites-enabled/example.com
+ '[' -d /etc/nginx/sites-enabled ']'
+ case $ACTION in
+ '[' example.com = default ']'
+ '[' -h /etc/nginx/sites-enabled/example.com ']'
+ echo 'Site example.com doesn'\''t exist.'
Site example.com doesn't exist.
+ exit 7

Note: Symlinks seem to still be created and deleted properly.

shorter command names? (ngxensite / ngxdissite)

For example apache a2ensite is much shorter than nginx_ensite.
When i tried to change it manually (mv nginx_ensite ngxensite) i realized dissite is just a symlink and it's all hardcoded into one script.

So how about shorter command names? apache commands is really good because they are short, i see no reason to make long nginx_ensite, because this script mimics apache version (and apache commands are not intuitive in names), so it should be short as well, and not neccessary intuitive.

Or as compatibility support both variants.

Signature server

Thanks Antonio for the script! Just a note ... the keys.gpg.net doesn't seem to exist anymore. Found your public key on keys.gnupg.net but the nginx_ensite.sig gives a bad signature.

Does your signature need to be updated or I should not trust the downloaded zip file from github? =)

Cheers!
JL

[mispost] [Nginx.repo] Unable to build from source on Debian 8 (Jessie)

[ Please excuse that I've posted this in an incorrect thread. If there is a better channel to raise questions on your repos, please advise. ]

Hello Perusio -

I'm trying to build on a fresh install of Debian 8/Jessie. I don't understand why it's failing. Have I a paths problem?

StupidNoobie

[snip]
checking for LuaJIT library in -lluajit-5.1 and /usr/include/luajit-2.0 (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... not found
checking for LuaJIT library in -lluajit-5.1 and /usr/include/luajit-2.0 (specified by the LUAJIT_LIB and LUAJIT_INC env) ... not found
./configure: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as -lluajit-5.1 and LUAJIT_INC (path for lua.h) /usr/include/luajit-2.0, but we cannot find LuaJIT there.
debian/rules:66: recipe for target 'config.arch' failed
make[1]: *** [config.arch] Error 1
make[1]: Leaving directory '/root/tmp/nginx-1.9.12'
debian/rules:58: recipe for target 'binary' failed
make: *** [binary] Error 2
root@host:/tmp/nginx-1.9.12:|# dpkg -l | grep "luajit"
ii libluajit-5.1-common 2.0.3+dfsg-3 all Just in time compiler for Lua - common files
ii luajit 2.0.3+dfsg-3 amd64 Just in time compiler for Lua programming language version 5.1
root@host:
/tmp/nginx-1.9.12:|# which luajit
/usr/bin/luajit
Try 'uname --help' for more information.
root@host:~/tmp/nginx-1.9.12:|# uname -srv
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2+deb8u3 (2016-07-02)
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.