<?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>CB1, INC. &#187; kdevelop</title>
	<atom:link href="http://www.cb1inc.com/category/kdevelop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cb1inc.com</link>
	<description></description>
	<lastBuildDate>Wed, 28 Sep 2011 17:54:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Creating a Hello World! Apache Module with KDevelop on Ubuntu</title>
		<link>http://www.cb1inc.com/2007/12/24/creating-a-hello-world-apache-module-with-kdevelop-on-ubuntu/</link>
		<comments>http://www.cb1inc.com/2007/12/24/creating-a-hello-world-apache-module-with-kdevelop-on-ubuntu/#comments</comments>
		<pubDate>Mon, 24 Dec 2007 10:06:55 +0000</pubDate>
		<dc:creator>Chris Barber</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[kdevelop]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<iframe src="http://rcm.amazon.com/e/cm?t=ci09-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0132409674&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;lc1=0000FF&#038;bc1=999999&#038;bg1=F6F6F6&#038;f=ifr" style="float:right;width:120px;height:240px;margin-left:10px;margin-bottom:10px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>

<p>The <a href="http://httpd.apache.org/">Apache HTTP Web Server</a> is a powerful and extensible web server that is the "A" in "LAMP". One of the neat things about Apache is its API for writing custom modules.</p>

<p><a href="http://people.apache.org/~niq/">Nick Kew</a> wrote an excellent book called <a href="http://www.amazon.com/dp/0132409674?tag=ci09-20&#038;camp=14573&#038;creative=327641&#038;linkCode=as1&#038;creativeASIN=0132409674&#038;adid=1MDYV18CJT05FYHY83PH&#038;">The Apache Modules Book</a>. Anyone who is serious about Apache module development must buy this book.</p>

<p>Modules can be written a number of ways, but the most common way is to use the C programming language. For an C/C++ development IDE, I use <a href="http://www.kdevelop.org/">KDevelop</a>. It is pretty easy to use once you figure out what you need to do.</p>

<p>It is possible to write modules in C++, but I don't recommend it if your module's source can't be contained in a single source file. There's all sorts of interesting issues with exported symbols and static function declarations. Another reason to stick with C is pretty much all core modules and examples are written using C. You may give it a try and determine that it works just fine for your project.</p>

<h4>Prerequisites</h4>

<p>Before you begin, there is a handful of applications and libraries you must have installed:
<ul>
<li>Apache 2</li>
<li>KDevelop 3.4</li>
<li>GCC (bundled with "build-essential" package)</li>
<li>automake</li>
<li>autoconf</li>
</ul></p>

<h4>Creating the Project</h4>

<p>Launch KDevelop and select "New Project" from the Project menu. Since we are focusing on using C, select "Simple Hello world program" under the "C" folder. Give your module an "Application name" and specify the location to create the project.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-new-project-01.png" alt="KDevelop New Project"/></div>

<p>On the next page of the wizard, you must enter your name, but your email address is not required.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-new-project-02.png" alt="KDevelop New Project"/></div>

<p>The next couple wizard screens ask about version control and source templates. After finishing the wizard, you will be back at the IDE with the new project created.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-ide.png" alt="KDevelop IDE"/></div>

<h4>The Code</h4>

<p>Delete all of the source code that the editor created. Next paste the following code which originated from Nick's version on <a href="http://www.apachetutor.org/book/">The Apache Modules Book Companion site</a>.</p>

<pre>#include &#60;httpd.h&#62;
#include &#60;http_protocol.h&#62;
#include &#60;http_config.h&#62;

