Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Apache installation and working mode

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Blog structure

Install apache

Working mode

one。 Compile and install apache2.4.23

The new version of httpd-2.4 adds the following features

New module; mod_proxy_fcgi (fcgi agent can be provided)

Mod_ratelimit (limit user bandwidth)

Mod_request (request module that filters requests)

Mod_remoteip (matches the IP address of the client)

The IP-based access control has been modified so that the allow,deny,order mechanism is no longer supported, but the require is used uniformly.

The following new features are added

1. MPM supports loading at run time; however, to enable this feature, you need to enable these three functions in the compilation installation;-- enable-mpms-shared=all-- with-mpm=event

2. Support event

3. Support asynchronous read and write

4. Specify the log level on each module and each directory

5. Enhanced expression parser

6. Configuration per request:

7. Millisecond keepalive timeout

8. FQDN-based virtual hosts no longer need NameVirtualHost instructions

9. Support the use of custom variables

Installation environment: operating system: Centos7.2, close selinux to check whether the httpd package is installed, and uninstall it if you check the installation

Download the source package

Note: apr (Apache Portable Runtime) Apache portable runtime, which is an abstract library for operating system calls, is used to realize the use of the operating system by Apache internal components and improve the portability of the system. Install apr and apr-util

Extract [root@localhost media] # lsapr-1.5.2.tar.gz cronolog-1.6.2.tar.gz openssl-1.0.1u.tar.gzapr-util-1.5.4.tar.gz httpd-2.4.23.tar.gz pcre-8.39.tar.gz [root@localhost media] # tar zxf apr-1.5.2.tar.gz-C / usr/src/ [root@localhost media] # tar zxf apr-util-1 .5.4.tar.gz-C / usr/src/ [root@localhost media] tar zxf zlib-1.2.8.tar.gz-C / usr/src/ [root@localhost media] # tar zxf pcre-8.39.tar.gz-C / usr/src/ [root@localhost media] # tar zxf httpd-2.4.23.tar.gz-C / usr/src/ [root@localhost media] # tar zxf openssl-1.0.1u.tar.gz-C / usr/ Src/ installation [root@localhost src] # cd / usr/src/ [root@localhost src] # lsapr-1.5.2 apr-util-1.5.4 debug httpd-2.4.23 kernels openssl-1.0.1u pcre-8.39 [root@localhost src] # cd apr-1.5.2/ [root@www apr-1.5.2] #. / configure-- prefix=/usr/local/apr [root@www apr-1.5.2] # make & & make Install [root@www ~] # cd apr-util-1.5.4/ [root@www apr-util-1.5.4] #. / configure-- prefix=/usr/local/apr-util-- with-apr=/usr/local/apr [root@www apr-util-1.5.4] # make & & make install installation zlib [root @ www ~] # cd zlib-1.2.8/ [root@www zlib-1.2.8] #. / configure-- Prefix=/usr/local/zlib [root@www zlib-1.2.8] # make & & make install install pcre[ root @ www ~] # cd pcre-8.39/ [root@www pcre-8.39] #. / configure-- prefix=/usr/local/pcre [root@www pcre-8.39] # make & & make install install openssl

When installing apache2.4.23, it is prompted that the version of openssl is too low, and centos7 comes with a version of openssl-1.0.1e.

[root@www ~] # cd openssl-1.0.1u/

[root@www openssl-1.0.1u] #. / config-fPIC-prefix=/usr/local/openssl enable-shared

[root@www openssl-1.0.1u] # make & & make install

[root@www ~] # mv / usr/bin/openssl / usr/bin/openssl.1.0.1e

[root@www] # ln-s / usr/local/openssl/bin/openssl / usr/bin/openssl

