How to set the DOCUMENT_ROOT and site root in my local PHP dev setup?

前端 未结 5 1640
孤街浪徒
孤街浪徒 2021-01-30 11:56

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

5条回答
  •  一生所求
    2021-01-30 12:46

    @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
    
    
        ServerName localhost
        DocumentRoot "/Applications/xampp/xamppfiles/htdocs/"
    
    
        ServerName client1.my-machine
        DocumentRoot "/Applications/xampp/xamppfiles/htdocs/clients/client1/"
    
    
        ServerName client2.my-machine
        DocumentRoot "/Applications/xampp/xamppfiles/htdocs/clients/client2/"
    
    

    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 effect:

    # 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.

提交回复
热议问题