Last active
February 9, 2017 09:33
-
-
Save fcolista/af7187ed00d78098821144e73d2e3eef to your computer and use it in GitHub Desktop.
AlpineLinux: Icingaweb2 webserver config script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# | |
# Script for automatic webserver setup for Icingaweb2, designed for Alpine Linux (https://www.alpinelinux.org) | |
# (c) 2017 Francesco Colista | |
# Email: fcolista@alpinelinux.org | |
# | |
icingaweb2_path="/usr/share/webapps/icingaweb2/public" | |
icingaweb2conf_path="/etc/icingaweb2" | |
configure_icinga2() { | |
local web=$1 | |
local conf=$2 | |
echo -e "\n ==> Configuring $web web server .... \n" | |
case $web in | |
apache*) | |
icingacli setup config webserver apache --document-root $icingaweb2_path > $conf | |
sed -i -e "s/^#.*rewrite_module/LoadModule rewrite_module/" /etc/apache2/httpd.conf | |
rc-service apache2 status || rc-service apache2 restart | |
;; | |
nginx*) | |
read -p "Warning: this setup disable the default.conf in nginx and autoconfigure php-fpm with ngixn user. Should I continue? [Y/N]" yn | |
case $yn in | |
[Yy]*) mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled | |
sed -i -e "s/user.*/user\ =\ nginx/g" /etc/php5/php-fpm.conf | |
sed -i -e "s/group.*/group\ =\ icingaweb2/g" /etc/php5/php-fpm.conf | |
;; | |
[Nn]*) echo -e "\nYou should fix manually $conf file in order to let nginx start without issues\n" ;; | |
esac | |
cat>$conf<<EOF | |
server { | |
listen 80; | |
location ~ ^/icingaweb2/index\.php(.*)\$ { | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_index index.php; | |
include fastcgi_params; | |
fastcgi_param SCRIPT_FILENAME $icingaweb2_path/index.php; | |
fastcgi_param ICINGAWEB_CONFIGDIR /$icingaweb2conf_path; | |
fastcgi_param REMOTE_USER \$remote_user; | |
} | |
location ~ ^/icingaweb2(.+)? { | |
alias /usr/share/webapps/icingaweb2/public; | |
index index.php; | |
try_files \$1 \$uri \$uri/ /icingaweb2/index.php\$is_args\$args; | |
} | |
} | |
EOF | |
rc-service php-fpm status || rc-service php-fpm start | |
rc-service nginx status || rc-service nginx start | |
;; | |
lighttpd*) | |
local modules="alias setenv rewrite" | |
local lighttpd_conf="/etc/lighttpd/lighttpd.conf" | |
sed -i -e "s/^#.*mod_fastcgi.conf/include \"mod_fastcgi.conf/g" $lighttpd_conf | |
for m in $modules; do | |
echo "Fixing lighttpd.conf with module $m" | |
sed -i -e "1,/^#.*mod_$m./s/^#.*mod_$m./\ \"mod_$m\"/" $lighttpd_conf | |
done | |
grep -q $conf $lighttpd_conf || echo -e "\ninclude \"$conf\"\n" >> $lighttpd_conf | |
cat>$conf<<EOF | |
alias.url += ( "/icingaweb2" => "$icingaweb2_path/" ) | |
\$HTTP["url"] =~ "^/icingaweb2" { | |
server.document-root = "$icingaweb2_path" | |
setenv.add-environment = ("ICINGAWEB_CONFIGDIR" => "$icingaweb2conf_path") | |
server.follow-symlink = "enable" | |
} | |
url.rewrite = ( "^/icingaweb2/(img|font)/(.*)?" => "\$0", | |
"^/icingaweb2/(.*)(\?(.*)=(.*))" => "/icingaweb2/index.php?\$3=\$4", | |
"^/icingaweb2/(.*)?" => "/icingaweb2/index.php/" ) | |
EOF | |
;; | |
esac | |
} | |
check() { | |
apk info -q -e icingaweb2 | |
if [ $? -ne "0" ]; then | |
echo -e "\nIcingaweb2 is not installed. Install it first, then re-run the script.\n"; | |
exit 1 | |
fi | |
} | |
# Main starts here | |
check | |
while true; do | |
echo | |
read -p " ==> Which webserver do you want to use? [A]pache, [N]ginx or [Lighttpd] ? " c | |
case $c in | |
[Aa]*) | |
apk info -e apache2 || apk add apache2 | |
apk info -e php5-apache2 || apk add php5-apache2 | |
webserver="apache" | |
conffile="/etc/apache2/conf.d/icingaweb2.conf" | |
break | |
;; | |
[Nn]*) | |
apk info -e php5-fpm || apk add php5-fpm | |
apk info -e nginx || apk add nginx | |
webserver="nginx" | |
conffile="/etc/nginx/conf.d/icingaweb2.conf" | |
break | |
;; | |
[Ll]*) | |
apk info -e php5-cgi || apk add php5-cgi | |
apk info -e lighttpd || apk add lighttpd | |
webserver="lighttpd" | |
conffile="/etc/lighttpd/icingaweb2.conf" | |
break | |
;; | |
*) echo -e "\n ==> Please choose [A]pache, [N]ginx or [L]ighttpd : " | |
;; | |
esac | |
done | |
adduser $webserver icingaweb2 | |
# Add the web server user to the icingacmd group in order to grant it write permissions to the external command pipe and livestatus socket: | |
adduser $webserver icingacmd | |
chown $webserver:icingaweb2 -R $icingaweb2_path/* | |
chown $webserver:icingaweb2 -R $icingaweb2conf_path/* | |
chmod 755 -R $icingaweb2_path/* | |
configure_icinga2 $webserver $conffile | |
icingacli setup token create && icingacli setup token show | |
echo " ==> Open your browser to your webserver pointing to the path: /icingaweb2 " | |
echo " ==> Paste the previously generated token and follow the steps on-screen." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment