Installing Apache + mod_fastcgi + PHP-FPM on Ubuntu Server Maverick

April 8th, 2011 by Alex Cabal 21 comments »

Update: After trying this setup on a production server, I got many random 500 internal server errors caused by PHP-FPM. Either it’s not quite stable yet or something in my configuration isn’t ready for prime-time. I’ve switched back to Apache + FCGID which is stable for me. If any sysadmins want to chime in on the config below, please do so!

Recently I was looking to speed up one of my Apache + mod_php installs. The problem with Apache + mod_php is that Apache must spawn and then destroy a new instance of PHP whenever a script is called. This leads to performance degradation and eventually process bloat as the Apache process expands in memory to astronomical sizes.

Lots of options with similar names

There are lots of alternatives to speed up Apache + PHP, but unfortunately they all have similar-sounding names and can be combined in a few permutations. It can be confusing deciding which alternative to pick, and even more confusing figuring out how to set it up. I want to quickly describe some of the main alternatives to help clear things up before we start (but for those of you with short attention spans, the best one appears to be Apache + mod_fastcgi + PHP-FPM).

  1. Apache + mod_fastcgi: FastCGI is a module that allows you to neatly solve mod_php’s big problem, namely that it must spin up and destroy a PHP instance with every request. FastCGI instead keeps an instance of PHP running in the background. When Apache receives a request it forwards it to FastCGI, which feeds it to its already running instance of PHP and sends the result back to Apache. Apache then serves the result.

    Without the constant build-and-destroy of new PHP processes, FastCGI is a great memory saver and performance booster. My Apache + mod_php install, which would constantly bloat to 1000′s of MB in memory usage and invoke OOM-Killer without mercy, has been humming along at a steady ~200MB for the past few months without a single problem after switching to mod_fastcgi.

  2. Apache + mod_fcgid: Why, oh why, did someone build an alternative to FastCGI only to call it by the almost-identical name of fcgid? From what I understand, fgcid is a binary-compatible alternative to FastCGI–that is, it does more or less the same thing, but in a different way. It seems that some people prefer mod_fcgid over mod_fastcgi because of better stability and maybe even slightly better performance. But for the kind of traffic I’m getting, there wasn’t any difference.
  3. Apache + mod_fastcgi + PHP-FPM: PHP-FPM (A.K.A. PHP5-FPM) is a process manager for PHP. Confusingly, it was only recently bundled with PHP, so you might find some tutorials telling you to download the source and others to just use apt-get. I believe that if you’re using PHP >= 5.3, which you would be if you installed it in Maverick with apt-get, that you don’t need to download the source to get it working. I’ll talk more about this later.

    From what I understand, PHP-FPM is like FastCGI, but with additional PHP-specific optimizations built in. Since it’s specially built for PHP, it should give you the best performance, and so is the best of these three alternatives. It’s the one we’ll be focusing on for this post.

Getting it installed

For some reason it seems that there’s a glut of tutorials for setting up Nginx + PHP-FPM, but almost none for Apache + PHP-FPM. While Nginx is great, Apache has worked well for me for many years and I’m a “if it ain’t broke, don’t fix it” kind of guy.

Thankfully, it turns out that despite the severe lack of simple online tutorials it’s not so hard to set up Apache + mod_fastcgi + PHP-FPM on Ubuntu Server. Here we go!