Install apache2.4.23 [root@www ~] # cd httpd-2.4.23/ [root@www httpd-2.4.23] #. / configure-- prefix=/usr/local/http-2.4.23-- enable-so-- enable-cgi-- enable-cgid-- enable-ssl-- with-ssl=/usr/local/openssl-- enable-rewrite-- with-pcre=/usr/local/pcre-- with-z=/usr/local/zlib-- with-apr= / usr/local/apr--with-apr-util=/usr/local/apr-util-- enable-modules=most-- enable-mods-shared=most-- enable-mpms-shared=all-- with-mpm=event--enable-proxy-- enable-proxy-fcgi-- enable-expires-- enable-deflate [root@www httpd-2.4.23] # make & & make install [root@www httpd-2.4.23] # ln-s / usr/local/http-2.4.23/bin/* / usr / local/bin/\\ optimize the http program execution path [root@www httpd-2.4.23] # ln-s / usr/local/http-2.4.23/bin/* / usr/local/bin\\ modify the configuration file httpd.conf Set the ServerName value [root@www /] # / usr/local/http-2.4.23/apachectl start [root@www httpd-2.4.23] # cp / usr/local/http-2.4.23/bin/apachectl / etc/init.d/httpd\\ automatically start [root@www httpd-2.4.23] # vi / etc/init.d/httpd\\ add the following two lines with # # chkconfig: 35 85 15 (in # description: apache 2.4.23 [root@www httpd-2.4.23] # chkconfig-- add httpd [root@www httpd-2.4.23] # chkconfig httpd on\\ add Apache to boot automatic startup [root@www httpd-2.4.23] # service httpd start [root@www httpd-2.4.23] # netstat-anplt | grep 80 boot 0 0: 80 :: * LISTEN 4807/httpd

Parameter explanation:

-- enable-so: support dynamic sharing module (that is, open DSO support)-- enable-rewrite: support url rewriting-- enable-ssl: support ssl-- with-ssl=/usr/local/openssl: specify ssl installation location-- enable-cgi: enable cgi-- enable-cgid:MPM uses event or worker to enable cgid-- enable-modules=most: specify the module to be statically compiled into httpd binaries A space-separated list of module names, all or most,all to contain all modules, most to contain most commonly used module blocks-enable-mods-shared=most: clearly indicate the module to be compiled in DSO, a space-separated list of module names, all or most,all to include all module blocks, most to include most modules-enable-mpms-shared=all: enable all supported modes of MPM In this way, event, worker, and prefork will be installed in a modular manner, and which one you want to use can be configured in httpd.conf. -- with-mpm=event: specifies the enabled mpm mode, which defaults to enevt mode. In earlier versions of apache, the default prefork,2.2 version is worker,2.4 version is event. -- with-pcre=/usr/local/pcre: support pcre-- with-z=/usr/local/zlib: use the zlib compression library-- with-apr=/usr/local/apr: specify the installation path for apr-- with-apr-util=/usr/local/apr-util: specify the installation path for apr-util II. Apache optimization and module interpretation of the hardware environment on which apache runs are the factors that have the greatest impact on sex, even if the hardware cannot be upgraded. It is also best to give apache a separate host to avoid interference from other applications. Among the various hardware indicators, memory has the greatest impact on performance. For static content (pictures, javascript files, css files, etc.), it determines how little content apache can cache. The more content it caches, the less chance to read content on the hard disk. Large memory can greatly improve the speed of static sites. For dynamic high-load sites, each request is saved for more time. Apache's mpm module will derive corresponding processes or threads for each request to be processed separately, and the number of processes or threads is approximately proportional to memory consumption, so increasing memory is also very beneficial to improve the load and running speed of dynamic sites, followed by the speed of hard drives, especially static sites. Apache is constantly reading files and sending corresponding requests, so the read and write of hard disk is extremely frequent. Dynamic sites also have to constantly load web programs (php, etc.), and even read more than a dozen files to complete a request, so as much as possible to improve the speed and quality of the hard disk is of positive significance to improve the performance of apache. Finally, cpu and network, cpu affects the execution speed of web programs, and the network affects the amount of traffic. Working mode

The Apache HTTP server is designed as a powerful and flexible server that can work on multiple platforms and in different environments. This modular design is called "multi-process processing module" (Multi-Processing Module,MPM), also known as working mode.

