0

I need to have multiple local sites in apache. So I add a virtual host for each one:

#/etc/apache2/sites-available/example.com:
<VirtualHost 127.0.0.1:80>
        ServerAdmin [email protected]
        ServerName  www.example.com
        ServerAlias example.com

        # Indexes + Directory Root.
        DirectoryIndex index.php
        DocumentRoot /var/www/example.com/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /var/www/example.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>


        # Logfiles
        ErrorLog  /var/www/example.com/logs/error.log
        CustomLog /var/www/example.com/logs/access.log combined
</VirtualHost>

And then I do sudo a2ensite example.com and sudo service apache2 restart. The problem is that root-relative paths don't work. For example when I say <link href="/css/somefile.css" rel="stylesheet"> somewhere in /var/www/example.com/htdocs/index.php, apache fails to serve /var/www/example.com/htdocs/css/somefile.css to browser (browsing: localhost/example.com/htdocs)

What I'm missing?

5
  • That looks good to me, did you make sure that the Apache process would have permission to access the .css file? Dec 4, 2013 at 22:11
  • @AlexisWilke Since it works with relative path, I guess yes apache can see the file.
    – sorush-r
    Dec 4, 2013 at 22:21
  • Why do you browse to localhost/example.com/htdocs it should be example.com only Dec 4, 2013 at 23:26
  • @rechengehirn example.com lists htdocs, cgi-bin, etc.
    – sorush-r
    Dec 5, 2013 at 6:37
  • If you're testing multiple sites in a non production environment, you need to set up some fake DNS entries. The browser should tell apache what domain for which it expects to get a response. Just what I think the problem probably is. Dec 5, 2013 at 8:11

2 Answers 2

2

I think the problem is that you access the site with localhost/example.com/htdocs

You can do the following:

In your vhost configuration switch ServerName and ServerAlias:

ServerName  example.com
ServerAlias www.example.com

restart apache2 with:

sudo /etc/init.d/apache2 restart

Now in /etc/hosts add the 2 lines:

127.0.0.1 example.com
127.0.0.1 www.example.com

You sould now be able to access the site with www.example.com or example.com. (This works if you are working on the same machine where you have your apache installed)

And your path should work as expected.

Please comment, if you have any problems.

1
  • That is what I was missing all the time :)
    – sorush-r
    Dec 5, 2013 at 11:06
0

Ah! I think I understand your problem...

The link tag can use a full path, but the full path starts at the document root:

<link href="/css/somefile.css" .../>

The full path on disk (/var/www/example.com/htdocs/css/somefile.css) would not work indeed.

What Apache does is this:

DocumentRoot /var/www/example.com/htdocs/
+
<link href="/css/somefile.css" .../>
=
/var/www/example.com/htdocs/css/somefile.css

The DocumentRoot is some kind of virtual root (/) directory as far as the website is concerned. This prevents your visitors from accessing files they should never have access to (think of a file such as: /etc/passwd)

1
  • I like apache to do so. but it doesn't!
    – sorush-r
    Dec 5, 2013 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .