In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Implementing web services on a LInux server is nothing more than Apache, Nginx, and Tomcat.
The difference between the three:
Apache: many modules are supported, such as servlet processing model, synchronous blocking model, changeable working mode, slow processing speed and stable operation for high concurrency scenarios; Nginx: lightweight web server with few self-supporting modules, which needs to be supported by third-party modules (Nginx official website can be downloaded), using epoll processing mode, asynchronous non-blocking, suitable for high concurrency scenarios, simple configuration The open source subproject under the Tomcat:apache Software Foundation has also become a container, mainly dealing with pages written in java language, as well as html pages, with small concurrent links.
Today's blog post focuses on the apache-2.4.23 version:
Friends who don't know much about apache can refer to the blog post: detailed explanation of building Web server based on Apache
There is no specific description of what apache is and its cause.
Blog outline:
I. httpd-2.4.23 version features
II. Compile and install httpd-2.4.23
III. Three working modes of apche
IV. The difference between processes and threads
I. httpd-2.4.23 version features
New modules in the new version of httpd-2.4.23:
Mod_proxy_fcgi (can provide fcgi proxy); mod_ratelimit (limit user bandwidth); mod_request (request module to filter requests); mod_remoteip (match client's IP address)
The IP-based access control has been modified to no longer support allow, deny, and order mechanisms, but to unify the use of require!
New features are:
MPM supports loading at run time; supports envet working mode; supports asynchronous read and write functions; each module can specify the log level of the output; enhanced expression parser; request configuration:,; millisecond keepalive timeout; supports FQDN virtual hosts; supports custom variables
Next, start the installation!
II. Compile and install httpd-2.4.23
Installation environment:
Use the centos 7 system; download the required software package (1) first check whether the httpd package [root@localhost ~] # rpm-Q httpd is not installed httpd / / if present, use the "- e" option to uninstall (2) install apr
Apr is a portable runtime library of Apache and an abstract library for operating system bars, which is used to realize the use of operating system by internal components of Apache and improve the portability of the system.
[root@localhost ~] # tar zxf apr-1.5.2.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/apr-1.5.2/ [root@localhost apr-1.5.2] #. / configure-- prefix=/usr/local/apr & & make & & make install (3) install apr-util [root@localhost] # tar zxf apr-util-1.5.4.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/apr-util-1.5.4/ [root@localhost apr-util-1.5.4] #. / configure-- prefix=/usr/local/apr-util-- with-apr=/usr/local/apr & & make & & make install (4) install zlib [root @ localhost ~] # tar zxf zlib-1.2.8.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/zlib-1.2.8 / [root@localhost zlib-1.2.8] #. / configure-- prefix=/usr/local/zlib & & make & make install (5) install root@localhost zlib-1.2.8 [root @ localhost ~] # tar zxf pcre-8.39.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/pcre-8.39/ [root@localhost pcre-8.39] #. / configure-- prefix=/usr/local/pcre & & make & & make install (6) install openssl
When you install the apache-2.4.23 version, you will be prompted that the version that comes with openssl is too low and you need to install the new version manually.
[root@localhost ~] # tar zxf openssl-1.0.1u.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/openssl-1.0.1u/ [root@localhost openssl-1.0.1u] #. / config-fPIC-- prefix=/usr/local/openssl enable-shared & & make & & make install//PIC makes the code snippet of the .so file truly shared / / if you don't add-fPIC, when loading the code snippet of the .so file The data objects referenced by the code snippet need to be relocated, and the relocation will modify the contents of the code snippet, which causes every process that uses the .so file code segment to generate the copy of the .so file code snippet in the kernel. Every copy is different. Depends on the location of the .so file code snippet and the data segment memory map. (7) install httpd-2.4.23 [root@localhost ~] # tar zxf httpd-2.4.23.tar.gz-C / usr/src [root@localhost ~] # cd / usr/src/httpd-2.4.23/ [root@localhost 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 & & make & & make install
The meaning of each option:
-- enable-so: support dynamic sharing module;-- enable-rewrite: support url rewriting;-- enable-ssl: support ssl;--with-ssl=/usr/local/openssl: specify ssl installation location;-- enable-cgi: enable cgi general network management interface;-- enable-cgid: open cig general network management interface management program;-- enable-modules=most: compile to binary is commonly used to install modules;-- enable-mpms-shared=all: install all working modes of apache -- with-mpm=event: specify the default operation mode-- with-pcre=/usr/local/pcre: support pcre;-- with-z=/usr/local/zlib: use zlib compression library;-- with-apr=/usr/local/apr: specify the installation path of apr;-- with-apr-util=/usr/local/apr-util: specify the installation path of apr-util;-- enable-expires: support caching;-- enable-deflate: support compression (8) optimize the execution path of http program [root@localhost httpd-2.4.23] # ln-s / usr/local/http-2.4.23/bin/* / usr/local/bin/ (9) add as system service [root@localhost httpd-2.4.23] # cp / usr/local/http-2.4.23/bin/apachectl / etc/init.d/httpd [root@localhost httpd-2.4.23] # vim / etc / chkconfig: 35 85 15 / / add. / / omit the following [root@localhost httpd-2.4.23] # chkconfig-- add httpd [root@localhost httpd-2.4.23] # systemctl start httpd [root@localhost httpd-2.4.23] # netstat-anpt | grep 80tcp6 0 0: 80: * LISTEN 108903/httpd [root@localhost ~] # httpd- M / / you can view the module AH00558: httpd: Could not reliably determine the server's fully qualified domain name loaded by the httpd service startup Using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message// can omit the above information when starting the service, or you can modify its configuration file (/ usr/local/http-2.4.23/conf/httpd.conf) file to find the line "ServerName" Modify Loaded Modules: core_module (static) so_module (static) http_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) reqtimeout_module (shared) filter_module (shared) mime_module (shared) log_config_module (shared) Env_module (shared) headers_module (shared) setenvif_module (shared) version_module (shared) mpm_prefork_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) dir_module (shared) alias_module (shared)
At this point, apache-2.4.23 has been installed.
Three working modes of apche (1) Prefork mode
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.
As shown in the figure:
Apache always tries to keep some spare or idle child processes for upcoming requests. In this way, the client's request does not have to wait for the child process to be generated after it is received.
Because each request corresponds to a child process in the mpm_prefork module, it consumes more system resources than the other two modules. However, the advantage of the mpm_prefork module is that each of its child processes processes the corresponding individual request independently, so that if there is a problem with one of the requests, the other requests will not be affected. Prefork is more efficient than Worker, but it uses much more memory and is not good at dealing with scenarios with high concurrency.
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 / / default existence delete "#" number # LoadModule mpm_event_module modules/mod_mpm_event.so// add # LoadModule mpm_prefork_module modules/mod_mpm_prefork.so// default existence, delete #! # 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.
The configuration files related to prefork mode are as follows:
[root@localhost ~] # vim / usr/local/http-2.4.23/conf/extra/httpd-mpm.conf / / these are the only parameters for setting the prefork mode 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.
For more information about its configuration, please refer to the picture:
/ / Note 1:MaxRequestWorkers is the most important of these instructions. It sets the request that Apache can process at the same time, and is the parameter that has the greatest impact on the performance of Apache. 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 4 gigabytes of memory, the initial value is 4, 000, 2, 000.
Note after the 2:prefork control process initially established a "StartServers" child process, in order to meet the needs of the MinSpareServers setting, 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.
(2) Worker working mode
Worker mode (multi-thread and multi-process): compared with prefork mode, worker uses a mixture of multi-process and multi-thread mode. 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. As shown in the figure:
Apache always tries to maintain a spare or idle pool of service threads. In this way, the client can be processed without waiting for a new thread or process to be established. In Unix, in order to bind port 80, the parent process is usually started as root, and then Apache sets up child processes and threads with lower-privileged users. The User and Group directives are used to configure permissions for Apache child processes. Although the child process must have read access to the content it provides, it should be given as few privileges as possible. In addition, unless suexec is used (responsible for processing requests from CGI and SSI programs, ensure that web users can execute remote programs with the directory or program's user ID. Otherwise, the permissions configured by these directives will be inherited by the CGI script
Threads are lighter than processes because threads usually share the memory space of the parent process, so the memory footprint is reduced and performs better than prefork mode in high concurrency scenarios.
Some people will wonder, so why not just use multithreading here? The main concern is stability. If a thread fails abnormally, it will cause the parent process to die as well as other normal child threads (they are all under the same process). In the multi-process + multi-threading mode, each process is independent. If an exception occurs in a thread, only part of the Apache service, not the whole service, will be affected. Other processes can still work.
For configuration of work mode:
/ / modify the httpd master configuration file, switch modes 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
For a detailed introduction of work mode, please refer to the picture:
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).
(3) Event MPM
This is the latest working mode of Apache, and it is very similar to worker mode, except that it solves the problem that thread resources are wasted when keep-alive persistent connections are made. In event working mode, there are some special threads used to manage these keep-alive type threads. When a real request comes, it passes the request to the thread of the server, and then allows it to be released after execution. This enhances request processing in high concurrency scenarios. As shown in the figure:
For configuration of Event MPM mode:
/ / modify the httpd master configuration file, switch the mode and restart the service, and then [root@localhost ~] # vim / usr/local/http-2.4.23/conf/extra/httpd-mpm.conf StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 / / its configuration is very similar to work mode. IV. The difference between processes and threads
A thread is an execution unit within a process, and it is also a schedulable entity within a process.
The difference between threads and processes:
Address space: an execution unit within a process; a process has at least one thread; they share the address space of a process; and a 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 resources of the process; threads are the basic unit of processor scheduling, but the process is not; both can be executed concurrently.
Processes and threads are the basic units of program running experienced by the operating system, and the system uses this basic unit to realize the concurrency of the system to the application.
The difference between a process and a thread is:
In short, a program has at least one process and a process has at least one thread.
The division scale of threads is smaller than that of processes, which makes multithreaded programs have high concurrency.
In addition, the process has independent memory units during execution, and multiple threads share memory, which greatly improves the process.
The operating efficiency of the order.
-this is the end of this article. Thank you for reading-
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.