Configuring resource limits in Apache virtual hosts involves setting directives that control the amount of server resources allocated to each virtual host. This can help prevent a single virtual host from consuming excessive resources and impacting the overall server performance. Below are the steps to configure resource limits in Apache virtual hosts:
Enable mod_cgid
or mod_cgi
:
Ensure that the mod_cgid
or mod_cgi
module is enabled in your Apache configuration. This module is responsible for executing CGI scripts and is required for setting resource limits.
- Use the following command to enable mod_cgid module:
a2enmod cgid
- Or, you can use the following command to enable the mod_cgi module:
a2enmod cgi
After enabling the module, restart Apache:\
systemctl restart apache2
Configure Resource Limits
Open the Apache configuration file for the specific virtual host you want to configure. This file is typically located in the /etc/apache2/sites-available/
directory.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html/example
<Directory "/var/www/html/example">
Options +ExecCGI
AddHandler cgi-script .cgi
LimitRequestBody 1048576
Timeout 30
MaxRequestWorkers 150
MaxConnectionsPerChild 100
RLimitCPU 10
RLimitMEM 1048576
RLimitNPROC 50
</Directory>
</VirtualHost>
Here are some commonly used directives for resource limits, along with their explanations:
LimitRequestBody
This sets the limit on the size of the request body:
LimitRequestBody 1048576 # Limit request body to 1 MB
Timeout
This sets the maximum time, in seconds, that the server will wait for a request to complete:
Timeout 30 # Set the maximum request processing time to 30 seconds
MaxRequestWorkers
This sets the maximum number of simultaneous connections that will be processed:
MaxRequestWorkers 150 # Set the maximum number of simultaneous connections to 150
MaxConnectionsPerChild
This sets the maximum number of connections a child process will handle before it is terminated and replaced with a new one.
MaxConnectionsPerChild 100 # Set the maximum connections per child process to 100
RLimitCPU and RLimitMEM
This limits the CPU time and memory a process can consume:
RLimitCPU 10 # Limit CPU usage to 10 seconds
RLimitMEM 1048576 # Limit memory usage to 1 GB
RLimitNPROC
This limits the number of processes a user can create:
RLimitNPROC 50 # Limit the number of processes to 50
Restart Apache
After making changes to the Apache configuration, restart the Apache web server to apply the changes:
systemctl restart apache2
Verify that the virtual host is working correctly, and the resource limits are enforced.