gladilov.org.ru gladilov.org.ua

Запуск Apache с IP в качестве имени сервера

Должен быть доступен ifconfig (в Debian 9 по умолчанию не установлен пакет net-tools), конфиг-файл апача лежит по пути /etc/apache/httpd.conf.

Показать

#!/usr/bin/env bash
APACHECONFFILE=/etc/apache/httpd.conf
if test $(id -u) != 0
    then echo "You must be root to run this script! Aborting."
    exit 1
fi
if ! type /sbin/ifconfig>/dev/null 2>&1
    then echo "ifconfig required but not installed! Aborting."
    exit 1
fi
LC_ALL=C
export LC_ALL
ALL=$(ifconfig -a | sed -n 's/^\([^ ]\+\):.*/\1/p' | paste -sd ' ')
IF=""
for if in $ALL; do
    test "$if" == "ppp0" -a "$IF" == "" && IF=$if
done
for if in $ALL; do
    test "$if" == "ippp0" -a "$IF" == "" && IF=$if
done
for if in $ALL; do
    test "${if/#eth*/eth}" == "eth" -a "$IF" == "" && IF=$if
done
for if in $ALL; do
    test "${if/#enp*/enp}" == "enp" -a "$IF" == "" && IF=$if
done
for if in $ALL; do
    test "${if/#wlan*/wlan}" == "wlan" -a "$IF" == "" && IF=$if
done
test "$IF" == "" && IF=lo
echo Using $IF
IP=$(ifconfig $IF|awk '/inet/{print $2}'|cut -d: -f2)
SERVERNAME=$(awk '/^ServerName/{print $2}' $APACHECONFFILE)
echo ServerName changed from $SERVERNAME to $IP
perl -pi -e "s/(^ServerName).*/\1 $IP/" $APACHECONFFILE
/etc/init.d/apache reload

Популярное
Наверх