{"id":1417,"date":"2017-05-19T14:43:23","date_gmt":"2017-05-19T18:43:23","guid":{"rendered":"https:\/\/easy-admin.ca\/?p=1417"},"modified":"2017-08-21T10:24:51","modified_gmt":"2017-08-21T14:24:51","slug":"how-to-harden-the-apache-web-server-on-centos-7","status":"publish","type":"post","link":"https:\/\/easy-admin.ca\/index.php\/2017\/05\/19\/how-to-harden-the-apache-web-server-on-centos-7\/","title":{"rendered":"How to Harden the Apache Web Server on CentOS 7"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1418 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/apache-http-server.jpg\" alt=\"\" width=\"1508\" height=\"706\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/apache-http-server.jpg 1508w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/apache-http-server-300x140.jpg 300w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/apache-http-server-768x360.jpg 768w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/apache-http-server-1024x479.jpg 1024w\" sizes=\"auto, (max-width: 1508px) 100vw, 1508px\" \/><\/p>\n<p><strong>NOTE:<\/strong> <a href=\"https:\/\/www.feistyduck.com\/library\/apache%2dsecurity\/online\/\">https:\/\/www.feistyduck.com\/library\/apache%2dsecurity\/online\/<\/a><\/p>\n<h2 id=\"introduction\">Introduction<\/h2>\n<p>Apache is one of the most widely-used and popular web servers. It is also one of the most secure web servers available. In this article, I will explain some tips and tricks that will secure your Apache server.<\/p>\n<h2 id=\"requirements\">Requirements<\/h2>\n<ul>\n<li>A server running CentOS v. 7 with Apache installed<\/li>\n<li>A static IP address for your server<\/li>\n<li>Firefox browser with the Firebug add-on installed (for testing)<\/li>\n<\/ul>\n<h2 id=\"hide-the-apache-version\">Hide the Apache version<\/h2>\n<p>Visit your web server in Firefox. Activate Firebug by clicking the Firebug icon on the top right side.<\/p>\n<p>If you check the HTTP response headers in Firebug, it will show the Apache version along with your operating system name and version, as shown in this screenshot:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1419 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info.png\" alt=\"\" width=\"800\" height=\"544\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info.png 800w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info-300x204.png 300w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info-768x522.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>To hide this information from browsers, you will need to make some changes in Apache&#8217;s main configuration file.<\/p>\n<p>You can do this by editing the <code>httpd.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line at the end of file:<\/p>\n<pre><code>ServerSignature Off\r\nServerTokens Prod\r\n<\/code><\/pre>\n<p>Save the file and restart the Apache service to reflect these changes:<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<p>Now, open Firefox and access your web server. Check the HTTP response headers in Firebug, You can see that setting <code>ServerSignature<\/code> to <code>Off<\/code> has removed the version information from <strong>Server<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1420 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info_2.png\" alt=\"\" width=\"800\" height=\"577\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info_2.png 800w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info_2-300x216.png 300w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/05\/centos_apache_version_info_2-768x554.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<h2 id=\"turn-off-directory-listing\">Turn off directory listing<\/h2>\n<p>Directory listing in the absence of an <code>index<\/code> file is enabled by default in Apache. Directory listing displays all the files from the Apache web root directory. If this is enabled, then a hacker can easily view any file, analyze it, and obtain sensitive information about an application of your Apache server.<\/p>\n<p>You can turn off this setting by using the <code>Options<\/code> directive in the Apache configuration file for a specific web directory.<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Find the section that begins with <code>Directory \/var\/www\/html<\/code> and add <code>-Indexes<\/code> in the <code>Options<\/code> directive:<\/p>\n<pre><code>&lt;Directory \/var\/www\/html\/&gt;\r\n    Options -Indexes\r\n    AllowOverride None\r\n    Require all granted\r\n&lt;\/Directory&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache service to reflect these changes.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<p>Next, try to visit your website in a browser. You will get a &#8220;Forbidden&#8221; error.<\/p>\n<h2 id=\"disable-unnecessary-modules\">Disable unnecessary modules<\/h2>\n<p>By default Apache comes with lots of unnecessary installed modules. It is a good policy to disable any unnecessary modules that are not in use.<\/p>\n<p>You can list all enabled modules on your server using the following command:<\/p>\n<pre><code>sudo grep LoadModule \/etc\/httpd\/conf.modules.d\/00-base.conf\r\n<\/code><\/pre>\n<p>From the enabled modules in <code>00-base.conf<\/code> file, some modules like <code>mod_info<\/code>, <code>mod_userdir<\/code>, <code>mod_autoindex<\/code> are enabled but not needed.<\/p>\n<p>You can disable this modules by editing the <code>00-base.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf.modules.d\/00-base.conf\r\n<\/code><\/pre>\n<p>Insert a <code>#<\/code> at the beginning of the following lines to disable the modules:<\/p>\n<pre><code>#LoadModule info_module modules\/mod_info.so\r\n#LoadModule userdir_module modules\/mod_userdir.so&lt;\/code&gt;&lt;\/pre&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache service to reflect these changes.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<h2 id=\"disable-apaches-followsymlinks\">Disable Apache&#8217;s FollowSymLinks<\/h2>\n<p>By default Apache follows symbolic links (symlinks). Turning this off is recommended for security.<\/p>\n<p>To do this, you need to edit <code>httpd.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Find the section that begins with <code>Directory \/var\/www\/html<\/code>. Add <code>-FollowSymLinks<\/code> in option directive:<\/p>\n<pre><code>&lt;Directory \/var\/www\/html\/&gt;\r\n    Options -Indexes -FollowSymLinks\r\n    AllowOverride None\r\n    Require all granted\r\n&lt;\/Directory&gt;\r\n<\/code><\/pre>\n<p>Now restart Apache to reflect changes.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<h2 id=\"turn-off-server-side-includes-ssi-and-cgi-execution\">Turn off server-side includes (SSI) and CGI execution<\/h2>\n<p>Server-side includes (SSI) are directives present on Web applications that are placed in HTML pages. An SSI attack allows a web application to be exploited by remotely executing arbitrary codes. The attacker can access sensitive information like password files, and execute shell commands. It is recommended that you disable server side includes and CGI execution if they are not needed.<\/p>\n<p>To do this, edit the main Apache config file:<\/p>\n<pre><code> sudo nano \/etc\/httpd\/conf\/httpd.conf&lt;\/code&gt;&lt;\/pre&gt;\r\n<\/code><\/pre>\n<p>Find the section that begins with <code>Directory \/var\/www\/html<\/code>, Add <code>-ExecCGI<\/code> and <code>-Includes<\/code> in option directive:<\/p>\n<pre><code>&lt;Directory \/var\/www\/html\/&gt;\r\n    Options -Indexes -FollowSymLinks -ExecCGI -Includes\r\n    AllowOverride None\r\n    Require all granted\r\n&lt;\/Directory&gt;\r\n<\/code><\/pre>\n<p>Now restart Apache to reflect the changes.<\/p>\n<pre><code>sudo apachectl restart<\/code><\/pre>\n<p>You can also do this for specific web directories. For example, to turn off Includes and CGI file executions for <code>\/var\/www\/html\/www.vhost1.com<\/code> directory:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line:<\/p>\n<pre><code>&lt;Directory \/var\/www\/html\/www.vhost1.com\/&gt;\r\n    Options -Includes -ExecCGI\r\n&lt;\/Directory&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<h2 id=\"limit-request-size\">Limit request size<\/h2>\n<p>By default Apache has no limit on the size of the HTTP request. This can allow hackers to send large number of data.<\/p>\n<p>You can limit the requests size by using the Apache directive <code>LimitRequestBody<\/code> in combination with the Directory tag. This can help protect your web server from a denial of service (DOS) attack.<\/p>\n<p>Suppose you have a site (www.example.com), where you allow uploads, and you want to limit the upload size on this site.<\/p>\n<p>You can set value from <code>0<\/code> (unlimited) to <code>2147483647<\/code> (2GB) in the main Apache config file.<\/p>\n<p>For example, to limit the request size for the <code>\/var\/www\/html\/www.example.com<\/code> directory to <code>200K<\/code>:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line:<\/p>\n<pre><code>&lt;Directory \/var\/www\/html\/www.example.com&gt;\r\n    LimitRequestBody 204800\r\n&lt;\/Directory&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<h2 id=\"disallow-browsing-outside-the-document-root\">Disallow browsing outside the document root<\/h2>\n<p>Unless you have a specific need, it is recommended to restrict Apache to being only able to access the document root.<\/p>\n<p>You can secure the root directory <code>\/<\/code> with Allow and Deny options in the <code>httpd.conf<\/code> file.<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add\/edit the following line:<\/p>\n<pre><code>&lt;Directory \/&gt;\r\n    Options None\r\n    Order deny,allow\r\n    Deny from all\r\n&lt;\/Directory&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache:<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<ul>\n<li><strong>Options None<\/strong> : This will turn off all options<\/li>\n<li><strong>Order deny,allow<\/strong> : The order in which the allow and deny commands are applied<\/li>\n<li><strong>Deny from all<\/strong> : This will deny request from all to the root directory<\/li>\n<\/ul>\n<h2 id=\"keep-apache-up-to-date\">Keep Apache up to date<\/h2>\n<p>The Apache Server has a good record for security. New Apache updates will contain patches that will reduce vulnerability of your Apache server. You should always be using the most recent version of Apache server.<\/p>\n<p>You can update your Apache to the most recent version by running the following command:<\/p>\n<pre><code>sudo yum update httpd\r\n<\/code><\/pre>\n<h2 id=\"secure-apache-from-clickjacking-attacks\">Secure Apache from clickjacking attacks<\/h2>\n<p>Clickjacking, also known as &#8220;User Interface redress attack,&#8221; is a malicious technique to collect an infected user&#8217;s clicks. Clickjacking tricks the victim (visitor) into clicking on an infected site.<\/p>\n<p>To avoid this, you need to use <code>X-FRAME-OPTIONS<\/code> to prevent your website from being used by clickjackers.<\/p>\n<p>You can do this by editing the <code>httpd.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line:<\/p>\n<pre><code>Header append X-FRAME-OPTIONS \"SAMEORIGIN\"\r\n<\/code><\/pre>\n<p>Save the file and restart Apache:<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<p>Now, open Firefox and visit your website. When you check the HTTP response headers in Firebug, you should see <code>X-Frame-Options<\/code><\/p>\n<h2 id=\"disable-etag\">Disable ETag<\/h2>\n<p>ETags (entity tags) are a well-known point of vulnerability in Apache web server. ETag is an HTTP response header that allows remote users to obtain sensitive information like inode number, child process ids, and multipart MIME boundary. ETag is enabled in Apache by default.<\/p>\n<p>To prevent this vulnerability, disabling ETag is recommended.<\/p>\n<p>You can do this by editing <code>httpd.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line:<\/p>\n<pre><code>FileETag None\r\n<\/code><\/pre>\n<p>Save the file and restart Apache:<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<p>Now, open Firefox and visit your website. When you check the HTTP response headers in Firebug, you should not see <code>Etag<\/code> listed.<\/p>\n<h2 id=\"http-request-methods\">HTTP request methods<\/h2>\n<p>Apache support the OPTIONS, GET, HEAD, POST, CONNECT, PUT, DELETE, and TRACE method in HTTP 1.1 protocol. Some of these may not be required, and may pose a potential security risk. It is a good idea to only enable HEAD, POST, and GET for web applications.<\/p>\n<p>You can do this by editing the <code>httpd.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Find the section that begins with <code>Directory \/var\/www\/html<\/code>. Add the following lines under this section:<\/p>\n<pre><code>&lt;LimitExcept GET POST HEAD&gt;\r\n    deny from all\r\n&lt;\/LimitExcept&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache:<\/p>\n<p>sudo apachectl restart<\/p>\n<h2 id=\"secure-apache-from-xss-attacks\">Secure Apache from XSS attacks<\/h2>\n<p>Cross-site scripting (XSS) is one of the most common application-layer vulnerabilities in Apache server. XSS enables attackers to inject client-side script into web pages viewed by other users. Enabling XSS protection is recommended.<\/p>\n<p>You can do this by editing the httpd.conf file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line:<\/p>\n<pre><code>&lt;IfModule mod_headers.c&gt;\r\n    Header set X-XSS-Protection \"1; mode=block\"\r\n&lt;\/IfModule&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache to reflect changes.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n<p>Now, open Firefox and visit your website. When you check HTTP response headers in Firebug, you should see that XSS Protection is enabled and <strong>mode<\/strong> is blocked.<\/p>\n<h2 id=\"protect-cookies-with-httponly-flag\">Protect cookies with HTTPOnly flag<\/h2>\n<p>You can protect your Apache server from most of the common Cross Site Scripting attacks using the <code>HttpOnly<\/code> and <code>Secure<\/code> flags for cookies.<\/p>\n<p>You can do this by editing the <code>httpd.conf<\/code> file:<\/p>\n<pre><code>sudo nano \/etc\/httpd\/conf\/httpd.conf\r\n<\/code><\/pre>\n<p>Add the following line:<\/p>\n<pre><code>&lt;IfModule mod_headers.c&gt;\r\n    Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure\r\n&lt;\/IfModule&gt;\r\n<\/code><\/pre>\n<p>Save the file and restart Apache to reflect changes.<\/p>\n<pre><code>sudo apachectl restart\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>NOTE: https:\/\/www.feistyduck.com\/library\/apache%2dsecurity\/online\/ Introduction Apache is one of the most widely-used and popular web servers. It is also one of the most secure web servers available. In this article, I will explain some tips and tricks that will secure your Apache server. Requirements A server running CentOS v. 7 with Apache installed A static IP address &hellip; <a href=\"https:\/\/easy-admin.ca\/index.php\/2017\/05\/19\/how-to-harden-the-apache-web-server-on-centos-7\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to Harden the Apache Web Server on CentOS 7<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"How to Harden the Apache Web Server on CentOS 7 - HP Server","description":"NOTE: https:\/\/www.feistyduck.com\/library\/apache%2dsecurity\/online\/ Introduction Apache is one of the most widely-used and popular web servers. It is also one of"},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1417","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/1417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/comments?post=1417"}],"version-history":[{"count":0,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/1417\/revisions"}],"wp:attachment":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media?parent=1417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/categories?post=1417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/tags?post=1417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}