Useful Linux Commands for Managing the Apache Web Server

Apache is a free, open-source Web server application that is most commonly used on Unix-like operating systems, but it can also be used in Windows. Knowing about the Apache web server will be very useful for you as a developer or system administrator. It includes a lot of noteworthy features, one of which is virtual hosting, which allows a single Apache Web Server to serve a variable number of websites.

As a developer or system administrator, you should be familiar with some of the most relevant commands for managing Apache web server. The commands that will be discussed must be run as root or as a sudo user.

Ensure that the following commands are run as root or sudo, and that they operate on any Linux distribution, including CentOS, RHEL, Fedora, Debian, and Ubuntu.

Install Apache Server

Use your default distribution package manager to install Apache web server, as indicated.

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

Checking Apache Version

Run the following command on your Linux machine to see what version of Apache is installed.

$ sudo httpd -v
OR
$ sudo apache2 -v$ sudo httpd -v
OR
$ sudo apache2 -v


Sample Output

Server version: Apache/2.4.6 (CentOS)
Server built:   Nov  5 2018 01:47:09

Use the -V flag to display the Apache version number and compilation parameters, as indicated.

$ sudo httpd -V
OR
$ sudo apache2 -V

Sample Output

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"

Checking Apache Configuration Syntax Errors

Before restarting the service, verify your Apache configuration files for any syntax issues with the following command, which will validate the config files.

$ sudo httpd -t
OR
$ sudo apache2ctl -t

Sample Output

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

Now Start Apache Service

Run the following command to start the Apache service.

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

Enabling Apache Services

The last command only starts the Apache service temporarily; perform the following command to make it auto-start upon system startup.

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

Restart Apache Service

Run the following command to restart Apache (stop and then relaunch the service).

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

View Apache Service Status

Run the following command to get information about the Apache service’s run time status.

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

Reloading Apache Services

If you’ve made any modifications to the Apache server configuration, run the following command to force the service to reload its settings.

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

Stopping Apache Services

Use the following command to halt the Apache service.

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

Show Apache Command Help

Last but not least, by running the following command under systemd, you can get help with Apache service instructions.

$ sudo httpd -h
OR
$ sudo apache2 -h
OR
$ systemctl -h apache2

Sample Output

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)

That’s all I’ve got for now! We’ve covered the most widely used Apache/HTTPD service administration commands, such as starting, enabling, resuming, and halting Apache, in this post.