{"id":120,"date":"2024-05-09T23:14:01","date_gmt":"2024-05-09T16:14:01","guid":{"rendered":"http:\/\/nitirat.ddns.net\/wordpress\/?p=120"},"modified":"2024-06-19T23:29:03","modified_gmt":"2024-06-19T16:29:03","slug":"apache-web-server-security-and-hardening-tips","status":"publish","type":"post","link":"http:\/\/nitirat.ddns.net\/wordpress\/?p=120","title":{"rendered":"Apache Web Server Security and Hardening Tips"},"content":{"rendered":"\n<p class=\"has-yuki-font-small-font-size\"><strong>1. How to Hide Apache Version and OS Information<\/strong><\/p>\n\n\n\n<p>By default, the Apache web server displays its version in case you browse the wrong URL of a website. Below is an example of an error page indicating that the page cannot be found on the site. The last line indicates the Apache version, the host OS, the IP address, and the port it is listening on.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1018\" height=\"378\" src=\"http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" class=\"wp-image-121\" srcset=\"http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image.png 1018w, http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image-300x111.png 300w, http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image-768x285.png 768w\" sizes=\"auto, (max-width: 1018px) 100vw, 1018px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo nano \/etc\/apache2\/apache2.conf<\/code><\/pre>\n\n\n\n<p>Add the following lines at the end of the file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ServerTokens Prod\nServerSignature Off<\/code><\/pre>\n\n\n\n<p>Save the changes and restart the Apache web server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo systemctl restart apache2   &#91;On Debian, Ubuntu and Mint]<\/code><\/pre>\n\n\n\n<p>Now reload the site and, this time around, the web server information will not be displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"291\" src=\"http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image-1.png\" alt=\"\" class=\"wp-image-124\" srcset=\"http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image-1.png 947w, http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image-1-300x92.png 300w, http:\/\/nitirat.ddns.net\/wordpress\/wp-content\/uploads\/2024\/05\/image-1-768x236.png 768w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><\/figure>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">2. Disable Directory Listing in Apache<\/h2>\n\n\n\n<p>By default,&nbsp;<strong>Apache<\/strong>&nbsp;allows directory listing, and visitors might see whatever files or directories you might have on your&nbsp;<strong>Document Root<\/strong>&nbsp;directory.<\/p>\n\n\n\n<p>To demonstrate this, we will create a directory called&nbsp;<strong>test<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo mkdir -p \/var\/www\/html\/test<\/pre>\n\n\n\n<p>Next, we will navigate into the directory and create a few files.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd \/var\/www\/html\/test\n$ sudo touch app.py main.py<\/pre>\n\n\n\n<p>Now, if we access the URL,&nbsp;<code>http:\/\/localhost\/test<\/code>&nbsp;we will be able to view the directory listing.<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_53069\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Disable-Directory-Listing.png\" alt=\"Disable Apache Directory Listing\" class=\"wp-image-53069\"\/><figcaption class=\"wp-element-caption\">Disable Apache Directory Listing<\/figcaption><\/figure>\n\n\n\n<p>To disable directory listing, head over to Apache\u2019s main configuration file and search for the \u2018<strong>Directory<\/strong>\u2018 attribute. Set the \u2018<strong>Options<\/strong>\u2018 parameter to&nbsp;<code>'-Indexes'<\/code>&nbsp;as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Directory \/opt\/apache\/htdocs&gt;\nOptions -Indexes\n&lt;\/Directory&gt;<\/pre>\n\n\n\n<p>Reload&nbsp;<strong>Apache<\/strong>, and this time around, when you visit the URL, the directories will no longer be displayed.<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_53070\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Disable-Directory-Listing-in-Apache.png\" alt=\"Disable Directory Listing in Apache\" class=\"wp-image-53070\"\/><figcaption class=\"wp-element-caption\">Disable Directory Listing in Apache<\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">3. Regularly Update Apache<\/h2>\n\n\n\n<p>It\u2019s always recommended to keep all your applications up to date, as the latest applications come with bug fixes and security patches that address underlying vulnerabilities present in older software versions.<\/p>\n\n\n\n<p>As such, regularly upgrading your applications to their latest versions is recommended.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo apt update &amp;&amp; sudo apt upgrade [On <strong>Debian, Ubuntu and Mint<\/strong>]\n$ sudo dnf upgrade                    [On <strong>RHEL\/CentOS\/Fedora<\/strong> and <strong>Rocky\/AlmaLinux<\/strong>]<\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_53072\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Update-System-Packages.png\" alt=\"Update System Packages\" class=\"wp-image-53072\"\/><figcaption class=\"wp-element-caption\">Update System Packages<\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">4. Use HTTPS Encryption on Apache<\/h2>\n\n\n\n<p><strong>Apache<\/strong>, by default, uses HTTP protocol which is a weak and insecure protocol that is prone to eavesdropping. To improve your site\u2019s security and, more so, improve your Google SEO rankings, consider encrypting your site using an SSL certificate.<\/p>\n\n\n\n<p>By so doing, it switches the default HTTP protocol to&nbsp;<strong>HTTPS<\/strong>, thereby making it harder for anyone to intercept and decipher communication being sent back and forth from the server.<\/p>\n\n\n\n<p>Check out how to secure the Apache web server using&nbsp;<strong>Let\u2019s Encrypt SSL<\/strong>&nbsp;on Linux.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tecmint.com\/secure-apache-with-lets-encrypt-ssl-certificate-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Secure Apache with Let\u2019s Encrypt SSL Certificate on RHEL<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.tecmint.com\/install-free-lets-encrypt-ssl-certificate-for-apache-on-debian-and-ubuntu\/\" target=\"_blank\">How to Secure Apache with Free Let\u2019s Encrypt SSL Certificate on Ubuntu and Debian<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">5. Enable HTTP Strict Transport Security (HSTS) for Apache<\/h2>\n\n\n\n<p>In addition to encrypting your website with a\u00a0<strong>TLS\/SSL<\/strong>\u00a0certificate, consider implementing the\u00a0<strong>HSTS<\/strong>\u00a0web security mechanism on top of\u00a0<strong>HTTPS<\/strong>.<\/p>\n\n\n\n<p><strong>HTTP Strict Transport Security<\/strong>&nbsp;(<strong>HSTS<\/strong>) is a policy mechanism that protects websites from man-in-the-middle attacks &amp; cookie hijacking. This happens when attackers downgrade the HTTPS protocol to the insecure HTTP protocol.<\/p>\n\n\n\n<p><strong>HSTS<\/strong>&nbsp;enables the web server to strictly declare that web browsers should only interact with it via HTTPS, and never through the HTTP protocol.<\/p>\n\n\n\n<p>To enable&nbsp;<strong>HSTS<\/strong>, ensure that your website is running&nbsp;<strong>HTTPS<\/strong>&nbsp;and has a valid&nbsp;<strong>TLS\/SSL<\/strong>&nbsp;certificate.<\/p>\n\n\n\n<p>Next, enable the headers module for Apache:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo a2enmod headers<\/pre>\n\n\n\n<p>Then reload&nbsp;<strong>Apache<\/strong>&nbsp;to apply the changes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl restart apache2<\/pre>\n\n\n\n<p>Next, access your domain\u2019s virtual host configuration file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo vim \/etc\/apache2\/sites-available\/mydomain.conf<\/code><\/pre>\n\n\n\n<p>Next, add this line within the\u00a0<code>&lt;VirtualHost *:443><\/code>\u00a0block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Header always set Strict-Transport-Security \"max-age=31536000; includeSubDomains\"<\/code><\/pre>\n\n\n\n<p>This looks as follows.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;VirtualHost *:443>\n        # .....\n        # ....\n        Header always set Strict-Transport-Security \"max-age=31536000; includeSubDomains\"\n&lt;\/VirtualHost><\/pre>\n\n\n\n<p>The&nbsp;<strong>max-age<\/strong>&nbsp;parameter instructs web browsers to only access your site using&nbsp;<strong>HTTPS<\/strong>&nbsp;for the next one year (<strong>31536000 = 1 year<\/strong>).<\/p>\n\n\n\n<p>Finally, restart&nbsp;<strong>Apache<\/strong>&nbsp;for the&nbsp;<strong>HSTS<\/strong>&nbsp;policy to take effect.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl restart apache2<\/pre>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">6. Enable HTTP\/2 on Apache<\/h2>\n\n\n\n<p>In 2015,&nbsp;<strong>HTTP\/2<\/strong>&nbsp;was released, which is a new version of the HTTP protocol that seeks to address or resolve multiple problems that the creators of HTTP\/1.1 did not foresee.<\/p>\n\n\n\n<p>While&nbsp;<strong>HTTP\/1.1<\/strong>&nbsp;is still widely in use, it is associated with performance issues associated with the use of multiple TCP connections to process multiple requests from the browser, which leads to high resource overhead on the client side, leading to degraded network performance.<\/p>\n\n\n\n<p>As applications grew in complexity and functionality,&nbsp;<strong>HTTP\/2<\/strong>&nbsp;was created to solve the shortcomings of&nbsp;<strong>HTTP\/1.1<\/strong>&nbsp;which include long HTTP headers, slow web page loading speeds, and general performance degradation.<\/p>\n\n\n\n<p><strong>HTTP\/2<\/strong>\u00a0provides more protection and privacy than its predecessor. Equally important, is enhanced performance through the use of multiplexed streams of data. With\u00a0<strong>HTTP\/2<\/strong>\u00a0a single TCP connection ensures effective bandwidth utilization, even when transmitting multiple streams of data.<\/p>\n\n\n\n<h3 class=\"has-yuki-font-small-font-size wp-block-heading\">Enable HTTP\/2 on a Apache Virtual Host<\/h3>\n\n\n\n<p>To get started, first confirm that the webserver is running&nbsp;<strong>HTTP\/1.1<\/strong>. You can do this on a browser by opening the developer tools section on&nbsp;<strong>Google chrome<\/strong>&nbsp;using the&nbsp;<code>Ctrl +SHIFT + I<\/code>&nbsp;combination. Click on the \u2018<strong>Network<\/strong>\u2019 tab and locate the \u2018<strong>Protocol<\/strong>\u2019 column.<\/p>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_37824\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2020\/06\/Confirm-HTTP-Protocol-Version.jpg\" alt=\"Confirm HTTP Protocol Version\" class=\"wp-image-37824\"\/><figcaption class=\"wp-element-caption\">Confirm HTTP Protocol Version<\/figcaption><\/figure>\n\n\n\n<p>Next, enable the&nbsp;<strong>HTTP\/2<\/strong>&nbsp;module on&nbsp;<strong>Ubuntu<\/strong>&nbsp;by running the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo a2enmod http2\n<\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_37825\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2020\/06\/Enable-HTTP2-on-Ubuntu.png\" alt=\"Enable HTTP\/2 on Ubuntu\" class=\"wp-image-37825\"\/><figcaption class=\"wp-element-caption\">Enable HTTP\/2 on Ubuntu<\/figcaption><\/figure>\n\n\n\n<p>Next, locate and edit your&nbsp;<strong>SSL<\/strong>&nbsp;virtual host file, if you\u2019ve enabled HTTPS using&nbsp;<strong>Let\u2019s Encrypt<\/strong>, a new file is created with a&nbsp;<strong>le-ssl.conf<\/strong>&nbsp;suffix.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo vim \/etc\/apache2\/sites-enabled\/your-domain-name-le-ssl.conf<\/pre>\n\n\n\n<p>Insert the directive below after the&nbsp;<code>&lt;VirtualHost *:443&gt;<\/code>&nbsp;tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Protocols h2 http\/1.1<\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_37826\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2020\/06\/Enable-HTTP2-on-Apache.png\" alt=\"Enable HTTP\/2 on Apache\" class=\"wp-image-37826\"\/><figcaption class=\"wp-element-caption\">Enable HTTP\/2 on Apache<\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">7. Restrict Access to Sensitive Directories in Apache<\/h2>\n\n\n\n<p>Another security measure you might take is to limit access to directories that might contain sensitive information such as user data, logs, and configuration files.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;VirtualHost *:80>\n    ServerName example.com\n    DocumentRoot \/var\/www\/html\n\n    # Other virtual host settings\n\n    &lt;Directory \/var\/www\/html\/sensitive_directory>\n        Require all denied\n    &lt;\/Directory>\n&lt;\/VirtualHost><\/pre>\n\n\n\n<p>In the above configuration the&nbsp;<code>Require all denied<\/code>&nbsp;denies access to anyone trying to access files in \/<strong>sensitive_directory<\/strong>.<\/p>\n\n\n\n<p>Save the changes and exit the file. Then restart Apache for the changes to come into effect.<\/p>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">8. Disable the ServerSignature Directive in Apache<\/h2>\n\n\n\n<p>The&nbsp;<strong>ServerSignature<\/strong>&nbsp;directive in the Apache configuration file appends a footer to server-generated documents that bear information about your web server\u2019s configuration such as the version and the OS on which it is running. Exposing crucial details about your web server to malicious actors will significantly increase the chances of an attack.<\/p>\n\n\n\n<p>To prevent exposure of such information, you need to disable this directive in the Apache configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ServerSignature Off<\/pre>\n\n\n\n<p>Save the changes and once again restart&nbsp;<strong>Apache<\/strong>&nbsp;for the changes to come into effect.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl restart apache2<\/pre>\n\n\n\n<p class=\"has-yuki-font-small-font-size\"><strong>9. Set the \u2018ServerTokens\u2019 Directive to \u2018Prod\u2019<\/strong><\/p>\n\n\n\n<p>The \u2018<strong>ServerTokens<\/strong>\u2018 directive controls what information the server sends including Apache version (major and minor version), OS, and the type of web server running.<\/p>\n\n\n\n<p>The least amount of information that you would want to expose to the public is that the web server is Apache. Anything else would only expose your server to potential attacks. Therefore, it\u2019s recommended to set the \u2018<strong>ServerTokens<\/strong>\u2018 directive in the Apache configuration file to \u2018<strong>prod<\/strong>\u2018.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ServerTokens Off<\/pre>\n\n\n\n<p>As always save the changes and be sure to restart Apache.<\/p>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">10. Secure Apache with Fail2ban<\/h2>\n\n\n\n<p><strong>Fail2ban<\/strong>&nbsp;is an open-source intrusion prevention application that protects Linux systems from external threats including DoS and brute-force attacks. It works by constantly monitoring systems logs for nefarious activity and banning hosts that match patterns that mimic attack behavior.<\/p>\n\n\n\n<p><strong>Fail2ban<\/strong>&nbsp;can be configured to protect Apache from DoS attacks by constantly monitoring Apache logs for failed login attempts and temporarily banning offending IPs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Install Fail2Ban on Debian\/Ubuntu<\/h4>\n\n\n\n<p>First, update your packages and install&nbsp;<strong>fail2ban<\/strong>&nbsp;as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># apt-get update &amp;&amp; apt-get upgrade -y\n# apt-get install fail2ban<\/pre>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">11. Disable Unnecessary Modules<\/h2>\n\n\n\n<p>Apache modules are simply programs that are loaded to extend the functionality of the web server Functions extended by modules include basic authentication, content caching, encryption, security, etc.<\/p>\n\n\n\n<p>To view all enabled modules, run the command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ apache2ctl -M<\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_53073\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/View-Apache-Modules.png\" alt=\"View Apache Modules\" class=\"wp-image-53073\"\/><figcaption class=\"wp-element-caption\">View Apache Modules<\/figcaption><\/figure>\n\n\n\n<p>To check if a specific module is enabled, for example, the rewrite module, run the command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ apache2ctl -M | grep rewrite<\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_53074\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Check-Apache-Module.png\" alt=\"Check Apache Module\" class=\"wp-image-53074\"\/><figcaption class=\"wp-element-caption\">Check Apache Module<\/figcaption><\/figure>\n\n\n\n<p>To disable the module, run the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo a2dismod rewrite <\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_53075\"><img decoding=\"async\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/10\/Disable-Apache-Module.png\" alt=\"Disable Apache Module\" class=\"wp-image-53075\"\/><figcaption class=\"wp-element-caption\">Disable Apache Module<\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">12. Use mod_security and mod_evasive Modules to Secure Apache<\/h2>\n\n\n\n<p>You can enable the&nbsp;<strong>mod_security<\/strong>&nbsp;and&nbsp;<strong>mod_evasive<\/strong>&nbsp;modules to secure Apache against brute-force attacks or DDoS attacks.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The&nbsp;<strong>mod_security<\/strong>&nbsp;module acts like a web application firewall (<strong>WAF<\/strong>) and blocks suspicious and unwanted traffic to your site.<\/li>\n\n\n\n<li>The&nbsp;<strong>mod_evasive<\/strong>&nbsp;module safeguards your server from brute force and denial of service attacks (DoS).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">13. Restricted Unwanted Services in Apache<\/h2>\n\n\n\n<p>To further secure\u00a0<strong>Apache<\/strong>, consider disabling certain services such as symbolic links and CGI execution if not currently required. By default, Apache follows symlinks, we can turn off this feature as well as the\u00a0<code>-Includes<\/code>\u00a0feature and CGI in one line.<\/p>\n\n\n\n<p>To do this, add the line&nbsp;<code>'-ExecCGI -FollowSymLinks -Includes'<\/code>&nbsp;for the&nbsp;<strong>\u2018Options\u2019<\/strong>&nbsp;directive in the \u2018<strong>Directory<\/strong>\u2018 section.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Directory \/your\/website\/directory>\nOptions -ExecCGI -FollowSymLinks -Includes\n&lt;\/Directory><\/pre>\n\n\n\n<p>This can also be achieved at a directory level. For example, here, we are turning off Includes and Cgi file executions for the&nbsp;<strong>\u201c\/var\/www\/html\/mydomain1\u201d<\/strong>&nbsp;directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Directory \"\/var\/www\/html\/mydomain1\">\n\tOptions -Includes -ExecCGI\n&lt;\/Directory><\/pre>\n\n\n\n<p>Save the changes and restart Apache.<\/p>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">14. Limit File Upload Size in Apache<\/h2>\n\n\n\n<p>Another way of securing your web server is to limit the total size of the HTTP request body sent to the web server from a client. You can set it in the context of server, per-directory, per-file, or per-location.<\/p>\n\n\n\n<p>For instance, if you want to allow file upload to a specific directory, say \/<strong>var\/www\/domain.com\/wp-uploads<\/strong>&nbsp;directory, and restrict the size of the uploaded file to&nbsp;<strong>4M = 4194304Bytes<\/strong>, add the following directive to your Apache configuration file or&nbsp;<strong>.htaccess<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Directory \"\/var\/www\/domain.com\/wp-uploads\">\n\tLimitRequestBody  4194304\n&lt;\/Directory><\/pre>\n\n\n\n<p>Save the changes and remember to restart Apache.<\/p>\n\n\n\n<p>You can set it in the context of server, per-directory, per-file, or per-location. The directive wards off abnormal client request behavior which sometimes can be a form of denial-of-service (DoS) attack.<\/p>\n\n\n\n<p class=\"has-yuki-font-small-font-size\"><strong>15. Enable Logging in Apache<\/strong><\/p>\n\n\n\n<p>Logging provides all the details about client requests and any other information pertaining to the performance of your web server. This provides useful information in case something goes awry. Enabling Apache logs, especially in&nbsp;<a href=\"https:\/\/www.tecmint.com\/apache-ip-based-and-name-based-virtual-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">virtual host files<\/a>&nbsp;allows you to pinpoint an issue in case something goes wrong with the web server.<\/p>\n\n\n\n<p>To enable logging, you need to include the&nbsp;<strong>mod_log_config<\/strong>&nbsp;module, which provides two main logging directives.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ErrorLog<\/strong>&nbsp;\u2013 Specifies the path of the error log file.<\/li>\n\n\n\n<li><strong>CustomLog<\/strong>&nbsp;\u2013 Creates and formats a log file.<\/li>\n<\/ul>\n\n\n\n<p>You can use these attributes in a virtual host file in the virtual host section to enable logging.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;VirtualHost 172.16.25.125:443>\n        ServerName example.com\n        DocumentRoot \/var\/www\/html\/example\/\n        ErrorLog ${APACHE_LOG_DIR}\/error.log\n        CustomLog ${APACHE_LOG_DIR}\/access.log combined\n&lt;\/VirtualHost><\/pre>\n\n\n\n<p>The&nbsp;<code>{APACHE_LOG_DIR}<\/code>&nbsp;directive in Debian systems is defined as&nbsp;<strong>\/var\/log\/apache2<\/strong>&nbsp;path.<\/p>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">16. Run Apache as a Separate User and Group<\/h2>\n\n\n\n<p>Running&nbsp;<strong>Apache<\/strong>&nbsp;as a separate user and group is a common security practice. By doing so, you can isolate the web server process from other system processes and minimize potential damage if the web server is compromised.<\/p>\n\n\n\n<p>First, you\u2019ll want to create a new user and group specifically for&nbsp;<strong>Apache<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo groupadd apachegroup\n$ sudo useradd -g apachegroup apacheuser<\/pre>\n\n\n\n<p>Next, you\u2019ll need to edit the Apache configuration file to specify the new user and group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">User apacheuser\nGroup apachegroup<\/pre>\n\n\n\n<p>Since you\u2019re changing the user and group that Apache runs as you might need to update the ownership of web directories and files to ensure that Apache can still read them.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo chown -R apacheuser:apachegroup \/var\/www\/html<\/pre>\n\n\n\n<p>After making these changes, restart Apache to apply them:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo systemctl restart httpd      # For RHEL\/CentOS\n$ sudo systemctl restart apache2    # For Debian\/Ubuntu<\/pre>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">17. Protect DDOS Attacks and Hardening<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TimeOut<\/strong>&nbsp;\u2013 This directive allows you to specify the duration the server will wait for certain events to complete before returning an error. The default value is 300 seconds. For sites susceptible to DDoS attacks, it\u2019s advisable to keep this value low. However, the appropriate setting largely depends on the nature of requests your website receives. Note: A low timeout might cause issues with some CGI scripts.<\/li>\n\n\n\n<li><strong>MaxClients<\/strong>&nbsp;\u2013 This directive sets the limit on the number of connections that can be served simultaneously. Any new connections beyond this limit will be queued. It\u2019s available in both the&nbsp;<strong>Prefork<\/strong>&nbsp;and&nbsp;<strong>Worker MPMs<\/strong>. The default value is&nbsp;<strong>256<\/strong>.<\/li>\n\n\n\n<li><strong>KeepAliveTimeout<\/strong>&nbsp;\u2013 This directive specifies the duration the server will wait for a subsequent request before closing the connection. The default value is 5 seconds.<\/li>\n\n\n\n<li><strong>LimitRequestFields<\/strong>&nbsp;\u2013 This directive sets a limit on the number of HTTP request header fields accepted by clients. The default value is 100. If DDoS attacks are occurring due to an excessive number of HTTP request headers, it\u2019s recommended to reduce this value.<\/li>\n\n\n\n<li><strong>LimitRequestFieldSize<\/strong>\u00a0\u2013 This directive sets a size limit for the HTTP request header.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"has-yuki-font-small-font-size wp-block-heading\">18. Perform Regular Vulnerability Scans<\/h2>\n\n\n\n<p>Another way of safeguarding your web server is by carrying out regular vulnerability scanning tests. This helps identify potential security loopholes that might be exploited by hackers to gain access to sensitive files or inject malware.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. How to Hide Apache Version and OS Information By default, the Apache web server displays its version in case you browse the wrong URL of a website. Below is an example of an error page indicating that the page cannot be found on the site. The last line indicates the Apache version, the host [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":125,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[10],"tags":[],"class_list":["post-120","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache"],"_links":{"self":[{"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":17,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":153,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions\/153"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/media\/125"}],"wp:attachment":[{"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nitirat.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}