Home » Tech Tips » Basic Apache Commands on Linux

Basic Apache Commands on Linux

Apache is the most popular web server developed by the Apache Foundation in 1995. It comes under Apache License 2.0. It is a cross-platform application available for most of the older operating systems like Linux, Windows, and macOS systems. With a 45% of market share, Apache is serving almost every second website on the internet. Which tells its popularity between users.

In this tutorial, we will discuss some basic commonly used commands for managing Apache servers on Ubuntu and Debian-based systems. This includes how to enable/disable a virtual host, module, or configuration file in the Apache server.

1. Check Apache Version

Use -v command-line option to check the running Apache version on Ubuntu and other Debina-based systems.

apache2 -v  Server version: Apache/2.4.41 (Ubuntu) Server built: 2021-06-17T18:27:53

To view the detailed information use capital letter -V with apache2 command.

apache2 -V 

2. Test Apache Configuration

After making any changes in Apache configuration files, You must run a check for the syntax. Use the following command to check the syntax for all the Apache2 configuration files.

sudo apachectl -t 

If all the configuration files are correct, You will see “Syntax Ok” in the results.
Output

Syntax OK

3. Enable and Disable Virtual Hosts

The website configuration files keep information about the Apache Virtual Hosts. In the case of Debian-based systems, the actual file is created under /etc/apache2/sites-available directory.

Related:  How to Run Linux Commands in the Background

Then, we enable the website using a2ensite, which simply creates a symlink of that file to /etc/apache2/sites-enabled. Apache loads all the files from this directory to the environment.

For example, if you have created a file named example.com.conf. Then use the following command.

sudo a2ensite example.com 

Similarly use a2dissite command to disable the sites, which are already disabled.

sudo a2dissite example.com 

4. Enable and Disable Configurations

The original configuration files are stored under /etc/apache2/sites-available/ the directory. Apache read the configuration files from /etc/apache2/sites-enabled/dthe directory, So you need to create a symbolic link of files to the sites-enabled directory.

The a2enconf command creates a symbolic link for the configuration file and a2disconf removes the symbolic link. For example to create a symbolic link for the configuration file phpmyadmin.conf run:

sudo a2enconf phpmyadmin 

To deactivate the configuration just disable it by a2disconf command like:

sudo a2disconf phpmyadmin 

5. Enable and Disable Modules

All the Apache module files are stored under /etc/apache2/mods-available/ directory. All the active modules are symlinked to /etc/apache2/mods-enabled/ directory.

Related:  Top 10 Useful Tools to Create Bootable USB from an ISO Image

Use a2enmod command to enable a module in Apache server and a2dismod to disable the module. For example to enable rewrite module, type:

sudo a2enmod rewrite 

To disable the rewrite module, run:

sudo a2dismod rewrite 

6. Manage Apache2 Service

The latest operating systems have opted the system for managing services. You can use the systemctl command-line utility for managing the Apache service on your system.

The following commands will stop, start, and restart the apache2 service on your system.

sudo systemctl stop apache2 
sudo systemctl start apache2 
sudo systemctl restart apache2 

The changes in configuration files can be reloaded in the running environment with reload option. This will not restart the Apache service.

sudo systemctl reload apache2 

Use the following command to enable or disable the apcahe2 service.

sudo systemctl enable apache2
sudo systemctl disable apache2 

7. Show Apache Command Help

For more help use the -h options on the command line.

sudo apache2 -h 

Output

Usage: apache2 [-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 -D DUMP_INCLUDES: show all included configuration files
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)

In this beginners guide, you have learned about basic commands to manage Apache server via command line.

Leave a Comment