简体   繁体   中英

Apache 2.4 virtual host ignored

I'm setting up a new web server using Ubuntu 14.0, following the instructions here for creating virtual hosts, however my single additional virtual host always loads the default site (/var/www/html).

Here is my vhost configuration (I have replaced my domain with example.com, but it is otherwise identical):

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com

  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/example/app

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have made NO OTHER CHANGES to the Apache 2.4 configuration which is installed in Ubuntu 14.

EDIT: I am correctly enabling sites and restarting apache after making changes to this vhost.

When I visit example.com in my browser, I get the default site. What am I doing wrong? How do I get this virtual host to load the correct directory?

EDIT: The solution is a missing "sudo" in the restart command ( https://stackoverflow.com/a/23713299/225682 ).

you need to restart apache we server after you edit vhost, also you need to add enable vhost. to enable:

sudo a2ensite example.conf (name of config file inside sites-available directory)

to restart server:

sudo service apache2 restart

also check out permissions for www directory, maybe apache's user can't read that directory.

您是否在配置中的某处启用了虚拟主机?

NameVirtualHost *:80

Finally found the answer at https://stackoverflow.com/a/23713299/225682 .

Apache must be restarted with sudo, or the changes actually have no effect.

Well this is my story. I had fresh install:

webmaster@ubuntuserver:/srv$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial
webmaster@ubuntuserver:/srv$
webmaster@ubuntuserver:/srv$ apache2 -v
Server version: Apache/2.4.18 (Ubuntu)
Server built:   2017-09-18T15:09:02
webmaster@ubuntuserver:/srv$

I used very simple virtual host config:

<VirtualHost *:80>
ServerAdmin admin@testsite.com
ServerName testsite.com
ServerAlias www.testsite.com
DocumentRoot /var/www/testsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Tried many suggestions such as:

  • directory permission settings;
  • directory content chmod to 774;
  • firewall settings;

Finally resolved with adding:

Listen 80

to the top of virtual host configuration, so it looks like:

Listen 80
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName testsite.com
ServerAlias www.testsite.com
DocumentRoot /var/www/testsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Eventually virtual hosts started responding and behaved as expected.

Interesting though, I later tried to recreate the issue and virtual hosts worked even without setting Listen 80 . However I am sure the trigger which made virtual hosts to respond was Listen 80 setting.

Anyway, you may also refer to Apache documentation:

https://httpd.apache.org/docs/2.4/bind.html

6 years too late but maybe this will help someone. When debugging issues like this, first check that apache can see your virtualhost configuration: apachectl -S

You should see your virtualhost configuration in the output:

*:80                   example.com (/etc/httpd/conf.d/example.com.conf:2)

If you cannot see it, maybe the config file path is not included into the main configuration file or the file itself is missing a .conf extension.

If you see your VirtualHost in the output but you're still getting the default VirtualHost instead, check the global ServerName directive in the main configuration file. It should NOT match your VirtualHost ServerName.

Beware - if your global ServerName is not set, apache will obtain one using rDNS. So if your IP address' reverse DNS record matches example.com (same as your VirtualHost), your VirtualHost might be ignored. I believe this is a very common source of problems and I haven't found this on the forums yet. Check your IP's rDNS record with host command if you face this issue.

Try running the apachectl command with the -t option, this checks all config is correct. Which file is this virtual host. Have you restarted apache to pickup these changes?

apachectl restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM