62

Can anyone tell me—in a nutshell—what the purpose of these two directories are in Debian?

/etc/apache2/sites-enabled
/etc/apache2/sites-available

I notice that diffing sites-available/000-default and sites-enabled/default shows they are identical.

What gives?

Jesse Nickles
  • 250
  • 1
  • 12
aaaidan
  • 722
  • 1
  • 5
  • 8

4 Answers4

65

sites-available contains the apache config files for each of your sites. For example:

<VirtualHost *:80>
  ServerName site.mysite.com
  ServerAdmin my@email.com

  DirectoryIndex index.php
  DocumentRoot /home/user/public_html/site.mysite.com/public

  LogLevel warn
  ErrorLog /home/user/public_html/site.mysite.com/logs/error.log
  CustomLog /home/user/public_html/site.mysite.com/logs/access.log combined
</VirtualHost>

When you want to add a new site (for example, site.mysite.com), you add it here, and use:

a2ensite site.mysite.com

To enable the site. Once the site is enabled, a symlink to the config file is placed in the sites-enabled directory, indicating that the site is enabled.

Jason Leveille
  • 766
  • 6
  • 3
  • 12
    If you want to disable a site, you would run a2dissite site.mysite.com –  Nov 11 '09 at 01:43
  • 2
    `a2ensite` and `a2dissite` are located in `/usr/sbin` which is currently not included in the default user path so tab completion won't work. When typing `sudo a2` and pressing the tab key however you will be offered both `a2ensite` and `a2dissite`. – Stefan Schmidt Jun 29 '13 at 19:39
  • I had to type in the name of my config file, such as a2ensite foo.conf and then restarted apache – Jimbo Jun 24 '21 at 20:42
25

More important than the mechanics of the system is the rationale...

Debian provides the two separate directories so that if you're automatically managing your Apache configs, you can just have all of the vhosts drop into sites-available on all your machines, and then individual vhosts can be enabled on the server that will actually serve them. It also means you can near-instantaneously disable a site if it's causing problems (a2dissite example.com; /etc/init.d/apache2 reload).

womble
  • 95,029
  • 29
  • 173
  • 228
7

Important information:

You should edit files only in sites-available directory.

Do never edit files inside the sites-enabled directory, otherwise you can have problems if your editor runs out of memory or, for any reason, it receives a SIGHUP or SIGTERM.

For example: if you are using nano to edit the file sites-enabled/default and it runs out of memory or, for any reason, it receives a SIGHUP or SIGTERM, then nano will create an emergency file called default.save, inside the sites-enabled directory. So, there will be an extra file inside the sites-enabled directory. That will prevent Apache or NGINX to start. If your site was working, it will not be anymore. You will have a hard time until you find out, in the logs, something related to the default.save file and, then, remove it.

In the example above, if you were editing the file inside the sites-available directory, nothing bad would have happened. The file sites-available/default.save would have been created, but it wouldn't do any harm inside the sites-available directory.

viniciussss
  • 185
  • 3
  • 8
  • Very interesting! Can anyone else confirm this? – aaaidan Jan 10 '17 at 23:51
  • 1
    Here is an example of someone who had this problem: http://stackoverflow.com/questions/36808705/deployed-rails-app-still-seeing-nginx-splash-page – viniciussss Jan 11 '17 at 12:51
  • Something similar happened in the following link. See Melvyn comment in the answaer, about editing files in sites-enabled. http://stackoverflow.com/questions/26210115/webserver-with-nginx-working-until-save-file-created – viniciussss Jan 11 '17 at 13:07
5

To add to those above, the file in sites-enabled is a symlink to the sites-available file:

ls -l /etc/apache2/sites-enabled/

It’s not just the same content, it’s the same actual file!

Giacomo1968
  • 3,522
  • 25
  • 38