根据apache2 module tutorial one在linux下安装配置apache

如题,根据http://threebit.net/tutorials/apache2_modules/tut1/tutorial1.html这个页面在linux安装配置tutorial one module的话,会出现一些问题,下面记录我在安装配置过程中遇到的问题和解决方案。

如下约定:

1.红色字要特别注意,有一些提醒;

2.打了‘#’号的是注释而不是命令;

3.粗体字是小节;

4.主流程是main steps,主流程中遇到的问题解决方案的分支是child steps,有时候会从主流程跳到相应的子流程或者问题解决完了要从子流程跳回主流程,请大家注意看。如果没有碰到相关的问题的话,可以直接跳过这个问题的解决方案的子流程。

 

Tutorial One

This tutorial is not intended to showcase Apache’s module API. Instead, it guides the reader through the other tasks that must be done to properly develop, compile and install a custom module - namely autoconf andautomake.

Preparation

If you don’t actually want to run or test the code in this tutorial, then feel free to skip this step. Otherwise, you’ll want to perform the following actions so your work area prepared for compiling and running the tutorial.

I have assumed in this tutorial that you have an account on a Linux (or Unix) machine and you have installed the GNU build tools (autoconf, automake, etc). If you haven’t then you’re not going to get very far - consult your OS documentation.

# Prepare the temporary directory
cd $HOME
mkdir threebit-tutorials
cd threebit-tutorials

# Remember the tutorial home directory for later.
export TUTORIAL_HOME=`pwd`

Download via HTTP

