VirtualHost always returns default host with Apache on Ubuntu 14.04

前端 未结 12 594
夕颜
夕颜 2021-01-30 07:16

I try to setup a virtual host besides the default localhost. Whenever I try to call my virtual host http://test I get the default Apache2 Index file t

相关标签:
12条回答
  • 2021-01-30 07:33

    Maybe there is a problem with .conf files of the virtual-hosts.

    First run the command:

    apache2ctl -S
    

    The response must be something like

    VirtualHost configuration:
    *:443                  is a NameVirtualHost
         default server localhost (/etc/apache2/sites-enabled/default-ssl.conf:2)
         port 443 namevhost localhost (/etc/apache2/sites-enabled/default-ssl.conf:2)
         port 443 namevhost myvhost.com (/etc/apache2/sites-enabled/myvhosts.com.conf:20)
                 alias www.myvhost.com
    
    *:80                   is a NameVirtualHost
         default server localhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost localhost (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost myvhost.com (/etc/apache2/sites-enabled/myvhost.com.conf:1)
                 alias www.myvhost.com
    

    But sometimes you can read:

    default server myvhost.com (/etc/apache2/sites-enabled/default-ssl.conf:2)
    

    so your vhost domain point to default configuration (http or https).

    The solution is to add ServerName localhost directive in the default-ssl.conf and in the 000-default.conf files.

    Apache don't know who is the name of your default site and it takes one random resolving 127.0.0.1.

    After setting the ServerName, you must run:

    service apache2 reload
    service apache2 restart
    

    If you run again apache2ctl -S you must see the correct match for the default server.

    0 讨论(0)
  • 2021-01-30 07:34

    Just pointing this out that may be obvious for experienced users, but not so much if you're a first timer.

    Make sure that the config you're using in /sites-enabled ends with a .conf as expected in your apache2.conf

    That was my problem and it fixed it.

    0 讨论(0)
  • 2021-01-30 07:34

    it has been driving me nuts too, I have cheked my .conf files over and over again all was absolutely defined correctly. I ended up on this post tried the a2ensite and a2dissite commands as a sudoer, restarting apache, still landing on the default apache welcome page as if apache would have never disabled the default site or taken my new virtual host onboard.

    It turns out I was trying to restart apache without using sudo...

    service apache2 restart
    

    use

    sudo service apache2 restart
    
    0 讨论(0)
  • 2021-01-30 07:37

    If you have already tried the following:
    1. Checked permissions of the document root and parent folder.
    2. a2dissite 000-default etc.
    3. Restarted apache with sudo service apache2 reload
    And it is still not working then do the following:

    1. Enable debug logging:
    vi /etc/apache2/apache2.conf
    LogLevel debug

    2. Restart apache2 and monitor the logs
    sudo service apache2 restart
    tail -f /var/log/apache2/*.log

    3. Notice if the IP of the server is showing in the logs as expected:

    ==> /var/log/apache2/other_vhosts_access.log <== ip-xxx-xx-xx-xx.eu-west-2.compute.internal:80 xx.78.xx.2xx - - [13/Sep/2017:18:40:44 +0100] "GET / HTTP/1.1" 200 3509 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0"

    You'll notice that instead of xxx.xx.xx.xx:80 like a typical IP address
    I've got some FQDN like "ip-xxx-xx-xx-xx.eu-west-2.compute.internal:80"
    I'm using AWS Elastic IP's to make a fixed IP for my Amazon EC2 web server.

    4. Check the head of your virtual-hosts .conf file

    <VirtualHost 127.0.0.1:80 11.22.33.44:80>
    # Added from nessus to make more secure
    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]
    ServerName mydomain.com
    ...
    

    OK so what we see here is we're telling Apache to serve this vhost for requests from 127.0.0.1 or 11.22.33.44 on port 80. But the server never sees these IPs as it get's that weird FQDN and so it never matches! Eureka!

    SOLUTION:
    Add *:80 to the VirtualHost tag so it looks like this:

    <VirtualHost 127.0.0.1:80 11.22.33.44:80 *:80>
    


    AND restart apache. I hope this helps. There may be other reasons but if this is the cause of your problem then it's time to sit back, have a cuppa tea and relax! If not then I hope you find the solution.

    PS. Remember to put your logs back to warn rather than debug!

    0 讨论(0)
  • 2021-01-30 07:38

    My case on Apache 2.4 the issue was solved by adding below setting to virtual host configuration:

    Listen 80
    

    so that settings are something 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>
    
    0 讨论(0)
  • 2021-01-30 07:44

    tl;dr: Call it with sudo: sudo service apache2 reload


    Looks like the behaviour of service apache2 reload fooled me. See the following log:

    user@Laptop:/etc/apache2/sites-available$ sudo a2ensite test.conf 
    Enabling site test.
    To activate the new configuration, you need to run:
      service apache2 reload
    user@Laptop:/etc/apache2/sites-available$ service apache2 reload
     * Reloading web server apache2                                                  * 
    user@Laptop:/etc/apache2/sites-available$
    

    Try to reach http://test: NOT working

    user@Laptop:/etc/apache2/sites-available$ sudo service apache2 reload
     * Reloading web server apache2                                                  * 
    user@Laptop:/etc/apache2/sites-available$
    

    Try to reach http://test: WORKING

    So, find the difference! The point is that I thought it would've reloaded correctly in the first place. There are no entries in the log files either. Calling it with sudo helped. Is this a bug?

    0 讨论(0)
提交回复
热议问题