Welcome to the adventure

Running multi-threaded Apache with PHP on Ubuntu

Friday September 22, 2006

If you try to install apache2-mpm-worker or apache2-mpm-perchild on Ubuntu, it will not let you do so alongside apache2-mod-php5.

The story is that many modules php might potentially load are not thread safe, and so running it in a threaded Apache is “not recommended”. However, with threaded Apache2, you get better performance and you might be able to wing running your PHP app without any problems. Mix these two at your own risk.

PHP has to have experimental multi-threading compiled in to run with multithreaded Apache, which is why you can’t just install the apache-worker and mod-php packages together. You’ll have to build it yourself, but this guide will show you how to compile php from source in Ubuntu.

# apt-get install apache2-mpm-worker

(or apt-get install apache2-mpm-perchild, the other multi-threaded Apache implementation).

I needed these to get Apache running on my production server, so you may need them too:

apt-get install build-essential flex libxml2-dev

If you want to compile PHP with a MySQL driver, you’ll need these development bits:

apt-get install libmysqlclient14-dev

Download the latest PHP sources (I’m using 5.1.6). Direct link to the latest version at the time of writing:

wget http://us3.php.net/get/php-5.1.6.tar.bz2/from/this/mirror
tar -xvjf php-5.1.6.tar.bz2
cd php-5.1.6

You must compile PHP with the location of your apache2 binary. I’m compiling in mysql support. You must also use the “–enable-maintainer-zts” switch to compile in threading support, or you will get “Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP. Pre-configuration failed!” when you try and load mod-php into Apache.

./configure --with-apxs2=/usr/bin/apxs2 --with-mysql --enable-maintainer-zts --prefix=/usr
make
sudo make install

You can then set the php module to load, and restart Apache

a2enmod php5
/etc/init.d/apache2 restart

And that should be it. I already had a working PHP/Apache setup on my system before I did this (although PHP was removed automatically when I installed apache2-mpm-worker), so I might be missing a step that you need from a fresh Ubuntu install (let me know and I’ll add it).

No hint of instability. apache2-mpm-worker is running this very blog (Wordpress) as I write.

12 Comments »

  1. hey phil,

    just wondering why you chose to compile from the PHP tarball opposed to the php5 package?

    a software you may like is stow: let’s one do make installs while keeping track of all the random files that get installed (so uninstalling won’t be a pain)..

    jason

    Said by jason parekh September 23, 2006 at about 5:54 pm

  2. Ah, good tip. Installing from the package source is also a good option. Sometime back I got in the habit of having all of these source trees on my hard drive (probably from mono) so by default I seem to go that route.

    I used to feel anxious about having misc files strewn about my hard drive from source installs, but I stopped caring. If I ever need to find/remove them, they’re always in well-defined places. Stow could be useful because make uninstall never seems to work.

    Said by Phil Crosby September 23, 2006 at about 6:59 pm

  3. Hi Phil, new to the Ubuntu OS, I found this online while trying to solve a problem. If I follow your steps exactly, with added packages in the php compilation (png, jpg, mbstring, etc). I get an error that looks like this:

    Warning! dlname not found in /usr/lib/apache2/modules/libphp5.la.
    Assuming installing a .so rather than a libtool archive.
    chmod 644 /usr/lib/apache2/modules/libphp5.so
    chmod: cannot access `/usr/lib/apache2/modules/libphp5.so’: No such file or directory
    apxs:Error: Command failed with rc=65536

    Any ideas?

    Cheers.
    Alex

    Said by Alex September 27, 2006 at about 3:27 am

  4. Hi Alex

    I do not have a libphp5.la, but I do have a libphp5.so in that directory, and its read-write permissions are 644.

    The result of this command should match mine:
    ~$ ls -alh /usr/lib/apache2/modules/libphp5.so
    -rw-r–r– 1 root root 9.4M 2006-09-22 20:52 /usr/lib/apache2/modules/libphp5.so

    If it doesn’t (you don’t have rw-r-r as the permissions) run

    sudo chmod 644 /usr/lib/apache2/modules/libphp5.so

    That’s the command apache is trying to run itself, and is failing. Don’t know why your permissions would be different, but that should fix the specific error you posted.

    Said by Phil Crosby September 27, 2006 at about 4:32 am

  5. Hi,

    I installed my apache with

    ./configure –with-pgsql –with-apxs2 –enable-maintainer-zts

    However, I’m still getting the “your PHP Module is not compiled to be threadsafe” error.

    What is this command:

    a2enmod php5 ???

    Could this be the step I’m missing?

    Said by Alex Egg August 10, 2007 at about 12:09 am

  6. I didn’t have the files for a2enmod for php5, but I copied ones I had left over from php4. For others that don’t have them, create these two files:
    == /etc/apache2/mods-available/php5.conf ==

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

    == /etc/apache2/mods-available/php5.load ==
    LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

    Then you can run ‘a2enmod php5′

    Said by gene August 11, 2007 at about 9:24 pm

  7. I also had to install an extra package called apache2-threaded-dev. This was required on my system because I got the error message “apxs not found”. I am running Ubuntu 7.04 Feisty Fawn. Just figured I’d let you know in case any one else ran into the problem!

    Said by Chris August 12, 2007 at about 3:27 pm

  8. Hi.
    Good design, who make it?

    Said by naisioxerloro November 29, 2007 at about 3:28 am

  9. Great guide - really helped out.

    I only have to second Chris’s comment that the package apache2-threaded-dev is necessary (because that’s where apxs2 comes from).

    So, you might want to add that to the packages you’ll need. There ARE still people reading, and being helped, by your article a year and a half later.

    Said by Ryan Weaver March 29, 2008 at about 7:11 pm

  10. lincoln mercury dealers j.k. rouling who fault is it the system or individual wgn hot tub heaters

    Said by Albina-yj May 9, 2008 at about 2:28 pm

  11. I’m testing 2.2.9 with php5.2.6, I’ll see how it goes. And it’s now nearly TWO years after…

    Said by Tom September 18, 2008 at about 3:57 pm

  12. Heya Phil, 2 years later.. =D just thought I’d add - for a clean install of Ubuntu, I needed to:

    apt-get install apache2-threaded-dev

    .. before ./configure would run. Hope this helps someone!

    Said by nevelis September 23, 2008 at about 1:53 am

Leave a comment