cd $TUTORIAL_HOME
wget ["http://threebit.net/tutorials/tutorials.tar.gz"](http://threebit.net/tutorials/tutorials.tar.gz)
tar zxvf tutorials.tar.gz
**Apache** _Note: You will get a "404 - Not Found" error if 2.0.43 is no longer the newest 
version of Apache. Just substitute the current version tag if that is the case.go to 
the websit to seethe last version of httpd server:_**[http://httpd.apache.org/](http://httpd.apache.org/).
**
cd $TUTORIAL_HOME
wget [http://www.apache.org/dist/httpd/httpd-2.0.43.tar.gz](http://www.apache.org/dist/httpd/httpd-2.0.43.tar.gz)
tar zxf httpd-2.0.43.tar.gz
cd httpd-2.0.43
./configure --prefix=$TUTORIAL_HOME/apache2 --enable-so
Apache Note:配置服务器,这里会出一些问题,比如APR not found,PCRE needed等,所以下面这一步比较
麻烦,请按照如下所述的步骤一步步的来。配置一下,如果出现APR not found错误,请按照下面步骤,如果没
有出现,请跳到main step。**

**Apache Note:the follow steps will show you the way to solove the APR not found problem
-child steps.

**Download and install APR**
wget http://labs.mop.com/apache-mirror//apr/apr-1.4.6.tar.gz
Apache Note:可能你下载的版本已经不是1.4.6了,用自己的版本号替代掉1.4.6即可。在[http://apr.apache.org/download.cgi](http://apr.apache.org/download.cgi)查看最新版本
tar zxvf apr-1.4.6.tar.gz
mv apr-1.4.6 apr
mv apr $TUTORIAL_HOME/httpd-2.4.4/srclib/
cd $TUTORIAL_HOME/httpd-2.4.4/srclib/apr
./configure --prefix=$TUTORIAL_HOME/apache2/apr
make && make install
**Download and install APR-util**
Apache Note:可能你下载的版本已经不是1.4.6了,用自己的版本号替代掉1.4.1即可。在[http://apr.apache.org/download.cgi](http://apr.apache.org/download.cgi)查看最新版本
wget http://labs.mop.com/apache-mirror//apr/apr-util-1.4.1.tar.gz
tar zxvf apr-util-1.4.1.tar.gz
mv apr-util-1.4.1 apr-util
mv apr-util $TUTORIAL_HOME/httpd-2.4.4/srclib/
cd $TUTORIAL_HOME/httpd-2.4.4/srclib/apr-util
./configure --prefix=$TUTORIAL_HOME/apache2/apr-util --with-apr=$TUTORIAL_HOME/apache2/apr/bin/apr-1-config
make && make install
**Download and install PCRE**
[http://sourceforge.net/projects/pcre/files/pcre/](http://sourceforge.net/projects/pcre/files/pcre/8.32/)下载最新版本,假如下载的是8.32.zip
Apache Note:可能你下载的版本已经不是8.30了,用自己的版本号替代掉8.30即可。
unzip pcre-8.32.zip
cd pcre-8.32
./configure --prefix=$TUTORIAL_HOME/apache2/pcre
make && make install
Apache Note**:**the follow steps are the main steps.
cd $TUTORIAL_HOME/httpd-2.4.4/
./configure --prefix=$TUTORIAL_HOME/apache2 --enable-so --enable-rewrite --with-apr=
$TUTORIAL_HOME/apache2/apr --with-apr-util=$TUTORIAL_HOME/apache2/apr-util 
--with-pcre=$TUTORIAL_HOME/apache2/pcre/bin/pcre-config

make make install
Apache Note:如果一下载的httpd版本里面包含了APR,即不会出现APR not found的错误的话,配置安装httpd的命令如下:
./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --with-pcre=/usr/local/pcre/bin/pcre-config
make && make install
**Now** we will fix the _ServerName_ and _Listen_ configuration directives so that we can run this installation as an unpriviledged user.
# store the location of the apache configuration file.
HTTPCONF=$TUTORIAL_HOME/apache2/conf/httpd.conf

# replace the ServerName directive
cat $HTTPCONF | sed 's/#ServerName new.host.name:80/ServerName localhost/' > $HTTPCONF.new
mv $HTTPCONF.new $HTTPCONF

# replace the Listen directive.
cat $HTTPCONF | sed 's/^Listen 80/Listen 21000/' > $HTTPCONF.new
mv $HTTPCONF.new $HTTPCONF
#and test the configuration:
$TUTORIAL_HOME/apache2/bin/apachectl configtest
Syntax OK

Apache Note:the purpose of this module is to write data to the error log for each HTTP 
request. We are obviously building a useless module - but by limiting what the module 
does it becomes easier to explain what everything is doing.
_接下来,这个教程介绍了apache2_modules/tut1目录下的一些文件及GNU的build tool,如果感兴趣可以
读一下,不感兴趣可以直接跳过.
_

mod_tut1.c

The source code to the module is pretty much self documenting but let us examine each block independently.


/*
 * Include the core server components.
 */
#include "httpd.h"
#include "http_config.h"
_Obviously an Apache module will require information about structures, macros 
and functionsfrom Apache's core. These two header files are all that is 
required for this module, but real modules will need to include other header
files relating to request handling, logging, protocols,etc._
/*
 * Declare and populate the module's data structure.  The
 * name of this structure ('tut1_module') is important - it
 * must match the name of the module.  This structure is the
 * only "glue" between the httpd core and the module.
 */
module [AP_MODULE_DECLARE_DATA](http://lxr.webperf.org/ident.cgi?i=AP_MODULE_DECLARE_DATA) tut1_module =
{
  // Only one callback function is provided.  Real
  // modules will need to declare callback functions for
  // server/directory configuration, configuration merging
  // and other tasks.
  [STANDARD20_MODULE_STUFF](http://lxr.webperf.org/ident.cgi?i=STANDARD20_MODULE_STUFF),
  NULL,
  NULL,
  NULL,
  NULL,
  NULL,
  mod_tut1_register_hooks,      /* callback for registering hooks */
};

_Every module must declare it's data structure as shown above. Since this 
module does not require any configuration most of the callback locations 
have been left blank, except for the last one - that one is invoked by the 
HTTPD core so that the module can declare other functions that should be 
invoked to handle various events (like an HTTP request)._
/*
 * This function is a callback and it declares what
 * other functions should be called for request
 * processing and configuration requests. This
 * callback function declares the Handlers for
 * other events.
 */
static void mod_tut1_register_hooks ([apr_pool_t](http://lxr.webperf.org/ident.cgi?i=apr_pool_t) *p)
{
  // I think this is the call to make to register a
  // handler for method calls (GET PUT et. al.).
  // We will ask to be last so that the comment
  // has a higher tendency to go at the end.
  ap_hook_handler(mod_tut1_method_handler, NULL, NULL, APR_HOOK_LAST);
}
_When this function is called by the HTTPD core, it registers a handler that 
should be invoked for all HTTP requests._
/*
 * This function is registered as a handler for HTTP methods and will
 * therefore be invoked for all GET requests (and others).  Regardless
 * of the request type, this function simply sends a message to
 * STDERR (which httpd redirects to logs/error_log).  A real module
 * would do *alot* more at this point.
 */
static int mod_tut1_method_handler ([request_rec](http://lxr.webperf.org/source.cgi/include/httpd.h#L707) *r)
{
  // Send a message to stderr (apache redirects this to the error log)
  fprintf(stderr,"apache2_mod_tut1: A request was made.n");

  // We need to flush the stream for messages to appear right away.
  // Performing an fflush() in a production system is not good for
  // performance - don't do this for real.
  fflush(stderr);

  // Return DECLINED so that the Apache core will keep looking for
  // other modules to handle this request.  This effectively makes
  // this module completely transparent.
  return [DECLINED](http://lxr.webperf.org/ident.cgi?i=DECLINED);
}
_This is the function that will be invoked for every HTTP request. This is 
where the _meat_ of an Apache module should go._

GNU Build Tools

_Looking in _TODO $TUTORIAL_HOME/tut1_, you will find some familiar files that 
are included with most GNU applications._
[Makefile.am](http://threebit.net/tutorials/apache2_modules/tut1/Makefile.am) An input file for _automake_
[configure.in](http://threebit.net/tutorials/apache2_modules/tut1/configure.in) An inputfile to _autoconf_.
[mod_tut1.c](http://threebit.net/tutorials/apache2_modules/tut1/mod_tut1.c) The source code to the tutorial module.
tutorial1.html This file.
_**The remaining files can safely be ignored.**_
AUTHORS automake will produce warnings if this file is not present.
COPYING The GPL license. automake will complain if this file is not present.
CVS/ CVS state directory. Ignore it. If you downloaded the tutorial using the tar ball then it won't even exist.
ChangeLog Another automake file.
INSTALL Standard install instructions. In this case, it points the reader to this file.
NEWS Another automake file.
README Another automake file.

This tutorial does not aim to be a complete reference for the GNU build tools. See the following references for information.



configure.in

_I would be lying to you if I told you that I understand everything in this file. 
However, it seems to work so I'll tell you what I know. :) See [configure.in](http://threebit.net/tutorials/apache2_modules/tut1/configure.in) for 
raw file._
AC_INIT
_The mandatory autoconf initialization macro._
# Automake initialization
AM_INIT_AUTOMAKE(mod_tut1, 1.0)
_This macro is provided by automake and is required when automake is used. The 
arguments are the package name and version number. I have provided reasonable 
values for the parameters but still haven't figured out what their impact is._
AC_PROG_CC
AM_PROG_LIBTOOL
_These two macros add checks for suitable **cc** and **libtool** programs._
AC_DEFUN([APACHE_DIR],[

  AC_ARG_WITH(
    apache,
    [  --with-apache[=DIR]     Apache server directory],
    ,
    [with_apache="no"]
  )

  AC_MSG_CHECKING(for Apache directory)

  if test "$with_apache" = "no"; then
    AC_MSG_ERROR( Specify the apache using --with-apache)
  else
    # make sure that a well known include file exists
    if test -e $with_apache/include/httpd.h; then
      apache_dir=$with_apache
      AC_MSG_RESULT(APACHE found!)
    else
      AC_MSG_ERROR( $with_apache not found. )
    fi
  fi

])
_This declares a new autoconf macro named _APACHE_DIR_. It is used to handle the 
_--with-apache=/usr/local/apache2_ argument to _configure_._
APACHE_DIR
_This runs the _APACHE_DIR_ macro that was just defined. When successfull, the 
directory location is stored in _apache_dir_._
AC_SUBST(apache_dir)
_Not all variables that are set in shell snippets are persisted to the 
configuration status file (config.status). This call to AC_SUBST persists 
the value of _apache_dir_._
AC_OUTPUT(Makefile)
_Finally, _AC_OUTPUT()_ saves the results of the configuration and causes a 
real Makefile to be generated._

Makefile.am

_This file is used by **automake** to generate a **Makefile.in** file. As stated earlier,
Makefile.in is then parsed using an invocation of **configure** to create an actual 
**Makefile**._
Since writing an Apache module is the same as writing, compiling and linking any standard shared library, **automake** is well suited to the task.

Again, consult the full automake documentation for all the info. See the raw [Makefile.am](http://threebit.net/tutorials/apache2_modules/tut1/Makefile.am).

lib_LTLIBRARIES = libmodtut1.la
_This tells automake that we are creating a shared library named **libmottut1.la**._
libmodtut1_la_SOURCES = mod_tut1.c
_This tells automake what source files should be compiled as part of the library. 
In this case there is only one, but there could be serveral._
INCLUDES = -I@apache_dir@/include
_Header files from the apache distribution are required when compiling the module. 
This directive provides a list of include directories to pass on to **gcc**. Does 
_apache_dir_ look familiar? If you said yes, then step to the front of the class
 - **configure** will subsitute the value that was passed in with **--with-apache** 
when the Makefile is written.
_
## aclocal, autoconf, automake
_Now that you have some idea of what those files mean we can run the utilities 
that use them._
**aclocal** is used to import macros defined by automake so that autoconf can understand what's going on. cd $TUTORIAL_HOME/apache2_modules/tut1 libtoolize Apache Note:这一步生成后面必须的文件Itmain.sh # import automake m4 macros. aclocal
Apache Note:可能会碰到一个问题,大概的意思是缺少AM_LIBTOOL之类的文件,造成这个问题的原因有两个:
-child steps

1.你没有安装libtool;
解决方案:sudo yum install libtool
_2.你的aclocal与libtool没有安装到同一个目录下.
解决方案:
_cp /usr/share/aclocal/* /usr/share/aclocal-1.12/
rm /usr/share/aclocal
ln -s /usr/share/aclocal-1.12/usr/share/aclocal

#然后把可执行程序也替换一下:

rm /usr/bin/aclocal
ln -s /usr/bin/aclocal-1.9 /usr/bin/aclocal
_原因:_由于新版的Fedora系统在安装新的automake工具的时候,并没有安装到默认的automake目录,而是安装到automake-version,比如automake-1.12目录中,于是aclocal也被安装到了aclocal-version中,跟默认的**libtool**显然就不是同一目录,出现找不到**libtool**的情况也是正常的了,修改的方案就是把默认的aclocal目录中的文件,copy到aclocal-version目录中,然后删除aclocal目录,做一个aclocal的Symbolink连接,连接到ac#local-version目录中。系统自带的aclocal文件夹安装在/usr/share/aclocal中,后来升级的aclocal就安装到了/usr/share/aclocal-1.12中。当aclocal找不到**libtool**的时候,可能的报错还有:**macro `AM_DISABLE_STATIC’ not found in ****library,****macro `AM_PROG_LIBTOOL’ not found in ****library,**用这种方法都可以解决。 Apache Note:the follow steps are main steps
# create configure based on configure.in
autoconf
# create Makefile.in based on Makefile.am and configure.in
automake -a

configure

Now we can run **configure** to prepare the module's **Makefile**.
# The ubiquitous configure script
./configure --with-apache=$TUTORIAL_HOME/apache2**
**

make

And now we can run **make** to compile the module. **Note:** don't run **make install**. We'll handle 
the module installation later.
make
Apache Note:这里也可能出现问题,比如apr.h找不到-child steps
cp $TUTORIAL_HOME/httpd-2.4.4/srclib/apr/include/* $TUTORIAL_HOME/apache2/include/
cp $TUTORIAL_HOME/httpd-2.4.4/srclib/apr-util/include/* $TUTORIAL_HOME/apache2/include/
#打开$TUTORIAL_HOME/apache2/include/apr.h,找到typedef off64_t apr_off_t;这一句(大概在358行),然后修改为typedef long long apr_off_t;
./configure --with-apache=$TUTORIAL_HOME/apache2
make
原因:主要是$TUTORIAL_HOME/apache2/include/目录下面没有apr.h,apr_hooks.h等头文件(这个就不知道是怎么个情况了),把$TUTORIAL_HOME/httpd-2.4.4/srclib/apr/include和$TUTORIAL_HOME/httpd-2.4.4/srclib/apr-util/include目录下的头文件全部拷到$TUTORIAL_HOME/apache2/include/这个目录中就可以了。apr.h头文件中的apr_off_t类型定义要修改一下。
## apxs **** DO NOT RUN _make install_ **** Ordinarially you would, but the install step for an Apache module is different. Instead, **apxs** is used to register the module in _httpd.conf_ and move the shared object into the apache lib directory.
$TUTORIAL_HOME/apache2/bin/apxs -i -a -n tut1 libmodtut1.la
**apxs** also addes the following line to **httpd.conf**:
LoadModule tut1_module        modules/libmodtut1.so

Run Apache

Now we are ready to run Apache and test the module.
# Change to the apache directory
cd $TUTORIAL_HOME/apache2

# Start Apache
bin/apachectl start

# Use Lynx to hit the web server.
lynx --source http://localhost:21000 | grep success

# Look for the module's message in the error log
cat logs/error_log | grep tut1
_apache2_mod_tut1: A request was made._

Success!

The tutorial one module has been successfully compiled and installed into the Apache 2
runtime.


参考资料:


出现APR not found等问题的解决方案参考资料:



automake -a出现问题的解决方案参考网站:



缺少apr.h等的解决方案参考资料: