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的系统]
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
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.
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后所看到的:
在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:
这是我保存到名为的文件的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
让我们运行测试并比较结果。注意性能统计:
正如您所看到的,带有事件的服务器的性能在此测试的每个方面都高于其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 - 我在此处使用事件继续前面提示中概述的概
五、了解您的应用程序
根据经验,您不应加载任何非严格需要的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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

This article will explain how to improve website performance by analyzing Apache logs under the Debian system. 1. Log Analysis Basics Apache log records the detailed information of all HTTP requests, including IP address, timestamp, request URL, HTTP method and response code. In Debian systems, these logs are usually located in the /var/log/apache2/access.log and /var/log/apache2/error.log directories. Understanding the log structure is the first step in effective analysis. 2. Log analysis tool You can use a variety of tools to analyze Apache logs: Command line tools: grep, awk, sed and other command line tools.

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://<server IP or domain name>/server-status), or view the Apache configuration file (ServerVersion: Apache/<version number>).

This article introduces several methods to check the OpenSSL configuration of the Debian system to help you quickly grasp the security status of the system. 1. Confirm the OpenSSL version First, verify whether OpenSSL has been installed and version information. Enter the following command in the terminal: If opensslversion is not installed, the system will prompt an error. 2. View the configuration file. The main configuration file of OpenSSL is usually located in /etc/ssl/openssl.cnf. You can use a text editor (such as nano) to view: sudonano/etc/ssl/openssl.cnf This file contains important configuration information such as key, certificate path, and encryption algorithm. 3. Utilize OPE
