<?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>arkinEx Codex &#187; apache2</title>
	<atom:link href="http://www.arkinex.com/contains/apache2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arkinex.com</link>
	<description>the place where all my code can be viewed</description>
	<lastBuildDate>Wed, 04 Nov 2009 20:39:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Managing Apache2 on Debian Etch</title>
		<link>http://www.arkinex.com/guides/33/managing-apache2-on-debian-etch/</link>
		<comments>http://www.arkinex.com/guides/33/managing-apache2-on-debian-etch/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 15:32:21 +0000</pubDate>
		<dc:creator>arkin</dc:creator>
				<category><![CDATA[Guides]]></category>
		<category><![CDATA[apache2]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[virtual host]]></category>

		<guid isPermaLink="false">http://www.arkinex.com/?p=33</guid>
		<description><![CDATA[Thought i&#8217;d write a quick guide on managing apache2 once installed and running under debian. Covers log file management, adding virtual hosts (with subdomains) and basic security. During this tutorial, it is important that you have root SSH access to your server. Although this guide is written for Debian, it can be used across many [...]]]></description>
			<content:encoded><![CDATA[<p>Thought i&#8217;d write a quick guide on managing apache2 once installed and running under debian.</p>
<p>Covers log file management, adding virtual hosts (with subdomains) and basic security.<br />
<span id="more-33"></span><br />
During this tutorial, it is important that you have root SSH access to your server.</p>
<p>Although this guide is written for Debian, it can be used across many Linux distributions, you will just need to find the locations of the files mentioned and may need to create some additional files.</p>
<p><strong>Part 1 &#8211; Log File Management</strong></p>
<p>First of all, we will navigate to our log file directory. Log files in debian are normally kept in <em>/var/log</em> and apache log files have their own directory, so <em>/var/log/apache2</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:~# cd /var/log/apache2
mars:/var/log/apache2# ls
access.log  access.log.1  error.log  error.log.1</pre></div></div>

<p>You may also have seperate log files for specific sites on your server.</p>
<p>Error log is normally the most useful and contains all the errors caught by Apache.<br />
It helps us determine hackers and exploiters and general errors from within our site.</p>
<p>We will use the <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?cat">cat</a> and <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?more">more</a> commands to view &amp; limit the output of the log.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/var/log/apache2# cat error.log | more
[Sun Aug 03 14:39:11 2008] [error] [client 125.65.112.135] File does not exist: /var/www/site/cms/favicon.ico
[Sun Aug 03 14:39:14 2008] [error] [client 125.65.112.135] File does not exist: /var/www/site/cms/favicon.ico
[Sun Aug 03 14:44:11 2008] [error] [client 125.65.112.135] File does not exist: /var/www/site/robots.txt, referer: http://your.site.com/
[Sun Aug 03 14:49:11 2008] [error] [client 125.65.112.135] File does not exist: /var/www/site/robots.txt, referer: http://your.site.com/
--More--</pre></div></div>

<p>and/or</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/var/log/apache2# cat access.log | grep 404 | more
mars:/var/log/apache2# cat access.log | grep 404
129.241.50.34 - - [01/Aug/2008:20:11:51 +0100] &quot;GET /support.html HTTP/1.1&quot; 404 359 &quot;-&quot; &quot;boitho.com-dc/0.86 ( http://www.boitho.com/dcbot.html )&quot;
82.36.220.196 - - [01/Aug/2008:20:29:03 +0100] &quot;GET /favicon.ico HTTP/1.1&quot; 404 358 &quot;-&quot; &quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1&quot;
82.36.220.196 - - [01/Aug/2008:20:29:06 +0100] &quot;GET /favicon.ico HTTP/1.1&quot; 404 358 &quot;-&quot; &quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1&quot;
--More--</pre></div></div>

<p>This shows us that robots.txt is missing and being requests (by <a href="http://www.robotstxt.org">search engines</a>) and your favicon.ico is also not present (used for bookmark icons &amp; webpage icon in firefox).</p>
<p>If you want to continue through the error log press your space bar, if you want to cancel and do something else, press control+c.</p>
<p>Now we will investigate the above IP address using <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?cat">cat</a>, <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?grep">grep</a> and <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?more">more</a> to pull specific entires from the file and limit them (incase theres alot).</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/var/log/apache2# cat access.log | grep 125.65.112.135 | more
125.65.112.135 - - [02/Aug/2008:07:21:27 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=FF6E471372BF51AF4E81E79401BB2C0AFB6111933982 HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;
125.65.112.135 - - [02/Aug/2008:07:21:27 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=E8DA0FF096B5A4DF4E81E794005023B09FEDA25BA2F8 HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;
125.65.112.135 - - [02/Aug/2008:07:21:27 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=12FC7BCD3E2683FB4E81E79601BB61BEC3540BEC7B0D HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;
125.65.112.135 - - [02/Aug/2008:07:21:27 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=AC0D7A2AC9C426304E81E797005052BD8A83720EF4F0 HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;
&nbsp;
125.65.112.135 - - [02/Aug/2008:07:21:27 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=ED17C0488BDCD4C34E81E7990050BFA51AADCB0A9512 HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;
125.65.112.135 - - [02/Aug/2008:07:21:27 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=B58AD75AA8EF6EDA4E81E79A01BB2B30977E22A9B821 HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;
125.65.112.135 - - [02/Aug/2008:07:21:30 +0100] &quot;GET http://www.wantsfly.com/prx1.php?hash=FF9A1DBB63B403CE4E81E79801BBEF64B8651E15FF6B HTTP/1.0&quot; 404 356 &quot;-&quot; &quot;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)&quot;</pre></div></div>

<p>So now we can see, this user has been naughty trying to referal spam us using a website that doesn&#8217;t exist on our server. This is quite common, and the suspected user is obviously forging his user agent (as internet explorer).</p>
<p>If there is one particular IP address or Browser you want to &#8220;count&#8221; you can use the word count <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?wc">wc</a> command.</p>
<p>For this example, I am going to count all pageviews from the IP range 127.0.0&#8230;.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/var/log/apache2# cat access.log | grep 127.0.0 | wc -l
78</pre></div></div>

<p>This shows that 127.0.0.* has accessed 78 files on our server in the log period. The ip address could be replaced with MSIE to count Internet Explorer accesses or Firefox etc.</p>
<p>If you require more examples or additional information, please leave questions in the comments section.</p>
<p><strong>Part 2 &#8211; Adding Apache Virtual Hosts</strong></p>
<p>So, if you are dealing with a default apache2 setup (like I am), your virtual hosts can be found in <em>/etc/apache2/sites-enabled/</em> directory and all these files are loaded as apache reloads. <strong>If you do not have this directory</strong>, add the entries to the end of your main Apache configuration file, and make sure <em>NameVirtualHost *</em> is above them all.</p>
<p>I try and create my virtual hosts in numeric sequence order.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/etc/apache2/sites-enabled# ls
000-default</pre></div></div>

<p>So were gonna create <em><strong>001</strong>-example</em> for example.com. I add all my domains in <em>/var/www</em> and give them all a single letter prefix also, so <em>/var/www/e/example</em> for our example site.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/etc/apache2/sites-enabled# mkdir -p /var/www/e/example
mars:/etc/apache2/sites-enabled# nano 001-example</pre></div></div>

<p>This creates a new file using nano. You can then start adding the configuration.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">&amp;lt;VirtualHost *%gt;
        ServerAdmin webmaster@localhost
        ServerName www.example.com
        ServerAlias example.com example.net  www.example.net
        DocumentRoot /var/www/e/example
&nbsp;
#        ErrorLog /var/log/apache2/error.log
&nbsp;
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
#        LogLevel warn
&nbsp;
#        CustomLog /var/log/apache2/access.log combined
#        ServerSignature On
&amp;lt;/VirtualHost&amp;gt;</pre></div></div>

<p>This adds <em>www.example.com</em> as the main domain, and also adds <em>example.com example.net www.example.net</em> as addon domains. You could also add <em>*.example.com</em> under ServerAlias to pick up <em>anything.example.com</em>.</p>
<p>If you are getting permission errors, dont forget to <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?chown">chown</a> and <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?chmod">chmod</a> your <em>/var/www/e/example</em> directory to the right user with the right permssions.</p>
<p>You will also need to reload apache2 for the changes to take effect.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;">mars:/etc/apache2/sites-enabled# /etc/init.d/apache2 restart
Forcing reload of web server (apache2)... waiting .
mars:/etc/apache2/sites-enabled#</pre></div></div>

<p><strong>Part 3 &#8211; Basic Security</strong></p>
<p>In the last part of this guide we will review basic security, the following lines can be added in the <VirtualHost> configuration, in the main configuration file, or even in a .htaccess in the desired directory.</p>
<p>How to block all users except 1 IP address:</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;"># If the REMOTE_ADDR (Ip address) isn't 127.0.0.1.
RewriteCond %{REMOTE_ADDR} !^127.0.0.1$
# On any page (.*), F=Forbidden, L=Last Rule.
RewriteRule .* - [F,L]</pre></div></div>

<p>How to block users to the downloads directory who didn&#8217;t come from your site.</p>

<div class="wp_syntax"><div class="code"><pre class="ssh" style="font-family:monospace;"># If the user didn't come from www.mydomain.com or sub.mydomain.com
RewriteRule %{HTTP_REFERER} !^www.mydomain.com [NC,OR]
RewriteRule %{HTTP_REFERER} !^sub.mydomain.com [NC]
# And they are trying to access anything under the downloads/ folder.
# Then redirect them to http://www.mydomain.com/invalid-referer.html
RewriteRule downloads/(.*) /invalid-referer.html [R=302,L]</pre></div></div>

<p>I hope you enjoyed reading this guide, please leave comments if you need help.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arkinex.com/guides/33/managing-apache2-on-debian-etch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
