Apache, For Instance

If you need a staging environment for a new network service, don't run out and buy new hardware — even a new virtual machine may be unnecessary. Instead, create another instance of the service with just a few simple steps.

Linux can run on just about any personal computer. Moreover, the Linux/Apache/MySQL/Perl (or PHP, Python, or Ruby) stack runs well on commonplace hardware, too, making it easy to (quite literally) carry a complete website — development tools, data, and daemons — on an off-the-shelf laptop.

In fact, if you must run other versions or instances of a service on the same portable computer, you can, with just a little bit of effort. For example, if you want to test a new version of Apache, test your code against the latest revision of PHP, or leverage new features in MySQL, you can create multiple LAMP environments and develop your application in one or more.

In this month’s “On the Desktop,” let’s look at how to establish, maintain, and use multiple instances of Apache on the same Linux system. You can also run manifold occurrences of MySQL and other configurable daemons using similar techniques.

Many Tangled Webs

Apache is readily multiplied because it’s available as source, and is modular and highly configurable, either at compile- or run-time. You can use more than one instance of Apache to manage a distinct groups of users and Web applications, to avoid the expense of adding another physical host to run a small, “one off” application, and to power combinations of modules.

In some cases, a single binary and numerous sets of configuration files are suitable. However, there are other cases where you need multiple binaries, such as testing varied releases of Apache. In any instance, there are important directives in the main Apache configuration that must be changed to make the instance unique. Table One lists the directives to affect in each of the case studies that follow.

Directive Description
The file that records the process of the Apache service (such as logs/httpd.pid)
The file that stores internal process information (such as logs/ logs/apache_runtime_status)
The file that records service errors (logs/error_log)
Refers to the series of files that record visitor information. Use a separate entry for each type of log. (logs/access_log common, logs/referer_log referrer, logs/agent_log agent, or logs/access_log combined). There are many options for the CustomLog
directive. Please refer to the Apache documentation for complete details.
The IP Address and port to which the service will be bound. Use instead of the deprecated Port
The DNS hostname of your server and port. An IP address and port may also be used.
The absolute path to the directory that contains your websites
The absolute path to the directory that contains all subfolders (conf, logs, htdocs, and so on)
The default Directory
directive that contains the global directives for the specified service.

Table One: Apache configuration directives unique to an instance

Same Binary, Different Configurations

