Skip to content

Commit

Permalink
Refactor NGINX/PHP-FPM configuration (#115)
Browse files Browse the repository at this point in the history
* Refactor NGINX/PHP-FPM configuration

* Use default ingress port
  • Loading branch information
frenck authored Jan 24, 2021
1 parent db3bbe6 commit f720ccb
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 74 deletions.
3 changes: 0 additions & 3 deletions grocy/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
"url": "https://github.com/hassio-addons/addon-grocy",
"webui": "[PROTO:ssl]://[HOST]:[PORT:80]",
"ingress": true,
"ingress_port": 1337,
"panel_icon": "mdi:cart",
"startup": "system",
"arch": ["aarch64", "amd64", "armhf", "armv7", "i386"],
"hassio_api": true,
"map": ["ssl"],
"ports": {
"80/tcp": null
Expand Down
44 changes: 16 additions & 28 deletions grocy/rootfs/etc/cont-init.d/nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,22 @@
# Home Assistant Community Add-on: Grocy
# Configures NGINX for use with Grocy
# ==============================================================================
declare certfile
declare dns_host
declare ingress_entry
declare ingress_interface
declare keyfile
declare port

port=$(bashio::addon.port 80)
if bashio::var.has_value "${port}"; then
bashio::config.require.ssl

if bashio::config.true 'ssl'; then
certfile=$(bashio::config 'certfile')
keyfile=$(bashio::config 'keyfile')
# Generate Ingress PHP-FPM configuration
bashio::var.json \
interface "$(bashio::addon.ip_address)" \
| tempio \
-template /etc/nginx/templates/ingress.gtpl \
-out /etc/nginx/servers/ingress.conf

mv /etc/nginx/servers/direct-ssl.disabled /etc/nginx/servers/direct.conf
sed -i "s#%%certfile%%#${certfile}#g" /etc/nginx/servers/direct.conf
sed -i "s#%%keyfile%%#${keyfile}#g" /etc/nginx/servers/direct.conf
else
mv /etc/nginx/servers/direct.disabled /etc/nginx/servers/direct.conf
fi
# Generate direct access configuration, if enabled.
if bashio::var.has_value "$(bashio::addon.port 80)"; then
bashio::config.require.ssl
bashio::var.json \
certfile "$(bashio::config 'certfile')" \
keyfile "$(bashio::config 'keyfile')" \
ssl "^$(bashio::config 'ssl')" \
| tempio \
-template /etc/nginx/templates/direct.gtpl \
-out /etc/nginx/servers/direct.conf
fi

ingress_interface=$(bashio::addon.ip_address)
sed -i "s/%%interface%%/${ingress_interface}/g" /etc/nginx/servers/ingress.conf

ingress_entry=$(bashio::addon.ingress_entry)
sed -i "s#%%ingress_entry%%#${ingress_entry}#g" /etc/php7/php-fpm.d/ingress.conf

dns_host=$(bashio::dns.host)
sed -i "s/%%dns_host%%/${dns_host}/g" /etc/nginx/includes/resolver.conf
24 changes: 24 additions & 0 deletions grocy/rootfs/etc/cont-init.d/php-fpm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Home Assistant Community Add-on: Grocy
# Configures PHP-FPM for use with Grocy
# ==============================================================================

# Generate Ingress configuration
bashio::var.json \
name "ingress" \
port "^9002" \
base "$(bashio::addon.ingress_entry)" \
| tempio \
-template /etc/php7/templates/php-fpm.gtpl \
-out /etc/php7/php-fpm.d/ingress.conf

# Generate direct access configuration, if enabled.
if bashio::var.has_value "$(bashio::addon.port 80)"; then
bashio::var.json \
name "www" \
port "^9001" \
| tempio \
-template /etc/php7/templates/php-fpm.gtpl \
-out /etc/php7/php-fpm.d/www.conf
fi
1 change: 0 additions & 1 deletion grocy/rootfs/etc/nginx/includes/resolver.conf

This file was deleted.

9 changes: 0 additions & 9 deletions grocy/rootfs/etc/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ pcre_jit on;
# Write error log to the add-on log.
error_log /proc/1/fd/1 error;

# Load allowed environment vars
env SUPERVISOR_TOKEN;
env DISABLE_HA_AUTHENTICATION;

# Load dynamic modules.
include /etc/nginx/modules/*.conf;

# Max num of simultaneous connections by a worker process.
events {
worker_connections 512;
Expand Down Expand Up @@ -50,7 +43,5 @@ http {
'' close;
}

include /etc/nginx/includes/resolver.conf;

include /etc/nginx/servers/*.conf;
}
1 change: 1 addition & 0 deletions grocy/rootfs/etc/nginx/servers/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Without requirements or design, programming is the art of adding bugs to an empty text file. (Louis Srygley)
14 changes: 0 additions & 14 deletions grocy/rootfs/etc/nginx/servers/direct.disabled

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
server {
{{ if not .ssl }}
listen 80 default_server;
{{ else }}
listen 80 default_server ssl http2;
{{ end }}

include /etc/nginx/includes/ssl_params.conf;
include /etc/nginx/includes/server_params.conf;

ssl_certificate /ssl/%%certfile%%;
ssl_certificate_key /ssl/%%keyfile%%;
{{ if .ssl }}
include /etc/nginx/includes/ssl_params.conf;

include /etc/nginx/includes/server_params.conf;
ssl_certificate /ssl/{{ .certfile }};
ssl_certificate_key /ssl/{{ .keyfile }};
{{ end }}

location ~ .php$ {
fastcgi_pass 127.0.0.1:9001;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server {
listen %%interface%%:1337 default_server;
listen {{ .interface }}:8099 default_server;

include /etc/nginx/includes/server_params.conf;

Expand Down
1 change: 1 addition & 0 deletions grocy/rootfs/etc/php7/php-fpm.d/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Without requirements or design, programming is the art of adding bugs to an empty text file. (Louis Srygley)
11 changes: 0 additions & 11 deletions grocy/rootfs/etc/php7/php-fpm.d/www.conf

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
[ingress]
[{{ .name }}]
user = nginx
group = nginx
listen = 127.0.0.1:9002
listen = 127.0.0.1:{{ .port }}
pm = dynamic
pm.max_children = 10
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 1024
clear_env = no
env[GROCY_BASE_URL] = '%%ingress_entry%%'
{{ if .base }}
env[GROCY_BASE_URL] = '{{ .base }}'
{{ end }}

0 comments on commit f720ccb

Please sign in to comment.