Nyttige kommandoer til at styre Apache Web Server i Linux


I denne vejledning beskriver vi nogle af de mest anvendte Apache (HTTPD) servicestyringskommandoer, som du bør kende som udvikler eller systemadministrator, og du skal holde disse kommandoer lige ved hånden. Vi viser kommandoer til både Systemd og SysVinit.

Sørg for, at følgende kommandoer skal udføres som en root- eller sudo-bruger og skal arbejde på enhver Linux-distribution som CentOS, RHEL, Fedora Debian og Ubuntu.

Installer Apache Server

For at installere Apache webserver skal du bruge din standard distributionspakkehåndtering som vist.

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

Tjek Apache-version

For at kontrollere den installerede version af din Apache-webserver på dit Linux-system skal du køre følgende kommando.

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

Hvis du vil vise Apache-versionsnummeret og kompilere indstillinger, skal du bruge flagget -V som vist.

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

Kontroller Apache-konfigurationssyntaksfejl

For at kontrollere dine Apache-konfigurationsfiler for syntaksfejl skal du køre følgende kommando, som kontrollerer gyldigheden af konfigurationsfilerne inden genstart af tjenesten.

$ 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

Start Apache Service

For at starte Apache-tjenesten skal du køre følgende kommando.

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

Aktivér Apache Service

Den forrige kommando starter kun Apache-tjenesten i mellemtiden, for at aktivere den automatisk start ved systemstart skal du køre følgende kommando.

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

Genstart Apache Service

For at genstarte Apache (stop og start derefter tjenesten) skal du køre følgende kommando.

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

Se Apache-servicestatus

For at kontrollere Apache-tjenestens statusoplysninger om status skal du køre følgende kommando.

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

Genindlæs Apache-tjenesten

Hvis du har foretaget ændringer i Apache-serverkonfigurationen, kan du bede tjenesten om at genindlæse dens konfiguration ved at køre følgende kommando.

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

Stop Apache Service

Brug følgende kommando for at stoppe Apache-tjenesten.

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

Vis Apache Command-hjælp

Sidst men ikke mindst kan du få hjælp til Apache-servicekommandoer under systemd ved at køre følgende kommando.

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

Du kan finde flere oplysninger om systemctl ved at konsultere: Sådan styres 'Systemd' tjenester og enheder ved hjælp af 'Systemctl' i Linux.

Du vil muligvis også læse disse følgende Apache-relaterede artikler.

  1. 5 tip til at øge effektiviteten af din Apache-webserver
  2. Sådan overvåges Apache-webserverbelastning og sidestatistik
  3. Sådan administreres Apache-webserveren ved hjælp af værktøjet "Apache GUI"
  4. Sådan ændres Apache HTTP-port i Linux
  5. 13 Apache-webserversikkerheds- og hærdningstip
  6. Beskyt Apache mod brutal kraft eller DDoS-angreb ved hjælp af Mod_Security og Mod_evasive-moduler

Det er alt for nu! I denne artikel har vi forklaret de mest anvendte Apache/HTTPD-servicestyringskommandoer, som du bør kende, herunder start, aktivering, genstart og stop af Apache. Du kan altid nå os via feedbackformularen nedenfor for spørgsmål eller kommentarer.