Setup Dynamic Virtual Hosts To Automize Drupal With WorkFlow

A tool WorkFlow has a new functionality “construct-container” that sets up a new container, so you can spin(i.e. drush spin-it) new Drupal instances automatically. Besides creating database, directories in file system, the construct container task also sets up and configures virtual host for the new Drupal instance. In this post, we cover the approach that was taken to provide multiple virtual hosts solution for WorkFlow tool users.

The solution utilizes Apache module virtual_alias. To be able use the Wildcard domain names on the local machine, the DNS server – dnsmasq is used

Install virtual_alias Module

First, check to see if virtual_alias module is already enable and loaded with apache:

apachectl -M | grep "virtual_alias"

If this doesn’t return anything, then the virtual_alias module is not enabled. Lets do so:

sudo a2enmod virtual_alias
sude service apache2 restart

This should enable the virtual_alias module and load in apache. Go ahead and verify with “apachectl -M” to see if its loading

Set Up Universal Virtual Host Alias

In this example, we configure one universal virtual host for DEV Environment(for WorkFlow Tool there is additional for TEST environment) that is going to service all of the new Drupal instances. To do so, lets add new file – dev-virtual.conf in the /path/to/apache/sites-available(by default /etc/apache2/sites-available) as following:

<VirtualHost *:80>
  ServerAlias *.dev
  VirtualDocumentRoot /path/to/dev/%1
  ServerAlias localhost *.dev
  <Directory "/path/to/dev">
    Options +SymLinksIfOwnerMatch
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

Afterwards, lets enable the universal virtual host

sudo service a2ensite dev-virtual 
sudo service apache2 reload

Here, we specify root dir with ‘%1’ at the end to service each Drupal instance at its own directory. The ‘%1’ refers to the first part of domain name separated by dot. For example, metronic.dev will result the ‘%1’ refer to ‘metronic'(for more info see section ‘Directory Name Interpolation’).

As you see from the universal virtual host configurations above, the domain name we have set up to be *.dev, so next we configure all request *.dev point to the right machine.

Configure DNS or Set Up Own Local DNS

For public server, you will point all “*.dev” requests to the public server. In that case, all you need is to add the new DNS A entry in the DNS service as following:

*.dev   IN A    177.77.177.77

For local machines, we cannot use a wildcard in /etc/hosts, so we have to set up local DNS. Let’s use the light weight DNS solution – dnsmasq

sudo apt-get install dnsmasq

Once installed, we configure by editing /etc/dnsmasq.conf and adding line:

address=/dev/127.0.0.1

Here, we set *.dev requests point to 127.0.0.1 which is the local machine
Afterwards, restart the dnsmasq:

sudo service dnsmasq restart

Now, all request with the wild card (i.e. “*.dev”) will be pointed to local machine where the universal virtual host should service them from the directory specified.

Troubleshooting

1. client denied by server configuration: /path/to/dev/some

This error was caused by adding %1 at the end of path for the Directory configurations. Here is correct way:


VirtualDocumentRoot /path/to/dev/%1
...
//<Directory "/path/to/dev/%1">
<Directory "/path/to/dev">
...
                Order allow,deny
		Allow from all
...
</Directory>

By removing ‘%1’, the actual directory is found and no more errors. In short, when you have VirtualDocumentRoot, then the Directory root is the path to the virtual aliases instead the full path as it is with static virtual hosts configurations

References

  • http://brunodbo.ca/blog/2013/04/26/setting-up-wildcard-apache-virtual-host-wildcard-dns
  • http://float64.uk/blog/2014/08/22/dynamic-development-area-apache-php-fpm/
  • http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html
  • https://httpd.apache.org/docs/2.4/rewrite/vhosts.html
  • https://httpd.apache.org/docs/2.4/vhosts/mass.html
  • https://code.google.com/p/mod-myvhost/wiki/Install
  • http://eosrei.net/articles/2012/08/create-dynamic-virtual-hosts-apache-http-vhostalias

Installing Apache on Mac/Linux

