How To Enable/Disable Apache Virtual Host [a2ensite and a2dissite]

This guide is intended for new and experienced Linux administrators who want to learn about Apache virtual hosts. It describes how to enable or disable an existing Apache Virtual host using the a2ensite and a2dissite commands.

Most of the time, when we work on our servers, the first thing that comes to mind is to install some new applications (Like WordPress, Web Apps, and others), or set up a new database.

But there are times when modifying the configuration file for an existing service may need to happen – and this is where knowing how to enable/disable Apache virtual hosts can prove useful.

As you probably already know, Apache has virtual hosts. This means that you can have multiple websites or applications (on the same server) instead of just one.

In this tutorial, we are going to look at how to enable and disable these virtual hosts on Ubuntu/Debian-based Linux system.

What is Apache Virtual Host?

Apache Virtual Host (Vhost) is a configuration file, and it allows you to host more than one website on a single machine.

Apache is the most commonly used Web Server, and it comes with several pre-configured Virtual Hosts. This post will show you how to enable or disable those Virtual Hosts.

Understanding the a2ensite/a2dissite Command With Examples

Before we begin, this guide assumes you already have at least one virtual host configured. If you do not have any virtual hosts configured, please refer to the How to Create an Apache Virtual Host in the Linux system (Ubuntu, Debian, or CentOS).

Breakdown of the a2ensite/a2dissite command:

  • a2 – apache2 (HTTP Server)
  • en – enable
  • dis – disable
  • site – website’s virtual host

Location of the Apache Virtual Host files:

Location of Apache Virtual Host Files
Virtual Host Location Directory

To enable or disable the Virtual host, you must know the exact directory of the vhost files, and the default directory of the Apache vhost is /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ as you can see in the image above.

Added Apache Virtual Host Files
Added/Default Apache Virtual Hosts

These two sub-directories are very important to working Apache HTTP server. Suppose we want to host multiple websites on a single server, then we must add different-different vhost configuration files (.conf) in /etc/apache2/sites-available/ directory, and then we must add a symbolic link (shortcut like Windows) of vhost files to /etc/apache2/sites-enabled/ directory files.

When the user requests the server’s specific site, the Apache webserver will open the same particular website in the browser as we have given the directive in the virtual host files.

How to Enable an Apache Virtual Host?

  • First, change the directory to cd /etc/apache2/sites-available/
  • Execute command sudo a2ensite example.conf
  • Reload Apache HTTP Server sudo service apache2 reload

In the second command, you must replace the conf file name from example.conf whichever file you have.

Use a2ensite command to enable vhost
Enable Virtual Host

How to Disable an Apache Virtual Host?

  • First, change the directory to cd /etc/apache2/sites-enabled/
  • Execute command sudo a2dissite example.conf
  • Reload Apache HTTP Server sudo service apache2 reload

In the second command, you must replace the conf file name from example.conf whichever file you have.

Use a2dissite command to disable vhost
Disable Virtual Host

FAQs

Where is Virtual Host File Located in Apache HTTP Server?

It varies according to the Linux distributions. The most commonly used systems are Debian and Ubuntu – and for that, you will find the Apache virtual host files in /etc/apache2/sites-available/ and /etc/apache2/sites-available/ sub-directory.
For CentOS, users can find the Apache Virtual Host files under /etc/httpd/conf.d/ sub-directory.

Is Nginx Uses the a2ensite/a2dissite Command To Enable or Disable Virtual Host?

Enabling and disabling Nginx Server Block (Virtual Host) is a little different. Nginx uses the symlinks method to enable or disable server blocks and a2ensite/a2dissite doesn’t work for Nginx.
Where Apache Virtual Host can use symlinks and a2ensite/a2dissite too.

Which Apache Virtual Host Is Default?

After installing the Apache HTTP Server, by default, the Virtual Host will be 000-default.conf file.

Where is Nginx Virtual Host File?

For Nginx web server users, you can find the server block (Virtual Host) files under the /etc/nginx/sites-enabled/ sub-directory.

Conclusion

After working through this guide, you will be able to enable or disable any of your Apache virtual hosts. You can also use the same process for Debian and Ubuntu 18.04, 20.4, and 22.04 LTS.

There are other methods to enable and disable Apache virtual host files, but this guide explained the easiest and safest method. Perhaps, if you have any suggestions or issues with your server, you can comment here, we will try to respond asap.


Pronay Sarkar

Hello!
I am Pronay Sarkar, I love to write about tech and stuff.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.