wmdeit-cf-wmdelib/apache.cf

590 lines
14 KiB
CFEngine3
Raw Normal View History

2023-09-24 16:15:57 +00:00
#
# Install and configure Apache with SSL and certbot
#
# Use:
# usebundle => install_apache;
# usebundle => apache_vhost(site_data);
#
#
bundle agent apache
{
2023-10-24 16:34:13 +00:00
classes:
# "apache enable_php";
2023-09-24 16:15:57 +00:00
vars:
2023-11-06 20:40:47 +00:00
"default_php_handler" string => "application/x-httpd-php", unless => isvariable( $(this.promiser));
2023-10-21 22:09:18 +00:00
"admin_email" string => "[]",unless => isvariable( $(this.promiser) ) ;
2023-09-24 16:15:57 +00:00
2023-10-24 16:34:13 +00:00
apache_php_enable::
"php_mod" string => '{m: "$(php.apache_m)",f:"$(php.apache_f)"},';
"service_deps" slist => {
"apache_pkgs_installed",
"apache_php_pkgs_installed",
"apache_www_dir_created",
2023-10-24 21:16:50 +00:00
"apache_dummy_conf_created",
2023-10-24 16:34:13 +00:00
"apache_main_cfg_created"
};
"cfg_deps" slist => { "apache_pkgs_installed","apache_php_installed" };
!apache_php_enable::
"php_mod" string => "";
"service_deps" slist => {
"apache_pkgs_installed",
"apache_www_dir_created",
2023-10-24 21:16:50 +00:00
"apache_dummy_conf_created",
2023-10-24 16:34:13 +00:00
"apache_main_cfg_created"
};
"cfg_deps" slist => { "apache_pkgs_installed" };
2023-11-04 21:07:30 +00:00
any::
2023-10-24 21:16:50 +00:00
"log_dir_mode" string => "750";
2023-10-24 16:34:13 +00:00
2023-09-24 16:15:57 +00:00
freebsd::
"pkgs" slist => {
"apache24",
2023-11-16 20:21:59 +00:00
"mod_dav_svn",
2023-09-24 16:15:57 +00:00
};
"log_dir" string =>"/var/log/httpd";
2023-10-21 22:09:18 +00:00
"log_dir_owner" string => "root";
"log_dir_group" string => "wheel";
2023-09-24 16:15:57 +00:00
"service_name" string => "apache24";
2023-11-13 00:17:32 +00:00
"service_cfg_name" string => "apache24";
2023-09-24 16:15:57 +00:00
"www_user" string => "www";
"www_group" string => "www";
"mime_types" string => "/usr/local/etc/apache24/mime.types";
"service_name" string => "apache24";
"restart_cmd" string => "/usr/sbin/service $(service_name) restart";
"etc_dir" string => "/usr/local/etc";
"cfg_dir" string => "$(etc_dir)/apache24";
"main_cfg" string => "$(cfg_dir)/httpd.conf";
"libexec_dir" string => "libexec/apache24";
"www_dir" string => "/usr/local/www",unless => isvariable( $(this.promiser) ) ;
2023-09-24 16:15:57 +00:00
"default_html_dir" string => "$(www_dir)/apache24";
"server_root" string => "/usr/local";
"modules" data => '[
2023-10-24 16:34:13 +00:00
$(php_mod)
2023-09-24 16:15:57 +00:00
{m: "autoindex",f:"mod_autoindex"},
{m: "log_config",f:"mod_log_config"},
{m: "version",f:"mod_version"},
{m: "unixd",f:"mod_unixd"},
2023-11-06 08:18:50 +00:00
{m: "ssl",f:"mod_ssl"},
{m:"proxy",f:"mod_proxy"},
2023-11-14 18:45:23 +00:00
{m:"proxy_fcgi",f:"mod_proxy_fcgi"},
2023-11-06 08:18:50 +00:00
{m:"proxy_http",f:"mod_proxy_http"},
{m:"proxy_http2",f:"mod_proxy_http2"},
2023-11-16 20:21:59 +00:00
{m:"remoteip",f:"mod_remoteip"},
{m: "dav",f:"mod_dav"},
{m: "dav_svn", f:"mod_dav_svn"},
{m: "authz_svn", f:"mod_authz_svn"}
2023-09-24 16:15:57 +00:00
]';
debian::
"pkgs" slist => {
"apache2",
"libapache2-mod-svn",
};
"log_dir" string =>"/var/log/apache2";
2023-10-21 22:09:18 +00:00
"log_dir_owner" string => "root";
"log_dir_group" string => "adm";
2023-09-24 16:15:57 +00:00
"service_name" string => "apache2";
"restart_cmd" string => "/usr/bin/systemctl restart $(service_name)";
"www_user" string => "www-data";
"www_group" string => "www-data";
"mime_types" string => "/etc/mime.types";
"www_dir" string => "/var/www",unless => isvariable( $(this.promiser) ) ;
2023-09-24 16:15:57 +00:00
"default_html_dir" string => "$(www_dir)/html";
"etc_dir" string => "/etc";
"cfg_dir" string => "$(etc_dir)/apache2";
"main_cfg" string => "$(cfg_dir)/apache2.conf";
"libexec_dir" string => "lib/apache2/modules";
"server_root" string => "/usr";
"modules" data => '[
2023-10-24 16:34:13 +00:00
$(php_mod)
2023-09-29 05:59:41 +00:00
{m:"ssl",f:"mod_ssl"},
2023-10-20 18:00:35 +00:00
{m:"proxy",f:"mod_proxy"},
2024-02-16 11:06:16 +00:00
{m:"proxy_fcgi",f:"mod_proxy_fcgi"},
{m:"proxy_http",f:"mod_proxy_http"},
{m:"proxy_http2",f:"mod_proxy_http2"}
2023-09-24 16:15:57 +00:00
]';
2023-10-24 16:34:13 +00:00
# {m:"php",f:"libphp$(php.version)"},
2023-09-24 16:15:57 +00:00
2023-10-24 23:18:16 +00:00
# "libapache2-mod-svn",
2023-09-24 16:15:57 +00:00
centos::
"pkgs" slist => {
"httpd",
2023-09-29 05:59:41 +00:00
"mod_ssl",
2023-10-25 07:34:30 +00:00
"mod_dav_svn",
"mod_fcgid"
2023-09-24 16:15:57 +00:00
};
2023-09-29 05:59:41 +00:00
fedora::
"pkgs" slist => {
"httpd",
# "mod_ssl",
};
fedora|centos::
"log_dir" string =>"/var/log/httpd";
2023-10-21 22:09:18 +00:00
"log_dir_owner" string => "root";
2023-10-24 21:16:50 +00:00
"log_dir_group" string => "root";
"log_dir_mode" string => "700";
2023-10-21 22:09:18 +00:00
2023-09-24 16:15:57 +00:00
"service_name" string => "httpd";
2023-09-29 05:59:41 +00:00
"restart_cmd" string => "/usr/bin/systemctl restart $(service_name)";
"www_user" string => "apache";
"www_group" string => "apache";
"mime_types" string => "/etc/mime.types";
"www_dir" string => "/var/www",unless => isvariable( $(this.promiser) ) ;
"default_html_dir" string => "$(www_dir)/html";
"etc_dir" string => "/etc";
"cfg_dir" string => "$(etc_dir)/httpd";
"main_cfg" string => "$(cfg_dir)/conf/httpd.conf";
"libexec_dir" string => "$(cfg_dir)/modules";
2023-10-24 21:16:50 +00:00
"server_root" string => "/etc/httpd";
2023-09-29 05:59:41 +00:00
"modules" data => '[
{m: "autoindex",f:"mod_autoindex"},
{m: "log_config",f:"mod_log_config"},
{m: "version",f:"mod_version"},
{m: "unixd",f:"mod_unixd"},
2023-11-14 18:45:23 +00:00
{m: "systemd",f:"mod_systemd"},
2023-09-29 05:59:41 +00:00
]';
# {m: "php",f:"libphp8"},
2023-10-24 23:18:16 +00:00
centos::
"modules" data => '[
2023-10-25 07:34:30 +00:00
{m:"proxy",f:"mod_proxy"},
2023-11-03 07:51:35 +00:00
{m:"proxy_http",f:"mod_proxy_http"},
{m:"proxy_http2",f:"mod_proxy_http2"},
2023-10-25 07:34:30 +00:00
{m:"proxy_fcgi",f:"mod_proxy_fcgi"},
2023-10-24 23:18:16 +00:00
{m: "autoindex",f:"mod_autoindex"},
{m: "log_config",f:"mod_log_config"},
{m: "version",f:"mod_version"},
{m: "unixd",f:"mod_unixd"},
{m: "systemd",f:"mod_systemd"},
{m:"ssl",f:"mod_ssl"},
2023-11-14 18:45:23 +00:00
{m: "remoteip",f:"mod_remoteip"}
2023-10-24 23:18:16 +00:00
]';
2023-09-24 16:15:57 +00:00
2023-09-29 05:59:41 +00:00
2023-09-24 16:15:57 +00:00
ubuntu_20::
"phpm" string => "php7";
ubuntu_22::
"phpm" string => "php";
ubuntu::
2023-10-21 22:09:18 +00:00
"log_dir_owner" string => "root";
"log_dir_group" string => "adm";
2024-02-16 11:06:16 +00:00
# {m:"$(phpm)",f:"libphp$(install_php.version)"},
2023-09-26 12:58:58 +00:00
"modules" data => '[
2024-02-16 11:06:16 +00:00
$(php_mod)
2023-09-24 16:15:57 +00:00
{m:"proxy",f:"mod_proxy"},
{m:"proxy_http",f:"mod_proxy_http"},
{m: "autoindex",f:"mod_autoindex"},
{m: "dav",f:"mod_dav"},
{m: "dav_svn", f:"mod_dav_svn"},
{m: "authz_svn", f:"mod_authz_svn"},
2023-09-30 07:58:07 +00:00
{m: "ssl",f:"mod_ssl"}
2023-09-24 16:15:57 +00:00
]';
2023-10-24 16:34:13 +00:00
reports:
2023-09-24 16:15:57 +00:00
}
bundle agent install_apache_php
{
vars:
2023-10-21 22:09:18 +00:00
freebsd::
"pkgs" slist => {"mod_php$(php.bsdvs)"};
debian::
"pkgs" slist => {"libapache2-mod-php$(php.version)"};
centos::
"pkgs" slist => {"libapache2-mod-php"};
methods:
"any" usebundle => wmde_install_packages(@(pkgs),"apache_php"),
handle => "apache_php_installed";
commands:
2023-10-21 22:09:18 +00:00
debian|ubuntu::
"/bin/sh"
args => "-c '/usr/sbin/a2dismod mpm_event > /dev/null'",
inform=>"false",
handle => "apache_mpm_event_disabled";
2023-10-24 16:34:13 +00:00
reports:
}
2023-11-06 20:40:47 +00:00
bundle agent install_apache(raw)
2023-09-24 16:15:57 +00:00
{
methods:
"any" usebundle => wmde_install_packages(@(apache.pkgs),"apache");
2023-10-24 16:34:13 +00:00
apache_php_enable::
"any" usebundle => install_apache_php,
depends_on => { "apache_pkgs_installed" };
reports:
2023-09-24 16:15:57 +00:00
files:
2023-10-21 22:09:18 +00:00
"$(apache.log_dir)/."
create=>"true",
2023-10-24 21:16:50 +00:00
perms=>mog("$(apache.log_dir_mode)","$(apache.log_dir_owner)","$(apache.log_dir_group)"),
2023-10-21 22:09:18 +00:00
handle => "apache_log_dir_created";
2023-09-24 16:15:57 +00:00
"$(apache.www_dir)/."
create=>"true",
2023-10-21 22:09:18 +00:00
perms => m("755"),
depends_on => { "apache_pkgs_installed" },
2023-09-24 16:15:57 +00:00
handle => "apache_www_dir_created";
"$(apache.default_html_dir)/."
create=>"true",
perms => uperm("$(apache.www_user)","$(apache.www_group)","755"),
depends_on => {"apache_www_dir_created"},
handle=>"apache_default_html_dir_created";
"$(apache.cfg_dir)/Vhosts/dummy.conf"
create=>"true",
content=>"",
2023-10-21 22:09:18 +00:00
perms => m("644"),
2023-10-24 21:16:50 +00:00
depends_on => { "apache_vhosts_dir_created" },
handle=> "apache_dummy_conf_created";
"$(apache.cfg_dir)/Vhosts/."
create => "true",
depends_on => { "apache_pkgs_installed" },
2023-10-24 21:16:50 +00:00
perms => m("755"),
handle=> "apache_vhosts_dir_created";
2023-09-24 16:15:57 +00:00
"$(apache.main_cfg)"
create => "true",
2023-10-24 21:16:50 +00:00
perms => m("644"),
2023-10-02 06:09:38 +00:00
edit_template => "$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/httpd.conf.mustache",
2023-09-24 16:15:57 +00:00
template_method => "mustache",
handle => "apache_main_cfg_created",
2023-10-24 16:34:13 +00:00
depends_on => { @(apache.cfg_deps) },
classes => if_repaired(apache_repaired);
methods:
2023-10-24 16:34:13 +00:00
2023-11-13 00:17:32 +00:00
"any" usebundle => wmde_enable_service("apache");
"any" usebundle => wmde_service("$(apache.service_name)","apache_kept","apache_repaired|php_repaired"),
2023-10-24 16:34:13 +00:00
depends_on => @(apache.service_deps) ;
#depends_on => {
# "apache_php_pkgs_installed"
2023-09-24 16:15:57 +00:00
#};
#
files:
"$(sys.workdir)/data/agent/apache/."
create => "true",
handle => "apache_work_dir_created";
commands:
"/bin/sh"
args => "$(sys.workdir)/inputs/$(def.wmde_libdir)/scripts/del-files-not-in-list.sh $(apache.cfg_dir)/Vhosts $(sys.workdir)/data/agent/apache/domains.txt && echo dummy.conf > $(sys.workdir)/data/agent/apache/domains.txt",
inform => "false",
2023-11-04 21:07:30 +00:00
handle => "apache_vhost_dir_cleaned",
depends_on => {"apache_dummy_conf_created"},
contain => wmde_cmd_useshell;
# "echo"
# args => "dummy.conf > $(sys.workdir)/data/agent/apache/domains.txt",
# inform => "false",
# contain => wmde_cmd_useshell;
2023-10-24 16:34:13 +00:00
reports:
2023-10-21 22:09:18 +00:00
2023-09-24 16:15:57 +00:00
}
bundle agent restart_apache(arg)
{
2023-10-21 22:09:18 +00:00
methods:
"any" usebundle => wmde_restart_service("$(apache.service_name)","$(arg)");
#commands:
# "/bin/sh -c "
# args => "'/bin/echo $(arg) > /dev/null && /usr/bin/systemctl restart $(apache.service_name)'";
2023-09-24 16:15:57 +00:00
}
bundle agent apache_vhost
(site_param)
2023-09-24 16:15:57 +00:00
{
classes:
"delete" expression => $(site[disable]);
"use_ssl" expression => $(site[ssl]);
2023-09-30 07:58:07 +00:00
"use_certbot" expression => strcmp("certbot","$(site[ssl_cert])");
2023-12-07 12:05:00 +00:00
"do_logrotate" expression => $(site[logrotate]);
2023-09-30 07:58:07 +00:00
2023-09-24 16:15:57 +00:00
vars:
2023-10-21 22:17:49 +00:00
# "email":$(apache.admin_email),
"site_defaults" data => '
{
"aliases":[],
2023-09-30 07:58:07 +00:00
"ssl_cert":"certbot",
"doc_root":"$(apache.www_dir)/$(site_param[domain])",
"doc_root_options":"Indexes FollowSymLinks",
"php":true,
2023-12-07 12:05:00 +00:00
"php_handler":"$(apache.default_php_handler)",
"logging80":true,
"logging443":true,
"logrotate":false,
}
';
"site" data => mergedata(site_defaults,site_param);
2023-09-24 16:15:57 +00:00
"cert_file" string => "$(certbot.certbot_dir)/live/$(site[domain])";
"template_file"
string =>
2023-10-02 06:09:38 +00:00
"$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/httpd-vhost.conf.mustache";
2023-09-24 16:15:57 +00:00
"domain_file" string => "$(apache.cfg_dir)/Vhosts/$(site[domain]).conf";
2023-10-24 23:18:16 +00:00
"domain_dir" string => "$(apache.www_dir)/$(site[domain])";
2023-09-24 16:15:57 +00:00
2023-09-30 07:58:07 +00:00
"ssl_cert" string => ifelse( use_certbot,
"$(certbot.certbot_dir)/live/$(site[domain])/fullchain.pem",
"$(site[ssl_cert])");
"ssl_key" string => ifelse( use_certbot,
"$(certbot.certbot_dir)/live/$(site[domain])/privkey.pem",
"$(site[ssl_key])");
use_ssl&(use_certbot)::
2023-11-04 21:07:30 +00:00
"vhostdeps" slist => {
"apache_ssl_created$(site[domain])",
"apache_vhost_dir_cleaned"
};
2023-09-30 07:58:07 +00:00
(!use_ssl)|(!use_certbot)::
2023-11-04 21:07:30 +00:00
"vhostdeps" slist => {
"apache_vhost_dir_cleaned"
};
2023-09-30 07:58:07 +00:00
files:
2023-09-24 16:15:57 +00:00
delete::
"$(domain_file)"
delete => tidy,
classes => if_repaired(apache_restart);
!delete::
"$(domain_file)"
perms => uperm("$(apache.www_user)","$(apache.www_group)","644"),
create => "true",
edit_template => "$(template_file)",
template_method => "mustache",
handle => "vhost_cfg_done$(site[domain])",
depends_on => @(vhostdeps),
classes => if_repaired("apache_vhost_restart");
2023-10-24 23:18:16 +00:00
"$(domain_dir)/."
2023-09-24 16:15:57 +00:00
create => "true",
perms => uperm("$(apache.www_user)","$(apache.www_group)","750");
methods:
2023-09-30 07:58:07 +00:00
use_ssl&use_certbot::
"any" usebundle => certbot_cert(@(site),"$(apache.default_html_dir)"), #,"$(site[domain])"),
2023-09-24 16:15:57 +00:00
handle => "apache_ssl_created$(site[domain])";
"apache_vhost_restart"::
"any" usebundle => restart_apache("$(site[domain])"),
2023-10-24 23:18:16 +00:00
depends_on => {
"vhost_cfg_done$(site[domain])",
"apache_ssl_created$(site[domain])" };
2023-09-30 07:58:07 +00:00
2023-09-24 16:15:57 +00:00
2023-09-30 07:58:07 +00:00
(!use_ssl)|(!use_certbot)::
2023-09-24 16:15:57 +00:00
"apache_vhost_restart"::
"any" usebundle => restart_apache("$(site[domain])"),
depends_on => {"vhost_cfg_done$(site[domain])" };
2023-12-07 12:05:00 +00:00
"any" usebundle => install_logrot;
files:
do_logrotate::
"$(logrot.dir)/$(site[domain])"
create => "true",
edit_defaults => backup("false"),
edit_template => "$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/httpd-weblogrot.mustache",
template_method => "mustache";
commands:
"echo"
args => "$(site[domain]).conf >> $(sys.workdir)/data/agent/apache/domains.txt",
inform => "false",
depends_on => {"vhost_cfg_done$(site[domain])"},
contain => wmde_cmd_useshell;
2023-09-24 16:15:57 +00:00
reports:
2023-12-07 12:05:00 +00:00
# "SITE: $(site[domain]): $(site[logrotate])";
# !do_logrotate::
# "LOGOROOTATE FOR $(site[domain]): FALSE";
# do_logrotate::
# "LOGOROOTATE FOR $(site[domain]): TRUE";
2023-09-24 16:15:57 +00:00
}
2023-09-29 05:59:41 +00:00
2023-09-29 06:56:45 +00:00
bundle agent apache_vhosts(sites)
2023-09-29 05:59:41 +00:00
{
vars:
"idx" slist => getindices(@(sites));
methods:
2023-09-29 09:03:22 +00:00
"any" usebundle => apache_vhost(@(sites[$(idx)]));
reports:
2023-09-29 05:59:41 +00:00
}
2023-11-12 00:38:11 +00:00
bundle agent apache_matomo
(site)
{
vars:
"pid" string => "/var/run/matomo-logger-$(site[domain]).pid";
"log" string => "$(apache.log_dir)/$(site[domain])-access.log";
"siteid" string => "$(site[matomoid])";
2023-11-16 20:21:59 +00:00
"logger_script" string => "/tmp/matomo-logger-$(site[domain]).sh";
2023-11-12 00:38:11 +00:00
freebsd::
2023-11-16 20:21:59 +00:00
"analyt_cmd" string => "/usr/local/bin/python3.9 $(matomo.log_importer)";
2023-11-12 00:38:11 +00:00
debian::
2023-11-16 20:21:59 +00:00
"analyt_cmd" string => "/usr/bin/python3 $(matomo.log_importer)";
2023-11-12 00:38:11 +00:00
centos::
2023-11-16 20:21:59 +00:00
"analyt_cmd" string => "/usr/bin/python3 $(matomo.log_importer)";
2023-11-12 00:38:11 +00:00
matomo::
2023-11-16 20:21:59 +00:00
"cmd" string => "/usr/bin/pkill -P `cat $(pid)` ; exec /bin/sh -c 'echo $$ > $(pid); echo \"hello\" | /usr/bin/tail -n+1 -f $(log) | $(analyt_cmd) --token-auth=$(site[matomotoken]) --disable-bulk-tracking --idsite=$(siteid) --url=$(site[matomosite]) --exclude-path=/wp-login.php --exclude-path=/wp-json/\\* --recorders=1 --recorder-max-payload-size=1 --log-format-name=ncsa_extended --exclude-path=\\*.php --exclude-path=/wp-admin/\\* - > /dev/null'
";
2023-11-12 00:38:11 +00:00
!matomo::
"cmd" string => "";
any::
"cmd_esc" string => escape ($(cmd));
# "cmd": "$(cmd_esc)",
"site_str" string => storejson(@(site));
# "site_json" string => '{
# "site" : "$(site_str)"
# }';
"site_json" string => '{
"cmd": "$(cmd)",
"service": "$(apache.service_name)",
"site": $(site_str)
}';
"template_file"
2023-12-07 12:05:00 +00:00
string => "$(sys.workdir)/inputs/$(def.wmde_libdir)/templates/httpd-weblogrot-matomo.mustache";
2023-11-12 00:38:11 +00:00
methods:
"any" usebundle => install_logrot;
files:
"$(logrot.dir)/$(site[domain])"
create => "true",
edit_defaults => backup("false"),
edit_template => "$(template_file)",
template_method => "mustache";
# template_data => parsejson("$(site_json)");
matomo::
"$(pid)"
create => "true";
2023-11-16 20:21:59 +00:00
"$(logger_script)"
create => "true",
perms => m("755"),
content => '#!/bin/sh
if [ "$1" != "child" ]; then
"$(logger_script)" child &
else
setsid >/dev/null 2>&1
exec 0<&- 1>/dev/null 2>&1
$(cmd)
fi
',
handle => "loggerscript_$(site[domain])_created";
2023-11-12 00:38:11 +00:00
classes:
"matomo" expression => isvariable("site[matomoid]");
commands:
matomo::
"/bin/sh -c "
args => "'kill -0 `cat $(pid)` && echo -start_matomo || echo +start_matomo'",
inform => "false",
module => "true";
2023-11-16 20:21:59 +00:00
start_matomo::
"$(logger_script)"
args => "$(logger_script)",
contain => wmde_cmd_useshell,
depends_on => {"loggerscript_$(site[domain])_created"};
# "/bin/sh"
# args => "$(logger_script) &",
# contain => wmde_cmd_useshell,
# depends_on => {"loggerscript_$(site[domain])_created"};
2023-11-12 00:38:11 +00:00
methods:
2023-11-16 20:21:59 +00:00
# start_matomo::
# "call" usebundle => daemonize( "/bin/sh $(logger_script)" ),
# depends_on => {"loggerscript_$(site[domain])_created"};
2023-11-12 00:38:11 +00:00
reports:
#start_matomo::
# "MUST START - WHY";
#"ESC ESC $(site_json)";
matomo::
}
body edit_defaults backup( edit_backup )
{
edit_backup => "$(edit_backup)";
}