e263c82e |
#!/bin/bash
# |
d98a5d0a |
# lib/apache
# Functions to control configuration and operation of apache web server
# Dependencies: |
6a5aa7c6 |
#
# - ``functions`` file |
d8864fea |
# - ``STACK_USER`` must be defined
# |
e578effb |
# lib/apache exports the following functions:
# |
6a5aa7c6 |
# - install_apache_wsgi |
a688bc65 |
# - apache_site_config_for |
6a5aa7c6 |
# - enable_apache_site
# - disable_apache_site
# - start_apache_server
# - stop_apache_server
# - restart_apache_server |
d98a5d0a |
# Save trace setting |
523f4880 |
_XTRACE_LIB_APACHE=$(set +o | grep xtrace) |
d98a5d0a |
set +o xtrace
# Allow overriding the default Apache user and group, default to
# current user and his default group. |
e578effb |
APACHE_USER=${APACHE_USER:-$STACK_USER} |
d98a5d0a |
APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
# Set up apache name and configuration directory
if is_ubuntu; then
APACHE_NAME=apache2 |
444a8d53 |
APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/sites-available} |
d98a5d0a |
elif is_fedora; then
APACHE_NAME=httpd |
444a8d53 |
APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/conf.d} |
d98a5d0a |
elif is_suse; then
APACHE_NAME=apache2 |
444a8d53 |
APACHE_CONF_DIR=${APACHE_CONF_DIR:-/etc/$APACHE_NAME/vhosts.d} |
d98a5d0a |
fi
# Functions
# ---------
# install_apache_wsgi() - Install Apache server and wsgi module |
aee18c74 |
function install_apache_wsgi { |
d98a5d0a |
# Apache installation, because we mark it NOPRIME
if is_ubuntu; then
# Install apache2, which is NOPRIME'd
install_package apache2 libapache2-mod-wsgi |
5470701e |
# WSGI isn't enabled by default, enable it
sudo a2enmod wsgi |
d98a5d0a |
elif is_fedora; then
sudo rm -f /etc/httpd/conf.d/000-*
install_package httpd mod_wsgi
elif is_suse; then
install_package apache2 apache2-mod_wsgi |
5470701e |
# WSGI isn't enabled by default, enable it
sudo a2enmod wsgi |
d98a5d0a |
else
exit_distro_not_supported "apache installation"
fi |
b4495eb4 |
# ensure mod_version enabled for <IfVersion ...>. This is
# built-in statically on anything recent, but precise (2.2)
# doesn't have it enabled
sudo a2enmod version || true |
d98a5d0a |
}
|
d074dc7f |
# get_apache_version() - return the version of Apache installed
# This function is used to determine the Apache version installed. There are
# various differences between Apache 2.2 and 2.4 that warrant special handling.
function get_apache_version {
if is_ubuntu; then |
ada886dd |
local version_str
version_str=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/) |
d074dc7f |
elif is_fedora; then |
ada886dd |
local version_str
version_str=$(rpm -qa --queryformat '%{VERSION}' httpd) |
d074dc7f |
elif is_suse; then |
ada886dd |
local version_str
version_str=$(rpm -qa --queryformat '%{VERSION}' apache2) |
d074dc7f |
else
exit_distro_not_supported "cannot determine apache version"
fi
if [[ "$version_str" =~ ^2\.2\. ]]; then
echo "2.2"
elif [[ "$version_str" =~ ^2\.4\. ]]; then
echo "2.4"
else
exit_distro_not_supported "apache version not supported"
fi
}
|
a688bc65 |
# apache_site_config_for() - The filename of the site's configuration file.
# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
#
# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
# files are 000-default.conf and default-ssl.conf.
#
# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
# sites' files are default and default-ssl.
# |
633a1290 |
# On Fedora and openSUSE, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled. |
a688bc65 |
#
# On RHEL and CentOS, things should hopefully work as in Fedora.
#
# The table below summarizes what should happen on each distribution:
# +----------------------+--------------------+--------------------------+--------------------------+
# | Distribution | File name | Site enabling command | Site disabling command |
# +----------------------+--------------------+--------------------------+--------------------------+
# | Ubuntu 12.04 | site | a2ensite site | a2dissite site |
# | Ubuntu 14.04 | site.conf | a2ensite site | a2dissite site |
# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
# +----------------------+--------------------+--------------------------+--------------------------+
function apache_site_config_for {
local site=$@
if is_ubuntu; then |
ada886dd |
local apache_version
apache_version=$(get_apache_version) |
d074dc7f |
if [[ "$apache_version" == "2.2" ]]; then |
a688bc65 |
# Ubuntu 12.04 - Apache 2.2 |
444a8d53 |
echo $APACHE_CONF_DIR/${site} |
a688bc65 |
else
# Ubuntu 14.04 - Apache 2.4 |
444a8d53 |
echo $APACHE_CONF_DIR/${site}.conf |
a688bc65 |
fi |
633a1290 |
elif is_fedora || is_suse; then |
a688bc65 |
# fedora conf.d is only imported if it ends with .conf so this is approx the same |
444a8d53 |
local enabled_site_file="$APACHE_CONF_DIR/${site}.conf" |
a688bc65 |
if [ -f $enabled_site_file ]; then
echo ${enabled_site_file}
else
echo ${enabled_site_file}.disabled
fi
fi
}
|
5470701e |
# enable_apache_site() - Enable a particular apache site |
aee18c74 |
function enable_apache_site { |
5470701e |
local site=$@
if is_ubuntu; then
sudo a2ensite ${site} |
633a1290 |
elif is_fedora || is_suse; then |
444a8d53 |
local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
# Do nothing if site already enabled or no site config exists
if [[ -f ${enabled_site_file}.disabled ]] && [[ ! -f ${enabled_site_file} ]]; then
sudo mv ${enabled_site_file}.disabled ${enabled_site_file}
fi |
5470701e |
fi
}
# disable_apache_site() - Disable a particular apache site |
aee18c74 |
function disable_apache_site { |
5470701e |
local site=$@
if is_ubuntu; then
sudo a2dissite ${site} |
633a1290 |
elif is_fedora || is_suse; then |
444a8d53 |
local enabled_site_file="$APACHE_CONF_DIR/${site}.conf"
# Do nothing if no site config exists
if [[ -f ${enabled_site_file} ]]; then
sudo mv ${enabled_site_file} ${enabled_site_file}.disabled
fi |
5470701e |
fi
}
|
d98a5d0a |
# start_apache_server() - Start running apache server |
aee18c74 |
function start_apache_server { |
d98a5d0a |
start_service $APACHE_NAME
}
# stop_apache_server() - Stop running apache server |
aee18c74 |
function stop_apache_server { |
d98a5d0a |
if [ -n "$APACHE_NAME" ]; then
stop_service $APACHE_NAME
else
exit_distro_not_supported "apache configuration"
fi
}
# restart_apache_server |
aee18c74 |
function restart_apache_server { |
2df0046f |
# Apache can be slow to stop, doing an explicit stop, sleep, start helps
# to mitigate issues where apache will claim a port it's listening on is
# still in use and fail to start. |
2ca8af45 |
time_start "restart_apache_server" |
2df0046f |
stop_service $APACHE_NAME
sleep 3
start_service $APACHE_NAME |
2ca8af45 |
time_stop "restart_apache_server" |
d98a5d0a |
}
# Restore xtrace |
523f4880 |
$_XTRACE_LIB_APACHE |
d98a5d0a |
|
6a5aa7c6 |
# Tell emacs to use shell-script-mode
## Local variables:
## mode: shell-script
## End: |