Apache,apache optimization

Source: Internet
Author: User
Tags server memory

Apache,apache Optimized reading:2516Time: 2011-01-04 12:07:30 font: [Big small] Apache2.0 PREFORK.C module and WORKER.C module
Idle child process: Refers to a child process that is not processing a request.
1. PREFORK.C Module (a non-threaded, pre-derived MPM)
Prefork MPM uses multiple child processes, with only one thread per child process. Each process can only maintain one connection at a certain time. On most platforms, the Prefork MPM is more efficient than the worker mpm, but the memory usage is much larger. Prefork's wireless path design in some cases will be more advantageous than a worker: it can use third-party modules that do not handle thread safety, and it is easier to debug for platforms that have difficulty debugging threads.
<ifmodule prefork.c>
Serverlimit20000
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients 1000
Maxrequestsperchild 0
</IfModule>
Serverlimit2000
The default maxclient maximum is 256 threads, if you want to set a larger value, add ServerlimitThis parameter. 20000 is ServerlimitThe maximum value of this parameter. If you need to be larger, you must compile Apache, which did not need to be recompiled before Apache
Effective premise: Must be placed in front of other directives
Startservers 5
Specifies the number of child processes that are established when the server starts, and Prefork defaults to 5.
Minspareservers 5
Specifies the minimum number of idle child processes, which defaults to 5. If the number of currently idle child processes is less than minspareservers, then ApacheNew sub-processes will be generated at a maximum rate of one per second. Do not set this parameter too large.
Maxspareservers 10
Sets the maximum number of idle child processes, which defaults to 10. If there are currently more than maxspareservers of idle child processes, the parent process kills the extra child processes. Do not set this parameter too large. If you set the value of this directive to be smaller than minspareservers, ApacheIt will be automatically modified to "minspareservers+1".
MaxClients 256
Limit the number of client maximum access requests at the same time (number of concurrent threads per process), which defaults to 256. Any request exceeding the maxclients limit will enter the waiting queue, and once a link is freed, the request in the queue will be serviced. To increase this value, you must increase the Serverlimit
Maxrequestsperchild 10000
The maximum number of requests that each child process allows for a servo during its lifetime, by default 10000. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end.
Setting Maxrequestsperchild to a value other than 0 has two benefits:
1. You can prevent (accidental) memory leaks from being carried out indefinitely, thus exhausting memory.
2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.
Working mode:
A separate control process (the parent process) is responsible for generating child processes that are used to listen for requests and respond. ApacheAlways try to keep some spare (spare) or idle child processes used to meet the incoming request. This way, the client does not need to wait for the child process to be generated before the service is received. In Unix systems, the parent process typically runs as root for 80 ports, while ApacheThe resulting child process is typically run with a low-privileged user. The user and group directives are used to set up low-privileged users of child processes. The user who runs the child process must have read permission to the content it serves, but must have as few permissions as possible for other resources than the service content.
2. WORKER.C Module (multi-Threading multi-process module supporting mixed multithreading)
The worker MPM uses multiple child processes, each of which has multiple threads. Each thread can only maintain one connection at a certain time. In general, worker MPM is a good choice on a high-traffic HTTP server because the worker MPM uses much less memory than Prefork MPM. But the worker mpm is also imperfect, and if a thread crashes, the entire process will "die" along with all of its threads. Because threads share memory space, a program must be recognized by the system as "every thread is safe" at run time.
<ifmodule worker.c>
Serverlimit50
Threadlimit 200
Startservers 5
MaxClients 5000
Minsparethreads 25
Maxsparethreads 500
Threadsperchild 100
Maxrequestsperchild 0
</IfModule>
Serverlimit16
Maximum number of processes allowed to be configured by the server. This instruction is used in conjunction with Threadlimit to set the value of the MaxClients maximum allowable configuration. Any changes to this instruction during the reboot will be ignored, but the changes to the maxclients will take effect.
Threadlimit 64
The maximum number of threads that can be configured per child process. This instruction sets the maximum number of threads that can be configured per subprocess Threadsperchild. Any changes to this instruction during the reboot will be ignored, but the changes to the Threadsperchild will take effect. The default value is "64".
Startservers 3
The number of child processes that were established when the server started, and the default value is "3".
Minsparethreads 75
The minimum number of idle threads, the default value is "75". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too small, the child process will produce a new idle thread.
Maxsparethreads 250
Sets the maximum number of idle threads. The default value is "250". This MPM will monitor the number of idle threads based on the entire server. If the total number of idle threads in the server is too many, the child process kills the extra idle threads. The value range of the maxsparethreads is limited. ApacheThe values you set are automatically corrected according to the following restrictions: The worker requires that it is greater than or equal to Minsparethreads plus threadsperchild and
MaxClients 400
Maximum number of Access requests (maximum number of threads) that allow simultaneous servos. Any requests exceeding the maxclients limit will enter the waiting queue. The default value is "400", 16 ( Serverlimit) multiplied by the result of (Threadsperchild). So to increase maxclients, you must simultaneously increase ServerlimitThe value.
Threadsperchild 25
The number of resident execution threads established by each child process. The default value is 25. When a child process establishes these threads at startup, no new threads are established.
Maxrequestsperchild 0
Sets the maximum number of requests per child process to allow the servo during its lifetime. When the Maxrequestsperchild limit is reached, the child process will end. If Maxrequestsperchild is "0", the child process will never end.
Setting Maxrequestsperchild to a value other than 0 has two benefits:
1. You can prevent (accidental) memory leaks from being carried out indefinitely, thus exhausting memory.
2. Give the process a limited lifespan, thus helping to reduce the number of active processes when the server load is reduced.
Attention
For a keepalive link, only the first request is counted. In fact, it changes the behavior of each child process to limit the maximum number of links.
Working mode:
The number of threads that each process can have is fixed. The server increases or decreases the number of processes depending on the load. A separate control process (parent process) is responsible for the creation of child processes. Each child process can establish a threadsperchild number of service threads and a listener thread that listens to the access request and passes it to the service thread for processing and answering. ApacheAlways try to maintain a standby (spare) or free service thread pool. This way, the client does not have to wait for new threads or new processes to be established to be processed. In Unix, in order to be able to bind port 80, the parent process is typically started as root, and then ApacheCreate child processes and threads with lower-privileged users. The user and group directives are used to set ApacheThe permissions of the child process. Although a child process must have read access to the content it provides, it should give it as little privilege as possible. Also, unless suEXEC is used, the permissions set by these directives will be inherited by the CGI script.
Formula:
Threadlimit >= Threadsperchild
MaxClients <= Serverlimit* Threadsperchild must be a multiple of threadsperchild
Maxsparethreads >= Minsparethreads+threadsperchild
Hard limit:
The two instructions for Serverlimi and threadlimit determine the number of active child processes and the hard limit of the number of threads per child process. To change this hard limit you must completely stop the server and then start the server (a direct restart is not possible).
ApacheIn the compilation ServerlimitWhen there is a hard limit inside, you cannot go beyond this limit.
Prefork MPM Max is " Serverlimit200000 "
The maximum number of other MPM (including work MPM) is " Serverlimit20000
ApacheThere is a hard limit inside when compiling threadlimit, you cannot go beyond this limit.
Mpm_winnt is "Threadlimit 15000"
Other MPM (including work prefork) are "Threadlimit 20000
Attention
Use ServerlimitBe especially careful when you are with Threadlimit. If the ServerlimitAnd Threadlimit is set to a higher value that actually takes a lot of values and will have too much shared memory to be allocated. When set to exceed the system's processing power, ApacheMay not start, or the system will become unstable.
http://www.linuxdiyf.com/bbs/viewthread.php?tid=16027
ApacheOptimized for sites with a slightly larger number of visits, ApacheThese default configurations are not sufficient, we still need to adjust ApacheSome of the parameters that make ApacheAbility to perform better performance in large traffic environments. Below we have ApacheThere are some descriptions of the parameters that have a large performance impact in the configuration file httpd.conf.
(1) Timeout This parameter specifies ApacheThe maximum wait time (in seconds) before the request is received or the requested content is sent, if the time is exceeded ApacheThe request is discarded and the connection is released. The default value for this parameter is 120, the recommended setting is 60, and for sites with a large amount of traffic you can set it to 30.
(2) KeepAlive this parameter control ApacheWhether to allow multiple requests in one connection, opens by default. However, for most forum type sites, it is usually set to off to turn off this support.
(3) Mpm-prefork.c by default ApacheUsing the Prefork (process) mode of operation, it can be said that this part of the parameter setting is ApacheThe core and key of the performance impact. The user can find the following configuration section in the configuration document: Copy content to Clipboard code: <ifmodule prefork.c>
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients 15
Maxrequestsperchild 0
</IfModule>
That's the control. ApacheProcess work configuration section, in order to better understand the parameters in the above configuration, let us first look at Apacheis how to control the process work. We know that in Unix systems, many services (service) daemons (Daemon) Create a process at startup to prepare for a possible connection request, and the service enters the port listening state when a request from the client is sent to the port that the service listens on. The service process processes the request, and during processing, the process is in an exclusive state, meaning that if there are other requests arriving at this time, the requests can only "queue" for the current request to be processed and the service process to be freed. This leads to more and more requests waiting in the queue, and the actual performance is that the service is very poorly handled. ApacheThe use of the Prefork model solves this problem very well. Now let's see. ApacheActually how to work efficiently.
When ApacheWhen you start, ApacheA startspareservers idle process is started to receive processing requests at the same time, and when multiple requests arrive, the starspareservers will become less, and when the idle process is reduced to minspareservers, ApacheIn order to be able to continue to have sufficient process processing requests, it will start startsservers process standby, which greatly reduces the possibility of request queue waiting, so that the efficiency of the service increased, this is what is called pre-fork; let's keep tracking. ApacheThe work that we assume Apache200 processes have been started to process the request, in theory, at this point ApacheA total of 205 processes, and over a period of time, assuming that 100 requests have been ApacheThe 100 processes are released as idle processes at this time, then ApacheThere are 105 idle processes. In the case of services, there is no point in starting too many idle processes, but it can degrade the overall performance of the server. ApacheIs there really going to be 105 free processes? Of course not! As a matter of fact ApacheCheck yourself at any time, and when you find that there are more than maxspareservers idle processes, you will automatically stop closing some processes to ensure that the idle process is too much. Speaking of which, the user should ApacheHave a certain understanding of the way of working, if you want to get more detailed instructions please see ApacheManual documentation.
We also have two parameters not described: MaxClients and maxrequestperchild;maxclients Specify ApacheThe maximum number of clients allowed to connect at the same time, and if more than maxclients connections are available, the client will get a "server Busy" error page. We see that by default maxclients is set to 15, which is obviously not enough for some medium-sized sites and large sites! Maybe you need to allow 512 client connections to meet your application needs, okay, so let's change maxclients to 512, save httpd.conf and exit, restart Apache, unfortunately, during the restart process you see some error hints, ApacheReboot failed. The error message tells you that maxclients can only be set to 256, I believe you must be very disappointed. But don't be depressed, ApacheAs a world-class Web server must not be so thin! By default, maxclients can only be set to no more than 256 integers, but if you need to be completely customizable, then you need to use the Serverlimit parameter to use, simply say serverlimit is like a bucket, While maxclients is like water, you can accommodate more water (maxclients) by replacing a larger bucket (set Serverlimit to a larger value), but be aware that The set value of maxclients is not greater than the set value of Serverlimit!
Let's look at the Maxrequestperchild parameter, which specifies how many threads in a connection process can work at the same time. Perhaps this explanation is too professional, then you just think "network Ant", "Internet Express FlashGet" in the "multi-point simultaneous download" Can, this parameter is actually limit can use a few "point". The default setting is 0, which is: No limit. However, it is important to note that if you set this value too small it will cause access problems, if there is no special need or the traffic pressure is not very large can maintain the default value, if the traffic is very large, it is recommended to set to 2048.
Well, explain so much, let's see the recommended configuration for the modified PERFORK.C configuration segment: Copy content to Clipboard code: <ifmodule prefork.c>
Startservers 5
Minspareservers 5
Maxspareservers 10
Serverlimit 1024
MaxClients 768
Maxrequestsperchild 0
</IfModule>
Completed the above ApacheThe adjustment, Apachehas achieved a greater performance improvement. Remember that you need to restart after you modify any parameters ApacheIn order to take effect. About ApacheThe optimization is far more than these, interested users can read ApacheManual documentation or find some literature to learn.
2. PHP Optimization for PHP optimization is mainly related to the main parameters in the php.ini reasonable adjustment and settings, the following we will see some of the php.ini in the performance of a large number of parameters should be set. Copy content to Clipboard code: # Vi/etc/php.ini
(1) PHP function disable find: Copy content to Clipboard code: Disable_functions =
This option can set which PHP functions are forbidden, there are some functions in PHP the risk is quite large, you can directly execute some system-level script commands, if you allow these functions to execute, when the PHP program vulnerabilities, the loss is very serious! Below we give the recommended disabling function settings: Copy content to Clipboard code: Disable_functions = Phpinfo,passthru,exec,system,popen,chroot,escapeshellcmd, Escapeshellarg,shell_exec,proc_open,proc_get_status
Note: If your server contains some PHP programs for system State detection, do not disable functions such as Shell_exec,proc_open,proc_get_status.
(2) PHP script execution time found: Copy content to Clipboard code: Max_execution_time = 30
This option sets the maximum execution time for a PHP program, and if a PHP script is requested and the PHP script does not complete within max_execution_time time, PHP will no longer execute and return a timeout error directly to the client. There is no special need this option to maintain the default setting of 30 seconds, if your PHP script does require a long execution time, you can increase the time setting appropriately.
(3) PHP script handles memory usage find: Copy content to Clipboard code: Memory_limit = 8M
This option specifies the maximum memory that PHP scripting can occupy, the default is 8MB, and if your server memory is more than 1GB, this option can be set to 12MB for faster PHP script processing efficiency.
(4) PHP global function declaration found: Copy content to Clipboard code: Register_globals = Off
Many articles on the web about PHP settings are recommended to set this option to on, which is a dangerous setting and is likely to cause serious security problems. If there is no special need, it is highly recommended to keep the default settings!
(5) PHP upload file size limit found: Copy content to Clipboard code: Upload_max_filesize = 2M
This option sets PHP to allow the maximum upload file size, which defaults to 2MB. Depending on the actual application needs, this setting can be increased appropriately.
(6) Session storage media found: Copy content to Clipboard code: Session.save_path
If your PHP program uses session dialog, you can set the session storage location to/DEV/SHM,/DEV/SHM is the Linux system unique TMPFS file system, is the memory as the primary storage mode of the file system, better than RAMDisk, Because Diskswap can be used as a supplement, and is the system comes with the function module, do not need to be configured separately. Think of how much faster it will be from disk IO operations to memory operations? Just be aware that the data stored in the/DEV/SHM will be lost after the server restarts. But this is insignificant for the session.
Http://blog.donews.com/zzw45/archive/2005/10/21/596603.aspx
Today in the compiled Apache2.0.48 (MPM for default prefork) want to change the size of the maxclient larger than 256, on the online forum to see the following increase ServerlimitOne line is on the line.
<ifmodule prefork.c>
Startservers 10
Minspareservers 10
Maxspareservers 15
Serverlimit2000
MaxClients 1000
Maxrequestsperchild 10000
</IfModule>
But as said above add a line, still not, hint maxclient more than 256, must add Serverlimitparameter is not OK.
Depressed for a long time, I would estimate is ServerlimitOne line did not take effect, and Google a bit, over the mountains, and finally found the reason
Need to put ServerlimitPut it to the front, just like this.
<ifmodule prefork.c>
Serverlimit2000
Startservers 10
Minspareservers 10
Maxspareservers 15
MaxClients 1500
Maxrequestsperchild 10000
</IfModule>

Apache,apache optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.