Mac comes with apache pre-installed, however. You may wish to install your own apache instance
http://margotskapacs.com/2013/09/installing-apache-on-mac/
Before start, make sure the default apache server is not running(see troubleshooting section below). To turn off, go to ‘System Preferences->Sharing’ and uncheck ‘Web Sharing’

Step 1. Installing Apache via macport

port selfupdate
sudo port install apache2

This installes Apache in /opt/local/apache2/ directory

Step 2. Configure Apache

Apache comes with sample configurations, lets copy it

sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf

In the apache configuration file ../conf/httpd.conf change the following lines to specify root folder from where your websites is going to be serviced

...
<Directory "/opt/local/apache2/htdocs">
...
DocumentRoot "/opt/local/apache2/htdocs"

The path /opt[..]/htdocs’ can be anything you wish but in both places it have to point to the same folder

Next, add ‘index.php’ so that Apache grabs index.php file if present

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Afterwards, configure Apache interpret all files ending with .php/.phps by adding these lines:

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Finally, to enable user directories uncomment:

Include conf/extra/httpd-userdir.conf

Step 3. Activate PHP module

After installing and compiling PHP, there is PHP package that Apache needs to load, so here how to activate it

cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
[activating module 'php5' in /opt/local/apache2/conf/httpd.conf] 

Step 4. Start Appache

To start and stop Apache:

sudo /opt/local/apache2/bin/apachectl start
sudo /opt/local/apache2/bin/apachectl stop

After starting, point your browser to ‘locahost’ and it should work. To test php, add the following file to the folder you specified ‘DocumentRoot’ in above:

<?php
phpinfo();
?>

Afterwards point to this file ‘localhost/fileName.php’ and it should display php configurations

Enable Virtual Hosting

To Enable virtual hosting, uncomment the following line in apache configuration file conf/httpd.conf

Include conf/extra/httpd-vhosts.conf

Afterwards, configure you virtual host at conf/extra/httpd-vhosts.conf. Here is an example to get you started:

NameVirtualHost *:80
...
<VirtualHost *:80>
    DocumentRoot "/path/to/your/site"
    ServerName domainName.com
    <Directory /path/to/your/site>
        Options FollowSymLinks
        AllowOverride All
        Allow from all
        Order Deny,Allow
    </Directory>
    ErrorLog "/private/var/log/apache2/domainName.com-error_log"
    CustomLog "/private/var/log/apache2/domainName.com-access_log" common
</VirtualHost>

Troubleshooting

1. To see Apache looaded configs:
httpd -V
//or for Debian servers
apache2ctl -V
2. To see if Apache deamon is running
ps -ef | grep httpd
3./.htaccess: Options not allowed here

This happen after specifying alias in the virtual host but never declaring directive:

     <Directory /path/to/the/alias>
        Options FollowSymLinks
        AllowOverride All
        Order Deny,Allow
        Allow from all
    </Directory>
    Alias /path /path/to/the/alias

So, by including the “<Directory..” gives the permissions for the .htaccess manage the ‘Options’ configuration solving the problem you see in the log

4. Virtual Hosting is Not Working

Make sure you have added "NameVirtualHost *:80" in one of the configurations that is being loaded such apache2.conf, some-v-host.conf,etc

5. client denied by server configuration (PHP FPM)

Make user to have Require all granted part of the Virtual Host configuration Directory section as following:

<Directory "/path/to/serving/dir">
		Options Indexes MultiViews FollowSymLinks
		AllowOverride All
		Order allow,deny
		Require all granted
	</Directory>
6. Forms Will not submit or “AH00128: File does not exist” in logs

The forms could be submitted and the apache logs were showing “AH00128: File does not exist[..]” for Apache 2.4. There issue is that mod_rewrite is not enabled by default. You can check if the mod_rewrite is enabled and loading by looking at phpinfo(). To enable mod_rewrite:

sudo a2enmod rewrite
sudo service apache2 restart

This will load mod_rewrite making the forms to Post for Drupal

caught SIGTERM, shutting down

