Cheatsheet For Apache2 Commands

Cheatsheet is one of the quickest reference guides that allows you to easily find the right commands.

This is very useful for a Linux administrator or developer.

In this tutorial, we have added all possible commands and configuration files for the Apache 2 service, which is useful for Ubuntu/Debian system users.

This Apache 2 cheatsheet contains Apache 2 service commands, log file location, configuration file location, syntax checking.

What’s Apache?

Apache HTTP Web Server is a free and open source cross-platform web server software.

It is the most widely used web server software, currently serving over 100 million websites.

Developed and maintained by the Apache Software Foundation. It is a fast, reliable and secure web server. It can be highly customized to meet the needs of different environments by using extensions and modules.

Implemented with the concept of Apache modules. The modules allow Apache to perform additional functions. Apache comes with some pre-installed modules, and you can easily enable or disable additional modules to suit your needs.

Also, it provides a CLI interface called apache2ctl, which is on the front end of the Apache Hypertext Transfer Protocol (HTTP server. It is designed to help the administrator control the functionality of the Apache daemon.

How to Install Apache on Debian/Ubuntu

You can easily install Apache on Debian/Ubuntu systems by executing the apt command or apt-get command below.

$ sudo apt install apache2
or
$ sudo apt-get install apache2

How to Check Apache Version on Debian/Ubuntu

Run the command below to check for the Apache version installed on your Linux system.

$ apache2 -v

Server version: Apache/2.4.29 (Ubuntu)
Server built:   2019-09-16T12:58:48

To find information about Apache compile settings, run the following command.

$ apache2 -V

Server version: Apache/2.4.29 (Ubuntu)
Server built:   2019-09-16T12:58:48
Server's Module Magic Number: 20120211:68
Server loaded:  APR 1.6.3, APR-UTIL 1.6.1
Compiled using: APR 1.6.3, APR-UTIL 1.6.1
Architecture:   64-bit
Server MPM:     
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/apache2"
 -D SUEXEC_BIN="/usr/lib/apache2/suexec"
 -D DEFAULT_PIDLOG="/var/run/apache2.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="mime.types"
 -D SERVER_CONFIG_FILE="apache2.conf"

How to Find Syntax Errors in Apache Configuration File

You can use one of the commands below to check for Apache syntax errors on Debian-based systems.

# apachectl configtest
or
# apachectl -t
or
# apache2ctl configtest
or
# apache2ctl -t

Syntax OK

How to Find Errors in Apache Virtual Host Definitions

You can see Apache virtual host errors by running one of the command below. It displays all virtual hosts on the server, their options, and the file names and line numbers that they define.

This will display an error message with the line number, which is very useful for troubleshooting the configuration file.

$ apache2ctl -S
or
$ apachectl -S

VirtualHost configuration:
*:80                   node3.2g.lab (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default 
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used

How to Use Apache2 Service Commands

The commands below allow you to manage Apache2 services.

# systemctl start apache2  		[To Start the Apache2 Service]
# systemctl stop apache2		[To Stop the Apache2 Service]
# systemctl restart apache2		[To Restart the Apache2 Service]
# systemctl reload apache2		[To Reload the Apache2 Service]
# systemctl status apache2		[To Status the Apache2 Service]
# systemctl enable apache2		[To Enable the Apache2 Service]

How to Enable/Disable Virtual Hosts in Debian/Ubuntu

a2ensite is a script that enables the specified site (which contains a block) within the apache2 configuration. It does this by creating symlinks within /etc/apache2/sites-enabled.

Likewise, a2dissite disables a site by removing those symlinks. It doesn’t show an error message if a site is already enabled or disabled.

$ sudo a2ensite xxxx.conf	[To Enable a New Website]
$ sudo a2dissite xxxx.conf	[To Disable a Website]

How to Enable/Disable Apache2 Modules in Debian/Ubuntu

You can easily enable or disable any available Apache modules according to your needs. You need to reload the Apache service to make the transition effect.

$ sudo a2enmod [Module]		[To Enable a New Module]
$ sudo a2dismod [Module]	[To Disable a Module]
$ sudo systemctl reload apache2

Use the following command to verify the loaded module of Apache 2.

$ apachectl -M
$ apache2ctl -M
$ a2query -m

Important Configuration Files of Apache2

Below is a list of Apache configuration files for the Debian/Ubuntu system.

$ ls -lh /usr/lib/apache2/modules/	[To List Available Modules]
$ ls -lh /etc/apache2/mods-available/	[To List Available Modules]
$ ls -lh /etc/apache2/mods-enabled/	[To List Enabled Modules]
$ ls -lh /var/www/html			[Apache2 Default Web root]
$ ls -lh /var/www/			[Other Website Web root]
$ less /etc/apache2/apache2.conf	[Apache2 Main Configuration File]
$ less /etc/apache2/ports.conf		[Apache2 Ports Configuration File]
$ less /var/log/apache2/error.log	[Apache2 Error Log File]
$ less /var/log/apache2/access.log	[Apache2 Access Log File]

How to Check Apache Concurrent Connections

The commands below allow you to count Apache current connections on your system.

# ss -ant | grep :80 | wc -l
or
# netstat -ant | grep :80 | wc -l
90

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...