Sunday, 21 April 2013

Changing from Apache MPM Prefork to Apache MPM Worker on Virtualmin in a post-install environment

On one of the sites I administrate, we have recently been experiencing some problems keeping it online when it had hundreds of people on it at once. I was told that Worker will run the site far more efficiently than Prefork, so we decided to try it.
After doing some research and trial and error, this is what I have come up with. Please note that this was done on an Ubuntu Server and may require slightly different procedures for CentOS and other OSes. Please also note that this is the experience off one install, as I have not extensively tested it. Experiences may vary and as such, there may be things missing that could apply to you.
While the actual process of changing a server from Prefork to Worker is literally a single line, you must first ensure it is compatible.

Ensuring Compatibility

 There are two main things you must do to ensure that your server is compatible. The first is simply to ensure that all of your virtual servers are not set to run on mod_php, but rather cgi or fastcgi.
You can find, and change, this under each virtual server, where Server Configuration > Website Options > PHP script execution mod. You must ensure it is set to either CGI wrapper or FCGID.

Worker is also incompatible with a few Apache directives for PHP. These are php_flag, php_admin_flag, php_value, php_admin_value. You must ensure that these are either removed or commented out of all configuration and htaccess files.

Prior to doing this, it would be a good idea to change to your root account in SSH.

A good way to find the files would be to use grep. I have provided the command for this in the example. There are most likely other, better, ways to find and fix the files, but here's a good way as, while it is fairly long winded, it'll help ensure no problems sneak up on you after switching.

First, cd to your public_html directory and run this command:

grep -E -r --include="*.htaccess" "php_flag|php_admin_flag|php_value|php_admin_value" *

It will display a list of all the mentions of any of the directives, so you may find some files mentioned more than once. You will need to go to and edit each one and comment out all the lines that are displayed. Sub-domains can be done all at once by using cd to "domains" folder, and performing the same command.

You'll need to repeat this for each user that you have on your system. If your site needs these overrides, you will need to find other ways of performing them, such as custom php.ini files.

Now, do the same to all the sites' Apache configuration files. This should be far quicker as I only needed to do it to my top-level domains and main apache2.conf file.

To do this, cd to /etc/apache2/ and perform this command:

grep -E -r --include="*.conf" "php_flag|php_admin_flag|php_value|php_admin_value" *

Repeat the same processing of commenting out the lines with these files. Files within sites-enabled are symlinks of files within sites-available, so you only need to change the ones in sites-enabled.

Installing Apache MPM Worker

An OS's package software should solve the dependencies automatically, and as such, you can switch them over with a single line.

sudo apt-get install apache2-mpm-worker

It should inform you of something that is close to the following:

The following packages will be REMOVED
  apache2-mpm-prefork libapache2-mod-php5
The following NEW packages will be installed

Enter "Y", and it will automatically stop the Apache service, uninstall Prefork, install Worker and attempt to start the Apache service again.

If you encounter any errors here, simply fix them (they will most likely be caused by errors similar to those we prevented above) and try to start the service again by doing the following:

sudo service apache2 start


It is worth mentioning that, as a foot note, I discovered an unexpected issue when doing this where our "Registered Only" plugin stopped working due to cases. It was fairly easy to fix, however. It is recommended to test your site out thoroughly on a developmental server before pushing the changes out to a production server.

It is also worth noting that you may be required to manually change the PHP Script Execution Mode (in Website Options) to FCGId for every domain you create.

No comments:

Post a Comment