nginx-vhost-from-folder

Example:

# Static site, setting the 'default_server' property for this vhost, using a custom document root.
- nginx-vhost-from-folder:
    default_server: true
    document_root: /var/www/vhost_1/html

Description

Create a Nginx server block configuration file for hosting static html pages.

Create a file '/etc/nginx/sites-enabled/[hostname].[http(s)].conf' and include configuration for a Nginx server block that uses the provided document root (default: '/var/www/html') to host a static web page.

If 'use_https' is set to true, an automatic redirect from port http://...:80 to https:/...:443 is set up (certificates need to be created/put in place seperately). This uses the nginx-server-block-file frecklet to create the actual configuration file. For now, not all of the possible configuration options are forwarded, but those will be added as a need arises.

Resources

Variables

Name Type Default Description

default_server

boolean False

One Nginx instance can only have one 'default' server, this is used for everything that hasn't been caught otherwise.

document_root

string /var/www/html

The document root.

gzip_enabled

boolean True

Whether to enable gzip.

hostname

string _

The domain name the webserver should listen on.

index

list ['index.html', 'index.htm']

The index file name(s).

server_admin

string --

The server admin email.

server_names

list --

The server names.

use_https

boolean False

Whether to use https.

All http traffic will be redirected to https.

Examples

Example 1

Static site, setting the 'default_server' property for this vhost, using a custom document root.

Code
- nginx-vhost-from-folder:
    default_server: true
    document_root: /var/www/vhost_1/html

Example 2

Simple static site, no https, using the default '/var/www/html' folder as document root.

Code
- nginx-vhost-from-folder

Code

doc:
  short_help: Create a Nginx server block configuration file for a static site.
  help: |
    Create a Nginx server block configuration file for hosting static html pages.

    Create a file '``/etc/nginx/sites-enabled/[hostname].[http(s)].conf``' and include configuration for a Nginx server block
    that uses the provided document root (default: '/var/www/html') to host a static web page.

    If 'use_https' is set to true, an automatic redirect from port ``http://...:80`` to ``https:/...:443`` is set up
    (certificates need to be created/put in place seperately). This uses the
    [``nginx-server-block-file``](https://freckles.io/frecklets/default/misc/nginx-server-block-file) *frecklet* to
    create the actual configuration file. For now, not all of the possible
    configuration options are forwarded, but those will be added as a need arises.

  examples:
  - title: Static site, setting the 'default_server' property for this vhost, using
      a custom document root.
    vars:
      default_server: true
      document_root: /var/www/vhost_1/html
  - title: Simple static site, no https, using the default '/var/www/html' folder
      as document root.
    vars: {}

  references:
    tutorial blog post: https://medium.com/@jgefroh/a-guide-to-using-nginx-for-static-websites-d96a9d034940

args:
  _import: nginx-server-block-file
  hostname:
    doc:
      short_help: The domain name the webserver should listen on.
    type: string
    required: false
    default: _
  use_https:
    doc:
      short_help: Whether to use https (and request a letsencrypt certificate).
    type: boolean
    required: false
    default: false
  document_root:
    doc:
      short_help: The document root.
    type: string
    required: false
    default: /var/www/html
#  webserver_user:
#    doc:
#      short_help: The webserver user.
#    type: string
#    required: false
  gzip_enabled:
    doc:
      short_help: Whether to enable gzip.
    type: boolean
    required: false
    default: true
    cli:
      param_decls:
      - --gzip-enabled/--gzip-disabled
  index:
    doc:
      short_help: The index file name(s).
    type: list
    schema:
      type: string
    default:
    - index.html
    - index.htm
    required: false
    cli:
      metavar: FILENAME
      show_default: true
  server_admin:
    doc:
      short_help: The server admin email.
    type: string
    required: false
    cli:
      metavar: EMAIL

frecklets:
- nginx-server-block-file:
    path: "/etc/nginx/sites-enabled/{%:: if hostname == '_' ::%}1_default{%:: else\
      \ ::%}{{:: hostname ::}}.{{:: 'https' if use_https else 'http' ::}}{%:: endif\
      \ ::%}.conf"
    owner: root
    document_root: '{{:: document_root ::}}'
    server_admin: '{{:: server_admin ::}}'
    server_names: '{{:: server_names ::}}'
    gzip_enabled: '{{:: gzip_enabled ::}}'
    use_https: '{{:: use_https ::}}'
    index: '{{:: index ::}}'
    default_server: '{{:: default_server ::}}'
    location_blocks:
    - location_match: /
      properties: |
- init-service-reloaded:
    name: nginx
frecklecute nginx-vhost-from-folder --help

Usage: frecklecute nginx-vhost-from-folder [OPTIONS]

  Create a Nginx server block configuration file for hosting static html
  pages.

  Create a file '``/etc/nginx/sites-enabled/[hostname].[http(s)].conf``' and
  include configuration for a Nginx server block that uses the provided
  document root (default: '/var/www/html') to host a static web page.

  If 'use_https' is set to true, an automatic redirect from port
  ``http://...:80`` to ``https:/...:443`` is set up (certificates need to be
  created/put in place seperately). This uses the [``nginx-server-block-
  file``](https://freckles.io/frecklets/default/misc/nginx-server-block-
  file) *frecklet* to create the actual configuration file. For now, not all
  of the possible configuration options are forwarded, but those will be
  added as a need arises.

