search
HomeOperation and MaintenanceApache5 tips to optimize apache server performance

5 tips to optimize apache server performance

Jan 05, 2021 am 10:03 AM
apacheserver

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的系统]

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

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

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:

$ 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的系统]

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

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

# a2dismod module_name

要启用它:

# a2enmod module_name

在任何一种情况下,请记住重新启动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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
What Defined Apache? Its Core FunctionalityWhat Defined Apache? Its Core FunctionalityMay 09, 2025 am 12:21 AM

The core function of Apache is modular design and high customization, allowing it to meet various web service needs. 1. Modular design allows for extended functions by loading different modules. 2. Supports multiple operating systems and is suitable for different environments. 3. Multi-process, multi-threaded and event-driven models improve performance. 4. The basic usage includes configuring the virtual host and document root directory. 5. Advanced usage involves URL rewriting, load balancing and reverse proxying. 6. Common errors can be debugged through syntax checking and log analysis. 7. Performance optimization includes adjusting MPM settings and enabling cache.

Apache's Continued Use: Web Hosting and BeyondApache's Continued Use: Web Hosting and BeyondMay 08, 2025 am 12:15 AM

What makes Apache still popular in modern web environments is its powerful capabilities and flexibility. 1) Modular design allows custom functions such as security certification and load balancing. 2) Support multiple operating systems to enhance popularity. 3) Efficiently handle concurrent requests, suitable for various application scenarios.

Apache: From Open Source to Industry StandardApache: From Open Source to Industry StandardMay 07, 2025 am 12:05 AM

The reasons why Apache has developed from an open source project to an industry standard include: 1) community-driven, attracting global developers to participate; 2) standardization and compatibility, complying with Internet standards; 3) business support and ecosystem, and obtaining enterprise-level market support.

Apache's Legacy: Impact on Web HostingApache's Legacy: Impact on Web HostingMay 06, 2025 am 12:03 AM

Apache's impact on Webhosting is mainly reflected in its open source features, powerful capabilities and flexibility. 1) Open source features lower the threshold for Webhosting. 2) Powerful features and flexibility make it the first choice for large websites and businesses. 3) The virtual host function saves costs. Although performance may decline in high concurrency conditions, Apache remains competitive through continuous optimization.

Apache: The History and Contributions to the WebApache: The History and Contributions to the WebMay 05, 2025 am 12:14 AM

Originally originated in 1995, Apache was created by a group of developers to improve the NCSAHTTPd server and become the most widely used web server in the world. 1. Originated in 1995, it aims to improve the NCSAHTTPd server. 2. Define the Web server standards and promote the development of the open source movement. 3. It has nurtured important sub-projects such as Tomcat and Kafka. 4. Facing the challenges of cloud computing and container technology, we will focus on integrating with cloud-native technologies in the future.

Apache's Impact: Shaping the InternetApache's Impact: Shaping the InternetMay 04, 2025 am 12:05 AM

Apache has shaped the Internet by providing a stable web server infrastructure, promoting open source culture and incubating important projects. 1) Apache provides a stable web server infrastructure and promotes innovation in web technology. 2) Apache has promoted the development of open source culture, and ASF has incubated important projects such as Hadoop and Kafka. 3) Despite the performance challenges, Apache's future is still full of hope, and ASF continues to launch new technologies.

The Legacy of Apache: A Look at Its Impact on Web ServersThe Legacy of Apache: A Look at Its Impact on Web ServersMay 03, 2025 am 12:03 AM

Since its creation by volunteers in 1995, ApacheHTTPServer has had a profound impact on the web server field. 1. It originates from dissatisfaction with NCSAHTTPd and provides more stable and reliable services. 2. The establishment of the Apache Software Foundation marks its transformation into an ecosystem. 3. Its modular design and security enhance the flexibility and security of the web server. 4. Despite the decline in market share, Apache is still closely linked to modern web technologies. 5. Through configuration optimization and caching, Apache improves performance. 6. Error logs and debug mode help solve common problems.

Apache's Purpose: Serving Web ContentApache's Purpose: Serving Web ContentMay 02, 2025 am 12:23 AM

ApacheHTTPServer continues to efficiently serve Web content in modern Internet environments through modular design, virtual hosting functions and performance optimization. 1) Modular design allows adding functions such as URL rewriting to improve website SEO performance. 2) Virtual hosting function hosts multiple websites on one server, saving costs and simplifying management. 3) Through multi-threading and caching optimization, Apache can handle a large number of concurrent connections, improving response speed and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools