Comandos útiles para administrar el servidor web Apache en Linux

En este tutorial, describiremos algunos de los comandos de gestión de servicios más comúnmente utilizados de Apache (HTTPD) que deberías conocer como desarrollador o administrador de sistemas y debes tener estos comandos a mano. Mostraremos comandos tanto para Systemd como para SysVinit.

Leer También: 10 Comandos Más Utilizados de Nginx que Todo Usuario de Linux Debe Conocer

Asegúrate de que los siguientes comandos deben ser ejecutados como usuario root o sudo y deberían funcionar en cualquier distribución de Linux como CentOS, RHEL, Fedora, Debian y Ubuntu.

Instalar Servidor Apache

Para instalar el servidor web Apache, utiliza el gestor de paquetes de tu distribución por defecto como se muestra a continuación.

$ sudo apt install apache2	    [On Debian/Ubuntu]
$ sudo yum install httpd	    [On RHEL/CentOS]
$ sudo dnf install httpd	    [On Fedora 22+]
$ sudo zypper install apache2	    [On openSUSE]

Verificar Versión de Apache

Para verificar la versión instalada de tu servidor web Apache en tu sistema Linux, ejecuta el siguiente comando.

$ sudo httpd -v
OR
$ sudo apache2 -v
Salida de Ejemplo
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09

Si deseas mostrar el número de versión de Apache y la configuración de compilación, utiliza la bandera -V como se muestra.

$ sudo httpd -V
OR
$ sudo apache2 -V
Salida de Ejemplo
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

Verificar Errores de Sintaxis en la Configuración de Apache

Para verificar tus archivos de configuración de Apache en busca de errores de sintaxis, ejecuta el siguiente comando, que comprobará la validez de los archivos de configuración antes de reiniciar el servicio.

$ sudo httpd -t
OR
$ sudo apache2ctl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using tecmint.com. 
Set the 'ServerName' directive globally to suppress this message
Syntax OK

Iniciar el servicio de Apache

Para iniciar el servicio de Apache, ejecuta el siguiente comando.

------------ On CentOS/RHEL ------------ 
$ sudo systemctl start httpd     [On Systemd]
$ sudo service httpd start 	 [On SysVInit]

------------ On Ubunt/Debian  ------------
$ sudo systemctl start apache2   [On Systemd]
$ sudo service apache2 start     [On SysVInit]

Habilitar el servicio de Apache

El comando anterior solo inicia el servicio de Apache temporalmente, para habilitarlo para que se inicie automáticamente al arrancar el sistema, ejecuta el siguiente comando.

------------ On CentOS/RHEL ------------ 
$ sudo systemctl enable httpd     [On Systemd]
$ sudo chkconfig httpd on 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
$ sudo systemctl enable apache2   [On Systemd]
$ sudo chkconfig apache2 on       [On SysVInit]

Reiniciar el servicio de Apache

Para reiniciar Apache (detener y luego iniciar el servicio), ejecuta el siguiente comando.

------------ On CentOS/RHEL ------------ 
$ sudo systemctl restart httpd     [On Systemd]
$ sudo service httpd restart 	   [On SysVInit]

------------ On Ubunt/Debian  ------------
$ sudo systemctl restart apache2   [On Systemd]
$ sudo service apache2 restart     [On SysVInit]

Ver el estado del servicio de Apache

Para verificar la información del estado de ejecución del servicio de Apache, ejecuta el siguiente comando.

------------ On CentOS/RHEL ------------ 
$ sudo systemctl status httpd     [On Systemd]
$ sudo service httpd status 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
$ sudo systemctl status apache2   [On Systemd]
$ sudo service apache2 status     [On SysVInit]

Recargar el servicio de Apache

Si has realizado cambios en la configuración del servidor Apache, puedes indicar al servicio que recargue su configuración ejecutando el siguiente comando.

------------ On CentOS/RHEL ------------ 
$ sudo systemctl reload httpd     [On Systemd]
$ sudo service httpd reload 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
$ sudo systemctl reload apache2   [On Systemd]
$ sudo service apache2 reload     [On SysVInit]

Detener el servicio de Apache

Para detener el servicio de Apache, utiliza el siguiente comando.

------------ On CentOS/RHEL ------------ 
$ sudo systemctl stop httpd       [On Systemd]
$ sudo service httpd stop 	  [On SysVInit]

------------ On Ubunt/Debian  ------------
$ sudo systemctl stop apache2     [On Systemd]
$ sudo service apache2 stop     [On SysVInit]

Mostrar la ayuda de comandos de Apache

Por último, puedes obtener ayuda sobre los comandos del servicio de Apache bajo systemd ejecutando el siguiente comando.

$ sudo httpd -h
OR
$ sudo apache2 -h		
OR
$ systemctl -h apache2	
Información adicional
Usage: httpd [-D name] [-d directory] [-f file]
             [-C "directive"] [-c "directive"]
             [-k start|restart|graceful|graceful-stop|stop]
             [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in  directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
  -t -D DUMP_RUN_CFG : show parsed run settings
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules 
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)

Puedes encontrar más información sobre systemctl consultando: Cómo gestionar los servicios y unidades de ‘Systemd’ usando ‘Systemctl’ en Linux.

También te puede interesar leer los siguientes artículos relacionados con Apache.

  1. 5 Consejos para Mejorar el Rendimiento de tu Servidor Web Apache
  2. Cómo Monitorear la Carga del Servidor Web Apache y las Estadísticas de Página
  3. Cómo Administrar el Servidor Web Apache Usando la Herramienta “Apache GUI”
  4. Cómo Cambiar el Puerto HTTP de Apache en Linux
  5. 13 Consejos de Seguridad y Fortificación del Servidor Web Apache
  6. Proteger Apache Contra Ataques de Fuerza Bruta o DDoS Usando los Módulos Mod_Security y Mod_evasive

Eso es todo por ahora. En este artículo, hemos explicado los comandos de gestión de servicios Apache/HTTPD más comúnmente utilizados que deberías conocer, incluyendo iniciar, habilitar, reiniciar y detener Apache. Siempre puedes contactarnos a través del formulario de comentarios abajo para cualquier pregunta o comentario.

Source:
https://www.tecmint.com/manage-apache-web-server-in-linux/