Ordres útils per gestionar el servidor web Apache a Linux


En aquest tutorial, descriurem algunes de les ordres de gestió de serveis d'Apache (HTTPD) més utilitzades que hauríeu de conèixer com a desenvolupador o administrador del sistema i hauríeu de mantenir aquestes ordres al vostre abast. Mostrarem ordres tant per a Systemd com per a SysVinit.

Assegureu-vos que les ordres següents s'han d'executar com a usuari root o sudo i haurien de funcionar en qualsevol distribució de Linux com CentOS, RHEL, Fedora Debian i Ubuntu.

Instal·leu Apache Server

Per instal·lar el servidor web Apache, utilitzeu el vostre gestor de paquets de distribució predeterminat tal com es mostra.

$ 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]

Comproveu la versió d'Apache

Per comprovar la versió instal·lada del vostre servidor web Apache al vostre sistema Linux, executeu l'ordre següent.

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

Si voleu mostrar el número de versió d'Apache i la configuració de compilació, utilitzeu el senyalador -V tal com es mostra.

$ sudo httpd -V
OR
$ sudo apache2 -V
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"

Comproveu els errors de sintaxi de configuració d'Apache

Per comprovar si hi ha errors de sintaxi als vostres fitxers de configuració Apache, executeu l'ordre següent, que comprovarà la validesa dels fitxers de configuració abans de reiniciar el servei.

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

Inicieu el servei Apache

Per iniciar el servei Apache, executeu l'ordre següent.

------------ 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]

Habilita el servei Apache

Mentrestant, l'ordre anterior només inicia el servei Apache; per activar-lo l'inici automàtic a l'arrencada del sistema, executeu l'ordre següent.

------------ 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]

Reinicieu el servei Apache

Per reiniciar Apache (aturar i iniciar el servei), executeu l'ordre següent.

------------ 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]

Veure l'estat del servei Apache

Per comprovar la informació d'estat del temps d'execució del servei Apache, executeu l'ordre següent.

------------ 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]

Torna a carregar el servei Apache

Si heu fet algun canvi a la configuració del servidor Apache, podeu indicar al servei que recarregui la seva configuració executant l'ordre següent.

------------ 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]

Atura el servei Apache

Per aturar el servei Apache, utilitzeu l'ordre següent.

------------ 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]

Mostra l'ajuda de l'ordre d'Apache

Finalment, però no menys important, podeu obtenir ajuda sobre les ordres del servei Apache a systemd executant l'ordre següent.

$ sudo httpd -h
OR
$ sudo apache2 -h		
OR
$ systemctl -h apache2	
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)

Podeu trobar més informació sobre systemctl consultant: Com gestionar els serveis i unitats Systemd mitjançant Systemctl a Linux.

També us agradaria llegir aquests articles següents relacionats amb Apache.

  1. 5 consells per augmentar el rendiment del vostre servidor web Apache
  2. Com supervisar la càrrega del servidor web Apache i les estadístiques de la pàgina
  3. Com administrar el servidor web Apache mitjançant l'eina \Apache GUI
  4. Com canviar el port HTTP d'Apache a Linux
  5. 13 Consells de seguretat i enduriment del servidor web Apache
  6. Protegiu Apache contra atacs de força bruta o DDoS mitjançant mòduls Mod_Security i Mod_evasive

Això és tot per ara! En aquest article, hem explicat les ordres de gestió de serveis Apache/HTTPD més utilitzades que hauríeu de conèixer, com ara iniciar, habilitar, reiniciar i aturar Apache. Sempre podeu posar-vos en contacte amb nosaltres mitjançant el formulari de comentaris a continuació per a qualsevol pregunta o comentari.