views:

2525

answers:

5

I'm doing a job for a guy with a site online. It's an alien site to me, and I'm slowly working through the strange code. I have MAMP locally and my http://localhost/ has many client folders coming off from that. Inside this code there is a lot of $_SERVER['document_root'] commands and references like which are just getting lost on my local PHP dev area.

How can I easily set the document_root reference to what it should be (just locally though, don't really want to mess with the site files, as I'll need to upload them again and don't want to break live site! And is there a way of indirect setting where PHP thinks the root of the site is so the image's src references "/images/..." will show up properly... My local PHP dev URL for this site is: http://localhost:8888/_CLIENTS/clientsite/www/ ...but in the code the '/' at the beginning of '/images/...' is referencing http://localhost:8888/ ??

Thank you.

A: 

For the current process, you can just do

$_SERVER["document_root"] = "whatever";

Be careful though.

Eli
A: 

Maybe you would find the following site useful. There are simple-to-follow tutorials that show you just what settings to manipulate to set up this'n'that to you satisfaction. I found a lot explained there.

Tanguay's tutorial

Peter Perháč
+1  A: 

It's a server-specific setting. If you're running Apache, all you'll need to do is edit your httpd.conf file (on a Unix-based system, it should be either in /etc/apache2/httpd.conf or /etc/httpd/httpd.conf, depending on which version of Apache you have). There should be a line in the file that looks like this:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/whatever/your/document/root/is"

Technically, Eli's way works as well, but I don't think editing server variables is really a good idea, in general.

htw
+2  A: 

What I would recommend is vhosts so you can serve up "alien site" locally without messing with your default web server.

  • localhost -> your start page or whatever
  • alien.localhost -> clients site, whatever path / doc root you want.
  • x.localhost -> another site

In apaches global config file or included vhost.conf;

NameVirtualHost localhost:80
# the mysql tool's url
<VirtualHost phpmyadmin.localhost:80>
# and absolute path
DocumentRoot "/srv/www/phpMyAdmin/"
</VirtualHost>

#Same for the Client Site
<VirtualHost foo.localhost:80>
DocumentRoot "/path/to/desired/webroot/"
</VirtualHost>

You can control permissons and set a overall global site by specifying the below first

in apache's global server config

DocumentRoot "/srv/www/htdocs"
#
# Configure the DocumentRoot Properties
#
<Directory "/srv/www/htdocs"> 
    Options All
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    AllowOverride All
    # Controls who can get stuff from this server.
    Order allow,deny
    Allow from all
</Directory>
#
# Configure Sub-Domain Properties. This prevents those nasty 403 errors
#

# mysql administration tool
<Directory "/srv/www/phpMyAdmin/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

# a client web site built with CakePHP
<Directory "/home/eddie/workspace/Digital_Business/app/webroot/">
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
Eddie
This looks perfect. But did exactly what you said, restarted Apache on MAMP, made sure all the filepaths were relavant to me... Nothing! Just doesn't work when I put the address in the browser...
Yea, I think in this situation it's best to simply leave the code alone and configure Apache properly. That's the correct way to do it anyway.
Calvin
James, on a Windows you may need to edit your hosts file: http://en.wikipedia.org/wiki/Hosts_file
Calvin
I'm pretty sure that holds true for Unix-based systems as well. (The hosts file would be in /etc/hosts).
htw
@James, Calvin and htw are correct. You will need to set the host file of your system to know that whatever.localhost is a valid URL that resolves to 127.0.0.1 (loopback)Once that is set Apache will receive the request and take over.
Eddie
An additional step I needed (maybe specific to the default XAMPP install) was to uncomment the following line in httpd.conf so that the virtual host config file would actually be used by apache: Include /Applications/xampp/etc/extra/httpd-vhosts.confWith that and the changes above I got this working. Thanks!
bmoeskau
+1  A: 

@Eddie's answer helped me a lot, but I had to still do a little extra research to solve the same problem for myself using XAMPP on OSX. I thought I would add my full solution here for the benefit of posterity.

First I added the following entries to httpd-vhosts.conf (under the "etc/extra/" folder in XAMPP):

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Applications/xampp/xamppfiles/htdocs/"
</VirtualHost>
<VirtualHost *:80>
    ServerName client1.my-machine
    DocumentRoot "/Applications/xampp/xamppfiles/htdocs/clients/client1/"
</VirtualHost>
<VirtualHost *:80>
    ServerName client2.my-machine
    DocumentRoot "/Applications/xampp/xamppfiles/htdocs/clients/client2/"
</VirtualHost>

Note that I specifically used a wildcard instead of "localhost" for the VirtualHost urls and added the ServerName config where I specified each subdomain name. Note also that I used my machine's name ("my-machine") instead of "localhost" -- that way all requests from any machine (not just localhost) can be properly evaluated. I develop in OSX but test browsers in various VM's via Parallels. Using this approach I can access http://client1.my-machine from any machine or VM on my network. With "localhost" specified it would only work on my development machine.

NOTE: The first VirtualHost entry is used as the default (as explained here: http://httpd.apache.org/docs/2.2/vhosts/name-based.html) and is required so that requests do not default to one of the custom sites.

I also added the permissions settings to httpd.conf as shown in @Eddie's answer. This is not always required, but I ran into 2 separate cases where I needed to make this change:

  • Basing a new site in a directory outside of the default XAMPP http root (basically any path that will be accessible via http must have explicit permissions set)
  • Adding mod_rewrite rules for a site set up as a virtual host (under the default http path), I was getting the error ".htaccess: RewriteEngine not allowed here". Reading the comments in httpd.conf about the AllowOverride option makes the cause of the error obvious, but I had overlooked that before. Changing this to "All" fixed the error.

Note that while editing httpd.conf, you may need to uncomment the following line (it was commented out for me by default), or the vhosts change made above will not take affect:

# Virtual hosts
Include /Applications/xampp/etc/extra/httpd-vhosts.conf

Finally, I also had to add the custom domain names to my hosts file as noted in the comments above. On OSX, you do this by editing "/private/etc/hosts" (on Windows this would be "Windows/System32/drivers/etc/hosts") and added the following lines:

127.0.0.1   my-machine
127.0.0.1   client1.my-machine
127.0.0.1   client2.my-machine

NOTE: In the default OSX Finder UI, hidden folders (including /private) are not visible. You can change this permanently by hacking internal Finder options (Google for details), or more simply to make an occasional change, just use the "Go > Go to folder" menu option which will let you open hidden folders directly by name. Personally, I use a third party OSX shell called PathFinder that I would heartily recommend (it is worth the small license fee). It includes a menu option to hide/show hidden files, among many other useful features.

One thing that's a drag is that I also did have to add matching entries in my Windows VM hosts file pointing to my physical dev machine so that the urls would resolve via Apache/OSX:

192.168.1.5 client1.my-machine
192.168.1.5 client2.my-machine

I don't need an entry for the machine name alone (that resolves automatically) but adding the subdomain to it does not resolve correctly without those host entries. This does suck in that on occasion my Mac's IP changes (via DHCP), but it's a minor nuisance. I would assume that I could set it up to not need those IP's, but I could not figure that out and am ready to move on :) (If someone knows the answer please leave a comment)

Now I have multiple client sites running in one place and accessible from all of my dev/test environments. Hope this helps someone else.

bmoeskau