dcsimg

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
PidFile
The file that records the process of the Apache service (such as logs/httpd.pid)
ScoreBoardFile
The file that stores internal process information (such as logs/ logs/apache_runtime_status)
ErrorLog
The file that records service errors (logs/error_log)
CustomLog
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.
Listen
The IP Address and port to which the service will be bound. Use instead of the deprecated Port
directive.
ServerName
The DNS hostname of your server and port. An IP address and port may also be used.
DocumentRoot
The absolute path to the directory that contains your websites
ServerRoot
The absolute path to the directory that contains all subfolders (conf, logs, htdocs, and so on)
<Directory>…</Directory>
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:

    Listen 10.0.1.250:81
    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.

Fatal error: Call to undefined function aa_author_bios() in /opt/apache/dms/b2b/linux-mag.com/site/www/htdocs/wp-content/themes/linuxmag/single.php on line 62