Apache Prefork vs Worker MPM
Asked Answered
M

8

124

Looking at the Apache config file, I see Prefork and Worker MPM defined. What is the difference and which one is Apache using?

Mimosa answered 14/12, 2012 at 17:38 Comment(0)
C
127

Prefork and worker are two type of MPM apache provides. Both have their merits and demerits.

By default mpm is prefork which is thread safe.

Prefork MPM uses multiple child processes with one thread each and each process handles one connection at a time.

Worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time.

For more details you can visit https://httpd.apache.org/docs/2.4/mpm.html and https://httpd.apache.org/docs/2.4/mod/prefork.html

Caltrop answered 20/12, 2012 at 8:38 Comment(2)
See also "How do I select which Apache MPM to use?" serverfault.com/a/383634Bicephalous
@arvind //Each thread handles one connection at a time// here connection means single user or single request? pls explainFaroff
F
23

Apache's Multi-Processing Modules (MPMs) are responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests (http://httpd.apache.org/docs/2.2/mpm.html).

They're like any other Apache module, except that just one and only one MPM must be loaded into the server at any time. MPMs are chosen during configuration and compiled into the server by using the argument --with-mpm=NAME with the configure script where NAME is the name of the desired MPM.

Apache will use a default MPM for each operating system unless a different one is choosen at compile-time (for instance on Windows mpm_winnt is used by default). Here's the list of operating systems and their default MPMs:

  • BeOS beos
  • Netware mpm_netware
  • OS/2 mpmt_os2
  • Unix/Linux prefork (update for Apache version ≥ 2.4: prefork, worker, or event, depending on platform capabilities)
  • Windows mpm_winnt

To check what modules are compiled into the server use the command-line option -l (here is the documentation). For instance on a Windows installation you might get something like:

> httpd -l
Compiled in modules:
  core.c
  mod_win32.c
  mpm_winnt.c
  http_core.c
  mod_so.c

As of version 2.2 this is the list of available core features and MPM modules:

  • core - Core Apache HTTP Server features that are always available
  • mpm_common - A collection of directives that are implemented by more than one multi-processing module (MPM)
  • beos - This Multi-Processing Module is optimized for BeOS.
  • event - An experimental variant of the standard worker MPM
  • mpm_netware Multi-Processing Module implementing an exclusively threaded web server optimized for Novell NetWare
  • mpmt_os2 Hybrid multi-process, multi-threaded MPM for OS/2
  • prefork Implements a non-threaded, pre-forking web server
  • mpm_winnt - This Multi-Processing Module is optimized for Windows NT.
  • worker - Multi-Processing Module implementing a hybrid multi-threaded multi-process web server

Now, to the difference between prefork and worker.

The prefork MPM

implements a non-threaded, pre-forking web server that handles requests in a manner similar to Apache 1.3. It is appropriate for sites that need to avoid threading for compatibility with non-thread-safe libraries. It is also the best MPM for isolating each request, so that a problem with a single request will not affect any other.

The worker MPM implements a hybrid multi-process multi-threaded server and gives better performance, hence it should be preferred unless one is using other modules that contain non-thread-safe libraries (see also this discussion or this on Serverfault).

Ferdinand answered 15/6, 2015 at 8:14 Comment(1)
A default installation of ubuntu-trusty-64 of apache 2.4.7 is using event MPMDumfound
U
9

Take a look at this for more detail. It refers to how Apache handles multiple requests. Preforking, which is the default, starts a number of Apache processes (2 by default here, though I believe one can configure this through httpd.conf). Worker MPM will start a new thread per request, which I would guess, is more memory efficient. Historically, Apache has used prefork, so it's a better-tested model. Threading was only added in 2.0.

Undertint answered 14/12, 2012 at 18:29 Comment(1)
What about Event MPM?Interjection
R
6

For CentOS 6.x and 7.x (including Amazon Linux) use:

sudo httpd -V

This will show you which of the MPMs are configured. Either prefork, worker, or event. Prefork is the earlier, threadsafe model. Worker is multi-threaded, and event supports php-mpm which is supposed to be a better system for handling threads and requests.

However, your results may vary, based on configuration. I've seen a lot of instability in php-mpm and not any speed improvements. An aggressive spider can exhaust the maximum child processes in php-mpm quite easily.

