The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Test::Instance::Apache - Create Apache instance for Testing


  use FindBin qw/ $Bin /;
  use Test::Instance::Apache;

  my $instance = Test::Instance::Apache->new(
    config => [
      "VirtualHost *" => [
        DocumentRoot => "$Bin/root",
    modules => [ qw/ mpm_prefork authz_core mime / ],



Test::Instance::Apache allows you to spin up a complete Apache instance for testing. This is useful when developing various plugins for Apache, or if your application is tightly integrated to the webserver.


These are the attributes available on Test::Instance::Apache.


The root folder for creating the Apache instance. This folder is passed to Apache during instantiation as the server root configuration, and normally contains all the configuration files for Apache. If not set during object creation, a new folder will be created using File::Temp.


The directory for holding the configuration files. Defaults to $server_root/conf. If set during object creation, then you will need to create the folder manually.


The directory for holding all the log files. Defaults to $server_root/logs. If set during object creation, then you will need to create the folder manually.


The path to the main configuration file. Defaults to $conf_dir/httpd.conf. This is then used by Test::Instance::Apache::Config to create the base configuration file.


Takes an arrayref of values to pass to Test::Instance::Apache::Config.


Takes an arrayref of modules to load into Apache. These are the same names as they appear in a2enmod, so only the modules which are available on your local machine can be used.


Path to the pid file for Apache. Defaults to $server_root/


Port for Apache master process to listen on. If not set, will use "empty_port" in Net::EmptyPort to find an unused high-number port.


Path to the main Apache process. Uses "which" in File::Which to determine the path of the binary from your $PATH.


Pid number for the main Apache process. Set during "run" and then used during "DEMOLISH" to kill the correct process.


These are the various methods inside this module either for internal or basic usage.


Sets up all the pre-required folders, writes the config files, loads the required modules, and then starts Apache itself.


Used internally to create folders under the server root. Will take an array of directory names, which are then passed to File::Spec - so if you do the following:

  $instance->make_server_dir( 'a', 'b', 'c' );

Then a path of $server_root/a/b/c will be created.


Returns the contents of the first line of the pid file. Used internally to set the pid after startup.


This will return all the items in the log directory as a hashref of filename and content. This is useful either during test development, or if you are testing exceptions on your application. Please note that it does not recurse subdirectories in the logs folder.


This is more for use during development of this module - prints out the path of all the files and folders stored as attributes in this module.


Kills the Apache instance started during run.


Tom Bloor <>

Initial development sponsored by Runbox


Copyright 2016 Tom Bloor


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.