Network Interface Statistics Collection Agent

Apache, MySQL, and PHP installation How-To


Compiling and installing from scratch
Installing from binary distributions


Getting PHP and Apache working together can sometimes be... well, challenging. This document helps to demystify some of the process. It covers both the Apache module and standalone binary versions of PHP.

There are two ways to install Apache/PHP: with everything compiled into one binary file, and with a whole bunch of compiled libraries that are loaded dynamically into the server as needed. For a production server, I don't recommend using the dynamic module method; it's slower, and less stable, in my opinion. I've always had nothing but problems with a bunch of dynamic modules in Apache; they have to be loaded in when it runs (and in a certain order), their versions can differ from what the server itself expects, etc. You never know what you're getting. When you compile everything yourself, and include just what you need and want, that means you're using your system libraries and your options and support; with a premade binary distribution, and/or a bunch of modules that are hard to keep track of, it's hard to know exactly what's going on. With the "compiling everything into one binary" method, there's only one file to keep track of... "httpd".

Doing it with premade binary distributions is initially faster (all you should have to do is untar Nisca, configure it, and use it), but you spend more time later on diagnosing why it doesn't work like it should than you would have just compiling it all yourself from scratch to begin with. :) As if that wasn't enough, you also have to add a line to your "php.ini" and/or "httpd.conf" files for every module you want loaded at runtime (all of them, that is). Plus, there are a few compile-time options that you won't find in any premade distribution. But if you want to just download some premade binaries and modules, feel free... :)

No matter how you eventually get it installed, you'll need several things installed to support the NISCA features you want. Below is a table of the applications and libraries you'll need to do different things. (All versions shown are minimums. Required packages are in red; optional packages are in that other color.)

PackageVersionFootnote
Apachev 1.3.x1
MySQLv 3.23.x 
PHPv 4.x.x 
zlibv 1.1.4 
UCD SNMPv 4.2.32
libGDv 1.8.x3
libJPEGv 6b4
libPNGv 2.15
The FreeType Libraryv 1.x/2.x6
The Ming Library (for Flash)v 0.27
The Zend Optimizer?8

  1. Theoretically, any web server can be used, but NISCA has been tested and developed using Apache's httpd. And PHP recommends not using Apache 2.x versions with PHP yet.
  2. The "net-snmp" package can also be used. Only required if you want to collect statistics using SNMP.
  3. Only required if you want to make graphs.
  4. Only required for JPEG support in graphs.
  5. Only required for PNG support in graphs.
  6. Only required if you want to use TrueType Fonts on graphs.
  7. Not required yet. Nisca will eventually support more-interactive menus and reports using this library.
  8. Optional. Get the version to match your PHP version. Zend will dramatically improve PHP performance.

NISCA also requires that Session Variables and Perl-Compatible Regular Expressions support be enabled in both the Apache and CGI binary versions of PHP. They're both enabled by default; please don't disable them. Another thing you'll have to do when you install PHP is double-check these settings in your "php.ini" file:

Make sure they're both "On" or Nisca won't work right.

You'll also be happy to know that Nisca no longer uses the SNMP functions built into PHP. It calls the binaries that come with ucd-snmp directly, and could probably use other SNMP client binaries as well.

The order to do things in to install the proper PHP support is:

Now on to the specific sections for however you want to install it; compiling it all yourself, which is the method I both use and recommend, or installing from some kind of binary distribution, which there are several to choose from.

One thing common to all, though, is you no longer need to hack the PHP source code to enable SNMPv2c support, because Nisca no longer uses PHP's SNMP functions. What a relief.


Compiling PHP From Scratch

