1

I have access to a machine running linux. Apache2 is installed on it. I need to add 3 virtual hosts.

In /etc/apache2/sites-available, there is a set of about 15 files displayed with ls -la.

  1. I was told Apache2 would read these files one-by-one in alphabetic order to create virtual hosts, is this correct?

  2. How does Linux give preference in case of conflict? Last read file wins? First read file wins?

  3. If I want to configure my 3 virtual hosts (which are not in conflict with existing virtual hosts), is it just a matter of creating an extra file with them in this directory?

P.S.: I have mixed-up available with enabled. One should read /etc/apache2/enabled. Sorry.

  • Please check if you have /etc/apache2/sites-enabled with symlinks to ../sites-available. (Is this SuSE Linux?) – Ulrich Schwarz Dec 23 '15 at 12:31
  • I am on Ubuntu 14. – Jérôme Verstrynge Dec 23 '15 at 13:09
3

You may want to read the Apache2 documentation.

I was told Apache2 would read these files one-by-one in alphabetic order to create virtual hosts, is this correct?

Virtual hosts are not read in /etc/apache2/sites-available but in /etc/apache2/sites-enabled. That said, apache2 uses the libc, and according to gnu.org, The order in which files appear in a directory tends to be fairly random.

edit:

You may want to read the answer from @nwildner too which is more accurate than me.

How does Linux give preference in case of conflict? Last read file wins? First read file wins?

Really not sure, but if there is a conflict, generally it displays a warning message and your httpd service won't (re)load.

If I want to configure my 3 virtual hosts (which are not in conflict with existing virtual hosts), is it just a matter of creating an extra file with them in this directory?

Yes, and once you've added your extra files, run these commands:

sudo a2ensite my_site1.conf my_site2.conf my_site3.conf
sudo service apache2 reload

It will search in /etc/apache2/sites-available for my_site1.conf, my_site2.conf and my_site3.conf.

  • I believe your meant it will search /etc/apache2/sites-enabled for my_site1.conf etc.. – Jérôme Verstrynge Dec 23 '15 at 13:00
  • Nope, because a2ensite means "Apache2 enable site", its role will be to create a symbolic link from /etc/apache2/sites-available to /etc/apache2/sites-enabled. But you can absolutely do it manually, or create your file in /etc/apache2/sites-enabled. But that's not recommended because it's hard if you want to quickly temporarily disable your site by keeping the vhost. – Sw0ut Dec 23 '15 at 13:04
3

Answer your question heavily depends on the distribution you are using. I will assume it´s Debian based, since they use this approach of sites-available and sites-enabled dirs and the /etc/apache2 configuration dir instead of /etc/httpd like the RPM based distributions.

Answer 1. That is correct. You can see the Include directive on your apache2.conf file:

host:/etc/apache2# grep sites apache2.conf
Include /etc/apache2/sites-enabled/

Taking a look at the Apache Include directive documentation:

... Shell-style (fnmatch()) wildcard characters can be used in the filename or directory parts of the path to include several files at once, in alphabetical order. ...

Answer 2. Not sure what will happen. I have duplicated on purpouse a VirtualHost on my Apache server and it did not complained about it. Not sure if first or last configuration was set as valid.

Edit1: It seems CentOS Apache uses the first entry of the VirtualHost configured, when i put 2 of the same VirtualHost inside /etc/httpd/conf.d/virtual_host.conf changing only the DocumentRoot. Config example:

<VirtualHost ip.address.A:80>
  DocumentRoot /var/www/sites/wpad
  ServerName wpad.xxx.net
  ServerAlias wpad.xxx.net wpad
</VirtualHost>

<VirtualHost ip.address.A:80>
  DocumentRoot /var/www/sites/wpad2
  ServerName wpad.xxx.net
  ServerAlias wpad.xxx.net wpad
</VirtualHost>

When i browse on Incognito/Private browser mode, i get the wpad.xxx.net/wpad.dat file that is provided by the first virtualhost. No errors found on Apache logs.

Answer 3. On Debian based distribution you have to create the file inside sites-available and symlink it to sites-enabled

cd /etc/apache2/sites-available/
ln -s your-site ../sites-enabled/

Or, you could use a2ensite as explained by @Sw0ut

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.