Printed from http://kimbriggs.com

Apache2: Virtual Hosts for Clients in Your Private LAN & PHP Configuration

Go to the first page Apache2 public_html & Virtual Directories.

Summary: Let the hacker beware

[Duplicate paragraph from first page.] These pages describes how I activated the public_html user directories to be served up by the Apache web server and set up virtual directories, meaning fake domains for a private LAN. The audience is developers and home users who want to work on their websites at their desktop computers. I describe what has worked for me and some problems you might encounter using Ubuntu Linux desktop software. I have no idea if this is applicable to the GUI-less server software. These are experiences I had using computers in my home. They may or may not work for you. I also don't know the distinctions between virtual hosts, virtual directories, and virtual domains.

NOTE: There have been more than a couple searches leading here that are similar to "configure PHP virtual hosts". I don't know what you're trying to do, but I can tell you definitively that Virtual Hosts are not a function of the PHP scripting language. You can use the link above to find out what I did on my private LAN for virtual hosts. The section below shows how I use PHP includes in a shared hosting environment. Otherwise, you need to learn more about web servers. If you are opening your web pages without using "http://" (localhost, etc.) you are not using Apache and therefore will never get PHP to function.

Step 4: Configure a Client Machine for Virtual Directories

Case A - Linux: No problemo. We have done this already in Step 3, Part A. I'm not going to paste the whole thing here. The objective is to modify /etc/hosts to relate the server's IP (you're glad it's fixed now, eh?) to ALL of the virtual domains you want to be able to use. It is as simple as adding a line to /etc/hosts in the form of "192.168.1.xx com farmdens", where "xx" is the fixed IP of your web server (not your client) and "com farmdens" are the virtual hosts separated by spaces.

Case B - Windows: Not really that much harder, I just can't help you out as much. Currently, I have one Windows XP computer, but I'm not sure why I still have it, as I don't use it for anything any more. There are two files "hosts" and "lmhosts" kept in C:\windows\system32\drivers\etc. You set them up just like /etc/hosts under linux: one line for each IP and aliases, separated by spaces (e.g., 192.168.1.xx com farmdens). The test I did seems to indicate you only need to set up the "hosts" files for web browser usage. I don't think it could hurt to keep them both up to date. Here are some notes on host files. I think I remember one time where only the Administrator could use the file, but this seems too silly to be true. It's worth a try if you have problems...


Step 5: Configuring PHP

Remarks: If you use the Debian or Ubuntu package managers, you are ready to go "out of the box" for just about everything. I'll list a couple of tweaks that might be applicable to a home server development environment. None of this is required if you only intend to use static HTML pages.

Setting the include_path: This is probably the most common thing you would want to do. I think the includes path is commented out of the original php.ini file. For a time I used an absolute path for this variable and it made the multiple websites NOT be completely the same as on-line. The simple trick is to use only relative paths and keep all of your includes in a directory of the same name on all of your sites. It is NOT the same physical directory, just the same name. "inc" seems to be a common standard. Here are the steps:

  1. Open a text editor as root ($sudo gedit) and open the file /etc/php5/apache2/php.ini.
  2. Go to the section "Paths and Directories", around lines 470-500.
  3. Copy the following:

    include_path=".:/inc:./inc:../inc:../../inc:../../../inc:../../../../inc"

  4. Optional: Or, if you are using shared resources, like PEAR:

    include_path=".:/inc:./inc:../inc:../../inc:../../../inc:../../../../inc:/usr/share/php"

  5. Create a blank line under the line that says "Unix", and paste in your include_path value. There should be only one without a semi-colon (comment) symbol.
  6. Here is the restart apache command again:

    $sudo /etc/init.d/apache2 restart

  7. If your site is "deeper" than I have the path set up for, then first rethink your site. Second, you can just extend the line with another colon and a ../../../../../inc entry.
  8. Note for shared Web Hosting: If you use a shared server, like my site at Lunarpages, I haven't found a work around. I put a php.ini file in each directory containing that one line from #3:

Enable PHP for .html Files: You might have migrated an existing .html page to PHP and want it processed by the PHP interpreter. I use a generic extension of ".file" for everything that is not an index page so that it is totally generic and I can change technologies in the future without changing the file name. To set it up:

  1. Open a text editor as root ($sudo gedit) and open the file /etc/apache2/mods-available/php5.conf
  2. Add the extensions you want to the line that starts off "AddType application/x-httpd-php" (e.g., .php .html .htm .file) separated by spaces.

    AddType application/x-httpd-php .php .html .htm .file

  3. ORYou may need to use this wording now. I don't know why it may have changed.

    AddHandler application/x-httpd-php .php .html .htm .file

  4. Here is the restart apache command again:

    $sudo /etc/init.d/apache2 restart

Seeing PHP As Text & The "phtml" Error: Okay, let me put on my Captain Obvious hat and say, "You don't have PHP configured correctly". I can think of 2 or 3 things to do for apache2 and php5:

Epilogue: Good Luck, I hope some of this is useful, but remember it may not work for you. Always make backups of files you are changing. I collected all this information after making lots of mistakes over a few years. Thanks to the Free Software community for posting all the information in various places. I can't say what information came from where. Recycle, rinse, repeat.

Buy gold online - quickly, safely and at low prices

 
CC License Ubuntu Get OpenOffice Graphics by GIMP Bluefish Editor Eliminate DRM Get Firefox php.net Play Ogg what's this?
 
Custom Search