This shouldn't take more than 150 meg of disk space even if you leave every source code directory installed and don't delete any of the source code archives you downloaded. (Hey, that's peanuts these days... :) Make sure you have enough room to do all this in!

Here are package-specific notes for everything listed above. These instructions should be followed in this order. You should probably do all this as root; I've never tried it as any other user. Make sure your umask is sane; "022" is a good value. Also, don't delete a package's source code directory after you've installed it; some packages need the right include files (*.h) to compile correctly.

If you already have one of these packages installed, just make sure it's a sufficiently-high version and skip its section below if so.

zlib

  1. Get the zlib source package and untar it. "cd" to that directory.
  2. Configure it using "./configure --shared".
  3. Compile it using "make".
  4. Install it using "make install".
  5. Run "ldconfig".

libjpeg

  1. Get the libjpeg source package and untar it. "cd" to that directory.
  2. Configure it using "./configure".
  3. Compile it using "make".
  4. Install it using "make install".
  5. Run "ldconfig".

libpng

  1. Get the libpng source package and untar it. "cd" to that directory.
  2. Run "./configure"; it will show you directions on copying the right Makefile for your system to the source directory.
  3. Edit the new Makefile. Make sure these two lines point to the right place:
    ZLIBLIB=../zlib
    ZLIBINC=../zlib
    
    For example, my zlib source directory was "../zlib-1.1.2", so that's what I put on those two lines.
  4. Compile it using "make".
  5. Install it using "make install".
  6. Run "ldconfig".

freetype
A special note about Freetype. I've traced most problems to trying to use only version 2.x on your system. For some odd reason, I can't get libgd and PHP to work together with only version 2.x; you have to compile libgd using v1.x and its header files (that is, use "-DHAVE_LIBTTF" instead of "-DHAVE_LIBFREETYPE" in libgd's Makefile) or PHP won't actually include TrueType support in itself. If you have the v2.x libfreetype.* installed, PHP will use that for gd's TTF support even though gd has v1.x in it, and (miraculously) it works. So you would install FreeType v1.x, then install FreeType v2.x, then compile libgd with FreeType v1.x support only, then compile PHP with "--enable-gd-native-ttf". Completely counter-intuitive and bizarre, but for some reason it works. shrug.

  1. Get the freetype source package and untar it. "cd" to that directory.
  2. Type "make". This will run a "configure" script.
  3. Type "make" again. This will actually compile it.
  4. Install it using "make install".
  5. Run "ldconfig".

libgd
Note that although PHP now comes with a bundled version of libgd, it's version 2.x of libgd. I'm not sure I'd trust it... plus, I couldn't even get PHP to compile with the bundled libgd. So do it this way if you experience trauma.

  1. Get the libgd source package and untar it. "cd" to that directory.
  2. Edit the "Makefile". Read through it and change as needed. Watch out for the various settings for whether you have libjpeg, libpng, and the freetype library. There's also libXpm, which comes with X-windows; if you don't have it, leave that out of these lines (the INCLUDEDIRS option for it is the "-I/usr/X11R6/include/X11" one). For example, for all four, you'd need these three lines in it somewhere (yes, there are only three lines below.. remember the function of the backslash :)
    CFLAGS=-O -DHAVE_LIBXPM -DHAVE_LIBPNG -DHAVE_LIBJPEG -DHAVE_LIBTTF
    LIBS=-lgd -lpng -lz -ljpeg -lm -lttf
    INCLUDEDIRS=-I. -I/usr/local/include/freetype \
                -I/usr/X11R6/include/X11 -I/usr/local/include
    
  3. Compile it using "make".
  4. Install it using "make install".
  5. Run "ldconfig".
  6. Make sure the various gd header (*.h) files got copied to, say, /usr/local/include. These are the ones you need:
    gd.h, gd_io.h, gdcache.h, gdfontg.h, gdfontl.h, gdfontmb.h, gdfonts.h, and gdfontt.h
    If they aren't available, PHP will not be able to compile libgd support.

ucd-snmp / net-snmp

  1. Get the net-snmp or ucd-snmp source package and untar it. (It doesn't really matter which you prefer, but the net-snmp one is newer.) "cd" to that directory.
  2. You may or many not need to specify where the zlib package's pieces are installed; if "./configure" doesn't work without it, instead use "./configure --with-zlib=../zlib-1.1.2" for example.
  3. I usually include "--enable-shared" as an option, but I'm not sure if that's still necessary when not installing the SNMP extension in PHP. YMMV.
  4. The configure script will ask you a few questions. They're all pretty self-explanatory; just answer them.
  5. Compile it using "make".
  6. Install it using "make install".
  7. Run "ldconfig".

mysql

  1. Get the MySQL source package and untar it. It's big. "cd" to that directory.
  2. Configure it with "./configure --without-debug". If your server has a RAID array, you can make MySQL faster by adding "--with-raid" to this command as well. In fact, there are billions of options you can give the configure script; "./configure --help" will give you a list of them all.

    If you want to try out the "InnoDb" features of mysql, use "--with-innodb". This is supposed to make mysql faster and give it support for row-level locking and transactions (Nisca doesn't use transactions, but row-level locking makes it a lot faster). I use it myself; it's just wonderful. Really. :) Hey, I'd tell you if it were otherwise. I would.

  3. Compile it using "make".
  4. Install it using "make install-strip".
  5. Run "ldconfig".
  6. Run the file "scripts/mysql_install_db" to set up the permission tables in the new server IF you don't already have permissions set up (as in, don't do this if you're just upgrading versions; there's another script for that purpose, the "scripts/mysql_fix_privilege_tables.sh" file, for use if you're upgrading from a version before 3.22.14).

PHP 4
Here's the big one. Let's take it slowly, starting with the Apache module version.

First of all, you have to install the Apache source code first or you can't compile PHP. Just install, not compile. So with that in mind:

  1. Get the Apache source package and untar it. "cd" to that directory.
  2. Run "./configure" with no options and ignore its warning about doing so.
  3. "cd .."
  4. Get the PHP 4 source package and untar it. "cd" to that directory.
  5. Run "./configure"... but you have to use lots of options with it. You'll need at least all of these; the backslashes just tell it to continue the command on the next line. Replace directory names as appropriate (e.g., don't actually put "/path/to/src/of/apache-1.3.xx" :) Put all the options you want to use into a file called, say, "do-conf" (which PHP used to make itself but no longer does)...
    ./configure \
    --with-apache=/path/to/src/of/apache-1.3.xx \
    --with-mysql=/usr/local \
    --with-zlib --enable-sysvshm=yes \
    --enable-sysvsem=yes --with-config-file-path=/home/httpd/conf \
    --disable-debug --enable-track-vars=yes \
    --enable-magic-quotes=yes
    
    Note that the magic-quotes thing is rather necessary for proper Nisca operation; otherwise you'll have to go through the script and add stripslashes() and addslashes() functions in a billion places. You can leave it out here and enable it with this directive, in "php.ini":
    magic_quotes_runtime = On
    

    Then there are the optional commands. If using libgd, you'll need this:

    --with-gd
    
    That, though, will install the bundled libgd (version 2). To use the one you installed earlier, use this instead:
    --with-gd=/usr/local
    
    You might also need the following if you see a warning about libxpm when running configure. You'll only need it if using libgd, too.
    --with-xpm-dir=/usr/X11R6
    
    To find the libjpeg header files, include this command, pointing to wherever you compiled libjpeg earlier:
    --with-jpeg-dir=/path/to/src/of/libjpeg6b
    
    If using the freetype library, include these as well:
    --enable-gd-native-ttf
    --with-freetype-dir=/usr/local
    
    I did have to play with PHP's configuration a bit to get freetype to work; it requires some combination of the following PHP "./configure" directives:
    --enable-gd-native-ttf
    --with-freetype-dir
    --enable-gd-imgstrttf
    --with-ttf
    
    Try the first two (which works for me) and if it doesn't work, play around with the options until you find a combination that works. Look in the "configure" output for a line saying:
        checking whether to enable truetype string function in gd... yes
    
    If it says "no" instead, it isn't working yet. Once it says "yes", it's ready.
  6. Compile it using "make".
  7. Install it using "make install". ("install-strip" has vanished completely.)

You no longer (with php-4.3.0 at least) have to make the standalone PHP binary separately. "make install" will install both it and the Apache module. It doesn't strip it, though, so after "make install", do "strip /usr/local/bin/php" and it'll be half the size.

I usually back up my "do-conf" file somewhere so it's easy to compile new versions as they come out. You just untar the new source code, copy the "do-conf" file into it, and run it. That's the plan, at least. :) For your edification, here are the contents of my do-conf file (used with PHP 4.3.0):

./configure \
--with-apache=/usr/src/apache/apache_1.3.27 \
--with-xpm-dir=/usr/X11R6 --with-freetype-dir=/usr/local/lib \
--with-jpeg-dir=/usr/src/apache/libjpeg6b --with-png-dir=/usr/src/apache/libpng-1.0.11 \
--with-pdflib --with-swf --with-ftp --with-dba --with-bz2 \
--with-gd=/usr/local \
--enable-gd-native-ttf --with-curl \
--with-mysql=/usr/local --with-pgsql --with-ldap --with-imap \
--with-openssl=/usr/local/ssl --enable-sysvshm=yes --with-zlib \
--enable-sysvsem=yes --with-config-file-path=/home/httpd/conf \
--with-tiff-dir=/usr/lib \
--disable-debug --enable-track-vars=yes \
--enable-magic-quotes=yes --enable-bcmath=yes

Apache
There are many ways to install Apache. I'll give you the method I use. It's probably not the method most people would recommend to you, but it works great for me so this is how I do it.

  1. Set up/install any add-on modules you want (mod_perl, mod_ssl, mod_frontpage, whatever).
  2. Go into the apache_1.3.xx source code directory.
  3. Run "./configure" but again with lots of options. Most people will want several different Apache modules included; these are all specified here with things like "--enable-module=auth_anon". Include one for each of the available modules that come with Apache, and for the ones you added to it, that you want. Then, include one that looks like this: "--activate-module=src/modules/php4/libphp4.a". When you run the configure script, it will create a file called "config.status"; this has the same function as PHP's "do-conf" file. Once you've run a successful "./configure" command, this file is created so you don't have to keep typing all those config options over and over. I include here the contents of my config.status file. It's pretty intense...
    CC="gcc" \
    OPTIM="-DHARD_SERVER_LIMIT=512 -DDOCUMENT_LOCATION=\\\\\"/home/www\\\\\" -DDEFAULT_PATH=\\\\\"/bin:/usr/bin:/usr/local/bin\\\\\" -DACCEPT_FILTER_NAME=\\\\\"httpready\\\\\"" \
    CFLAGS="-O2 -mcpu=pentiumpro -fomit-frame-pointer" \
    SSL_BASE="/usr/local/ssl" \
    ./configure \
    "--prefix=/home/httpd" \
    "--with-layout=Apache" \
    "--server-uid=nobody" \
    "--server-gid=nogroup" \
    "--with-perl=/usr/local/bin/perl" \
    "--suexec-docroot=/home/www" \
    "--activate-module=src/modules/php4/libphp4.a" \
    "--activate-module=src/modules/perl/libperl.a" \
    "--enable-module=auth_db" \
    "--enable-module=auth_anon" \
    "--enable-module=log_agent" \
    "--enable-module=log_referer" \
    "--enable-module=rewrite" \
    "--enable-module=vhost_alias" \
    "--enable-module=unique_id" \
    "--enable-module=digest" \
    "--enable-module=usertrack" \
    "--enable-module=expires" \
    "--enable-module=cern_meta" \
    "--enable-module=mmap_static" \
    "--enable-module=headers" \
    "--enable-module=info" \
    "--enable-module=php4" \
    "--enable-module=perl" \
    "--enable-module=ssl" \
    "--disable-rule=SSL_COMPAT" \
    "$@"
    
  4. Once the configuration has run, type "make" to compile it.
  5. Run "make install" to install your shiny new httpd server and related support files (htpasswd, apachectl, etc). Not sure if it strips it anymore; Apache has also removed "install-strip" as a Makefile target.
  6. I usually back up my "config.status" file somewhere so it's easy to compile new versions as they come out. You just untar the new source code, copy the "config.status" file into it, and run it.
  7. Set up the various Apache/PHP configuration files. I'm not even going to begin to try explaining that process; see the files themselves (php.ini, access.conf, httpd.conf, and srm.conf) for details.
  8. See if it works. Run "apachectl start" from wherever it got installed. If it runs, great... if not, ummm, examine any error messages and see if you can figure out where it broke down. I'll help if I can... :)
  9. Install the NISCA package (see the INSTALL file for details).

The Zend Optimizer
This is really cool. It speeds up Nisca execution by at least 25%. It's just a little library you load in your "php.ini" file that optimizes your PHP source code before it runs it. It's available as a free download, too; you no longer have to sign up with them for the privilege of downloading it.

  1. Get the Zend package and untar it. "cd" to that directory.
  2. Read the README file... but, basically:
  3. Install it by copying "ZendOptimizer.so" to a handy library directory, like /usr/local/bin/. It's in "data/4_3_0_comp/" for php4.3.0, for example.
  4. Add it to your "php.ini" file as directed in the README that comes with it. This usually involves just adding three lines to php.ini:
    zend_optimizer.optimization_level=1023
    zend_extension="/usr/local/lib/ZendOptimizer.so"
    zend_optimizer.enable_loader = 0
    
    This last one shouldn't be used if you're using the commercial version of the Zend optimizer (the one that compiles actual binary versions of your PHP code), or are somehow otherwise creating precompiled, Zend-optimized PHP executables. Otherwise, please do use it, as it's responsible for a large part of my speed increase. :)
  5. Run "ldconfig" (not really necessary, but why not??).
  6. Restart your "httpd" process ("killall -HUP httpd" isn't good enough; it must be killed and restarted... I think... could be wrong :)


Installing from binary distributions

Please note... although this procedure involves far less work, I don't recommend it for a production server. There's no substitute for compiling something yourself; trust me. I've seen too many people who can't get everything installed and running correctly, yet desperately trying to keep using, say, RPMs of things because they don't want to install something any other way... so they never get it to work. Compiling it yourself isn't as hard as the incredibly long procedure makes it look; it just takes more time, and (I believe) results in a far more stable system. I've timed myself; I can go from blank system to having everything above compiled and installed in less than two hours. YMMV, again, of course.

I've been referred to this page, which has full PHP RPMs for RedHat 6.x and 7.x. I haven't tried them myself, and probably won't, since I always try to make things myself unless it's, like, XFree86. But give it a try if you want to RPM your way to Nisca.

  1. Download the various packages for the support you want.
  2. Install them in the order shown above, one after the other, using whatever method is appropriate for the package in question.
  3. Set up the various Apache/PHP configuration files. I'm not even going to begin to try explaining that process; see the files themselves (php.ini, access.conf, httpd.conf, and srm.conf) for details, as well as the Apache, MySQL, and PHP documentation.
  4. See if it works. Run the "httpd" binary from wherever it got installed. If it runs, great... if not, ummm, examine any error messages and see if you can figure out where it broke down. I'll help if I can... :)
  5. Install the NISCA package (see the INSTALL file for details).


As always, let me know if you have problems.