This error was coming up for Apache 2.4 on Ubuntu after logging into Drupal. I am not sure which exactly fix it but did the following:

  • turn off limit for MaxKeepAliveRequests in the apache2.conf configuration file
  • Increase memory in php.ini by changing memory_limit
  • Increased max_input_time in php.ini
  • Increased max_execution_time in php.ini

In addition, it appears that apache restarts with the above error every time there is php syntax error.

Blank Screen

By default Apache will display blank screen when there is php error. To turn on errors to be displayed in browser instead of blank screen, edit php.ini as following:

display_errors=On
html_errors=On
error_reporting=E_ALL | E_STRICT 
display_startup_errors=On

This will enable error reporting in the browser. In case it doesn’t, then at your application level there must be ErrorHandler defined that hides all the errors. See more info here http://stackoverflow.com/questions/1475297/phps-white-screen-of-death

Troubleshooting Tip

When there is error, turn on max logging by changing ErrorLevel to trace8 in virtual host settings or Apache global settings:

...
 LogLevel trace8
 ErrorLog ${APACHE_LOG_DIR}/dev-virtual/error.log
..

Afterwards, tail the logs specified in ErrorLog from command line to follow real time Apache execution:

tail -f APACHE_LOG_DIR/dev-virtual/error.log

At last, make the requests and follow the logs in command line:

Useful Links

  • http://www.thegeekstuff.com/2008/08/9-tips-to-use-apachectl-and-httpd-like-a-power-user/
  • http://2tbsp.com/content/install_apache_2_and_php_5_macports

Installing PHP 5.2 and configure Apache 2.2 on Windows & Linux

 

Over the years it has been times when i have to reinstall PHP to configure with Apache to work together. Today, it has been that occasion where i have to set up my local developing environment on new laptop to support some WordPress, Zend and Magento developments. It has never been a smooth process and it was no different this time….i guess i was too naive thinking that things have changed….Perhaps, things have changed, i just should have looked at new ways – deploying on cloud via Saas, however. This post is PHP(3.2.17) installed and configured with Apache 2.2.22

 

Installing PHP 5.2

My first failure come with the fact that the newest PHP 5.4 and 5.3 does not support Apache. Make sure you download the right  version VC6 (on Sep 13,2012).  Later, i also learn there is differences between Thread Safe and Non-Thread Safe versions. To run with Apache 2.2.22, you need Thread safe version

1. Download proper PHP version to work with Apache on Windows

2. Extract to your chosen directory. In our case, we use ‘C:/DevTools/php’

3. Update the windows environment variable ‘Path’.

We create new variable ‘PHP_HOME’ and then append ‘%PHP_HOME%’ to the ‘PATH’ variable. To check it works, open command line and type ‘php -v’. If it works then you should see php version displayed

 

Configuring Apache 2.2

I have Apache 2.2.x installed and tested currently. For how to install Apache server, please, consult other source

1. Edited Apache Configuration file httpd.conf (by default in C:\Program Files\Apache Software Foundation\Apache2.2)

In the httpd.conf, we add these lines,  right below the other LoadModule lines:
LoadModule php5_module "c:/DevTools/php/php5apache2_2.dll" #(change the path to wherever the file is)
AddType application/x-httpd-php .php .phtml .inc .php3
AddType application/x-httpd-php-source .phps

!!! Important to note, if you have Apache2.0, then use php5apache2.dll instead of php5apache2_2.dll

 

2. For Apache to use index.php as the default index page, change the line
DirectoryIndex index.html
-to-
DirectoryIndex index.php index.html index.htm

 

3. Copy the file “php5ts.dll” to “c:\Program Files\Apache Group\Apache2\bin\”.

The ‘ts’ at the end of php5ts.dll must mean Thread Safe, so if you don’t see it you must have happen to download & install non thread safe version of PHP that will not work with Apache 2.2.22

 

4. Copy the file “php.ini-recommended” to c:\Program Files\Apache Group\Apache2\ and rename it to “php.ini”

 

5. For PHP sessions to work properly, you need to specify where to save the sessions.

