<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>No pain no gain &#187; Apache</title>
	<atom:link href="http://www.dikant.de/category/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dikant.de</link>
	<description>Personal blog of Peter Dikant</description>
	<lastBuildDate>Sun, 23 May 2010 20:57:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Nginx as a reverse proxy for Apache</title>
		<link>http://www.dikant.de/2008/07/10/nginx-as-a-reverse-proxy-for-apache/</link>
		<comments>http://www.dikant.de/2008/07/10/nginx-as-a-reverse-proxy-for-apache/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 20:56:27 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.dikant.de/?p=48</guid>
		<description><![CDATA[While Apache is a great server for delivering dynamic content and especially hosting PHP-based websites, it has a high memory footprint and a high overhead when forking new worker processes during high server load. In this article I will describe &#8230; <a href="http://www.dikant.de/2008/07/10/nginx-as-a-reverse-proxy-for-apache/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While Apache is a great server for delivering dynamic content and especially hosting PHP-based websites, it has a high memory footprint and a high overhead when forking new worker processes during high server load. In this article I will describe how you can use the <a href="http://nginx.net">nginx</a> web server as a <a href="http://en.wikipedia.org/wiki/Reverse_proxy">reverse proxy</a> for your Apache to deliver static files instead of Apache. Nginx has a very small memory footprint and can deliver static files lightning fast.</p>
<p>The idea behind this setup is that nginx will listen on port 80 for incoming connections, identify whether the client requests a static file or a dynamic webpage. In case of a static file it will deliver the file itself. In case of a dynamic request it will forward that request to the Apache server. </p>
<p><span id="more-48"></span></p>
<p>So let&#8217;s get started. First we need to download and unzip the lates stabel version of nginx. Currently this is verions 0.6.32. Compilation and installation is a done with the usual steps:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>This will install nginx in the directory <code>/usr/local/nginx</code>. I usually like to have all my configuration files under <code>/etc</code>, so let&#8217;s copy the configuration folder over:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx</pre></div></div>

<p>The sample configuration file <code>nginx.conf</code> is well suited as a starting point. I would recommend to uncomment / change the following settings in the main section:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">user</span> www-data www-data;
worker_processes <span style="color: #ff0000;">2</span>;
pid /var/run/nginx.pid;</pre></div></div>

<p>In the http-section you could alter the following settings:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">tcp_noauth <span style="color: #0000ff;">on</span>;
gzip <span style="color: #0000ff;">on</span>;</pre></div></div>

<p>The English nginx wiki contains a very good <a href="http://wiki.codemongers.com/NginxModules">documentation</a> on these settings.</p>
<p>Nginx has full support for name based virtual hosts and you need to create a server-section in the config for every virtual host that is configured in Apache. But first create a new configuration file <code>/etc/nginx/proxy.conf</code> which contains the basic proxy settings as found in the nginx wiki:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">proxy_redirect          <span style="color: #0000ff;">off</span>;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   <span style="color: #ff0000;">90</span>;
proxy_send_timeout      <span style="color: #ff0000;">90</span>;
proxy_read_timeout      <span style="color: #ff0000;">90</span>;
proxy_buffers           <span style="color: #ff0000;">32</span> 4k;</pre></div></div>

<p>These settings will be reused in every virtual host. The default virtual host is used as fallback in case no specific configuration for that virtual host can be found. To configure this default host, replace the server-section in the file <code>/etc/nginx.conf</code> with the following block:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">server {
    <span style="color: #00007f;">listen</span>       XXX.XXX.XXX.XXX:<span style="color: #ff0000;">80</span> default;
    server_name  _;
    access_log /var/log/nginx/default.access.log main;
&nbsp;
    <span style="color: #00007f;">location</span> / {
        proxy_pass http://127.0.0.1:<span style="color: #ff0000;">80</span>;
        <span style="color: #00007f;">include</span> /etc/nginx/proxy.conf;
   }
}</pre></div></div>

<p>Replace <code>XXX.XXX.XXX.XXX</code> with the extern IP address of the server. The above configuration is a pure proxy configuration which will pass all the traffic to the Apache server that is listening on <code>127.0.0.1:80</code>.</p>
<p>To be flexible in the virtual host configuration, I like to maintain one configuration file per virtual host in a separate directory. We can include all configuration files from a certain directory into the nginx configuration by adding the following line after the server-section:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">include</span> /etc/nginx/sites-enabled/*;</pre></div></div>

<p>So every time you want to setup a new virtual host, you just need to add a new configuration file to the directory <code>/etc/nginx/sites-enabled</code>. Here is a template for that file:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">server {
    <span style="color: #00007f;">listen</span> XXX.XXX.XXX.XXX:<span style="color: #ff0000;">80</span>;
    server_name foobar.com www.foobar.com;
&nbsp;
    <span style="color: #00007f;">location</span> / {
        proxy_pass http://127.0.0.1:<span style="color: #ff0000;">80</span>;
        <span style="color: #00007f;">include</span> /etc/nginx/proxy.conf;
    }
&nbsp;
    <span style="color: #00007f;">location</span> ~* ^.+.(jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|avi|mp3)$ {
        expires 30d;
        root /var/www/foobar.com/htdocs;
    }
}</pre></div></div>

