Apache Commands You Should Know

Apache services are the most frequently used web server in Linux as the windows operating system. Programmers mostly use these services for developing any website. It gives quick access to the user for the web interface. This post will demonstrate the various commands to the Apache web server. The content for the post is as follows:

Let’s start with the installation.

How to Install Apache on Linux?

Before executing the apache2 services commands, install the apache2 server in the Linux operating system if not already installed. To install apache2, run this command in the terminal:

$ sudo apt install apache2

The above image shows the execution of the installation command.

If you are using other Linux distributions, then run this command.

For Fedora/CentOS/RHEL:

$ sudo dnf install httpd

Let’s move and check how to use the apache services commands in Linux.

How to Use Apache Commands on Linux?

There are various commands that the user should know about the apache2 services. Some of the most common and important commands are implemented below.

Example 1: Checking Apache Version

The below command is used to verify whether the apache2 server is installed. It will display the version of the apache2 web server:

$ apache2 -v

The execution of the above command shows that apache 2.4.52 is installed.

Example 2: Starting Apache Services

To start the apache service in Linux is obtained as follows:

$ sudo service apache2 start

The successful execution of the command shows that apache services have been started.

Example 3: Restarting Apache Services

Similarly, to restart the apache services use the following command:

$ sudo systemctl restart apache2

The apache service has been restarted.

Example 4: Reloading Apache Services

The reloading of the apache service is the same as the apache restart service. But the only difference is the reload command is a bit faster than the restart command. To reload apache service is obtained as follows:

$ sudo systemctl reload apache2

The execution of the above command shows that apache2 services have been reloaded.

Example 5: Stopping Apache Services

To stop apache services in Linux, run the following command in the terminal:

$ sudo systemctl stop apache2

By executing the above commands, the apache service will be stopped.

Example 6: Testing Apache Virtual Host

User can also test the apache virtual host by executing the below command:

$ apache2ctl -S

This command shows the error message along with its options, file names, and line numbers is quite useful in the troubleshooting process.

Example 7: Testing Apache Configuration

In this command, the configuration of the apache service will be tested, and if there is any error, it will display it. Let’s test this command in the terminal:

$ apache2ctl -t

The above image shows that our server name is not set. You can use this command in the troubleshooting process.

Example 8: Checking Apache Service Status

To check the status of the apache service, use the following command:

$ sudo systemctl status apache2.service

The above image shows the running status for the apache service.

Example 9: Retrieving Apache Service Help

Lastly, to obtain help for apache services use the “h” option with the apache2 command:

$ sudo apache2 -h

The help command has displayed all the options that can be used with the apache2 service command.

Example 10: Retrieving the List Modules in Apache Service

To obtain the list of modules used in the apache service, use the “M” option with “apachectl”:

$ apachectl -M

All the modules in apache have been listed.

Example 11: Checking the Specific Modules in Apache Service

You can also display the specific module using the “grep” command. To do so, run the given command:

$ apachectl -M | grep so_module

In the above image, “so_module” is displayed.

Bonus Tip: Apache Services Commands in Fedora/CentOS/RHEL

To start, stop and restart the apache services in Fedora/CentOS/RHEL is given below:

# for starting the apache services #
$ sudo systemctl start httpd.service

# for stopping the apache services #
$ sudo systemctl stop httpd.service

# for stopping the apache services #
$ sudo systemctl restart httpd.service

That’s it from this guide.

Conclusion

In Linux, There are various commands that are used for apache services, such as starting, restarting, stopping, testing configuration, and virtual host. This post has covered various commands for apache services in Linux. Apart from that, apache service commands for Fedora/CentOS/RHEL Linux distributions have also been given in this post.