0

In Ubuntu 2016.04's default Apache2, I tried to add a virtual host with an alias:

sudo mkdir /var/www/simplesamlphp
echo "Hello" > /var/www/simplesamlphp/index.html (as root)
sudo chown -R www-data:www-data /var/www/simplesamlphp
sudo chmod -R 755 /var/www

Second, I created /etc/apache2/sites-available/simplesamlphp.conf:

<VirtualHost *>
    ServerName simplesamlphp
    DocumentRoot /var/www/simplesamlphp

    SetEnv SIMPLESAMLPHP_CONFIG_DIR /var/simplesamlphp/config

    Alias /simplesaml /var/simplesamlphp/www

    <Directory /var/simplesamlphp/www>
        <IfModule !mod_authz_core.c>
        # For Apache 2.2:
        Order allow,deny
        Allow from all
        </IfModule>
        <IfModule mod_authz_core.c>
        # For Apache 2.4:
        Require all granted
        </IfModule>
    </Directory>
</VirtualHost>

Third, I added this line to /etc/hosts:

127.0.0.1       simplesamlphp

Fourth, I ran:

sudo a2enmod rewrite
sudo a2ensite simplesamlphp.conf
sudo service apache2 restart

PROBLEM: Accessing http://simplesamlphp/simplesaml gives The requested URL /simplesaml was not found on this server and the following appears in /var/log/apache2/error.log:

AH00128: File does not exist: /var/www/html/simplesaml

What did I do wrong?
By the way, I am following these instructions. Actually, I am not sure why a DocumentRoot is needed despite all web content being in /var/simplesamlphp/www.

Nicolas Raoul
  • 11,603
  • Multiple problems: http://askubuntu.com/questions/230476/when-using-sudo-with-redirection-i-get-permission-denied, you enable simplesamlphp.conf but your file is named simplesaml.conf, your commands use /var/www/simplesamlphp and your config uses /var/simplesamlphp/www. Maybe you should go sleep a while. – muru Nov 25 '16 at 11:33
  • @muru: I fixed the first two errors in the question, thanks for pointing out! About the third one, I am just faithfully reproducing the configuration seen at https://simplesamlphp.org/docs/stable/simplesamlphp-install#section_6 and chose the hostname simplesamlphp instead of service.example.com. – Nicolas Raoul Nov 25 '16 at 15:10

1 Answers1

2

Note to readers: Please read Henning Kockerbeck's comments. Apache 2.4 virtual hosts functionality is well described in the official guide. The following answer is out of date.

The best explanation why the procedure works is the following (from the documentation):

When a request arrives, the server will find the best (most specific) matching argument based on the IP address and port used by the request. If there is more than one virtual host containing this best-match address and port combination, Apache will further compare the ServerName and ServerAlias directives to the server name present in the request.


If you haven't disabled the default site 000-default you now have two <VirtualHost *> declarations, and the default one is not name-based. Apache cannot distinguish between them, and in order to solve the ambiguity it will simply ignore the second one. You can proceed in three different ways:
  • You may choose to disable the default site, or
  • You may choose to make your new virtual host listen on a different port, or
  • You may choose to convert the default site into a name-based virtual host by editing 000-default.conf, uncommenting the ServerName directive and assigning a different name to it.
AlexP
  • 10,197
  • Sorry, but that's incorrect. The basic purpose of virtual hosts is to run multiple websites or hosts on the same physical host, ip and port, hence the name virtual host. So of course it's possible to use the same port for multiple vhosts. – Henning Kockerbeck Nov 25 '16 at 15:10
  • You are describing name-based virtual hosts. The original poster did not disable the default site, and as a consequence that virtual host is still active. And as you well know, the default virtual host is not name-based. Thank you for retiring your downvote. – AlexP Nov 25 '16 at 15:20
  • It seems like that solved the problem! A strange thing is that the tutorial at https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts shows how to create two virtual hosts by using <VirtualHost *:80> in both... – Nicolas Raoul Nov 25 '16 at 15:36
  • Please mark the answer as accepted if your problem has been solved. The official guide to virtual hosts. – AlexP Nov 25 '16 at 15:36
  • @AlexP I'm sorry, but I disagree. Since Apache 2.3.11, name-based virtual hosting is automatically enabled as soon as the same IP:port combo is used in multiple vhosts. So as soon as another vhost on, say, *:80 is activated, the default vhost "switches" to name-based and doesn't block the ip and/or port. Maybe the problem was that the OP used <VirtualHost *> without explicitely defining the port like <VirtualHost *:80>. But I didn't specifically test that. – Henning Kockerbeck Nov 25 '16 at 16:10
  • You are right and I was wrong. I am editing the response to point to your comments. – AlexP Nov 25 '16 at 16:18
  • Thanks :) I've retired the downvote. Do you happen to know if the difference between <VirtualHost *> and <VirtualHost *:80> could cause the problem the OP described? – Henning Kockerbeck Nov 25 '16 at 18:10
  • The documentation says: "When a request arrives, the server will find the best (most specific) matching argument based on the IP address and port used by the request. If there is more than one virtual host containing this best-match address and port combination, Apache will further compare the ServerName and ServerAlias directives to the server name present in the request". Maybe Apache considers *:80 to be more specific than *; that would explain why the default virtual host took precedence. – AlexP Nov 25 '16 at 18:15
  • Agreed. Do you want to add this to your answer as well? – Henning Kockerbeck Nov 25 '16 at 22:59
  • To be sure I created a new VM and performed the exact same steps as I described in my question. I met the exact same problem. And once again, your answer solved the problem :-) Namely a2dissite 000-default.con then service apache2 reload. – Nicolas Raoul Nov 29 '16 at 09:27