Options:
  --default-server / --no-default-server
                                  Whether this server is the 'default'
                                  (catchall) server.
  --document-root PATH            The document root.
  --gzip-enabled / --gzip-disabled
                                  Whether to enable gzip.
  --hostname HOSTNAME             The domain name the webserver should listen
                                  on.
  --index FILENAME                The index file name(s).
  --server-admin EMAIL            The server admin email.
  -n, --server-name SERVER_NAMES  The server names.
  --use-https / --no-use-https    Whether to use https (and request a
                                  letsencrypt certificate).
  --help                          Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.nginx_vhost_from_folder.NginxVhostFromFolder
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class NginxVhostFromFolder(AutoPycklet):
    """Create a Nginx server block configuration file for hosting static html pages.

     Create a file '``/etc/nginx/sites-enabled/[hostname].[http(s)].conf``' and include configuration for a Nginx server block
     that uses the provided document root (default: '/var/www/html') to host a static web page.

     If 'use_https' is set to true, an automatic redirect from port ``http://...:80`` to ``https:/...:443`` is set up
     (certificates need to be created/put in place seperately). This uses the
     [``nginx-server-block-file``](https://freckles.io/frecklets/default/misc/nginx-server-block-file) *frecklet* to
     create the actual configuration file. For now, not all of the possible
     configuration options are forwarded, but those will be added as a need arises.

       Args:
         default_server: Whether this server is the 'default' (catchall) server.
         document_root: The document root.
         gzip_enabled: Whether to enable gzip.
         hostname: The domain name the webserver should listen on.
         index: The index file name(s).
         server_admin: The server admin email.
         server_names: The server names.
         use_https: Whether to use https (and request a letsencrypt certificate).

    """

    FRECKLET_ID = "nginx-vhost-from-folder"

    default_server: bool = None
    document_root: str = None
    gzip_enabled: bool = None
    hostname: str = None
    index: List = None
    server_admin: str = None
    server_names: List = None
    use_https: bool = None


    def __post_init__(self):
        super(NginxVhostFromFolder, self).__init__(var_names=["default_server", "document_root", "gzip_enabled", "hostname", "index", "server_admin", "server_names", "use_https"])


frecklet_class = NginxVhostFromFolder
# -*- coding: utf-8 -*-


#
# module path: pycklets.nginx_vhost_from_folder.NginxVhostFromFolder
#


from pyckles import AutoPycklet

class NginxVhostFromFolder(AutoPycklet):
    """Create a Nginx server block configuration file for hosting static html pages.

     Create a file '``/etc/nginx/sites-enabled/[hostname].[http(s)].conf``' and include configuration for a Nginx server block
     that uses the provided document root (default: '/var/www/html') to host a static web page.

     If 'use_https' is set to true, an automatic redirect from port ``http://...:80`` to ``https:/...:443`` is set up
     (certificates need to be created/put in place seperately). This uses the
     [``nginx-server-block-file``](https://freckles.io/frecklets/default/misc/nginx-server-block-file) *frecklet* to
     create the actual configuration file. For now, not all of the possible
     configuration options are forwarded, but those will be added as a need arises.

       Args:
         default_server: Whether this server is the 'default' (catchall) server.
         document_root: The document root.
         gzip_enabled: Whether to enable gzip.
         hostname: The domain name the webserver should listen on.
         index: The index file name(s).
         server_admin: The server admin email.
         server_names: The server names.
         use_https: Whether to use https (and request a letsencrypt certificate).

    """

    FRECKLET_ID = "nginx-vhost-from-folder"

    def __init__(self, default_server=None, document_root="/var/www/html", gzip_enabled=True, hostname="_", index=['index.html', 'index.htm'], server_admin=None, server_names=None, use_https=None):

        super(NginxVhostFromFolder, self).__init__(var_names=["default_server", "document_root", "gzip_enabled", "hostname", "index", "server_admin", "server_names", "use_https"])
        self._default_server = default_server
        self._document_root = document_root
        self._gzip_enabled = gzip_enabled
        self._hostname = hostname
        self._index = index
        self._server_admin = server_admin
        self._server_names = server_names
        self._use_https = use_https

    @property
    def default_server(self):
        return self._default_server

    @default_server.setter
    def default_server(self, default_server):
        self._default_server = default_server

    @property
    def document_root(self):
        return self._document_root

    @document_root.setter
    def document_root(self, document_root):
        self._document_root = document_root

    @property
    def gzip_enabled(self):
        return self._gzip_enabled

    @gzip_enabled.setter
    def gzip_enabled(self, gzip_enabled):
        self._gzip_enabled = gzip_enabled

    @property
    def hostname(self):
        return self._hostname

    @hostname.setter
    def hostname(self, hostname):
        self._hostname = hostname

    @property
    def index(self):
        return self._index

    @index.setter
    def index(self, index):
        self._index = index

    @property
    def server_admin(self):
        return self._server_admin

    @server_admin.setter
    def server_admin(self, server_admin):
        self._server_admin = server_admin

    @property
    def server_names(self):
        return self._server_names

    @server_names.setter
    def server_names(self, server_names):
        self._server_names = server_names

    @property
    def use_https(self):
        return self._use_https

    @use_https.setter
    def use_https(self, use_https):
        self._use_https = use_https



frecklet_class = NginxVhostFromFolder