现在的位置: 首页 > 综合 > 正文

Apache学习笔记(二)Apache服务器的安装

2017年11月28日 ⁄ 综合 ⁄ 共 7963字 ⁄ 字号 评论关闭
Apache的安装一般情况下有两种:
1、rpm包安装
2、源代码编译安装

一、rpm包安装

    rpm安装是最快速的安装方法,但是在定制程度或者性能上要低于编译的版本。二进制安装包是作为DSO模块进行编译的,用户只需要选择自己想要的模块或者通过禁止其他的模块来减少内在的消耗。
     二进制包的安装比较简单,如下:
1、首先检查系统中有没有安装apache

[root@localhost]#rpm -qa httpd
[root@localhost]:/root/httpd-2.2.22

2、查询rpm包的信息

[root@localhost]#rpm -qip http-2.X.X.rpm

3、安装

[root@localhost]#rpm -ivh http-2.X.X.rpm

补充:
删除rpm包,e参数表示删除

[root@localhost]#rpm -e http-2.X.X

二、源代码安装

   一般第一次安装Apache都较为顺利。1. 下载并解压官网地址:http://httpd.apache.org/

[root@localhost]#tar zxvf httpd-2.2.22.tar.gz
[root@localhost]#cd httpd-2.2.22

在http-2.2.22里有文件README和INSTALL,用more命令可以阅读。
2. 配置

[root@localhost]# ./configure --prefix=/usr/local/apache

--prefix参数指定了将要安装到的目录。此时/usr/local下还没有该目录,make install后才会出现。
补充:
(1)--prefix=/usr/local/apache

体系无关文件的顶级安装目录PREFIX,也就Apache的安装目录。
(2)--enable-module=so
打开so模块,so模块是用来提DSO支持的apache核心模块
(3)--enable-mods-shared=all
编译全部的模板,对于不需要我们可以在httpd.conf去掉。
(4)--enable-cache
支持缓存
(5)--enable-file-cache
支持文件缓存
(6)--enable-mem-cache
支持记忆缓存
(7)--enable-disk-cache
支持磁盘缓存
(8)--enable-static-support
支持静态连接(默认为动态连接)
(9)--enable-static-htpasswd
使用静态连接编译htpasswd:管理用于基本认证的用户文件
(10)--enable-static-htdigest
使用静态连接编译htdigest:管理用于摘要认证的用户文件
(11)--enable-static-rotatelogs
使用静态连接编译rotatelogs:滚动Apache日志的管道日志程序
(12)--enable-static-logresolve
使用静态连接编译logresolve:解析Apache日志中的IP地址为主机名
(13)--enable-static-htdbm
使用静态连接编译htdbm:操作DBM密码数据库
(14)--enable-static-ab
使用静态连接编译ab:Apache HTTP 服务器性能测试工具
(15)--enable-static-checkgid
使用静态连接编译checkgid
(16)--disable-cgid
禁止用一个外部CGI守护进程执行CGI脚本
(17)--disable-cgi
禁止编译CGI版本的PHP我们不再使用worker模式编译apache,worker模式和php貌似有一些不协调不稳定之处。所以使用了默认的perfork模式。
注意:
     Apache在安装时不会检查参数是否正确,错误的参数会直接被丢弃,不会报告给用户。所以使用echo $?命令检查是否有错误,当输出结果为0时表示没有错误。
 3. 编译
[root@localhost]#make

4. 安装

[root@localhost]#make install

 5. 启动服务器

[root@localhost]#cd /usr/local/apache/bin
[root@localhost]#./apachectl start

说明:

    出现AH00557: httpd: apr_sockaddr_info_get() failed for Chunk3这个问题,解决办法, vi /usr/local/apache/conf/httpd.conf 找到#ServerName www.example.com:80 这行,把注释拿掉,然后改成ServerName localhost:80 
    为了以后使用方便,可以把启动文件apachectl复制到/sbin下,以后直接apachectl start启动。
#vi /etc/rc.local

增加一行

/sbin/apachectl start

6. 验证Apache是否工作
此时,服务器端窗口应该显示:

[root@apache bin]# ps -ef | grep httpd | grep -v grep
root 25317 1 0 21:12 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 25417 25317 0 21:13 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 25419 25317 0 21:13 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 25420 25317 0 21:13 ? 00:00:00 /usr/local/apache2/bin/httpd -k start
daemon 25605 25317 0 21:20 ? 00:00:00 /usr/local/apache2/bin/httpd -k start

    在客户端浏览器输入服务器的IP地址(或者http://localhost),IE应该显示:It works!画面。

三、主要目录和文件

服务目录:/usr/local/apache/
主配置文件:/usr/local/apache/conf/httpd.conf
网页目录:/usr/local/apache/htdocs/
服务脚本:/usr/local/apache/bin/apachectl
执行程序:/usr/local/apache/bin/httpd
访问日志: /usr/local/apache/log/access_log
错误日志: /usr/local/apache/log/error_log

四、apache自动运行脚本

    以源码方式编译安装的Apache是没有启动脚本的,也就是说用户不能通过简单的Server httpd start/stop/restart等操作。要想自动运行一是写一个启动脚本,将它放入/etc/init.d目录中,二是使用ln制作链接文件到相应的启动级别目录中。把Apache加入系统SysV服务。
    可以直接使用/usr/local/apache2/bin/目录下的apachectl文件,将它复制到/etc/init.d中,然后将它链接到rc3.d目录和rc6.d目录中。
    apachectl文件的内容如下:

[root@apache bin]# cat apachectl
#!/bin/sh

ACMD="$1"
ARGV="$@"

if test -f /usr/local/apache2/bin/envvars; then
  . /usr/local/apache2/bin/envvars
fi

LYNX="links -dump"

STATUSURL="http://localhost:80/server-status"

ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"

if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi
ERROR=0
if [ "x$ARGV" = "x" ] ; then
    ARGV="-h"
fi
case $ACMD in
start|stop|restart|graceful|graceful-stop)
    $HTTPD -k $ARGV
    ERROR=$?
    ;;
startssl|sslstart|start-SSL)
    echo The startssl option is no longer supported.
    echo Please edit httpd.conf to include the SSL configuration settings
    echo and then use "apachectl start".
    ERROR=2
    ;;
configtest)
    $HTTPD -t
    ERROR=$?
    ;;
status)
    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '
    ;;
fullstatus)
    $LYNX $STATUSURL
    ;;
*)
    $HTTPD "$@"
    ERROR=$?
esac
exit $ERROR
脚本说明:
#!/bin/bash
#初始化化解变量
# Startup script for the Apache2.0.X Web Server
#设置与chkconfig相关的选项
# chkconfig: 345 85 15 #设置启动级别以及启动顺序
#description:some words you like!! #描述信息
#调用系统初始化脚本,执行/etc/rc.d/init.d/functions脚本
# Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
  exit 0
fi
#设置与启动关闭apache服务相关的选项
//获取命令输入的参数
ARGV="$@"
//设置httpd命令的路径
HTTPD='/usr/local/apache2/bin/httpd'
//如果envvars文件存在,则执行envvars文件设置apache的环境变量
if test -f /usr/local/apache2/bin/envvars;then
  ./usr/local/apache2/bin/envvars
fi
//设置命令行的HTML格式
LYNX="links -dump"
//设置mod_status模块的状态页面的URL
STATUSURL="http://localhost:80/server-status"
//解除子进程的文件描述器的限制
ULIMIT_MAX_FILES="ulimit -S -n 'ulimit -H -n'"
//如果ULIMIT_MAX_FILES参数不为空,则运行ULIMIT_MAX_FILES参数中的指令,解除子进程文件描述器的限制
if [ "x$ULIMIT_MAX_FILES" != "x" ];then
    $ULIMIT_MAX_FILES
fi
ERROR=0
//如果命令选项为空,则把命令选项设置为-h
if [ "x$ARGV" = "x" ];then
   ARGV="-h"
fi
#程序逻辑
case $ARGV in
//启动、关闭、重启apache
start|stop|restart|graceful|graceful-stop)
  $HTTPD -k $ARGV
  ERROR=$?
;;
//不支持的命令参数
startssl|sslstart|start-SSL)
   echo "The startssl option is no longer supported"
   echo "Please edit httpd.conf to include the SSL configuration settings and then use "apachectl statrt"."
   ERROR=2
;;
//检查httpconf配置文件的格式是否正确
configtest)
  $HTTPD -t
  ERROR=$?
;;
//检查apache服务的状态
status)
  $LYNX $STATUSURL | awk ' /process$/ {print;exit } { print } '
;;
//输出完整的状态信息
fullstatus)
  $LYNX $STATUSURL
;;
*)
  $HTTPD $ARGV
  ERROR=$?
esac
exit $ERROR

授予httpd运行权限 

# chmod 755 /etc/rc.d/init.d/httpd

将httpd加入系统SysV服务并设置其开机自启动

# chkconfig --add httpd
# chkconfig --level 3 httpd on