<p>This configuration will setup the virtual host <code>foobar.com</code>. You can define all alias addresses with the configuration directive <code>server_name</code>. All requests that match one of the above file extensions will be delivered directly by nginx from the directory <code>/var/www/foobar.com/htdocs</code>. All other requests are forwarded to the Apache server.</p>
<p>Before nginx can be started we need to make sure that Apache only listens on the address <code>127.0.0.1</code> for requests and not on the external IP address. In Debian this is done in <code>/etc/apache2/ports.conf</code>. Change this file to:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">Listen</span> 127.0.0.1:<span style="color: #ff0000;">80</span></pre></div></div>

<p>Also make sure, that the VirtualHost directives in the Apache configuration files do not include an IP address. They should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;
    <span style="color: #00007f;">ServerName</span> foobar.com
...
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</pre></div></div>

<p>The directive NameVirtualHost should look like this (also without an IP address):</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">NameVirtualHost</span> *:<span style="color: #ff0000;">80</span></pre></div></div>

<p>Now you can restart Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>And start nginx:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>nginx.conf</pre></div></div>

<p>You can reload the nginx configuration without stopping nginx:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-HUP</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx.pid<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>On my server some quick benchmarks have shown that nginx can deliver static content up to 10 times faster than Apache. The amazing thing is that not only did it deliver the content faster, there was nearly no impact on CPU or memory. With combining Apache and nginx we can have the best of both worlds, nginx for static files and Apache for dynamic content.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2008/07/10/nginx-as-a-reverse-proxy-for-apache/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Speeding up PHP in 5 minutes</title>
		<link>http://www.dikant.de/2008/06/09/speeding-up-php-in-5-minutes/</link>
		<comments>http://www.dikant.de/2008/06/09/speeding-up-php-in-5-minutes/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 08:41:12 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.dikant.de/?p=45</guid>
		<description><![CDATA[By default PHP scripts are compiled on each access. This will become a real performance killer once your website hits a certain load. There is a number of Opcode caches available which try to overcome this recompiling issue by storing &#8230; <a href="http://www.dikant.de/2008/06/09/speeding-up-php-in-5-minutes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>By default PHP scripts are compiled on each access. This will become a real performance killer once your website hits a certain load. There is a number of Opcode caches available which try to overcome this recompiling issue by storing precompiled versions of your scripts in a cache. This blog entry will explain how to integrate the Alternative PHP Cache (<a href="http://pecl.php.net/package/apc">APC</a>) into your PHP5 installation. This tutorial is based on a Debian installation, but it should also work with alternative distributions.</p>
<p>APC ist installed using the PHP Extension Community Library (<a href="http://pecl.php.net/">PECL</a>). Using PECL ist similar to using the PEAR Library. Before you can install APC via PECL, make sure that you have the following packages installed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> php5-dev php5-gd</pre></div></div>

<p>Downloading compiling and installation of APC using PECL ist a breeze. Just run from the command line:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pecl <span style="color: #c20cb9; font-weight: bold;">install</span> APC</pre></div></div>

<p>Now all you need to do is to add the following line to your <code>php.ini</code> file which you should find in <code>/etc/php5/apache2/</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">extension=apc.so</pre></div></div>

<p>Once you restart Apache, caching will be enabled with default settings of APC. By default APC will use 30 MB memory to cache your PHP files. It is a good idea to tailor this setting to your server. This can be done with the following line in <code>php.ini</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">apc.shm_size=<span style="color: #ff0000;">30</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2008/06/09/speeding-up-php-in-5-minutes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Howto enable WebDAV in Apache</title>
		<link>http://www.dikant.de/2007/10/28/howto-enable-webdav-in-apache/</link>
		<comments>http://www.dikant.de/2007/10/28/howto-enable-webdav-in-apache/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 21:12:44 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[webdav]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/10/28/howto-enable-webdav-in-apache/</guid>
		<description><![CDATA[Time for another enhancement for the Apache web server. If you followed my other 2 Apache postings, you will have a secure web server configuration which has SSL enabled and is monitored by the mod_security application level firewall. Now it &#8230; <a href="http://www.dikant.de/2007/10/28/howto-enable-webdav-in-apache/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Time for another enhancement for the Apache web server. If you followed my other 2 Apache postings, you will have a secure web server configuration which has SSL enabled and is monitored by the <a href="http://www.modsecurity.org/">mod_security</a> application level firewall. Now it is time to setup the <a href="http://en.wikipedia.org/wiki/WebDAV">WebDAV</a> module so that you can use your web server as an external file storage.</p>
<p><span id="more-41"></span></p>
<p>The first step is to activate the dav_fs module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2enmod dav_fs</pre></div></div>

<p>The rest of the configuration needs to be done inside one of your virtual host configuration files. Please note that it is recommended to enable the WebDAV service in an SSL secured virtual host, because Windows seems to have issues connecting to WebDAV services which are not secured via SSL. </p>
<p>In the first <a href="http://www.dikant.de/2007/08/13/apache-php-and-mysql-setup/">Apache tutorial</a> we have created the virtual host foobar.org. To add the WebDAV service to that virtual host, open the configuration file <code>/etc/apache2/sites-available/foobar.org</code> and  add a new <code>Directory</code>-directive into the SSL-enabled VirtualHost:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:<span style="color: #ff0000;">443</span>&gt;
        SSLEngine <span style="color: #0000ff;">On</span>
        ....
        &lt;<span style="color: #000000; font-weight:bold;">Directory</span> /srv/www/vhosts/foobar.org/httpsdocs/webdav&gt;
                DAV <span style="color: #0000ff;">On</span>
                <span style="color: #00007f;">AllowOverride</span> AuthConfig
                <span style="color: #00007f;">AuthType</span> Basic
                <span style="color: #00007f;">AuthName</span> <span style="color: #7f007f;">&quot;WebDAV Login&quot;</span>
                <span style="color: #00007f;">AuthUserFile</span> /srv/www/vhosts/foobar.org/webdav-users
                <span style="color: #00007f;">Require</span> valid-<span style="color: #00007f;">user</span>
        &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</pre></div></div>

<p>The above configuration defines a new subdirectory in the foobar.org SSL webspace which has the WebDAV module activated and is secured via a basic login mechanism. So if you try to access the address https://www.foobar.org/webdav you will be prompted for a login and password. The logins are stored in the file <code>/srv/www/vhosts/foobar.org/webdav-users</code>. We need to create this file and define a valid user:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">htpasswd <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>webdav-users username</pre></div></div>

<p>Where <code>username</code> ist the login you would like to use. You will be prompted for a password and the user will be created in the specified file.</p>
<p>Now create the webdav directory and make it writeable for the Apache server:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>httpsdocs<span style="color: #000000; font-weight: bold;">/</span>webdav
<span style="color: #c20cb9; font-weight: bold;">chown</span> www-data.www.data <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>httpsdocs<span style="color: #000000; font-weight: bold;">/</span>webdav
<span style="color: #c20cb9; font-weight: bold;">chmod</span> g+<span style="color: #c20cb9; font-weight: bold;">w</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>httpsdocs<span style="color: #000000; font-weight: bold;">/</span>webdav</pre></div></div>

<p>That&#8217;s it. After restarting your Apache server, you can now mount the above directory via WebDAV with the address <code>https://www.foobar.org/webdav</code> and use it as an external file store.</p>
<p>If you want to restrict only write access to the WebDAV directory and allow read-only access to anybody, replace <code>Require valid-user</code> with:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;LimitExcept GET&gt;
        <span style="color: #00007f;">Require</span> valid-<span style="color: #00007f;">user</span>
&lt;/LimitExcept&gt;</pre></div></div>

<p>This change will only require a login when uploading, modifying or deleting files in the WebDAV directory. You could also enable directory browsing with the following configuration setting:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">Options</span> <span style="color: #0000ff;">Indexes</span></pre></div></div>

<p>Should you use mod_security you also need to disable some rules which would block WebDAV traffic. This is best done inside of the above <code>Directory</code>-directive:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mod_security2.c&gt;
        SecRuleRemoveById <span style="color: #ff0000;">960032</span> <span style="color: #ff0000;">960038</span> <span style="color: #ff0000;">960904</span>
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;</pre></div></div>

<p>Please note that the rule ids may change depending on the mod_security version you use. So if WebDAV does not seem to work, take a look at the mod_security audition log to see which rules are blocking your traffic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/10/28/howto-enable-webdav-in-apache/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Securing Apache with mod_security</title>
		<link>http://www.dikant.de/2007/08/17/securing-apache-with-mod_security/</link>
		<comments>http://www.dikant.de/2007/08/17/securing-apache-with-mod_security/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 21:50:43 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/08/17/securing-apache-with-mod_security/</guid>
		<description><![CDATA[Now that the basic Apache configuration is working, let&#8217;s take some time to harden the installation. The module mod_security is really handy for this task. It is an application level firewall meaning that it will inspect incoming requests to the &#8230; <a href="http://www.dikant.de/2007/08/17/securing-apache-with-mod_security/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Now that the basic Apache configuration is working, let&#8217;s take some time to harden the installation. The module <a href="http://www.modsecurity.org/" target="_blank">mod_security</a> is really handy for this task. It is an application level firewall meaning that it will inspect incoming requests to the webserver and try to identify possible attacks like code injection, SQL injection and cross-site-scripting. As handy as this tool might be, it is not meant as a substitute for a secure PHP or Apache configuration, so you still need to take care not to open potential security holes there. If you followed my basic Apache and PHP setup you should be on the safe side, as the most exploited security holes have already been closed.</p>
<p><span id="more-25"></span></p>
<p>To be always up to date with mod_security I prefer to compile it myself. To do this we need to install some missing development files:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libxml2-dev apache2-prefork-dev</pre></div></div>

<p>Now we can go ahead and grab the latest sources from <a href="http://www.modsecurity.org" target="_blank">http://www.modsecurity.org</a>  (in this case version 2.1.2) and unpack them in your home directory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> modsecurity-apache_2.1.2.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> modsecurity-apache_2.1.2<span style="color: #000000; font-weight: bold;">/</span>apache2</pre></div></div>

<p>We need to define the location of our Apache home directory in the Makefile. Find the line <code>top_dir =</code> and set it to <code>top_dir = /usr/share/apache2</code>. Now you should be able to compile and install the module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Before we activate mod_security, we need to setup filtering rules. The current release of mod_security comes bundled with a sophisticated set of rules, which is called the core rule set. We can use these configuration files as starting point for our customized set. We will store the csutomized mod_security rules in <code>/etc/apache2/mod_security</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mod_security
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>modsecurity-apache_2.1.2<span style="color: #000000; font-weight: bold;">/</span>rules<span style="color: #000000; font-weight: bold;">/*</span>.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mod_security<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span> ~<span style="color: #000000; font-weight: bold;">/</span>modsecurity-apache_2.1.2<span style="color: #000000; font-weight: bold;">/</span>rules<span style="color: #000000; font-weight: bold;">/</span>blocking<span style="color: #000000; font-weight: bold;">/*</span>.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>mod_security<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>As you can see, first we copy the standard rule set to our new directory and next we overwrite some of the configuration files with config files which include versions of the rules which will block the traffic matching the rule.</p>
<p>Most of the configuration will be done in the file <code>/etc/apache2/mod_security/modsecurity_crs_10_config.conf</code>. So open this file and make the following modifications:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">SecRuleEngine DetectionOnly
SecResponseBodyAccess <span style="color: #0000ff;">Off</span>
SecDefaultAction <span style="color: #7f007f;">&quot;phase:2,log,deny,status:500&quot;</span>
<span style="color: #adadad; font-style: italic;">#SecServerSignature &quot;Apache/2.2.0 (Fedora)&quot;</span>
SecAuditLog /var/log/apache2/modsec_audit.log
SecAuditLogRelevantStatus <span style="color: #7f007f;">&quot;^(?:5|4<span style="color: #000099; font-weight: bold;">\d</span>[^4])&quot;</span>
SecDebugLog /var/log/apache2/modsec_debug.log</pre></div></div>

<p>The above configuration sets the path to the logfiles and most important it will start the module in detection mode. This means that the configured rules will not block an access, but log only. Without this logging step you will likely break some of your applications.</p>
<p>Now we need to activate the module and load all security rules. To do this we will create a file <code>/etc/apache2/mods-available/security2.load</code> which will load the mod_security module. It has the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">LoadModule</span> security2_module /usr/lib/apache2/modules/mod_security2.so</pre></div></div>

<p>We also need a configuration file which will load all of our security rules. The files is named <code>/etc/apache2/mods-available/security2.conf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mod_security2.c&gt;
    <span style="color: #00007f;">Include</span> /etc/apache2/mod_security/*.conf
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;</pre></div></div>

<p>Now we can enable mod_security:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2enmod security2
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>After restarting the server, browse all your applications and try to simulate normal use. You could also leave the server for a couple of hours. In the meantime mod_security will log all requests that it would block into the file <code>/var/log/apache2/modsec_audit.log</code>. So, take a look at the logfile and identify all rules which are false positives. Meaning that these rules should not trigger for your applications to work. Example of a message:</p>

<div class="wp_syntax"><div class="code"><pre class="xxx" style="font-family:monospace;">--bf9af342-H--
Message: Warning. Pattern match &quot;(?:\\b(?:\\.(?:ht(?:access|passwd|group)|www_?acl)|global\\.asa|httpd\\.conf|boot\\.ini)\\b|\\/etc\\/)&quot; at ARGS:content. [id &quot;950005&quot;] [msg &quot;Remote File Access Attempt. Matched signature &lt;/etc/&gt;&quot;] [severity &quot;CRITICAL&quot;]
Stopwatch: 1187386802219993 629097 (117984* 168889 -)
Producer: ModSecurity v2.1.2 (Apache 2.x)
Server: Apache
&nbsp;
--bf9af342-Z--</pre></div></div>

<p>Each rule has a unique id. In the above example the id is 950005. If we want to remove the rule with this id, we need to create a file <code>/etc/apache2/mod_security/modsecurity_crs_99_customrules.conf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">SecRuleRemoveById <span style="color: #ff0000;">950005</span></pre></div></div>

<p>You can also specify multiple rule ids or put the above configuration setting in any other Apache configuration file, for example inside a Location-directive so that the whitelisting will only apply to a specific URL-Location. You should disable the rules in a small context to keep the general ruleset strong.</p>
<p>When you are happy with the configuration you need to change in <code>/etc/apache2/modsecurity_crs_10_config.conf</code> the line:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">SecRuleEngine <span style="color: #0000ff;">On</span></pre></div></div>

<p>which will set mod_security into active mode and now really block malicious requests.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/08/17/securing-apache-with-mod_security/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache, PHP and MySQL setup</title>
		<link>http://www.dikant.de/2007/08/13/apache-php-and-mysql-setup/</link>
		<comments>http://www.dikant.de/2007/08/13/apache-php-and-mysql-setup/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 13:54:02 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/08/13/apache-php-and-mysql-setup/</guid>
		<description><![CDATA[After setting up the mail system, I continued today with the basic webserver setup. This setup includes a basic Apache2 configuration with SSL and name based virtual hosts. PHP5 and MySQL5 are also needed for serving dynamic content. Again we &#8230; <a href="http://www.dikant.de/2007/08/13/apache-php-and-mysql-setup/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After setting up the mail system, I continued today with the basic webserver setup. This setup includes a basic Apache2 configuration with SSL and name based virtual hosts. PHP5 and MySQL5 are also needed for serving dynamic content.</p>
<p><span id="more-24"></span> Again we will start with installing all needed software components:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> apache2 libapache2-mod-php5 php5-cli php5-common mysql-common mysql-server mysql-client</pre></div></div>

<p>Please choose a secure password for the database installation. The Debian configuration for the MySQL server is fine and does not pose any security issues, as it will only accept connections from the localhost, so only local applications can access the database.</p>
<p>The PHP configuration file can use some tweaks to increase the security. Make sure, that you have the following settings in <code>/etc/php5/apache2/php.ini</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">disable_functions</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> show_source, system, shell_exec, passthru, exec, popen, proc_open, symlink</span>
<span style="color: #000099;">expose_php</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span>
<span style="color: #000099;">register_globals</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span>
<span style="color: #000099;">allow_url_fopen</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span>
<span style="color: #000099;">allow_url_include</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> Off</span></pre></div></div>

<p>The above settings will close some script injection vulnerabilities and disable some insecure function calls. The rest of the configuration file can be set to your own preferences.</p>
<p>For the Apache configuration we first need a custom certificate to enable SSL encryption:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl req <span style="color: #660033;">-new</span> <span style="color: #660033;">-x509</span> <span style="color: #660033;">-days</span> <span style="color: #000000;">4312</span> <span style="color: #660033;">-nodes</span> <span style="color: #660033;">-keyout</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>web.pem <span style="color: #660033;">-out</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>web.pem</pre></div></div>

<p>When filling out the certificate details, make sure that you enter your domain name in the field &#8220;Common Name&#8221;. The above command will create a self signed certificate file.</p>
<p>Now you need to configure the Apache server to listen on both port 80 and port 443. This is done in the configuration file <code>/etc/apache2/ports.conf</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">Listen</span> <span style="color: #ff0000;">80</span>
<span style="color: #00007f;">Listen</span> <span style="color: #ff0000;">443</span></pre></div></div>

<p>Enable the SSL Apache module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2enmod ssl</pre></div></div>

<p>Now we need to enable the name based virtual host support. Open <code>/etc/apache2/sites-available/default</code> and make sure that the file starts with the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">NameVirtualHost</span> *:<span style="color: #ff0000;">80</span>
<span style="color: #00007f;">NameVirtualHost</span> *:<span style="color: #ff0000;">443</span>
&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:<span style="color: #ff0000;">80</span>&gt;</pre></div></div>

<p>Now we are all set to creating our virtual hosts. We only need to decide where these hosts will be stored. In this example I choose the path <code>/srv/www/vhosts</code> as the base path for the virtual hosts and each host will get its own directory named after the domain name (ie. <code>mydomain.com</code>) with the subdirectories <code>httpdocs</code> for non-ssl webpages, <code>httpsdocs</code> for ssl webpages and <code>logs</code> for the access and error log.</p>
<p>Let&#8217;s create a new virtual host foobar.org. First we create the directories:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>httpdocs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>httpsdocs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>srv<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>vhosts<span style="color: #000000; font-weight: bold;">/</span>foobar.org<span style="color: #000000; font-weight: bold;">/</span>logs</pre></div></div>

<p>Now we will define the virtual host inside apache. Create a new configuration file <code>/etc/apache2/sites-available/foobar.org</code> with the following contents:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:<span style="color: #ff0000;">80</span>&gt;
        SSLEngine <span style="color: #0000ff;">Off</span>
        <span style="color: #00007f;">ServerName</span> foobar.org:<span style="color: #ff0000;">80</span>
        <span style="color: #00007f;">ServerAlias</span> www.foobar.org
        <span style="color: #00007f;">UseCanonicalName</span> <span style="color: #0000ff;">Off</span>
        <span style="color: #00007f;">ServerAdmin</span> your@email.com
        <span style="color: #00007f;">DocumentRoot</span> /srv/www/vhosts/foobar.org/httpdocs
        <span style="color: #00007f;">CustomLog</span> /srv/www/vhosts/foobar.org/logs/access_log combined
        <span style="color: #00007f;">ErrorLog</span> /srv/www/vhosts/foobar.org/logs/error_log
        &lt;<span style="color: #000000; font-weight:bold;">directory</span> /srv/www/vhosts/foobar.org/httpdocs&gt;
                <span style="color: #00007f;">Order</span> <span style="color: #00007f;">Deny</span>,<span style="color: #00007f;">Allow</span>
                <span style="color: #00007f;">Allow</span> <span style="color: #00007f;">from</span> <span style="color: #00007f;">all</span>
                <span style="color: #00007f;">Options</span> -<span style="color: #0000ff;">Indexes</span>
        &lt;/<span style="color: #000000; font-weight:bold;">directory</span>&gt;
&lt;/<span style="color: #000000; font-weight:bold;">virtualhost</span>&gt;
&nbsp;
&lt;<span style="color: #000000; font-weight:bold;">virtualhost</span> *:<span style="color: #ff0000;">443</span>&gt;
        SSLEngine <span style="color: #0000ff;">On</span>
        SSLCertificateFile /etc/apache2/web.pem
        <span style="color: #00007f;">ServerName</span> foobar.org:<span style="color: #ff0000;">443</span>
        <span style="color: #00007f;">ServerAlias</span> www.foobar.org
        <span style="color: #00007f;">UseCanonicalName</span> <span style="color: #0000ff;">Off</span>
        <span style="color: #00007f;">ServerAdmin</span> your@email.com
        <span style="color: #00007f;">DocumentRoot</span> /srv/www/vhosts/foobar.org/httpsdocs
        <span style="color: #00007f;">CustomLog</span> /srv/www/vhosts/foobar.org/logs/access_log combined
        <span style="color: #00007f;">ErrorLog</span> /srv/www/vhosts/foobar.org/logs/error_log
        &lt;<span style="color: #000000; font-weight:bold;">directory</span> /srv/www/vhosts/foobar.org/httpsdocs&gt;
                <span style="color: #00007f;">Order</span> <span style="color: #00007f;">Deny</span>,<span style="color: #00007f;">Allow</span>
                <span style="color: #00007f;">Allow</span> <span style="color: #00007f;">from</span> <span style="color: #00007f;">all</span>
                <span style="color: #00007f;">Options</span> -<span style="color: #0000ff;">Indexes</span>
        &lt;/<span style="color: #000000; font-weight:bold;">directory</span>&gt;
&lt;/<span style="color: #000000; font-weight:bold;">virtualhost</span>&gt;</pre></div></div>

<p>The above configuration defines an non-ssl site which will point to the httpdocs folder and an ssl site with points to the httpsdocs folder. Of course you could configure the same DocumentRoot for both sites so that ssl and non-ssl content would be the same.</p>
<p>The last step to enable the new site is to declare it as an active site and reload the apache configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2ensite foobar.org
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 reload</pre></div></div>

<p>Now you should be able to access your new site with ssl and non-ssl. And you will also be able to run PHP scripts inside your web space.</p>
<p>The logfiles for different virtual hosts are now stored in separate directories. We therefore need to adapt the logrotate configuration to include the new logfiles in the log rotation. So open the configuration file <code>/etc/logrotate.d/apache2</code> and change the first line to:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">/var/log/apache2/*.log /srv/www/vhosts/*/logs/*_log <span style="">&#123;</span></pre></div></div>

<p>In the next article we will take a look at securing the Apache server with <a href="http://www.modsecurity.org/" target="_blank">mod_security</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/08/13/apache-php-and-mysql-setup/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
