Comandos Úteis para Gerenciar o Servidor Web Apache no Linux

Neste tutorial, descreveremos alguns dos comandos de gerenciamento de serviço mais comumente usados do Apache (HTTPD) que você deve conhecer como desenvolvedor ou administrador de sistema e deve manter esses comandos à mão. Mostraremos comandos tanto para Systemd quanto para SysVinit.

Leia Também: 10 Comandos Mais Usados do Nginx Que Todo Usuário Linux Deve Conhecer

Garanta que os seguintes comandos devem ser executados como usuário root ou sudo e devem funcionar em qualquer distribuição Linux como CentOS, RHEL, Fedora, Debian e Ubuntu.

Instalar Servidor Apache

Para instalar o servidor web Apache, use o gerenciador de pacotes padrão da sua distribuição, conforme mostrado.

$ 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 Versão do Apache

Para verificar a versão instalada do seu servidor web Apache no seu sistema Linux, execute o seguinte comando.

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

Se você deseja exibir o número da versão do Apache e as configurações de compilação, use a opção -V como mostrado.

$ sudo httpd -V
OR
$ sudo apache2 -V
Saída de Exemplo
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 Erros de Sintaxe na Configuração do Apache

Para verificar se há erros de sintaxe em seus arquivos de configuração do Apache, execute o seguinte comando, que verificará a validade dos arquivos de configuração antes de reiniciar o serviço.

$ sudo httpd -t
OR
$ sudo apache2ctl -t
Saída de exemplo
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 Serviço Apache

Para iniciar o serviço Apache, execute o seguinte 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]

Ativar Serviço Apache

O comando anterior apenas inicia o serviço Apache temporariamente; para ativá-lo para iniciar automaticamente no boot do sistema, execute o seguinte 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 Serviço Apache

Para reiniciar o Apache (parar e depois iniciar o serviço), execute o seguinte 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]

Exibir Status do Serviço Apache

Para verificar as informações de status de tempo de execução do serviço Apache, execute o seguinte 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]

Recarregar Serviço Apache

Se você fez quaisquer alterações na configuração do servidor Apache, você pode instruir o serviço a recarregar sua configuração executando o seguinte 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]

Parar Serviço Apache

Para parar o serviço Apache, use o seguinte 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 Ajuda de Comando Apache

Por último, mas não menos importante, você pode obter ajuda sobre os comandos de serviço Apache sob o systemd executando o seguinte comando.

$ sudo httpd -h
OR
$ sudo apache2 -h		
OR
$ systemctl -h apache2	
Saída de exemplo
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)

Você pode encontrar mais informações sobre systemctl consultando: Como Gerenciar Serviços e Unidades ‘Systemd’ Usando ‘Systemctl’ no Linux.

Você também pode gostar de ler os seguintes artigos relacionados ao Apache.

  1. 5 Dicas para Melhorar o Desempenho do Seu Servidor Web Apache
  2. Como Monitorar a Carga e Estatísticas de Páginas do Servidor Web Apache
  3. Como Administrar o Servidor Web Apache Usando a Ferramenta “Apache GUI”
  4. Como Alterar a Porta HTTP do Apache no Linux
  5. 13 Dicas de Segurança e Reforço do Servidor Web Apache
  6. Proteja o Apache Contra Ataques de Força Bruta ou DDoS Usando os Módulos Mod_Security e Mod_evasive

Por enquanto é isso! Neste artigo, explicamos os comandos de gerenciamento de serviço Apache/HTTPD mais comumente usados que você deve conhecer, incluindo iniciar, habilitar, reiniciar e parar o Apache. Você sempre pode entrar em contato conosco através do formulário de feedback abaixo para quaisquer perguntas ou comentários.

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