Note: Before you start, back up any files we’re going to touch–especially /etc/apt/sources.list!

  1. Enable the Multiverse repository, which is where libapache2-mod-fastcgi lives. For some reason it’s not included in Ubuntu Server, so we have to append it to our /etc/apt/sources.list:
    user@computer:$ echo -e "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -cs) main restricted multiverse\ndeb-src http://archive.ubuntu.com/ubuntu $(lsb_release -cs) main restricted multiverse" | sudo tee -a /etc/apt/sources.list > /dev/null
  2. Install Apache and other required software. You must use the Apache Worker variant, not the Apache Prefork variant. The difference between the two is important, but you can look that up somewhere else.
    user@computer:$ sudo apt-get install apache2-mpm-worker libapache2-mod-fastcgi php5-fpm
  3. When you start the PHP5-FPM service on a default install, you’ll get a warning about a setting called “pm.start_servers” not being initialized. This won’t affect your install, but it can get annoying. To stop this message from showing up when starting or stopping the PHP5-FPM service, run:
    user@computer:$ sudo sed -i 's/;pm.start_servers/pm.start_servers/g' /etc/php5/fpm/pool.d/www.conf
  4. Enable some Apache modules that we’ll be using:
    user@computer:$ sudo a2enmod actions fastcgi alias
  5. And now for the secret sauce: Setting up your Apache virtual host so that it shuffles PHP requests to PHP-FPM/FastCGI. For this example I’ll use Apache’s default site, but you’ll probably want to use your own. Edit /etc/apache2/sites-available/default using Nano (or your favorite editor):
    user@computer:$ sudo nano /etc/apache2/sites-available/default

    Paste the following somewhere in your VirtualHost block using ctrl+shift+v:

    FastCgiExternalServer /var/www/php5.external -host 127.0.0.1:9000
    AddHandler php5-fcgi .php
    Action php5-fcgi /usr/lib/cgi-bin/php5.external
    Alias /usr/lib/cgi-bin/ /var/www/

    Remember that when using your own VirtualHost, you’ll probably be changing the default web root from /var/www/ to a different directory.

    Also note that the “file” php5.external doesn’t have to exist–it’s just a socket.

  6. Restart both Apache and the PHP-FPM services:
    user@computer:$ sudo service apache2 restart && sudo service php5-fpm restart

And that should do it!

Testing your install

To test this default install, create a new PHP file in Apache’s default document root and access it from your web browser.

  1. Create the PHP file:
    user@computer:$ echo "<?php phpinfo(); ?>" | sudo tee /var/www/test.php > /dev/null && sudo chgrp www-data /var/www/test.php
  2. Test it in your browser! Visit http://localhost/test.php and check the output:

And that should be about it. Enjoy your new speedy Apache install!

By the way, I’m more of a developer than a sysadmin, so if any experienced sysadmins want to chime in on this setup then by all means please do so.

References:
http://learnix.net/fastasscgi-part2/
http://blog.myprod.net/2010/08/14/apache2-suexec-fastcgi-php-5-3-3-fpm-cache-opcode-apc/

PHP 5.3.3: “Methods with the same name as the last element of a namespaced class name will no longer be treated as constructor.”

October 10th, 2010 by Alex Cabal 1 comment »

Please kill yourself, PHP.

Ubuntu Lucid’s New Indicator Applet Is A Joke

May 26th, 2010 by Alex Cabal 23 comments »

One of the big changes (besides window button placement, another facepalm that I won’t even get in to) in Lucid is the introduction of the new Indicator area. This area is supposed to merge and standardize various icons from the system tray (system tray: an area of the taskbar where various applications can place status and function icons) into an area on the taskbar where various applications can place status and function icons. Brilliant!

The first bit of joy is the messaging menu, a small envelope. It merges email, instant messaging, and social garbage like Twitter into a single clickable menu. But of course since this is Ubuntu, it only supports one email client, Evolution, and one instant messenger, Empathy. Both of which are pure shit. (Update: Shawn in the comments informs me that Pidgin is compatible with the messaging menu.) At least Evolution has the decency to be fairly stable; Empathy is still a half-baked piece of junk that doesn’t deserve the label of Beta. I’m assuming Gwibber is the only client Ubuntu supports for the social aspect, but I’m not interested in broadcasting my latest dump on Twitter, so I didn’t bother setting Gwibber up. But we’re not talking about the applications themselves, just the indicator, so let me get back on track.

Back in the halcyon days of yore, I had one icon for Pidgin and one icon for Thunderbird in my system tray. You would left click on either one of those to restore the windows; closing the windows would return them to the tray. Pidgin changed icons when I had a message waiting, and Thunderbird showed me the number of unread mails in my account. I could tell whether or not I had an IM queued or new mail, and a single click would open the right window so that I could get on top of it. Pure bliss.

