Apache2: Virtual Host Clients & PHP Configuration
Updated 2017-02, Ubuntu 16.04 LTS. Apache 2.4.18 and PHP 7.0.13.
Go to the first page Apache2: Activating User Directories & Virtual Hosts and read the disclaimer. Thanks.
Step 4 – 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.
# Contents of file /etc/apache2/mods-available/php7.0.conf AddHandler application/x-httpd-php .html .htm .file <FilesMatch ".+\.ph(p[3457]?|t|tml)$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch ".+\.phps$"> SetHandler application/x-httpd-php-source # Deny access to raw php sources by default # To re-enable it's recommended to enable access to the files # only in specific virtual host or directory Require all denied </FilesMatch> # Deny access to files without filename (e.g. '.php') <FilesMatch "^\.ph(p[3457]?|t|tml|ps)$"> Require all denied </FilesMatch> # Running PHP scripts in user directories is disabled by default # # 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_flag engine Off # </Directory> #</IfModule>
NOTE: This method is working with the data from the phpinfo() command stating “Virtual Directory Support” is “Disabled”.
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 computer, but I’m not sure why I still have it, as I don’t use it for anything any more (okay, 35mm negative scanning software and photo panoramas). There are two files “hosts” and “lmhosts” kept in C:\windows\system32\drivers\etc. Same place under Windows 10, 64bit machines. 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.
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 the webserver. 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:
Open a text editor as root ($sudo gedit) and open the file /etc/php5/apache2/php.ini.
Go to the section “Paths and Directories” (search for “include_path”). Copy the following:
include_path=".:/inc:./inc:../inc:../../inc:../../../inc:../../../../inc"
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. Here is the restart apache command again
$sudo /etc/init.d/apache2 restart
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. Note for shared Web Hosting: If you use a shared server, I haven’t found a work-around. I put a php.ini file in each directory containing the one line in #3 above.
Seeing PHP As Text & The “phtml” Error: If the page is getting served, but not as PHP, you don’t have PHP configured correctly.
- Make sure you that the file /etc/apache2/mods-available/php7.conf exists. If not, you don’t have a chance.
- See the note on the first page about reinstalling apache and php.
- If you make changes, it can’t hurt to restart the server (you know, $sudo service apache2 restart).
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.