这样,今后如需启动、停止、重启Apache就可以用以下方式了:

# service httpd start
# service httpd stop

五、安装过程中常见问题

(一)编译安装过程中的问题

    通常,第一次安装Apache没有什么问题,但以后安装,如果没有apr,apr-util,pcre,在执行./configure的配置过程中可能遇到的错误:
make[2]: *** [install] Error 1
make[2]: Leaving directory `/tmp/httpd-2.2.22/srclib/apr-util'
make[1]: *** [install-recursive] Error 1
make[1]: Leaving directory `/tmp/httpd-2.2.22/srclib'
make: *** [install-recursive] Error 1
    Apache2.0.x与Apache2.2.x在apr上有本质的区别,前者为依赖公用apr,后者依赖于自身的apr。2.0.x的编译基本上没有apr方面的问题,除非,在编译前,安装了非2.0.x所需的apr,如果是这样,则需要将已经安装的apr去除,然后再编译。HTTP Sever2.2.22修复了不少重要安全问题,包含APR(Apache Portable Runtime)1.4.5和APR-util(Apache Utility Library)1.4.2。因此不需要像网上其它教程那样从外部找源码安装apr和apr-util。将安装前已存在于系统中的apr去除后,再编译apache2.2.x自身srclib里的apr和apr-util。其它操作系统这样解决就没问题了,但是Ubuntu不太一样。
    安装完apr和apr-util后,./configure配置Apache时可能会出现下面错误:
configure: error: Cannot use an external APR with the bundled APR-util

configure: error: APR version 1.2.0 or later is required
【推荐完整步骤】
1. 安装apr

[root@localhost]# cd httpd-2.2.22
[root@localhost]# cd apr
[root@localhost]# ./configure --prefix=/usr/local/apr
[root@localhost]# make
[root@localhost]# make install

2. 安装apr-util

[root@localhost]# cd ..
[root@localhost]# cd apr-util
[root@localhost]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost]# make
[root@localhost]# make install

3. 安装pcre

tar xzf pcre-8.32.tar.gz
./configure –prefix=/usr/local/prce
make
make install 

4. 安装apache

[root@localhost]# cd /root/httpd-2.4.2
[root@localhost]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-dav --enable-mainer-mode --enable-rewrite
[root@localhost]# make
[root@localhost]# make install

说明:
    Apache作为开源服务器,在编译前需要了解系统的库安装情况,某些模块需要依赖于特定的库,如果这些库不存在,配置脚本将自动忽略这些库的编译。经过检测时候会生成合适的MakeFile文件。这里特别提醒一句,如果直接执行配置脚本,是不会编译额外的模块的,我们希望使用额外模块时,需要在运行配置脚本命令后加入参数,让其尽最大可能编译可用的库。
5. 启动apache

[root@localhost]:/root/httpd-2.4.2# /usr/local/apache/bin/apachectl start

(二)“service httpd does not support chkconfig”问题

    在 Redhat 上面使用编译的方式安裝 Apache后,将bin/apachectl复制到/etc/rc.d/init.d/目录,并想用chkconfig将Apache设定成自动启动,但出现了“service httpd does not support chkconfig”。
解決方法:
打开/etc/rc.d/init.d/httpd (或 /etc/init.d/httpd),在第二行加入以下两句:

# chkconfig: 2345 10 90
# description: Activates/Deactivates Apache Web Server

这样就会将httpd强制置换chkconfig。

(三)chkconfig --add httpd Error服务不支持

   将apachectl文件拷贝到/etc/rc.d/init.d 中,然后在/etc/rc.d/rc5.d/下加入链接即可。
命令如下:

cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd //如果有其他的版本的Apache存在,也可以直接覆盖掉

建立链接

ln -s /etc/rc.d/init.d/httpd /etc/rc.d/rc5.d/S85httpd //建立链接

此时Apache就可以自动启动了。 运行chkconfig --list,发现没有linux服务列表中httpd,通过chkconfig --add httpd来添加,但是提示:httpd服务不支持 chkconfig。需要编辑/etc/rc.d/init.d/httpd,添加以下注释信息: 

# chkconfig: 345 85 15
# description: Activates/Deactivates Apache Web Server 

    第一行3个数字参数意义分别为:哪些Linux级别需要启动httpd(3,4,5);启动序号(85);关闭序号(15)。
    保存后执行:chkconfig --add httpd,成功添加。
    在rc3.d、rc4.d、rc5.d路径中会出现S85httpd的链接文件,其他运行级别路径中会出现K61httpd的链接文件。

抱歉!评论已关闭.