5 tips to optimize apache server performance

王林
Release: 2021-01-05 10:03:11
forward
5516 people have browsed it

5 tips to optimize apache server performance

The following are five tips for optimizing apache server performance:

(Learning video sharing: Programming video)

一,Always update Apache to its latest version

No doubt, installing the latest version of Apache is probably the first thing you need to consider. As of November 19, 2015, the latest version of Apache in the CentOS 7 repository is 2.4.6, while the latest version in Debian is 2.4.10.
However, there may be an improvement or bug fix recently added to a newly released stable version, which can then be downloaded and installed from source. Compilation and installation instructions are also provided here - please keep in mind that if you choose this update method you may need to back up your current configuration files/sites/vhosts as a precaution.

You can check the currently installed version as follows:

# httpd -v [基于RedHat / CentOS的系统]
# apache2 -v [基于Debian / Ubuntu的系统]
Copy after login

5 tips to optimize apache server performance

As a rule of thumb, stick to packages from your chosen distribution unless there is no other way The update method provided by the manager (yum update httpd or aptitude safe-upgrade apache2, for CentOS or Debian respectively).

2. If you are using a kernel earlier than 2.4, please consider upgrading immediately

Why? Kernel versions 2.4 and higher enable the sendfile kernel system call by default. This, in turn, facilitates high-performance network file transfers (required in the context of web server-client communication) and enables Apache to serve static content faster and reduce CPU utilization by performing simultaneous read and send operations. .

You can use the following command to view the currently installed kernel:

# uname -r
Copy after login

5 tips to optimize apache server performance

While this is a process not suitable for beginners, upgrading the kernel is a fun exercises to learn more about Linux internals.

3. Choose the Multi-Processing Module (MPM) that best suits your situation

In fact, MPM accepts requests from Client requests and the use of subprocesses (and threads, either) to handle such requests, extending Apache's modular capabilities.

Starting with version 2.4, Apache provides three different MPMs for you to choose from, depending on your needs:

The preforkMPM uses multiple child processes without threading. Each process handles one connection at a time without creating separate threads for each process. Without going into details, we can say that this MPM is used only when debugging applications or when the application needs to handle non-thread-safe modules like mod_php.
The workerMPM uses multiple threads per child process, and each thread handles one connection at a time. This is a good choice for high-traffic servers, as it allows handling more concurrent connections using less RAM than the previous case.
Finally, eventMPM is the default MPM in most Apache installations from version 2.4 and above. It is similar to the worker MPM in that it also creates multiple threads per child process but has one advantage: it causes KeepAlive or idle connections (while they remain in that state) to be handled by a single thread, thus freeing the memory that can be allocated to other threads. This MPM is not suitable for use with non-thread-safe modules such as mod_php and must be replaced with such PHP-FPM.

To check which MPM your Apache installation is using, you can do the following:

# httpd -V
Copy after login

The image below shows that this particular web server is using the prefork MPM.

5 tips to optimize apache server performance

To change this setting you need to edit:

/etc/httpd/conf.modules.d/00-mpm.conf [based on RedHat/ CentOS system]
/etc/apache2/mods -available/ load [Debian/Ubuntu-based system]

Which can be mpm_event, mpm_worker or mpm_prefork.

And uncomment the line that loads the required modules as follows:

#LoadModule mpm_event_module modules/mod_mpm_event.so
Change to:
LoadModule mpm_event_module modules/mod_mpm_event.so

Note: For event MPM to work in Debian, you may have to install the libapache2-mod-fastcgi package from a non-free repository.

Also, for CentOS, you need php-fpm (along with fcgi and mod_fcgid), while in Debian it is called php5-fpm (along with apache2-mpm-event).

Last, but not least, restart the web server and the newly installed php-fpm (or php5-fpm) service:

On RedHat/CentOS

# systemctl restart httpd php-fpm && systemctl enable httpd php-fpm

On Debian/Ubuntu

# systemctl restart apache2 php5-fpm && systemctl enable apache2 php5-fpm

While you can set up Apache to use a specific MPM, this configuration can be overridden on a per-vhost basis in the same manner as previously described.

只需将相应的标签放入每个虚拟主机的配置文件中即可开始使用 - 但请确保每个虚拟主机使用一个且只有一个MPM。

最后,请注意,无论您选择的发行版如何,php-fpm都依赖于FastCGI的实现,这就是为什么我之前推荐了额外的软件包安装的原因。