Note: if you want to change the process in the working mode, you need to go to the main configuration file

[root@www ~] # vi / usr/local/http-2.4.23/conf/httpd.conf

Include conf/extra/httpd-mpm.conf\ the previous # can be removed to change the process (search for mpm on about 456 pages)

[root@www /] # ps-ef | grep httpd\ View the process

[root@www /] # apachectl-t\ jian check syntax

Prefork mode (a non-threaded)

How it works: when the Apache service starts, the mpm_prefork module will create multiple child processes in advance (default is 5), each child process has only one thread. After receiving the client request, the mpm_prefork module transfers the request to the child process for processing, and each child process can only be used for a single request at the same time. If the current number of requests will exceed the number of pre-created child processes, the mpm_prefork module creates new child processes to handle additional requests.

How to turn on prefork mode: [root@localhost ~] # vim / usr/local/http-2.4.23/conf/httpd.conf// write the main configuration file of httpd, modify the content Include conf/extra/httpd-mpm.conf / / delete "#" by default, then # LoadModule mpm_event_module modules/mod_mpm_event.so// add # LoadModule mpm_prefork_module modules/mod_mpm_prefork.so// exists by default Just delete the #! # LoadModule mpm_worker_module modules/mod_mpm_worker.so// these three lines are the three working modes of apache You can switch [root@localhost ~] # systemctl restart httpd / / restart the httpd service [root@localhost ~] # systemctl start httpd / / manually restart [root@localhost ~] # httpd-V / / check the working mode of httpd / / find this line Server MPM: prefork / / you can see that Apache is now working in prefork mode [root@www ~] # apachectl-l / / lowercase L. Only static modules are shown) modify prefork parameters [root@www ~] # vi / usr/local/http-2.4.23/conf/extra/httpd-mpm.conf / / set prefork mode only these parameters Specific meaning: / / this is the parameter of prefork working mode-StartServers 5 / / the default number of child processes on apache startup MinSpareServers 5 / / the minimum number of idle processes MaxSpareServers 10 / / the maximum number of idle processes MaxRequestWorkers 250 / / setting the maximum number of access requests allowed at the same time MaxConnectionsPerChild 0 / / 0 means that the process will never be destroyed after each child process processes the data. When set to non-0, memory leaks can be prevented, and the server automatically reduces the number of its own processes during the load hour.

Note 1:MaxRequestWorkers is the most important of these instructions, setting requests that Apache can process at the same time, and is the parameter that has the greatest impact on Apache performance. If the total number of requests has reached this value (which can be confirmed by ps-ef | grep http | wc-l), then subsequent requests will be queued until a request has been processed. This is the main reason why there are a lot of system resources left and HTTP access is slow. Although in theory, the higher the value, the more requests can be processed, it is recommended that the initial value be set to (maximum physical memory / 2 in Mb), and then dynamically adjusted according to the load. For example, for a machine with 4G memory, the initial value is 4000GRAM 2000. After the 2:prefork control process initially established the "StartServers" child process, in order to meet the needs of the MinSpareServers setting, create one process, wait one second, continue to create two, and then wait one second to continue to create four. This increases the number of processes created exponentially, up to 32 per second, until the value set by MinSpareServers is met. This mode eliminates the need to generate new processes when the request arrives, thus reducing system overhead to increase performance. MaxSpareServers sets the maximum number of idle processes, and if the number of idle processes is greater than this value, Apache will automatically kill some extra processes. Do not set this value too high, but if the value is lower than MinSpareServers, Apache will automatically adjust it to MinSpareServers+1. If the site is heavily loaded, consider increasing MinSpareServers and MaxSpareServers at the same time. Note what's the difference between 3:ServerLimit and MaxClients (MaxRequestWorkers)? This is because in the apache1 era, the only parameter that controls the maximum number of processes is MaxClients, and this parameter has a maximum value of 256. it is invalid to try to set it to more than 256. this is due to the limitation of the server hardware in the apache1 era. However, in the apache2 era, due to the upgrade of server hardware, the hardware is no longer limited, so use the parameter ServerLimit to control the maximum number of processes, and the ServerLimit value > = MaxClient value is valid. ServerLimit should be placed before MaxClients, and the value should not be less than MaxClients.

