Easily create, setup, enable and disable nginx VHOSTS

So one thing I hate as a developer is re-configuring everything a hundred times every time I decide I want to launch a new side project or test app.

So to make life easy I developed a bash script that does everything for me.

What it does:

  1. Create nginx sites using a basic template, this one’s very common to laravel applications since that’s my default setup, but I’ve found it usually works for most things, if not you may need to edit a little.

    The create method does the following:

    1. First it checks to make sure $root is set, $root means you’ve provided a directory in the terminal.
      eg: $ ng create site.test /var/www/site.test/public
    2. Then it checks if the file already exists in $sitesAvailable/$domain.
    3. Then it checks to make sure the root directory exists if it doesn’t you probably have a typo and it’ll let you know and exit.
    4. Then it outputs the nginx conf to the new vhost file in /etc/nginx/sites-available.
    5. Then if $useHosts is enabled it’ll also update /etc/hosts
    6. Then it sets the chown properly for the folder so that it works with nginx.
    7. Then it links the file in sites-enabled.
    8. Then it generates a wildcard self-signed ssl for all sites in the sites-available folder. This is reused for convenience, but can be disabled by setting $useSSL toggle to false.
    9. Then we restart nginx and god willing we’re good to go and you can visit yourdomain.test.
  2. Delete. This simply undoes everything that create did, it does NOT touch your files though, so if you want your project files deleted too you’ll need to $ rm -rf /path/to/project.
  3. Enable. This will enable a domain in sites-available (setup a symbolic link). If no domain is provided it’ll enable ALL sites in sites-available.
  4. Disable. This will disable a domain in sites-available (unlink the symbolic link). You can choose to disable all or a single vhost.

    All: $ ng disable all
    Single: $ng disable somesite.test

I’m thinking eventually adding mysql/postgres support to create a dbuser locally as well, personally virtualbox/docker eat a lot of memory for me, and I’ve been working and had my pc freeze, being able to just use nginx/mysql natively for dev is just a personal preference.

It of course depends on the size and scope of the project, if I’m working on an enterprise level app with lots of moving parts like elasticsearch, beanstalkd, supervisor, crons, etc… I’ll definitely go with docker most likely simply so that I can test all parts as if it’s a production environment. For simple side project ideas/scratching an itch, this config works fine.

Hope this helps, let me know if you have thoughts/suggestions on the script.

You can download from here or view the entire source below: