Apache 2.4 : Usando diversas instâncias no Apache instalado via YUM

Neste post será apresentado como configurar diversas instâncias do Apache em um mesmo servidor.

Por exemplo em um projeto será necessário criar duas instâncias para dois projetos distintos na mesma máquina.

1) Ambiente :

Antes de começarmos , gostaria de deixar claro que podemos usar o Apache em duas portas diferentes , uma em 80 e outra na 81 por exemplo.
Em nosso exemplo estamos usandos dois serviços segregados por ip , conforme apresentado abaixo.

Em um servidor RHEL 7 configurarei as instâncias em seus respectivos ips :

apache-01 - 10.0.0.141
apache-02 - 10.0.0.142

2) Requisitos :

Instale o Apache via YUM

yum install httpd -y 

Instale todos os módulos necessários antes de configurar as instâncias.

3) Criando as instâncias :

Crie as entradas nos arquivos /etc/hosts :

10.0.0.141 apache-01
10.0.0.142 apache-02

Crie os usuários de cada instância :

useradd -s /sbin/nologin/ apache-01
useradd -s /sbin/nologin/ apache-02

Crie os diretórios :

mkdir -p /etc/httpd/instances 
mkdir -p /etc/httpd/instances/apache-01
mkdir -p /etc/httpd/instances/apache-02

Copie os diretórios conf e conf.d para dentro das instâncias :

cp -Rv /etc/httpd/conf/  /etc/httpd/instances/apache-01
cp -Rv /etc/httpd/conf.d/  /etc/httpd/instances/apache-01
cp -Rv /etc/httpd/conf/  /etc/httpd/instances/apache-02
cp -Rv /etc/httpd/conf.d/  /etc/httpd/instances/apache-02

Crie o diretório de logs e pid :

mkdir /etc/httpd/conf/instances/<INSTANCIA>/logs
mkdir /etc/httpd/conf/instances/<INSTANCIA>/pid
mkdir /etc/httpd/conf/instances/<INSTANCIA>/lockfile

Dentro de cada httpd.conf de cada instância altere os parâmetros :

Onde deixo definido a tag deverá ser trocado pelo nome da instância.

Pidfile "/etc/httpd/instances/<INSTANCIA>/pid/<INSTANCIA>.pid"
ServerName <IP DA INSTANCIA>:80
Listen <IP DA INSTANCIA>:80
DocumentRoot "/var/www/html/<INSTANCIA>/"
Include /etc/httpd/conf.modules.d/*.conf
ErrorLog "logs/<INSTANCIA>-error_log"

Crie o index da instância :

echo "FAJLINUX <INSTANCIA>" >>  /var/www/html/<INSTANCIA>/index.html

Start das instâncias :

Apache-01

/usr/sbin/httpd -f /etc/httpd/instances/apache-01/conf/httpd.conf -k start

Apache-02

/usr/sbin/httpd -f /etc/httpd/instances/apache-02/conf/httpd.conf -k start

4) Testes Finais :

Para os testes finais vou chamar as Instâncias :

APACHE-01

Captura de Tela 2015-09-29 às 22.49.32

APACHE-02

Captura de Tela 2015-09-29 às 22.50.17

Observando os processos da máquina :

Captura de Tela 2015-09-29 às 22.52.55

5) Extras :

O script abaixo pode ser usado para iniciar a instância , no exemplo estou configurando o script para a instância apache-01 .

#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#              server implementing the current HTTP standards.

### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
#  implementing the current HTTP standards.
### END INIT INFO

APACHE_HOME=/etc/httpd
APACHE_SITE=$APACHE/instances/apache-01

#echo "APACHE_HOME=/etc/httpd"
# Source function library.
. /etc/rc.d/init.d/functions


# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpdSTART=${HTTPD-/usr/sbin/httpd -f "$APACHE_SITE/httpd/httpd.conf"}
export httpdSTART
httpdSTOP=${HTTPD-/usr/sbin/httpd}
export httpdSTOP

prog=$APACHE_SITE
pidfile=${PIDFILE-$APACHE_SITE/pid/httpd.pid}
lockfile=${LOCKFILE-$APACHE_SITE/lockfile/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpdSTART $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpdSTOP
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpdSTOP $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpdSTOP due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpdSTOP -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpdSTOP shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p ${pidfile} $httpdSTOP
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  condrestart|try-restart)
        if status -p ${pidfile} $httpdSTOP >&/dev/null; then
                stop
                start
        fi
        ;;
  force-reload|reload)
        reload
        ;;
  graceful|help|configtest|fullstatus)
        $apachectl $@
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
        RETVAL=2
esac

exit $RETVAL