Advantages: mature, compatible with all new and old modules. Processes are completely independent of each other, making it very stable. At the same time, there is no need to worry about thread safety. (our commonly used mod_php,PHP extension does not need to support thread safety)

Disadvantages: a process takes up more system resources and consumes more memory. Moreover, it is not good at handling highly concurrent requests, in which case it puts the request in a queue until a process is available before the request is processed.

Worker mode (multithreaded and multiprocess)

Compared with prefork mode, worker uses a mixed mode of multi-process and multi-thread. Worker mode also pre-derives some child processes, and then each child process creates some threads, including a listener thread, and each request is assigned to a thread to serve. Threads are lighter than processes because threads share the memory space of the parent process, so the memory footprint is reduced, and there are more threads available than prefork in high concurrency scenarios, resulting in better performance. In addition, if there is a problem with one thread, it will also cause problems with the threads under the same process. If there are problems with multiple threads, it will only affect part of the Apache, not all. Because of the use of multi-process and multi-threading, thread safety needs to be taken into account. When using keep-alive persistent connections, a thread will always be occupied, even if there is no request in the middle, it will have to wait until the timeout is released (this problem also exists in prefork mode) generally speaking, prefork is slightly faster than worker, but it requires slightly more cpu and memory resources than woker.

Work mode configuration / modify the httpd main configuration file, switch mode and restart the service Next [root@localhost ~] # vim / usr/local/http-2.4.23/conf/extra/httpd-mpm.conf / / this is the parameter of worker working mode StartServers 3 / / the default number of child processes started at apache startup is MinSpareThreads 75 / / the minimum number of idle worker threads-MaxSpareThreads 250 / / maximum number of idle worker threads-ThreadsPerChild 25 / / number of threads generated by each child process MaxRequestWorkers 400 / / maximum number of requests accepted by each process MaxConnectionsPerChild 0 / / means never destroyed

The total number of requests that can be processed simultaneously in Worker mode is determined by the total number of child processes multiplied by the ThreadsPerChild value, which should be greater than or equal to MaxRequestWorkers. If the load is so heavy that the number of existing child processes cannot be satisfied, the control process will derive new child processes. By default, the maximum total number of child processes is 16, and you also need to explicitly declare ServerLimit when enlarged (the maximum is 20000). It is important to note that if ServerLimit is explicitly declared, its value multiplied by ThreadsPerChild must be greater than or equal to MaxRequestWorkers, and MaxRequestWorkers must be an integral multiple of ThreadsPerChild, otherwise Apache will automatically adjust to a corresponding value.

Advantages: take up less memory and perform better with high concurrency.

Cons: thread safety must be considered because multiple child threads share the memory address of the parent process. If you use keep-alive 's long connection, there may be few requests in between, then blocking occurs, the thread is suspended, and you have to wait until the timeout before it is released. If too many threads are occupied in this way, it will also lead to the availability of unserviced threads in high concurrency scenarios. (this problem also occurs in prefork mode).

Event mode multi-process + multi-thread + epoll

This is the latest working mode of Apache and a variant of worker mode, which separates the service process from the connection. The difference of worker mode is that it solves the problem that thread resources are wasted during keep-alive persistent connections. In event working mode, there are some special threads used to manage these keep-alive type threads.

When the request comes over, the request is passed to the thread of the server, and when the execution is finished, it is allowed to release. This enhances request processing in high concurrency scenarios. Event mode does not support https access very well (issues related to HTTP authentication).

The difference between a process and a thread address space: an execution unit within a process; a process has at least one thread; they share the process's address space; and the process has its own independent address space; resource ownership: a process is a unit of resource allocation and ownership, and threads within the same process share the process's resources; threads are the basic unit of processor scheduling, but the process is not; both can be executed concurrently.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report