Apache local configuration to resolve files correctly

Alex E. picture Alex E. · Apr 9, 2010 · Viewed 13.8k times · Source

I have just configured Apache and PHP to work on my local Mac OS X computer. Now PHP works fine, except when I try to load the files for my live sites. The live sites have separate directories and are sorted by client name etc.

I've created symlinks in the default root for the local web server documents. My issue is that Apache doesn't seem to want to load any of the relative paths that are found in the HTML pages. For example, I have src="/css/main.css" but Apache doesn't load the file, similarly for images, it just resolves as a file not found 404 error. I then thought it might be the symlinks so I copied the full directory into the Apache document root, and still had the same result.

I would really love to setup my local development environment to run Apache, PHP, MySQL to develop locally then publish when ready. I also tried the MAMP installation, and had the same issues.

Answer

Robert McIntyre picture Robert McIntyre · Apr 9, 2010

First you might want to try using src="./css/main.css".

When dealing with multiple live sites I like to setup a single configuration file for each site with apache and then load them all together in the httpd.conf file.

for my setup it looks like this:

in /etc/apache2/httpd.conf

I have:

# Begin virtual host directives.

Include conf/bortreb.conf

Include conf/rlmcintyre.conf

Include conf/laserkard.conf

Include conf/judyates.conf

and then in /etc/apache2/conf/judyates.conf

I have:

    <VirtualHost *:80>
    #localhost site

    ServerAdmin [email protected]
    DocumentRoot "/home/r/Desktop/web/judyates"
    ServerName localhost
    ServerAlias judyates.localhost
    ErrorLog "/home/r/Desktop/web/judyates/log/error_log.log"
    ScriptAlias /cgi-bin/ "/home/r/Desktop/web/judyates/cgi-bin/"

    <Directory "/home/r/Desktop/web/judyates">
        Options Indexes FollowSymLinks
        Options +ExecCGI
        AddHandler cgi-script cgi pl py
        AllowOverride Options
        Order allow,deny
        Allow from all
    </Directory>

    </VirtualHost>


  <VirtualHost *:80>

    #live site
    ServerAdmin [email protected]
    DocumentRoot "/home/r/Desktop/web/judyates"
    ServerName judyates.com
    ServerAlias *.judyates.com
    ErrorLog "/home/r/Desktop/web/judyates/log/error_log.log"
    ScriptAlias /cgi-bin/ "/home/r/Desktop/web/judyates/cgi-bin/"


    <Directory "/home/r/Desktop/web/judyates">
        Options Indexes FollowSymLinks
        Options +ExecCGI
        AddHandler cgi-script cgi pl py
        AllowOverride Options
        Order allow,deny
        Allow from all

    </Directory>

    </VirtualHost>

This way works really well, because you can set the subdomain yoursite.localhost to loop back to your home ip address.

With this setup, when I work on judyates.com on my computer and want to test anythig, I just go to judyates.localhost in my web browser.

I have about 5 other sites all set up this way in their own *.conf file, so they can each live in their own directories on my computer that exactly match the directories they'll be in on the server.

The key is to use virtual hosts to go to different sites based on the subdomain.

You can learn how to configure subdomains that point to yourself here: http://digitalpbk.blogspot.com/2007/01/making-subdomains-on-localhost.html

My setup goes even one step further because I setup the server too. Whenever I want to update I load both the webfiles AND the apache config files, and that way the server exactly mirrors my local setup. The only difference is that the real judyates.com points to the server and not my home computer, so when people try to visit the site they get everything from the server.