Now, there’s a single envelope to tell me whether I have a new IM, mail message, Twitter trackback, or whatever. Which one do I have? Fuck me, I have no idea. It’s just a green envelope. How do I find out? I have to click the envelope to open a menu, scan the menu, and figure out what’s waiting for me. Then I have to click again to open the right application. We’ve moved from two clear icons that clearly represent the state of two different applications, and behave consistently when left-clicked, to a single icon that represents many applications, and whose state is unclear unless the user refocuses their attention and clicks on it to open a menu to read, then clicks again to open the right app. Brilliant job guys! Way to reduce the number of clicks and attention shifts I have to do to understand the state of my system! Oh, and too bad Evolution doesn’t minimize to the tray on close–you have to have it open in your taskbar at all times if you want to have your mail checked.

I don’t use Rhythmbox, because it’s terrible. I use Banshee, a far superior program. Banshee has it’s own plugin for the indicator applet. The old Banshee tray icon behaved this way: single left click opens Banshee (just like a single left click opens every single app in my system tray), hover shows you what’s playing, and right click opens a context menu with some handy options. Perfect.

The indicator applet for Banshee does this: left click opens a context menu with far fewer options. Right click opens the panel options menu (remove indicator applet from panel, lock to panel, etc.). Hover does nothing. To open the Banshee window I have to click twice. I can no longer tell what song is currently playing without opening the Banshee window. I still have an icon at the top of my screen, but now it’s called an indicator instead of a tray icon. How is this more clear or more useful? More to the point, HOW IS THIS DIFFERENT FROM THE REGULAR TRAY ICON, BESIDES IT SUCKING WAY MORE?

It’s not different. In his blind zeal, Shuttleworth has unwittingly re-created the system tray under the guise of “shiny,” “new,” “usable,” and “consistent.” Unfortunately for us, “usability” and “consistancy” aren’t the result.

The old system tray was consistent. It had a clear HIG from Gnome that all the Gnome apps I use adhere to. Deluge, Transmission, Thunderbird, Banshee, Dropbox, Pidgin, Empathy–all of these apps open on left click, menu on right click, and info on hover. There is no ambiguity. THE SYSTEM TRAY WAS ALREADY CONSISTENT. Now, the indicator app does many things: left-click on Banshee to open a menu. Left click on the envelope to open a list of apps. Left click on Volume to open a volume bar. Left click on Battery to tell you that it’s charged. The indicator app is actually LESS consistent than the system tray. And it’s still a bunch of icons at the top of the screen. Way to lead the revolution, guys.

I Love My Dell Vostro 1400 Laptop

January 10th, 2010 by Alex Cabal No comments »

Today I had a near-disaster. I was at a coffee shop doing a little work. The software updater came up showing there some updates available, including a kernel update. I said OK, let it do its thing, and dismissed a message telling me to restart because I had some work open. An hour or two later, I shut the laptop lid, stashed it in my laptop sleeve, and went home.

Unfortunately for me, I forgot that I had to do a restart. I have my laptop set to hibernate when the lid is shut. But apparently, when a restart is required in Ubuntu, your system will restart come hell or high water–and that’s just what it did instead of hibernating, zipped up nice and snug in my neoprene laptop sleeve.

In case you haven’t heard of neoprene, it’s the material that wetsuits are made of: water resistant and able to keep a body warm while surrounded in freezing waters. They’re made to retain heat. Needless to say, they retain heat pretty damn well when there’s a running laptop zipped inside. A few hours later, I went to get my laptop out of the bag. My bag was pretty warm, and I instantly realized what had happened. I scrambled to pull the laptop out of the neoprene oven—my bag stank of burning plastic and the laptop was so hot I couldn’t hold it. Sure that it been damaged, I turned it off and put it under a floor fan to cool down.

Not only did it cool down in a few minutes, but it emerged unscathed from the neoprene oven. Everything is working perfectly. I’ve carried this laptop around the world in all sorts of environments, and it’s still alive.

There’s no stupid media keys, there’s 2 USB ports on either side for easy accessibility, there’s a Home button that I map to my home folder, the keyboard is responsive and the touchpad buttons are satisfyingly clicky. The screen is matte so I can use it in the sun. In short, I love this laptop.

Ubuntu, on the other hand, can go fuck itself for restarting when I told it to hibernate. At the very least it could have just shut down completely.

Using shared folders with Windows 7 in VirtualBox on Ubuntu Karmic

January 4th, 2010 by Alex Cabal No comments »

VirtualBox allows you to specify “shared folders,” which are folders located on your host that your virtual machine can see. This is different from having a folder shared over the network: anyone on your network can see network shares, but only your VirtualBox instance can see vbox shared folders.