The setting for prefork, worker, or event is set in sudo nano /etc/httpd/conf.modules.d/00-mpm.conf (for CentOS 6.x/7.x/Apache 2.4).

# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#LoadModule mpm_worker_module modules/mod_mpm_worker.so

# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#LoadModule mpm_event_module modules/mod_mpm_event.so
Reactive answered 24/11, 2016 at 6:16 Comment(0)
C
3

You can tell whether Apache is using preform or worker by issuing the following command

apache2ctl -l

In the resulting output, look for mentions of prefork.c or worker.c

Caprifig answered 5/6, 2014 at 4:59 Comment(4)
Apache can be compiled with both MPM modules so this isn't always reliable. If it lists two MPM modules try apachectl -V and look at the output next to Server MPM. Also can check ps aux and look for either httpd or httpd.worker.Nebulous
In my case apache2ctl -l did not work; had to use apachectl -l.Swithbert
none of them is listed for me, yet apache works fine, Apache/2.4.7 (Ubuntu)Vouvray
In centos 7.x which is running apache 2.4.6, httpd -V will give something like: Server MPM: workerBespectacled
N
2

Its easy to switch between prefork or worker mpm in Apache 2.4 on RHEL7

Check MPM type by executing

sudo httpd -V

Server version: Apache/2.4.6 (Red Hat Enterprise Linux)
Server built:   Jul 26 2017 04:45:44
Server's Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

Now to change MPM edit following file and uncomment required MPM

 /etc/httpd/conf.modules.d/00-mpm.conf 

# Select the MPM module which should be used by uncommenting exactly
# one of the following LoadModule lines:

# prefork MPM: Implements a non-threaded, pre-forking web server
# See: http://httpd.apache.org/docs/2.4/mod/prefork.html
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

# worker MPM: Multi-Processing Module implementing a hybrid
# multi-threaded multi-process web server
# See: http://httpd.apache.org/docs/2.4/mod/worker.html
#
#LoadModule mpm_worker_module modules/mod_mpm_worker.so

# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
#LoadModule mpm_event_module modules/mod_mpm_event.so
Noisy answered 9/3, 2018 at 13:46 Comment(1)
Why when i tried to use mpm_worker or mpm_event , it doesnt work my pageDeclinature
D
1

Apache has 2 types of MPM (Multi-Processing Modules) defined:

1:Prefork 2: Worker

By default, Apacke is configured in preforked mode i.e. non-threaded pre-forking web server. That means that each Apache child process contains a single thread and handles one request at a time. Because of that, it consumes more resources.

Apache also has the worker MPM that turns Apache into a multi-process, multi-threaded web server. Worker MPM uses multiple child processes with many threads each.

Diadem answered 14/5, 2020 at 19:28 Comment(0)
P
0

First check which module running:-

if you are configuring on ubuntu/debian:-

sudo apachectl -M | grep 'mpm'

vi /etc/apache2/mods-available/mpm_prefork.conf

..

.. StartServers 8

.. MinSpareServers 20

.. MaxSpareServers 40

.. MaxRequestWorkers 200

.. MaxConnectionsPerChild 4500

..

apachectl -t for syntax check or apachectl configtest

If same you want to change in worker or event then:-

/etc/apache2/mods-available/mpm_event.conf

And after change in different module, you require to switch module If you are using event module and want to change to Prefork Module:-

Disable event module:-

a2dismod mpm_event

Enable Prefork Module:-

a2enmod mpm_event

And restart apache:

service apache2 restart

if httpd (configure on redhat, centos,..)

First check which module running:-

httpd -V | grep -i ‘mpm’

vi /etc/httpd/conf.modules.d/00-mpm.conf

uncomment prefork Module line (LoadModule mpm_prefork_module modules/mod_mpm_prefork.so):-

LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

Same if you want to change other module then uncomment that module line(event or worker) and other comment

httpd -t for syntax check

Now start the apache service:- systemctl restart httpd

but sometimes its giving you error for php module before changing mpm module then all complete tutorial and calculation:-

https://www.youtube.com/watch?v=8V4TFgFvurU&list=PL5gKsZrSyQQIBNOTLGedvzlKLD9hKoTmS&index=10

Privet answered 29/6, 2023 at 5:0 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Yeo

© 2022 - 2024 — McMap. All rights reserved.