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

How to optimize the performance of apache Server with ​

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to optimize the performance of the apache server, I hope you will gain something after reading this article, let's discuss it together!

Five tips for optimizing apache server performance:

Always update Apache to its latest version

There is no doubt that installing the latest version of Apache may be the first thing you need to consider. As of November 19, 2015, the latest version of Apache in the CentOS 7 repository was 2.4.6, while the latest version in Debian was 2.4.10.

Recently, however, an improvement or bug fix may be added to the newly released stable version, which can then be downloaded and installed from the source code. Compilation and installation instructions are also provided here-keep in mind that if you choose this update method, you may need to back up the current configuration file / site / virtual host as a precaution.

You can check the currently installed version as follows:

# httpd-v [RedHat / CentOS-based system] # apache2-v [Debian / Ubuntu-based system]

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

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

Why? Sendfile kernel system calls are enabled by default in kernel version 2.4 and later. This, in turn, facilitates high-performance network file transfer (required in the context of Web server-client communication) and enables Apache to provide static content faster and reduce CPU utilization by performing simultaneous read and send operations.

You can view the currently installed kernel using the following command:

# uname-r

Although this is not a process for beginners, upgrading the kernel is an interesting exercise to learn more about the internals of Linux.

Third, choose the multiprocessing module (MPM) that best suits your situation.

In fact, MPM extends the modularity of Web by allowing you to decide how to configure the Apache server to bind to a network port on your computer, accept requests from clients, and use child processes (and threads, or) to handle such requests.

Starting with version 2.4, Apache offers three different MPM options, depending on your needs:

The preforkMPM uses multiple child processes and does not need to be wired. Each process processes one connection at a time without creating a separate thread for each process. Without detailed instructions, we can say that this MPM is only used when debugging applications that use or if the application needs to deal with non-thread-safe modules such as mod_php.

The workerMPM uses each child process, and each thread handles connecting multiple threads at a time. This is a good choice for high-traffic servers because it allows more concurrent connections to be handled using less RAM than in the previous case.

Finally, eventMPM is the default MPM in most Apache installations of version 2.4 and later. Similar to worker MPM, it also creates multiple threads for each child process, but has one advantage: it causes KeepAlive or idle connections (while they remain in that state) to be processed by a single thread, thus freeing memory to be allocated to other threads. This MPM is not suitable for use with non-thread-safe modules such as mod_php, and must be used to replace this type of PHP-FPM.

To check the MPM used by the Apache installation, you can do the following:

# httpd-V

The following figure shows that this particular Web server is using prefork MPM.

To change this setting, you need to edit:

/ etc/httpd/conf.modules.d/00-mpm.conf [RedHat / CentOS based system]

/ etc/apache2/mods-available/ load [Debian / Ubuntu based system]

Where can be mpm_event,mpm_worker or mpm_prefork.

And uncomment the lines that load the required modules, as follows:

# LoadModule mpm_event_module modules/mod_mpm_event.so

Modified to:

LoadModule mpm_event_module modules/mod_mpm_event.so

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

In addition, for CentOS, you need php-fpm (as well as 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

Although you can set Apache to use a specific MPM, you can override the configuration on a per-virtual host basis in the same way described earlier.

Just put the appropriate label in the configuration file of each virtual host to get started-but make sure that each virtual host uses one and only one MPM.

Finally, note that regardless of the distribution you choose, php-fpm relies on the implementation of FastCGI, which is why I previously recommended additional package installations.

For more details and examples of php-fpm and how it works with event MPM to improve the performance of Apache, you should refer to the official documentation.

This is what I saw when I changed the default MPM from prefork to event in the same box shown in the previous picture:

In CentOS 7, you need to ensure that the http and https services are enabled through the firewall and that the network interface is correctly added to the default zone.

For example:

# 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

The reason I raise this problem is that I recently encountered a problem where the default firewalld configuration settings in the cloud VPS prevent php-fpm and Apache from processing php files.

As a basic test (I'm sure you can think of something more complex or stressful), I'll create a php file to check if there are files in the same directory of the two other test.php CentOS 7 servers with the same hardware characteristics and load but with different MPM. One of them will use events and the other will use prefork:

This is the PHP code checkiffileexists.php that I saved to the file named:

Then we will run the Apache benchmark tool (ab) and issue 200 requests at the same time until 2000 requests are completed:

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

Let's run the test and compare the results. Pay attention to performance statistics:

As you can see, servers with events outperform their prefork counterparts in every aspect of this test.

4. Assign RAM to Apache wisely

Perhaps the most important hardware item is the amount of RAM to be allocated for each Apache process. Although you can't control it directly, you can limit the number of child processes through the MaxRequestWorkers directive (formerly known as MaxClients in Apache 2.2), which limits Apache's use of RAM. Similarly, you can set this value on a per-host or per-virtual-host basis.

To do this, you should pay attention to the average amount of RAM used by Apache, and then multiply it by the number of MaxRequestWorkers, which is the amount of memory allocated to the Apache process. One of the things you never want a Web server to do is start using swap because this can significantly degrade its performance. Therefore, you should always limit the RAM use of Apache to what you can afford, and never rely on swapping.

For example, the following block limits the number of simultaneous clients to 30. If more clients access the host, they may experience delays or temporary failures, which can be easily resolved by refreshing the browser. While this may be considered undesirable, it is healthier for the server and best for your site in the long run.

You can put this block inside, / etc/httpd/conf/httpd.conf or / etc/apache2/apache2.conf depending on whether you are using CentOS or Debian.

Note that the same principle applies to all MPM-I use events here to continue the outline outlined in the previous hint

5. Understand your application

As a rule of thumb, you should not load any Apache modules that are not strictly required to run. This requires at least a complete knowledge of the applications running on the server, especially if you are a system administrator and have another team responsible for development.

You can list the currently loaded modules:

# httpd-M [RedHat / CentOS-based system] # apache2ctl-M [Debian / Ubuntu-based system]

To uninstall / disable modules in CentOS, you need to comment out lines that start with LoadModule (in the main configuration file or in the secondary file in / etc/httpd/conf.modules.d).

On the other hand, Debian provides a tool called a2dismod to disable modules, which are used as follows:

# a2dismod module_name

To enable it:

# a2enmod module_name

In either case, remember to restart Apache for the changes to take effect.

Summary

In this article, we reviewed five tips that will help you tune your Apache Web server and improve its performance. In addition, you should keep in mind that optimization and performance without security are meaningless, so you may need to refer to installing mod_pagespeed to improve Web server performance and Apache hardening tips in Tecmint.com.

After reading this article, I believe you have a certain understanding of "how to optimize the performance of apache server". If you want to know more about it, you are welcome to follow the industry information channel. 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.

Share To

Servers

Wechat

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

12
Report