Apache dosn’t load virtual host files

5,147

Solution 1

Typically if one wants to setup separate virtual host configurations, you would place them in this directory:

/etc/apache2/sites-available/

So if you have a virtual host named www.example.com the config filename can be named www.example.com.conf like this:

/etc/apache2/sites-available/www.example.com.conf

You would then open that file in a text editor like this; I’m using nano as an example but feel free to use whatever text editor you prefer:

sudo nano /etc/apache2/sites-available/www.example.com.conf

And then place content similar to this into www.example.com.conf:

<VirtualHost *:80>
  ServerAdmin [email protected]
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Close and save that file and then use a2ensite like this to have the system set a symbolic link from sites-available to sites-enabled like this:

sudo a2ensite www.example.com.conf

Or—if you wanted to—you could manually use ln -s to set a symbolic link like this:

ln -s /etc/apache2/sites-available/www.example.com.conf /etc/apache2/sites-enabled/www.example.com.conf

When that’s done just reload Apache like this:

sudo service apache2 reload

And if for some reason reload doesn’t work, just force a restart like this:

sudo service apache2 restart

The benefit of using reload before restart is that if there is a typo in a config file, Apache will report an error but not load the config files. Meaning your Apache web server will still be up and running based on the last stable configuration file it loaded.

The risk is that if you force a restart and there is a typo in a configuration file, Apache will be dead because restart forces a complete service stop followed by a start. And if that start chokes, the server is dead until the config typo/issue is cleared up.

Solution 2

Check you httpd.conf file, it should contain a line like Include conf/*.conf. Check whether it is a wildcard or a fixed file (like 000-default.conf).

Also, check that your new conf files match the wildcard patern and are stored on the same folder as the 000-default.conf.

Finally, there are apache distributions where they have a available and a active conf files, so in some cases a soft link is needed in order for it to work

Share:
5,147

Related videos on Youtube

Zlug
Author by

Zlug

Updated on September 18, 2022

Comments

  • Zlug
    Zlug almost 2 years

    I’m trying to set up an Apache2 server, but I can’t get it to load my virtual hosts. Or to be more precise, it works fine as long as I write them in 000-default.conf, but whenever I try to use a separate file it seem to completely ignore it.

    Why is this not working? And how do I make it work?

    • NuTTyX
      NuTTyX over 9 years
      Check you httpd.conf file, it should contain a line like Include conf/*.conf. Check whether it is a wildcard or a fixed file (like 000-default.conf). Also, check that your new conf files match the wildcard patern and are stored on the same folder as the 000-default.conf. Finally, there are apache distributions where they have a available and a active conf files, so in some cases a soft link is needed in order for it to work.
    • Giacomo1968
      Giacomo1968 over 9 years
      You need to edit your question to provide more details. Where are you writing the files that are not working? Also, what are the contents of the stand-alone virtual host files? You can neuter specifics, but what is the overall structure like?
    • Zlug
      Zlug over 9 years
      NuTTyX was right though
    • Giacomo1968
      Giacomo1968 over 9 years
      @NuTTyX Since your comment seems to be the answer that solved this issue, you should post it as an actual answer so Zlug can check it off as an answer and you are rewarded for this effort.
  • Giacomo1968
    Giacomo1968 over 9 years
    @Zlug Please be sure to check off this answer as the answer since NuTTyX solved the issue for you.