The problem with Windows 7 on VirtualBox is that even if you specify shared folders, there’s no obvious way to access them from the virtual machine. They don’t show up in Network Places.

To access shared folders, you have to type in the following in your address bar:
\\vboxsvr\shared_folder_name

Don’t use your host’s name instead of vboxsvr; you have to say vboxsvr. shared_folder_name is the name of the folder you shared in VirtualBox.

To make your folder easier to access, you can map that shortcut to a network drive in your virtual machine.

Fixing garbled or popping sound on Intel sound cards in Ubuntu Karmic

December 18th, 2009 by Alex Cabal No comments »

Update: this doesn’t appear to solve the problem 100%, but I am experiencing it much less often.

Enhancing my love/hate relationship (towards the ‘hate’ side) with Ubuntu is a nasty audio glitch introduced in Karmic for some Intel sound cards. Different people describe it differently: I’ve heard it described as random ‘popping,’ but my symptom is more like ‘laptop randomly starts sounding like a garbage disposal until you run $>killall pulseaudio’. I’ll be listening to an MP3 through Banshee, and randomly when it goes to the next track, every subsequent sound will sound like a train wreck until I kill the pulseaudio process. Some MP3s are more consistent than others with this problem.

Either way, I’ve found a potential fix for this problem. Here goes:

  1. Open the ALSA configuration file using GEdit as root from the terminal:

    user@computer:$ gksudo gedit /etc/modprobe.d/alsa-base.conf

  2. At the very end, you should find a line that looks like this:

    # Power down HDA controllers after 10 idle seconds
    options snd-hda-intel power_save=10 power_save_controller=N

    You want to comment out the line starting with ‘options,’ so that it looks like this:

    # Power down HDA controllers after 10 idle seconds
    #options snd-hda-intel power_save=10 power_save_controller=N
  3. Save the file and restart Pulseaudio by running:

    user@computer:$ killall pulseaudio

That should solve your sound problems.

Turn your laptop screen off with a keyboard shortcut in Ubuntu Karmic

December 18th, 2009 by Alex Cabal 23 comments »

Back in the pre-Karmic days (or, the good ole’ days, as I call them) you could turn your laptop screen off by issuing this simple command:

user@computer:$ xset dpms force off

I had a nice script set up and mapped to a keyboard shortcut to turn my screen off whenever I wanted (say if I was using my laptop to play music and wanted to save some battery life). In Karmic, however, a regression causes this command to turn your screen off only briefly. After a few seconds, your screen will turn back on, on its own. This might be an Intel-specific problem—I have the misfortune of having an Intel video card on my Ubuntu install—but I’m not sure.

To get around this problem, I created a slightly different (and slightly less ideal) script that turns the screen off when a shortcut key is pressed, and then turns it back on after they same key is pressed again. Here’s how to get it for your own laptop:

  1. Create a new file using Nano. I put it in a directory called .scripts, you can put it wherever you like:
    user@computer:$ nano ~/.scripts/screen-off
  2. Paste the following into your new file using ctrl+shift+v:
    #!/bin/bash
    screenOffLockFile=/tmp/screen-off-lock

    if [ -f $screenOffLockFile ];
    then
            rm $screenOffLockFile
            notify-send "Screen on." -i /usr/share/icons/gnome/48×48/devices/display.png
    else
            touch $screenOffLockFile
            sleep .5
            while [ -f  $screenOffLockFile ]
            do
                    xset dpms force off
                    sleep 2
            done
            xset dpms force on
    fi

    (An explanation follows). Press ctrl+x to quit Nano, then y to save your script.

  3. Make the script executable:

    user@computer:$ chmod u+x ~/.scripts/screen-off
  4. Open the System menu, go to Preferences, then Keyboard Shortcuts (or press alt+f2) and run gnome-keybinding-properties.
  5. Click Add, enter ‘Turn Off Screen’ under Name and ‘~/.scripts/screen-off’ (without quotes) under Command.
  6. After you’ve added the command, it will appear under the Custom Shortcuts header. Click on your new command to assign it a shortcut key of your choice (I use ctrl+alt+shift+s).

And that’s it. Press your shortcut key to turn your screen off, and press the shortcut key again to turn it back on.

