Printed from http://kimbriggs.com

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

Updated 2012-01, Ubuntu 10.04.03 LTS.

Go to the first page Apache2 public_html & Virtual Directories and read the disclaimer. Thanks.

Enable (Activate) PHP for User Directories. There have been more than a couple searches leading here that are similar to "configure PHP virtual hosts". There is a message in the /etc/apache2/mods-available/php5.conf file that tells you "comment the following lines". Just add the hash marks and remember to restart apache and clear your browser's cache after the changes are made. A more detailed description is given at the Ubuntu User-Directory-PHP Wiki. Here is my copy with the (totally unprofessional) hack to get ".html" and ".file" files processed as PHP.

# Contents of file /etc/apache2/mods-available/php5.conf
<IfModule mod_php5.c>
  AddHandler application/x-httpd-php .html .htm .file
  <FilesMatch "\.ph(p3?|tml)$">
    SetHandler application/x-httpd-php
  </FilesMatch>
  <FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
  </FilesMatch>
  # To re-enable php in user directories comment the following lines
  # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
  # prevents .htaccess files from disabling it.
    #<IfModule mod_userdir.c>
    # <Directory /home/*/public_html>
    # php_admin_value engine Off
    # </Directory>
    #</IfModule>
</IfModule>

Step 4: Configure a Client Machine for Virtual Directories

Case A - Linux:This was done 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.


Step 5: Configuring PHP

Remarks: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" (search for "include_path").
  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 the one line in #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 "AddHandler application/x-httpd-php" (e.g., .php .html .htm .file) separated by spaces.

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

  3. Here is the restart apache command again:

    $sudo /etc/init.d/apache2 restart

Seeing PHP As Text & The "phtml" Error: If the page is getting served, but not as PHP, you don't have PHP configured correctly.

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.

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?