static int helloworld_handler(request_rec* r)
{
	if (!r-&#62;handler &#124;&#124; strcmp(r-&#62;handler, "helloworld"))
		return DECLINED;
	
	if (r-&#62;method_number != M_GET)
		return HTTP_METHOD_NOT_ALLOWED;
	
	ap_set_content_type(r, "text/html;charset=ascii");
	ap_rputs("&#60;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&#62;n", r);
	ap_rputs("&#60;html&#62;&#60;head&#62;&#60;title&#62;Hello World!&#60;/title&#62;&#60;/head&#62;", r);
	ap_rputs("&#60;body&#62;&#60;h1&#62;Hello World!&#60;/h1&#62;&#60;/body&#62;&#60;/html&#62;", r);
	return OK;
}

static void register_hooks(apr_pool_t* pool)
{
	ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA helloworld_module = {
	STANDARD20_MODULE_STUFF,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	register_hooks
};
</pre>

<h4>Configuring the Project</h4>

<p>Next we need to reconfigure the build target to create a library instead of a normal program. Right-click the build target and click "Remove":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-remove-target.png" alt="Remove build target"/></div>

<p>When the dialog displays, uncheck the "Also remove from disk" option before clicking "OK". Now we need to add a new build target. Right-click the "src" folder and select "Add Target":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-add-target-01.png" alt="Add new build target"/></div>

<p>From the "Add Target" dialog, change the type to "Libtool Library" and enter the name of the module. Also check the "-avoid-version" and "-module" options.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-add-target-02.png" alt="Add new build target dialog"/></div>

<p>As soon as the target is created, right-click on it and make sure the checked options saved properly.  Right-click on the target again and select "Make Target Active":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-make-target-active.png" alt="Make target active"/></div>

<p>Edit the project's options by right-clicking the "src" folder and selecting "Options":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-project-options-01.png" alt="Src dropdown menu"/></div>

<p>Since this is a C project, we want to add the following options to the "CFLAGS" field:</p>

<pre>-DLINUX=2 -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -D_REENTRANT -pthread</pre>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-project-options-02.png" alt="Options - Compiler"/></div>

<p>From the "Includes" tab, add the following outside include directories:
<ul><li>/usr/includes/apr-1.0</li><li>/usr/includes/apache2</li></ul>
</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-project-options-03.png" alt="Options - Includes"/></div>

<p>Reorder the include paths so that "$(all_includes)" is first. Due to a bug or poor design, you must edit the two paths you just added and prepend a "-I":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-project-options-04.png" alt="Include path prepend -I"/></div>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-project-options-05.png" alt="Include path prepend -I"/></div>

<p>Since we deleted the old build target, we need to add the source files to the target by right-clicking the target and selecting "Add Existing Files":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-add-files-01.png" alt="Adding files to target"/></div>

<p>From the dialog, drag and drop the files you want to be apart of the target. For this simple example, we only move the "mod_helloworld.c" file.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-add-files-02.png" alt="Adding files to target"/></div>

<h4>Building the Project</h4>

<p>We are all set to compile the project. From the "Build" menu, select "Build Active Target":</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-build-01.png" alt="Building the active target"/></div>

<p>If this is the first time you are performing the build, KDevelop will prompt you whether or not you want to run automake. Click the "Run Them" button to continue.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-build-02.png" alt="Run automake dialog"/></div>

<p>When the build is finished, the "Messages" panel will show up and display the build results. If everything went as planned, the output will say the build was successful.</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-build-03.png" alt="Build results"/></div>

<h4>Deploying the Module</h4>

<p>The build process put the shared library file in the following location (assuming debug build):</p>

<pre>/path/to/mod_helloworld/debug/src/.libs/libmod_helloworld</pre>

<p>We need to install that file in the Apache modules directory which on Ubuntu is:</p>

<pre>/usr/lib/apache2/modules</pre>

<p>From a terminal, run the following command as root or sudo:</p>

<pre>cp /path/to/mod_helloworld/debug/src/.libs/libmod_helloworld 
    /usr/lib/apache2/modules/mod_helloworld.so</pre>

<p>Next you'll need to edit the Apache configuration file. In Ubuntu, the file is located at:

<pre>/etc/apache2/apache2.conf</pre>

<p>You need to add the <code>LoadModule</code> and <code>&#60;Location&#62;</code> directives so Apache knows when to invoke the module.</p>

<pre>LoadModule helloworld_module /usr/lib/apache2/modules/mod_helloworld.so
&#60;Location /helloworld&#62;
    SetHandler helloworld
&#60;/Location&#62;</pre>

<p>I've had spotty luck where to actually insert those settings. After the <code>LogLevel</code>, but before any other <code>LoadModule</code> entries seems to work for me. After you save the changes, restart Apache using the following command as root or sudo:</p>

<pre>apache2ctl restart</pre>

<p>If your Apache acts funny, try restarting it again.</p>

<h4>Testing the Module</h4>

<p>The last step is to test the module. Open up your favorite web browser and hit <code>http://localhost/helloworld</code>:</p>

<div align="center"><img src="http://cb1inc.com/sites/default/blog/20071224-browser.png" alt="Hello World in Firefox"/></div>

<p>If everything worked, you should see something similar to the image above.</p>

<h4>Where To Go From Here</h4>

<p>We have only scratched the surface. The Apache Portable Runtime (APR) provides a ton of functionality that makes developing modules much easier. <a href="http://www.amazon.com/dp/0132409674?tag=ci09-20&#038;camp=14573&#038;creative=327641&#038;linkCode=as1&#038;creativeASIN=0132409674&#038;adid=1MDYV18CJT05FYHY83PH&#038;">The Apache Modules Book</a> dives into several topics such as configuration settings, content generators, filters, and database connectivity.</p>

<p>Another great resource is Apache's own module source code in their Subversion repository: <a href="http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/">http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/</a>.</p>

]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://rcm.amazon.com/e/cm?t=ci09-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=0132409674&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;lc1=0000FF&#038;bc1=999999&#038;bg1=F6F6F6&#038;f=ifr" style="float:right;width:120px;height:240px;margin-left:10px;margin-bottom:10px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></p>
<p>The <a href="http://httpd.apache.org/">Apache HTTP Web Server</a> is a powerful and extensible web server that is the &#8220;A&#8221; in &#8220;LAMP&#8221;. One of the neat things about Apache is its API for writing custom modules.</p>
<p><a href="http://people.apache.org/~niq/">Nick Kew</a> wrote an excellent book called <a href="http://www.amazon.com/dp/0132409674?tag=ci09-20&#038;camp=14573&#038;creative=327641&#038;linkCode=as1&#038;creativeASIN=0132409674&#038;adid=1MDYV18CJT05FYHY83PH&#038;">The Apache Modules Book</a>. Anyone who is serious about Apache module development must buy this book.</p>
<p>Modules can be written a number of ways, but the most common way is to use the C programming language. For an C/C++ development IDE, I use <a href="http://www.kdevelop.org/">KDevelop</a>. It is pretty easy to use once you figure out what you need to do.</p>
<p>It is possible to write modules in C++, but I don&#8217;t recommend it if your module&#8217;s source can&#8217;t be contained in a single source file. There&#8217;s all sorts of interesting issues with exported symbols and static function declarations. Another reason to stick with C is pretty much all core modules and examples are written using C. You may give it a try and determine that it works just fine for your project.</p>
<h4>Prerequisites</h4>
<p>Before you begin, there is a handful of applications and libraries you must have installed:</p>
<ul>
<li>Apache 2</li>
<li>KDevelop 3.4</li>
<li>GCC (bundled with &#8220;build-essential&#8221; package)</li>
<li>automake</li>
<li>autoconf</li>
</ul>
<h4>Creating the Project</h4>
<p>Launch KDevelop and select &#8220;New Project&#8221; from the Project menu. Since we are focusing on using C, select &#8220;Simple Hello world program&#8221; under the &#8220;C&#8221; folder. Give your module an &#8220;Application name&#8221; and specify the location to create the project.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-new-project-01.png" alt="KDevelop New Project"/></div>
<p>On the next page of the wizard, you must enter your name, but your email address is not required.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-new-project-02.png" alt="KDevelop New Project"/></div>
<p>The next couple wizard screens ask about version control and source templates. After finishing the wizard, you will be back at the IDE with the new project created.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-ide.png" alt="KDevelop IDE"/></div>
<h4>The Code</h4>
<p>Delete all of the source code that the editor created. Next paste the following code which originated from Nick&#8217;s version on <a href="http://www.apachetutor.org/book/">The Apache Modules Book Companion site</a>.</p>
<pre class="brush: cpp; title: ;">
#include &lt;httpd.h&gt;
#include &lt;http_protocol.h&gt;
#include &lt;http_config.h&gt;

static int helloworld_handler(request_rec* r)
{
	if (!r-&gt;handler || strcmp(r-&gt;handler, &quot;helloworld&quot;))
		return DECLINED;

	if (r-&gt;method_number != M_GET)
		return HTTP_METHOD_NOT_ALLOWED;

	ap_set_content_type(r, &quot;text/html;charset=ascii&quot;);
	ap_rputs(&quot;&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;&gt;n&quot;, r);
	ap_rputs(&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello World!&lt;/title&gt;&lt;/head&gt;;&quot;, r);
	ap_rputs(&quot;&lt;body&gt;&lt;h1&gt;Hello World!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;&quot;, r);
	return OK;
}

static void register_hooks(apr_pool_t* pool)
{
	ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA helloworld_module = {
	STANDARD20_MODULE_STUFF,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	register_hooks
};
</pre>
<h4>Configuring the Project</h4>
<p>Next we need to reconfigure the build target to create a library instead of a normal program. Right-click the build target and click &#8220;Remove&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-remove-target.png" alt="Remove build target"/></div>
<p>When the dialog displays, uncheck the &#8220;Also remove from disk&#8221; option before clicking &#8220;OK&#8221;. Now we need to add a new build target. Right-click the &#8220;src&#8221; folder and select &#8220;Add Target&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-add-target-01.png" alt="Add new build target"/></div>
<p>From the &#8220;Add Target&#8221; dialog, change the type to &#8220;Libtool Library&#8221; and enter the name of the module. Also check the &#8220;-avoid-version&#8221; and &#8220;-module&#8221; options.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-add-target-02.png" alt="Add new build target dialog"/></div>
<p>As soon as the target is created, right-click on it and make sure the checked options saved properly.  Right-click on the target again and select &#8220;Make Target Active&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-make-target-active.png" alt="Make target active"/></div>
<p>Edit the project&#8217;s options by right-clicking the &#8220;src&#8221; folder and selecting &#8220;Options&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-project-options-01.png" alt="Src dropdown menu"/></div>
<p>Since this is a C project, we want to add the following options to the &#8220;CFLAGS&#8221; field:</p>
<pre class="brush: plain; title: ;">
-DLINUX=2 -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -D_REENTRANT -pthread
</pre>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-project-options-02.png" alt="Options - Compiler"/></div>
<p>From the &#8220;Includes&#8221; tab, add the following outside include directories:</p>
<ul>
<li>/usr/includes/apr-1.0</li>
<li>/usr/includes/apache2</li>
</ul>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-project-options-03.png" alt="Options - Includes"/></div>
<p>Reorder the include paths so that &#8220;$(all_includes)&#8221; is first. Due to a bug or poor design, you must edit the two paths you just added and prepend a &#8220;-I&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-project-options-04.png" alt="Include path prepend -I"/></div>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-project-options-05.png" alt="Include path prepend -I"/></div>
<p>Since we deleted the old build target, we need to add the source files to the target by right-clicking the target and selecting &#8220;Add Existing Files&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-add-files-01.png" alt="Adding files to target"/></div>
<p>From the dialog, drag and drop the files you want to be apart of the target. For this simple example, we only move the &#8220;mod_helloworld.c&#8221; file.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-add-files-02.png" alt="Adding files to target"/></div>
<h4>Building the Project</h4>
<p>We are all set to compile the project. From the &#8220;Build&#8221; menu, select &#8220;Build Active Target&#8221;:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-build-01.png" alt="Building the active target"/></div>
<p>If this is the first time you are performing the build, KDevelop will prompt you whether or not you want to run automake. Click the &#8220;Run Them&#8221; button to continue.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-build-02.png" alt="Run automake dialog"/></div>
<p>When the build is finished, the &#8220;Messages&#8221; panel will show up and display the build results. If everything went as planned, the output will say the build was successful.</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-build-03.png" alt="Build results"/></div>
<h4>Deploying the Module</h4>
<p>The build process put the shared library file in the following location (assuming debug build):</p>
<pre class="brush: plain; title: ;">
/path/to/mod_helloworld/debug/src/.libs/libmod_helloworld
</pre>
<p>We need to install that file in the Apache modules directory which on Ubuntu is:</p>
<pre class="brush: plain; title: ;">
/usr/lib/apache2/modules
</pre>
<p>From a terminal, run the following command as root or sudo:</p>
<pre class="brush: plain; title: ;">
cp /path/to/mod_helloworld/debug/src/.libs/libmod_helloworld /usr/lib/apache2/modules/mod_helloworld.so
</pre>
<p>Next you&#8217;ll need to edit the Apache configuration file. In Ubuntu, the file is located at:</p>
<pre class="brush: plain; title: ;">
/etc/apache2/apache2.conf
</pre>
<p>You need to add the <code>LoadModule</code> and <code>&lt;Location&gt;</code> directives so Apache knows when to invoke the module.</p>
<pre class="brush: plain; title: ;">
LoadModule helloworld_module /usr/lib/apache2/modules/mod_helloworld.so
&lt;Location /helloworld&gt;
    SetHandler helloworld
&lt;/Location&gt;
</pre>
<p>I&#8217;ve had spotty luck where to actually insert those settings. After the <code>LogLevel</code>, but before any other <code>LoadModule</code> entries seems to work for me. After you save the changes, restart Apache using the following command as root or sudo:</p>
<pre class="brush: plain; title: ;">
apache2ctl restart
</pre>
<p>If your Apache acts funny, try restarting it again.</p>
<h4>Testing the Module</h4>
<p>The last step is to test the module. Open up your favorite web browser and hit <code>http://localhost/helloworld</code>:</p>
<div align="center"><img src="/wp-content/uploads/2009/12/20071224-browser.png" alt="Hello World in Firefox"/></div>
<p>If everything worked, you should see something similar to the image above.</p>
<h4>Where To Go From Here</h4>
<p>We have only scratched the surface. The Apache Portable Runtime (APR) provides a ton of functionality that makes developing modules much easier. <a href="http://www.amazon.com/dp/0132409674?tag=ci09-20&#038;camp=14573&#038;creative=327641&#038;linkCode=as1&#038;creativeASIN=0132409674&#038;adid=1MDYV18CJT05FYHY83PH&#038;">The Apache Modules Book</a> dives into several topics such as configuration settings, content generators, filters, and database connectivity.</p>
<p>Another great resource is Apache&#8217;s own module source code in their Subversion repository: <a href="http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/">http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cb1inc.com/2007/12/24/creating-a-hello-world-apache-module-with-kdevelop-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