有关php-fpm的更多详细信息和示例以及它如何与事件MPM一起提高Apache的性能,您应该参考官方文档。

这是我在上一张图片所示的同一个框中将默认MPM从prefork更改为event后所看到的:

5 tips to optimize apache server performance

在CentOS 7中,您需要确保通过防火墙启用了http和https服务,并且网络接口已正确添加到默认区域。

例如:

# firewall-cmd --zone = internal --add-interface = tun6to4
# firewall-cmd --zone = internal --add-interface = tun6to4 --permanent
# firewall-cmd --set-default-zone = internal
# firewall-cmd --add-service = http
# firewall-cmd --add-service = https
# firewall-cmd --add-service = http --permanent
# firewall-cmd --add-service = https --permanent
# firewall-cmd --reload

我提出这个问题的原因是因为我最近遇到了一个问题,即云VPS 中的默认firewalld配置设置阻止了php-fpm和Apache处理php文件。

作为一个基本的测试(我相信你可以想到更复杂或更紧张的),我将创建一个php文件,检查是否存在另外test.php两个CentOS 7服务器的同一目录中具有相同硬件特性和负载的文件但是与不同的MPM。其中一个将使用事件,另一个将使用prefork:

5 tips to optimize apache server performance

这是我保存到名为的文件的PHP代码checkiffileexists.php:

<?PHP
$ filename =‘test.php’;
if(file_exists($ filename)){
echo“文件$ filename存在”;
} else {
echo“文件$ filename不存在”;
}
?>

然后我们将运行Apache基准测试工具(ab),同时发出200个请求,直到2000个请求完成:

# ab -k -c 100 -n 2000 localhost/checkiffileexists.php

让我们运行测试并比较结果。注意性能统计:

5 tips to optimize apache server performance

正如您所看到的,带有事件的服务器的性能在此测试的每个方面都高于其prefork对应物。

四、明智地为Apache分配RAM

也许最重要的硬件项是要为每个Apache进程分配的RAM量。虽然您无法直接控制它,但您可以通过MaxRequestWorkers指令(以前在Apache 2.2中称为MaxClients)限制子进程的数量,这将限制Apache对RAM的使用。同样,您可以在每个主机或每个虚拟主机的基础上设置此值。

要做到这一点,你应该注意Apache使用的平均RAM量,然后乘以MaxRequestWorkers的数量,这就是为Apache进程分配的内存量。您从不希望Web服务器做的一件事是开始使用swap,因为这会显着降低其性能。因此,您应始终将Apache的RAM使用限制在您能够承受的范围内,并且永远不要依赖交换。

例如,以下块将同时客户端的数量限制为30。如果有更多客户端访问主机,他们可能会遇到延迟或暂时故障,可以通过刷新浏览器轻松解决。虽然这可能被认为是不合需要的,但它对于服务器来说更健康,从长远来看,对您的网站也是最好的。

您可以将此块放在内部,/etc/httpd/conf/httpd.conf或者/etc/apache2/apache2.conf取决于您使用的是CentOS还是Debian。

请注意,同样的原则适用于所有MPM - 我在此处使用事件继续前面提示中概述的概

5 tips to optimize apache server performance

五、了解您的应用程序

根据经验,您不应加载任何非严格需要的Apache模块才能运行。这至少需要了解服务器上运行的应用程序的全部知识,特别是如果您是系统管理员并且还有另一个负责开发的团队。

您可以列出当前加载的模块:

# httpd -M [基于RedHat / CentOS的系统]
# apache2ctl -M [基于Debian / Ubuntu的系统]
Copy after login

要卸载/禁用CentOS中的模块,您需要注释掉以LoadModule开头的行(在主配置文件中或在/etc/httpd/conf.modules.d中的辅助文件中)。

另一方面,Debian提供了一个名为a2dismod的工具来禁用模块,其用法如下:

# a2dismod module_name
Copy after login

要启用它:

# a2enmod module_name
Copy after login

在任何一种情况下,请记住重新启动Apache以使更改生效。

Summary
In this article, we reviewed 5 tips that will help you tune your Apache web server and improve its performance. Additionally, you should remember that optimization and performance without security are meaningless, so you may want to refer to Install mod_pagespeed to improve web server performance and the Apache hardening tips article from Tecmint.com.

Related recommendations: apache tutorial

The above is the detailed content of 5 tips to optimize apache server performance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template