Printed from http://kimbriggs.com

Apache2: Virtual Hosts for Clients & 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. The audience is mainly 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 7.10 (Gutsy Gibbons) 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.

Step 4: Configure a Client Machine for Virtual Directories

Case A - Linux: No problemo. We have done this already in Step 3, Part A, #5-7. 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. The link goes to the GUI explanation, but it is as simple as adding a line to /etc/hosts in the form of "192.168.1.xx com newcom", where "xx" is the fixed IP of your web server (not your client) and "com newcom" are the virtual domains 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 newcom). 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 line 476 in 2007-12.
  3. Copy the following: include_path=".:/inc:./inc:../inc:../../inc:../../../inc:../../../../inc"
  4. 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.
  5. Here is the restart apache command again: $sudo /etc/init.d/apache2 restart
  6. 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.
  7. Note for shared ISP: 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.
  3. 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.

Google
 
CC License Ubuntu Bluefish Editor Graphics by GIMP Eliminate DRM Get Firefox php.net Play Ogg Valid XHTML 1.0 why the icons?