Edit the php.ini file, and find the setting for session.save_path. Set it to “c:\windows\temp\” or another folder you use as a temp directory.

5a.Enable GD extension for Image handling
The GD extension is used so often to handle the images that we recommend to enabled this extension at this time. To do so uncomment the following line and specify path to the GD extension in php directory

extension="C:\path_to_php\ext\php_gd2.dll"

 

6.  Restart Apache server:

Go to start -> run -> services.msc

or

From Apache Service Monitor (Start->All Programs->Apache HTTP Server 2.2->Apache Service Monitor->ReStart)

Test Drive

Its time to test our php configuration to work with appache server. To do so, create a file phpinfo.php with content:

<?php print phpinfo(); ?>

Save this file into dir Apache is serving pages(by default its “c:\Program Files\Apache Group\Apache2\htdocs”)
This should display php configuration info file that is also useful when installing add-ons of PHP because you can come here and always check if a particular add-on was installed for the PHP

Configure MySql

We assume you have mysql installed. To configure PHP to work with mysql you have to uncomment the following line and specify path to the libraries in php.ini file

extension="C:\path_to_php\ext\php_mysql.dll"
extension="C:\path_to_php\ext\php_pdo_mysql.dll"

This will enable necessary libraries for your php sites to talk to mysql database. We also enabled pdo_mysql library that is often used by opens source platforms such as Drupal

Virtual Hosts

Virtual host allows you easy to configure multiple sites to run on your apache server with different domain names

1. Enable Virtual Hosts

To enable the virtual hosts, uncoment the following line in path_to_apache/conf/httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Here we uncomment line ‘Include..’ so the vhosts configurations are loaded

2. Configure Virtual host

To configure virtual host, add the following in path_to_apache/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "C:\path_to_sites\my-site"
    ServerName some-domain
    <Directory C:\path_to_sites\my-site>
        Options FollowSymLinks
        AllowOverride All
	Allow from all
	Order Deny,Allow
    </Directory>
    ErrorLog "C:\server\apache\logs\stefan-cascade-error_log"
    CustomLog "C:\server\apache\logs\stefan-cascade-access_log" common
</VirtualHost>

Here, the ‘DocumentRoot and ‘Direcory’ specifies the path where is the code base for your site located. The ‘ServerName’ specifies domain name of your site(i.e. some-domain). This can be anything as long as you update the hosts file to point request to your local computer/apache server the next step

3. Point Domain name to Apache server

In previous step, you specified domain. Lets update hosts file, so all requests made to this domain name from your browser goes to the local apache server. To do so update %SystemRoot%\system32\drivers\etc\hosts as following

127.0.0.1      some-domain

This will make all requests to ‘some-domain’ go to your local computer/apache server

Test Virtual Host

Create file of index.html with some text and put it into your virtual host as specified above(i.e.C:\path_to_sites\my-site). Afterwards restart apache and open browser with ‘some-name’ as url. This should display the text in your index.html file

Issue Encountered

1. When restarting Apache, it was giving the following error:

“The Requested operation has failed!”

The Apache logs showed no clues of whats the problem, so I run 'httpd.exe -e debug' from command line in apache/bin folder. From there, it was clear the problem:

Solution: this is because we are installing PHP that is Non-Thread safe version but Appache 2.2.22, apparently, requires to have PHP Thread Safe

2. “Permission denied: AH00529: […].htaccess pcfg_openfile: unable to check htaccess file”

This happens when the Apache user (often www-data or if not, check the file – envvars in apache dir) doesn’t have access to read your web directory. The solution is to add Appache user to the group assigned to the web dir

usermod www-data --append --groups root

Here, we add user www-data to a group named root that is currently assigned to the web directory serving your site

AH01630: client denied by server configuration

This error came after upgrading from Apache 2.2 to Apache 2.4 with old configurations of “Order allow,deny

Allow from all” in virtual host setting in 2.2 with new configuration of”Require all granted” in the 2.4

Other

1. Host File

In case you forgotten, the host file in windows are located

 	%SystemRoot%\system32\drivers\etc\hosts