If the same version of Apache is suitable for all of your requirements, you can create a new configuration file, httpd.conf, but use the same binary to create a new Apache instance. Here’s how.

  1. Create a new Apache configuration file by copying an existing one to its new home or name. For example, if your new instance lives in /opt/ws2, make a copy like this:

    # sudo mkdir -p /opt/ws2/conf /opt/ws2/logs /opt/ws2/htdocs
    # sudo cp -p /etc/apache/conf/httpd.conf /opt/ws2/conf

  2. Edit the new httpd.conf, replacing existing values with new values:

    DocumentRoot "/opt/ws2/htdocs"
    ServerRoot "/opt/ws2"
    <Directory "/opt/ws2/htdocs">

  3. Start your new instance with sudo apachectl -f /opt/ws2/conf/httpd.conf -k start. The -f option specifies the configuration file to use, and -k is a signal to the Apache server, one of start, restart, graceful, and stop. And the new instance is up and running. A ps -ef|grep httpd yields something like Figure One. (You may get errors when starting a new instance, even if you change all of the directives listed. Depending on your configuration of the original instance, there may be files that you need to copy to the new location. For example, you may have to copy the Apache modules directory to /opt/ws2 and the mime.types file from your original conf directory to /opt/ws2/conf. Another solution is to use absolute paths to the original modules directory and mime.types file.

    root      9406     1  0 15:24 ?        00:00:00 /usr/local/bin/httpd -k start
    nobody    9407  9406  0 15:24 ?        00:00:00 /usr/local/bin/httpd -k start
    nobody    9411  9406  0 15:24 ?        00:00:00 /usr/local/bin/httpd -k start
    root      9417     1  0 15:24 ?        00:00:00 /usr/local/bin/httpd -f /opt/ws2/conf/httpd.conf -k start
    nobody    9418  9417  0 15:24 ?        00:00:00 /usr/local/bin/httpd -f /opt/ws2/conf/httpd.conf -k start
    nobody    9422  9417  0 15:24 ?        00:00:00 /usr/local/bin/httpd -f /opt/ws2/conf/httpd.conf -k start

    Figure One: Two instances of Apache running concurrently.

  4. To test this new instance, create an index.html file in /opt/ws2/htdocs and point a browser to http://server_name:81, where server_name is the name or IP address of your server. You should see the index file.

  5. To stop this instance of Apache, type sudo apachectl -f /opt/ws2/conf/httpd.conf -k stop. This command specifically identifies the instance to stop. If you simply enter sudo apachectl stop, the primary instance is stopped instead.

Same Version, New Options

If you need a new instance of the same version of a service yet compiled with different options enabled, copy your source tree or unzip the original tarball to a new directory and compile anew with your chosen options.

  1. When you run ./configure, specify a new install directory with --prefix= dirname to avoid overwriting your original executables, libraries, and so on.

    $ rm -f config.{log,nice,status}
    $ ./configure --prefix=/opt/ws3 --enable-so
    $ make
    $ sudo make install

    The first command, rm -f, purges the results of the previous ./configure, if any. If you omit this step. your new installation will use the same options as your original version plus any new ones.

  2. Change the Listen directive to 82. DocumentRoot ServerRoot, and other directives are configured for you by specifying the prefix option.

  3. Start the new instance with /opt/ws3/bin/apachectl start.

  4. To connect to your new instance, point your browser to http://server_name:82. You should see default Apache Web page.

Multiple Versions of a Service

It’s often necessary to run a different version of a service. Indeed, some administrators run two (or more) versions of Apache, MySQL, PHP, and others on the same system. For example, MySQL 4 may run a production environment, while MySQL 5 is required for a staged environment. Frequently, sites run both Apache 1.x and Apache 2.x side-by-side. Let’s look at that example, assuming you’re already running the newer version of Apache.

  1. Download Apache 1.x, extract the tarball, and issue the following in the Apache 1.x source directory:

  2. $ ./configure --prefix=/opt/ws4 --enable-module=so
    $ make
    $ sudo make install

  3. Change the Listen directive to use port 83.

  4. Start the new instance with sudo /opt/ws4/bin/apachectl start. Use ps -ef | grep httpd to verify the new instance is running.

  5. Point your browser to: http://server_name:83 and you will see the default Apache index page.

  6. To stop this instance, simply issue sudo /opt/ws4/bin/apachectl stop.

Go Forth and Multiply

While this article focused on running many instances of Apache, the same technique can be applied to almost any TCP/IP network service. The key points to remember, for any service, are to use a different TCP port for each instance, create a unique configuration file for each instance; specify a unique log file, process ID file, and any custom or related service file for each instance; and start and stop each instance separately.

Running multiple instances of a service can be an inexpensive and relatively simple way to expand an environment and the possibilities for developers and users. However, keep a close watch for performance bottlenecks. For a heavily used production system, monitor memory and CPU usage carefully.

Comments on "Apache, For Instance"


I’m asking on behalf of those who don’t know, like me, exactly what the additional consequences of running multiple instances are. For example: should one instance become compromised(hack, bug, ddos) what happens to the other instances? My guess would be that they would happily truck along, though under reduced bandwidth and slices. Is there a way to “pre-direct” a url to the correct port number so the client doesn’t have to know the port number in advance? How do multiple instances affect overall system performance, especially under heavy loads? Should chroot techniques be used?


So one answer – nothing about one Apache crashing or being hacked will per se affect another. However, there’s a lot of exceptions here since the Apaches are sharing the same OS. If the DoS runs the comp. out of network sockets, or if one Apache’s MaxClients setting lets it get run high enough to take up all the box memory, or if an Apache hack allows access to files under another Apache, you have problems.

chrooting would help with some of these issues – adding VMs, with even more; adding separate servers, with still more; adding distributed data centers, still more… I think the point of the article is that if you really don’t care about any of that and just want 2 Apaches running, you can totally do that.


Aside from some exceptions (running 2+ services that only run only on different versions, or with mutually exclusive configurations) almost any configuration can be crammed into one instance of apache with a combination of virtual servers and reverse-proxying. The best use that I can think of would be for testing something like a deployable service that needs to work with php4 and php5, because that type of concurrency is tough to pull off in a single apache instance.

Can I just say what a aid to find someone who really knows what theyre talking about on the internet. You definitely know learn how to convey a difficulty to light and make it important. More folks have to read this and perceive this facet of the story. I cant believe youre not more popular since you undoubtedly have the gift.

Very neat article post.Thanks Again. Much obliged.

313423 395353Hi, you used to write exceptional posts, but the last several posts have been kinda boring? I miss your wonderful posts. Past couple of posts are just slightly bit out of track! 275519

Leave a Reply