What the script does

When you first press the shortcut key with your screen on, the script will check if there’s a certain file in your /tmp/ directory. If it doesn’t find it, it will create it and then enter an infinite loop that turns your screen off every 2 seconds if the special file still exists (thus working around Karmic’s regression). When you press the shortcut key with your screen already off, it will delete the file and do nothing else, causing the first instance of the script to exit its infinite loop (since it no longer sees the special file) and turn your screen back on again.

It’s as simple as that. It’s not as ideal as the pre-Karmic solution, but it’ll have to do until Canonical QA gets their act together.

Getting localhost to work over HTTP in VirtualBox

December 2nd, 2009 by Alex Cabal No comments »

For some reason, VirtualBox doesn’t consistently resolve localhost to the host machine’s IP. Sometimes it’ll work, sometimes it’ll bounce you to the dns error page. To fix this, just add this to your c:\windows\system32\drivers\etc\hosts:

10.0.2.2 [your computer name]

That’s it–you should now be able to do things like http://localhost:8080/

Changing the size of a VirtualBox disk image

November 28th, 2009 by Alex Cabal No comments »

I use VirutalBox to virtualize a copy of Windows 7 on my Ubuntu machine. I use it for when I want to test sites in IE/Safari or when I need Visual Studio to do some .NET work. Naturally I want to keep the disk image as small as possible, but I often find that with all of the ginormous service packs that Microsoft is so fond of distributing, my guesses as to what a good size for the hard disk are usually too small.

There’s no easy way to resize a VirtualBox hard disk image, but it’s not that hard to do. Here are the steps:

  1. Get a copy of an Acronis Boot Rescue ISO.
  2. Create a new hard disk image of the size you want using the Virtual Box Virtual Media Manager.
  3. Attach the new hard disk image as a slave to the OS you want to grow.
  4. Boot the VirtualBox OS into the Acronis ISO.
  5. Use the Acronis tools to mirror the master disk into the new slave disk. The tools will automatically expand or contract the partition to match the size of the new disk.
  6. Toss the old disk image and attach the new one as the master. You’re done!

Things that used to work in Ubuntu Intrepid but broke in Karmic

November 11th, 2009 by Alex Cabal 5 comments »

After a clean install of Ubuntu Karmic on my plain vanilla Dell Vostro 1400 laptop, I found that (as usual) a lot of stuff that used to work fine in Jaunty has broken in Karmic:

  1. My Broadcom wireless card is completely unrecognized. Hardy was polite enough to recognize it but let me know that there were no drivers. Intrepid recognized it and automatically installed the right drivers. So did Jaunty. Karmic, however, just plain old doesn’t see it. Not even in lspci.
  2. Resume from Hibernate hard crashes about half the time.
  3. The Firefox spell check dictionary defaults to en_AU instead of en_US. This is great if you live in Australia, but I happen to be from California.
  4. The famous IPv6 issues. DNS lookups take forever unless I specify a DNS server to use. With all of my traveling, the last thing I want to be doing is setting the DNS server for every single new wireless connection I encounter.
  5. I use a script to turn off my laptop’s screen when I want to save power (say if I’m playing music for a group at a hostel). I’ve mapped this short but handy script to ctrl+alt+shift+s:
    #!/bin/bash
    #turn the screen off after half a second.
    sleep .5 && xset dpms force off

    This used to work like a dream in previous distros. Now the screen turns off, but turns back on after a few seconds.

  6. My Intel GM965 (heinously borked in Jaunty, see this lovely section in the Jaunty release notes) now works at least, but with horrible slowness. Key presses and mouse clicks register slowly, there’s tearing when scrolling in any application, and things are generally dog slow. And I’m not the only one with this problem.

Come on, Canonical. How do you expect to be taken seriously when the OS you release next to Windows 7 has so many shamefully amateur problems? I can guarantee you that Windows 7 can at least resume from fucking hibernate and perform a fucking DNS request.

The thing that really pisses me off is that for a lot of these issues, we won’t be seeing a fix for another 6 months. That’s the pain of the fast release cycle: no time to do QA, so beta-quality software gets shipped. But no time to fix the problems, because to make the next release by the deadline, they have to start work yesterday.

Year of the Linux desktop? I’m not holding my breath.