Apache uses a set of values called the Prefork MPM to determine how many
servers it will utilize and how many threads each server can process.
Out of the box all Apache installations use the same values regardless
of whether your server has 512Mb of RAM or 8Gb of RAM. It is important
that as the server administrator you configure these values to work with
your server load.
The Apache Prefork MPM can be found in the
Apache configuration file; usually /etc/httpd/conf/httpd.conf. The
default values are...
<IfModule prefork.c>
StartServers 2
MinSpareServers 3
MaxSpareServers 3
ServerLimit 75
MaxClients 75
MaxRequestsPerChild 1000
</IfModule>
Each Directive taken from "http://httpd.apache.org/docs/trunk/mod/mpm_common.html" is detailed below.
- - - - - - - - - - - -
The
StartServers directive sets the number of child server processes
created on startup. As the number of processes is dynamically
controlled depending on the load there is usually little reason to
adjust this parameter.
- - - - - - - - - - - -
The
MinSpareServers directive sets the desired minimum number of idle child
server processes. An idle process is one which is not handling a
request. If there are fewer than MinSpareServers idle then the parent
process creates new children until satisfies the MinSpareServers
setting.
- - - - - - - - - - - -
The MaxSpareServers
directive sets the desired maximum number of idle child server
processes. An idle process is one which is not handling a request. If
there are more than MaxSpareServers idle, then the parent process will
kill off the excess processes.
- - - - - - - - - - - -
The
ServerLimit directive is only used if you need to set MaxClients higher
than 256 (default). Do not set the value of this directive any higher
than what you might want to set MaxClients to.
- - - - - - - - - - - -
The
MaxClients directive sets the limit on the number of simultaneous
requests that will be served. Any connection attempts over the
MaxClients limit will normally be queued, up to a number based on the
ListenBacklog directive. Once a child process is freed at the end of a
different request, the connection will then be serviced.
For
non-threaded servers (i.e., prefork), MaxClients translates into the
maximum number of child processes that will be launched to serve
requests. The default value is 256; to increase it, you must also raise
ServerLimit.
- - - - - - - - - - - -
The
MaxConnectionsPerChild directive sets the limit on the number of
connections that an individual child server process will handle. After
MaxConnectionsPerChild connections, the child process will die. If
MaxConnectionsPerChild is 0, then the process will never expire.
Setting
MaxConnectionsPerChild to a non-zero value limits the amount of memory
that process can consume by (accidental) memory leakage.
- - - - - - - - - - - -
The
single most important directive is MaxClients as this determines the
amount of Apache child processes that will be launched to server
requests. A simple calculation for MaxClients would be:
(Total Memory - Critical Services Memory) / Size Per Apache process
I
define Critical Services as services such as mySQL, Plesk, Cpanel; any
service that is required for proper operation of your server.
I've
used the following commands via shell to determine values for Total
Memory, OS Memory, MySQL Memory, and Apache Process Size
TOTAL MEMORY
[root@vps httpd]# free -m
total used free shared buffers cached
Mem: 1002 599 402 0 28 337
-/+ buffers/cache: 233 769
Swap: 2047 124 1922
MYSQL MEMORY
[root@vps httpd]# ps aux | grep 'mysql' | awk '{print $6}'
408
21440
704
APACHE PROCESS SIZE
[root@vps httpd]# ps aux | grep 'httpd' | awk '{print $6}'
22468
11552
41492
40868
41120
41696
39488
41704
15552
16076
16084
728
In
this case the server has 1002Mb of memory allocated, xx used by the OS
itself, 21Mb used by mySQL, and each Apache thread averages about 30Mb.
MaxClients = (1002 - 21) / 30 therefore MaxClients = 32.7
The
next important aspect is MaxConnectionsPerChild as this is the amount
of threads that will be processed before the child is recycled.
A good calculation for MaxConnectionsPerChild would be:
(total amount of daily requests / total number of daily processes)
Determining
these values is a bit more complex as it requires some type of
statistics package or thorough knowledge of interpreting Apache access
logs.
As this does not adversely effect memory usage, only cpu
time to cycle the process if you are unable to determine this
information the standard 1000 should be used.
Thus a good configuration for this server would be
<IfModule prefork.c>
StartServers 2
MinSpareServers 3
MaxSpareServers 3
ServerLimit 30
MaxClients 30
MaxRequestsPerChild 1000
</IfModule>
Be sure once you've saved the file to perform a configuration test before restarting Apache.
[root@vps httpd]# service httpd configtest
Syntax OK
[root@vps httpd]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
Nguồn: http://www.hosting.com/support/linux/tuning-the-apache-prefork-mpm
Thứ Tư, 24 tháng 4, 2013
Optimize and Tune Apache for performance
Today we will talk about Linux and apache tips and tricks
that will help webmaster to tune their dedicated server to handle high
traffic.
KeepAlive options:
1. Timeout
Nguồn: http://www.linuxexpert.ro/Linux-Tutorials/optimize-and-tune-apache-for-performance.html
Apache configuration
httpd.conf is a file file, usually located at /usr/local/apache/conf/httpd.conf path containing configuration settings for apache server. Cannot find the location of the file? Use locate command: >cd / then >locate httpd.conf. You may find the values in conf/extra directory. Search for http-mpm.conf and http-default.conf
Main parameters to tune are:
In Apache MPM Prefork Module:
- StartServers
- MinSpareServers
- MaxSpareServers
- ServerLimit
- MaxClients
- MaxRequestsPerChild
KeepAlive options:
Timeout options:
- KeepAlive
- KeepAliveTimeout
- MaxKeepAliveRequests
1. Timeout
Apache MPM Prefork Module
Another handy tweak is to make some adjustments to the Apache MPM prefork module. This is assuming you are using Apache in prefork mode, which is highly recommended and likely if you are on a small VPS.
This module controls the number of processes and spare processes Apache will start and run. This is especially important if you are running a small VPS that is handling MySQL and Apache. Unless you are getting slammed with really heavy traffic on a regular basis (in which case you should be on a dedicated server) there is no need to be running the default configuration. Find these lines in your httpd.conf file:
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
MinSpareservers and MaxSpareServers control the number of spare processes your webserver is permitted to run and StartServers controls how many are started by default.
ServerLimit - controls the maximum configured value for MaxClients.
MaxClients – sets the limit on the number of simultaneous requests that can be supported.
Reducing MaxClients on a webserver that is serving dynamic content (e.g. WordPress) can make a big difference. If you experience a traffic spike on your VPS and your MaxClients is set too high your server will more than likely get stuck in an endless loop of swapping pages from physical memory to virtual memory, commonly referred to as thrashing. The accepted way of calculating an appropriate MaxClients value is dividing your total available system memory by the size per Apache process. For example, if you had a 500MB left for Apache to use and each Apache process was using around 20MB you would set your MaxClients to (512-12) / 10 = 50. To check real time memory usage on your VPS use top.Want to set it to a larger value? Add more physical RAM. In other words, calculate the average of your httpd process, divide total available memory by the average leaving some for the system. E.g. in case of 1 Gig RAM and average httpd process size 7MB on this server it is safe to set it to 100.
If you use MySQL database, MaxClients parameter should be equal or close to max_connections parameter in MySQL conf file (my.cnf)
MaxRequestsPerChild - limits the number of requests a child server will handle during it’s life. We can safely reduce this value and realize a small gain.
So let’s go ahead and pare down those values:
<IfModule prefork.c>
StartServers 3
MinSpareServers 3
MaxSpareServers 10
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 2000
</IfModule>
Remember these are not concrete “best” values, they depend on the size of your VPS and how small or large you Apache process is.
Optimize Your KeepAlive
KeepAlive allows your visitors to issue multiple requests over the same TCP connection, in theory this helps improve latency because your visitors can request your webpage, images, and javascripts all over one connection. Unfortunately, Apache must use a worker process to service each and every request. The worker process stays busy servicing each request for a full 15 seconds by default, even if your visitor is no longer using it! This means you have less worker processes available on your system at any given time. With the limited system resources you have on your small VPS we always want open worker processes to be actually working. One way of accomplishing this is turning off KeepAlive. Find this line in your httpd.conf file:
KeepAlive On
and change it to:
KeepAlive Off
If you have a site with lots of images and javascripts it is usually better leave KeepAlive turned on and make some additional tweaks.
If you decide to leave KeepAlive turned on it is important you change the default KeepAliveTimeout value. This prevents unused connections from staying open for excessive amounts of time. Find this line in your httpd.conf file:
KeepAliveTimeout 15
You want to leave this connection open for 2 seconds, just long enough for the client to request most if not all of the necessary files. It is best to se it to minimum, 1-3 seconds. So change that line to:
KeepAliveTimeout 2
If you are going to leave KeepAlive on you will want to increase MaxKeepAliveRequests. Setting this higher allows more requests per connection and increases efficiency. Find this line:
MaxKeepAliveRequests 100
and change it to:
MaxKeepAliveRequests 200
MaxRequestsPerChild – sets how many requests to serve per new httpd child process. You may set it very low, thus constantly freeing the memory, however on a particular case values like 15 or 20 may work well. As an example our site showing 10 images per page has this parameter set to 15.
MinSpareServers
MaxSpareServers
StartServers
Adjust Timeout
Another minor tweak that will give you a small performance boost as well as help reduce the effects of a DOS attack is changing the TimeOut Directive. This directive tells Apache how many seconds to wait while receiving an incoming request, processing it, and sending back a response. Find this line:
Timeout 120
and change it to:
Timeout 40
After you change settings in httpd.conf do not forget to restart apache. You may do this from control panel or from command line > service httpd restart
Nguồn: http://www.linuxexpert.ro/Linux-Tutorials/optimize-and-tune-apache-for-performance.html
1 số cấu hình apache mpm-prefork nên tham khảo
<IfModule mpm_prefork_module>
StartServers 50 #Khởi tạo 50 process khi start apache
MinSpareServers 25 #Sinh ra tối thiểu 25 idle process chờ xử lý
MaxSpareServers 50 #Sinh ra tối đa 50 idle process chờ xử lý
ServerLimit 512#Giá trị tối đa của MaxClients
MaxClients 512 #Đối với mpm_prefork, giá trị này có nghĩa là tối đa sẽ tạo ra 512 child processes để xử lý các request
MaxRequestsPerChild 10000 #Sau khi xử lý 10000 request thì process sẽ bị kill. Nếu muốn process tồn tại mãi mãi -> set giá trị =0. Tuy nhiên cần đề phòng memory leak
#
ThreadsPerChild
25 #Mặc định mỗi process sinh ra 25 thread xử lý</IfModule>
=> Công thức cho hệ thống XXX đang phát triển:
<IfModule mpm_prefork_module>
StartServers 50
MinSpareServers 25
MaxSpareServers 50
ServerLimit 125
MaxClients 125 (=3800MB Ram/30MB per process)
MaxRequestsPerChild 10000
#ThreadsPerChild 25 (default)
#MaxConnectionsPerChild (default)
</IfModule>
The Linux top command
Linux top command FAQ: Is there a utility to show Linux processes
interactively, like the Unix/Linux ps command, but more like a GUI ro
character-based interactive tool?
I was just writing a friend about how to use the Linux ps command, when I thought why bother, what he really needs to see right now is the Linux top command. The Linux top command is an interactive utility that displays a character-based screen of all processes running on the current system. The screen updates itself every few seconds, and you can sort the screen contents by characteristics like CPU Usage or Memory Use.
In this tutorial we'll take a look at the most common uses of the top command, including showing how to sort the output by CPU and memory use, and change the top command display.
As mentioned, this display will update itself automatically,
depending on any settings that you have configured, and the version of
the top command you're using.
In case I made that image too small, here's the content of this top help screen in text format:
Here are two quick examples. First, to sort the top command output by memory use, follow these steps:
The top command implementation is pretty different on Mac OS X. The top command itself brings up a screen, but the columns are different by default, and the commands you can issue are different. I'm not going to get into all the details for Mac OS X, but just use the '?' to get help.
You can control "summary" lines that are displayed on the top lines of the top command display. Use the following one-letter commands to toggle the display of the lines at the top of the display:
Nguồn: http://alvinalexander.com/linux/unix-linux-top-command-cpu-memory
I was just writing a friend about how to use the Linux ps command, when I thought why bother, what he really needs to see right now is the Linux top command. The Linux top command is an interactive utility that displays a character-based screen of all processes running on the current system. The screen updates itself every few seconds, and you can sort the screen contents by characteristics like CPU Usage or Memory Use.
In this tutorial we'll take a look at the most common uses of the top command, including showing how to sort the output by CPU and memory use, and change the top command display.
The Unix and Linux top command
When you issue the top command like this:topyou'll see a screen that looks something like this:
Linux top command help
To get help on how to use the top command, just press the letter '?' or 'h' when the top command is running. Doing so will display the following top command help screen:Z,B Global: 'Z' change color mappings; 'B' disable/enable bold l,t,m Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info 1,I Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode f,o . Fields/Columns: 'f' add or remove; 'o' change display order F or O . Select sort field <,> . Move sort field: '<' next col left; '>' next col right R,H . Toggle: 'R' normal/reverse sort; 'H' show threads c,i,S . Toggle: 'c' cmd name/line; 'i' idle tasks; 'S' cumulative time x,y . Toggle highlights: 'x' sort field; 'y' running tasks z,b . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y') u . Show specific user only n or # . Set maximum tasks displayed k,r Manipulate tasks: 'k' kill; 'r' renice d or s Set update interval W Write configuration file q Quit ( commands shown with '.' require a visible task display window ) Press 'h' or '?' for help with Windows,
Sorting the Linux top command display
In my opinion, the most important thing you need to know from this help screen is how to sort the top command display/output. Using the top command that came with my CentOS Linux system as of September, 2009, you sort the top command output by first pressing the 'O' key (the uppercase letter 'o'), then choosing a sort column on the following screen.Here are two quick examples. First, to sort the top command output by memory use, follow these steps:
- Press 'O'
- Press 'N'
- Press [Enter]
- Press 'O'
- Press 'K'
- Press [Enter]
Unix/Linux top command sort options
Here's the text version of the Linux top command sort options screen:Current Sort Field: K for window 1:Def Select sort field via field letter, type any other key to return a: PID = Process Id z: Flags = Task Flags <sched.h> b: PPID = Parent Process Pid c: RUSER = Real user name Note1: d: UID = User Id If a selected sort field can't be e: USER = User Name shown due to screen width or your f: GROUP = Group Name field order, the '<' and '>' keys g: TTY = Controlling Tty will be unavailable until a field h: PR = Priority within viewable range is chosen. i: NI = Nice value j: P = Last used cpu (SMP) Note2: * K: %CPU = CPU usage Field sorting uses internal values, l: TIME = CPU Time not those in column display. Thus, m: TIME+ = CPU Time, hundredths the TTY & WCHAN fields will violate n: %MEM = Memory usage (RES) strict ASCII collating sequence. o: VIRT = Virtual Image (kb) (shame on you if WCHAN is chosen) p: SWAP = Swapped size (kb) q: RES = Resident size (kb) r: CODE = Code size (kb) s: DATA = Data+Stack size (kb) t: SHR = Shared Mem size (kb) u: nFLT = Page Fault count v: nDRT = Dirty Pages count w: S = Process Status x: COMMAND = Command name/line y: WCHAN = Sleeping in FunctionAs you can see from that output, you can sort the top command output by many different pieces of process information. The asterisk indicates that the output is currently sorted by the %CPU column.
Linux and Unix top command - closing notes
To exit/quit the top command, just type the letter 'q'.The top command implementation is pretty different on Mac OS X. The top command itself brings up a screen, but the columns are different by default, and the commands you can issue are different. I'm not going to get into all the details for Mac OS X, but just use the '?' to get help.
You can control "summary" lines that are displayed on the top lines of the top command display. Use the following one-letter commands to toggle the display of the lines at the top of the display:
- l - load average
- m - memory
- t - task/cpu stats
Nguồn: http://alvinalexander.com/linux/unix-linux-top-command-cpu-memory
Đăng ký:
Bài đăng (Atom)