Bog'liq Mastering Ubuntu Server Gain expertise in the art of deploying, configuring, managing, and troubleshooting Ubuntu Server by Jay LaCroix (z-lib.org)
[ 352 ] Enabling virtual hosts is handled a bit differently in Debian and Ubuntu than in
other platforms. In fact, there are two specific commands to handle this purpose:
a2ensite
for enabling a site and
a2dissite
for disabling a site. You won't find these
commands on distributions such as CentOS, for example. Configuration files for each
site are stored in the
/etc/apache2/sites-available/
directory and we would use
the
a2ensite
command to enable each configuration. Assuming a site with the URL
acmeconsulting.com
is to be hosted on our Ubuntu server, we would create the
/etc/apache2/sites-available/acmeconsulting.com.conf
configuration file, and
enable the site with the following commands:
sudo a2ensite acmeconsulting.com.conf
sudo systemctl reload apache2
If we wanted to disable the site for some reason, we would execute the
a2dissite
command
against the site's configuration file:
sudo a2dissite acmeconsulting.com.conf
sudo systemctl reload apache2
If you're curious about how this works behind the scenes, when the
a2ensite
command is run against a configuration file, it basically creates a symbolic link to
that file and stores it in the
/etc/apache2/sites-enabled
directory. When you run
a2dissite
to disable a site, this symbolic link is removed. Apache, by default, will
use any configuration files it finds in the
/etc/apache2/sites-enabled
directory.
After enabling or disabling a site, you'll need to refresh Apache's configuration,
which is where the
reload
option comes in. This command won't restart Apache
itself (so users who are using your existing sites won't be disturbed) but it does give
Apache a chance to reload its configuration files. If you replace
reload
with
restart
in the preceding commands, Apache will perform a full restart. You should only
need to do that if you're having an issue with Apache or enabling a new plugin, but
in most cases the
reload
option is preferred on a production system.
The main configuration file for Apache is located at
/etc/apache2/apache2.conf
. Feel
free to view the contents of this file; the comments contain a good overview of how
Apache's configuration is laid out. The following lines in this file are of special interest:
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
I'm not using absolute paths in my examples; as long as you've
copied the configuration file to the correct place, the
a2ensite
and
a2dissite
commands will know where to find it.