---
title: "apache2.2.4  PHP 环境的安装及扩展配置"
author: "jontan"
date: "2014-01-08T22:18:24Z"
tags: []
categories: ["企业架构"]
slug: "apache2-2-4-php"
draft: false
lastmod: "2014-01-08T22:18:24Z"
id: 2001557
url: "https://summary.iteye.com/blog/2001557"
---

**apache2.2.4的安装及扩展配置**

 

**当前系统**

redhat9

apache2.0.54  /usr/local/apache2

mysql5.0.16  /usr/local/mysql

php 5.0.4    /usr/local/php5

 

现在需要安装apache2.2.4

过程如下

**编译**:安装目录为--prefix=/usr/local/apache22

./configure --prefix=/usr/local/apache22 --with-layout=apache --enable-module=so --enable-module=setenvif --enable-module=rewrite --with-mpm=prefork --enable-ssl

**安装**

make

make install

 

在安装了apache2.2.4之后,直接启动,会报有关ServerName的警告,但是还是可以启动的.通过客户端访问看到的默认页面是一句话 it works

在配置文件中修改如下:

#ServerName \[url]www.example.com:80\[/url]

ServerName 127.0.0.1

就不会报错了

 

修改网站主目录如下

#DocumentRoot "/usr/local/apache22/htdocs"

DocumentRoot "/var/www/html"

访问的时候会报错

403 禁止访问

您无权查看该网页

您可能没有权限用您提供的凭据查看此目录或网页

例如在网站的主目录/var/ww/html下有1.html.在客户端访问\[url]http://IP/1.html\[/url]就会报这个错.

解决办法:

&lt;Directory /&gt;

    Options FollowSymLinks

    AllowOverride None

#    Order deny,allow

#    Deny from all

   Order allow,deny

   Allow from all

&lt;/Directory&gt;

 

分析错误原因

查看配置文件httpd.conf,相关部分内容如下:

\# Each directory to which Apache has access can be configured with respect

\# to which services and features are allowed and/or disabled in that

\# directory (and its subdirectories).

\#

\# First, we configure the "default" to be a very restrictive set of

\# features.

\# 首先配置的缺省的限制,这个限制是非常严格的

&lt;Directory /&gt;

    Options FollowSymLinks

    AllowOverride None

   Order deny,allow  次序是先拒绝,再允许

    Deny from all  默认是拒绝所有

&lt;/Directory&gt;

 

\#

\# Note that from this point forward you must specifically allow

\# particular features to be enabled - so if something's not working as

\# you might expect, make sure that you have specifically enabled it

\# below.

#从这里开始你必须指定允许启用特定的功能,如果某些不能正常工作,需要查看你是否已经启用了它.

 

\#

\# This should be changed to whatever you set DocumentRoot to.

#这里应该改为你设的DocumentRoot

&lt;Directory "/usr/local/apache22/htdocs"&gt;  可以看到这里是对缺省的主目录的设置

    #

    # Possible values for the Options directive are "None", "All",

    # or any combination of:

    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

    #

    # Note that "MultiViews" must be named \*explicitly* --- "Options All"

    # doesn't give it to you.

    #

    # The Options directive is both complicated and important.  Please see

    # \[url]http://httpd.apache.org/docs/2.2/mod/core.html#options\[/url]

    # for more information.

    #

    Options Indexes FollowSymLinks

 

    #

    # AllowOverride controls what directives may be placed in .htaccess files.

    # It can be "All", "None", or any combination of the keywords:

    #   Options FileInfo AuthConfig Limit

    #

    AllowOverride None

 

    #

    # Controls who can get stuff from this server.

    #控制谁能访问这个网站

    Order allow,deny  顺序是先允许再拒绝

    Allow from all  默认是允许所有

 

&lt;/Directory&gt;

 

所以缺省状态下只是对默认的主目录/usr/local/apache22/htdocs设的是允许所有访问,而对于其他的目录,采用默认的设置是拒绝所有访问的.

所以我们之前做的修改

&lt;Directory /&gt;

    Options FollowSymLinks

    AllowOverride None

#    Order deny,allow

#    Deny from all

   Order allow,deny

   Allow from all

&lt;/Directory&gt;

是将默认的限制调大了,让默认对所有的目录都允许所有人访问.或许会带来安全威胁.

安全的做法是按照文档说的

\# This should be changed to whatever you set DocumentRoot to.

#这里应该改为你设的DocumentRoot

&lt;Directory "/usr/local/apache22/htdocs"&gt; 把它改为你设的主目录

启动以后就可以正常访问了.

 

**做多端口的虚拟主机**

2.2.4里面一个很大的不同就是将很多的配置以单独的文件存放(路径是conf/extra),如ssl,虚拟主机vhost等.在使用的时候先要在主配置文件里面包含此配置文件,然后到相应的配置文件里面去具体配置.下面来配置虚拟主机

修改httpdconf,增加监听端口

Listen 80

Listen 81

 

启用虚拟主机配置

\# Virtual hosts

#Include conf/extra/httpd-vhosts.conf

Include conf/extra/httpd-vhosts.conf

 

编辑配置文件目录下的/extra/httpd-vhosts.conf

\[root@server1 extra]# vi httpd-vhosts.conf

 

\#

\# Virtual Hosts

\#

\# If you want to maintain multiple domains/hostnames on your

\# machine you can setup VirtualHost containers for them. Most configurations

\# use only name-based virtual hosts so the server doesn't need to worry about

\# IP addresses. This is indicated by the asterisks in the directives below.

\#

\# Please see the documentation at

\# &lt;URL:\[url]http://httpd.apache.org/docs/2.2/vhosts/&gt;\[/url]

\# for further details before you try to setup virtual hosts.

\#

\# You may use the command line option '-S' to verify your virtual host

\# configuration.

 

\#

\# Use name-based virtual hosting.

\#

NameVirtualHost \*:80

 

\#

\# VirtualHost example:

\# Almost any Apache directive may go into a VirtualHost container.

\# The first VirtualHost section is used for all requests that do not

\# match a ServerName or ServerAlias in any &lt;VirtualHost&gt; block.

\#

&lt;VirtualHost \*:80&gt;

    ServerAdmin [webmaster@dummy-host.example.com](mailto:webmaster@dummy-host.example.com)

    DocumentRoot /www/docs/dummy-host.example.com

    ServerName dummy-host.example.com

    ServerAlias \[url]www.dummy-host.example.com\[/url]

    ErrorLog logs/dummy-host.example.com-error\_log

    CustomLog logs/dummy-host.example.com-access\_log common

&lt;/VirtualHost&gt;

 

&lt;VirtualHost \*:80&gt;

    ServerAdmin [webmaster@dummy-host2.example.com](mailto:webmaster@dummy-host2.example.com)

    DocumentRoot /www/docs/dummy-host2.example.com

    ServerName dummy-host2.example.com

    ErrorLog logs/dummy-host2.example.com-error\_log

    CustomLog logs/dummy-host2.example.com-access\_log common

&lt;/VirtualHost&gt;

 

做如下修改

\[root@server1 extra]# vi httpd-vhosts.conf

 

\#

\# Virtual Hosts

\#

\# If you want to maintain multiple domains/hostnames on your

\# machine you can setup VirtualHost containers for them. Most configurations

\# use only name-based virtual hosts so the server doesn't need to worry about

\# IP addresses. This is indicated by the asterisks in the directives below.

\#

\# Please see the documentation at

\# &lt;URL:\[url]http://httpd.apache.org/docs/2.2/vhosts/&gt;\[/url]

\# for further details before you try to setup virtual hosts.

\#

\# You may use the command line option '-S' to verify your virtual host

\# configuration.

 

\#

\# Use name-based virtual hosting.

\#

#NameVirtualHost \*:80  注释掉此句的原因见我有关虚拟主机的文章

 

\#

\# VirtualHost example:

\# Almost any Apache directive may go into a VirtualHost container.

\# The first VirtualHost section is used for all requests that do not

\# match a ServerName or ServerAlias in any &lt;VirtualHost&gt; block.

\#

&lt;VirtualHost \*:80&gt;

    ServerAdmin [webmaster@dummy-host.example.com](mailto:webmaster@dummy-host.example.com)

    DocumentRoot /var/www/html/s1

    ServerName dummy-host.example.com

    ServerAlias \[url]www.dummy-host.example.com\[/url]

    ErrorLog logs/dummy-host.example.com-error\_log

    CustomLog logs/dummy-host.example.com-access\_log common

&lt;/VirtualHost&gt;

 

&lt;VirtualHost \*:81&gt;

    ServerAdmin [webmaster@dummy-host2.example.com](mailto:webmaster@dummy-host2.example.com)

    DocumentRoot /var/www/html/s2

    ServerName dummy-host2.example.com

    ErrorLog logs/dummy-host2.example.com-error\_log

    CustomLog logs/dummy-host2.example.com-access\_log common

&lt;/VirtualHost&gt;

 

**配置ssl**

\# Secure (SSL/TLS) connections

#Include conf/extra/httpd-ssl.conf

Include conf/extra/httpd-ssl.conf

 

将之前生成的server的那些证书文件copy到conf目录下(因为是按照httpd-ssl.conf文件中的证书路径和名称配置的)

 

 \[root@server1 conf]# ../bin/httpd -S

VirtualHost configuration:

wildcard NameVirtualHosts and \_default_ servers:

\*:80                   dummy-host.example.com (/usr/local/apache22/conf/extra/httpd-vhosts.conf:27)

\*:81                   dummy-host2.example.com (/usr/local/apache22/conf/extra/httpd-vhosts.conf:36)

\_default\_:443          \[url]www.example.com\[/url] (/usr/local/apache22/conf/extra/httpd-ssl.conf:74)

Syntax OK

 

重启服务以后通过https访问

 

**安装php**

原来想用现成的php,不想重新编译安装php.毕竟我感觉需要的也只是那个so文件而已.

首先我想用原来的apache那个php的模块文件,将他copy到现在的模块目录

cp /usr/local/apache2/modules/libphp5.so /usr/local/apache2/modules/libphp5.so

然后修改httpd.conf来支持php

\# Example:

\# LoadModule foo\_module modules/mod\_foo.so

\#

LoadModule php5\_module        modules/libphp5.so

AddType application/x-httpd-php .php

AddType application/x-httpd-php-source .phps

 

\# DirectoryIndex: sets the file that Apache will serve if a directory

\# is requested.

\#

&lt;IfModule dir\_module&gt;

    DirectoryIndex index.html index.php

&lt;/IfModule&gt;

 

启动报错

\[root@server1 conf]# ../bin/httpd -k start

httpd: Syntax error on line 54 of /usr/local/apache22/conf/httpd.conf: API module structure \`php5\_module' in file /usr/local/apache22/modules/libphp5.so is garbled - perhaps this is not an Apache module DSO?

唉,失败了.**(****个人觉得如果是相同版本的apache应该是可以的copy使用的,但未测试**)

 

经过sery指点想用apxs独立生成PHP的dso模块.可就是不知道该编译PHP里面哪个文件是模块的源文件.只得放弃.没办法了重新编译安装一个PHP.(如果有谁知道如何用apxs来生成php的dso模块而不用编译安装php就告诉我一声哈.)

其实安装的过程与apache2.0.x一样.我写在这里留给自己以后参考

**编译php**:新装在另一个目录php5.04(这是为了不影响现有php的运行,节省空间的话就直接安装在原目录下),指向的apache也是新的apache的路径.

./configure --prefix=/usr/local/php5.04 --with-apxs2=/usr/local/apache22/bin/apxs --enable-track-vars  --enable-url-includes --enable-sockets --enable-force-cgi-redirect --enable-calendar --with-config-file-path=/usr/local/lib --with-jpeg-dir=/usr/local/jpeg6 --enable-gd-native-ttf --with-ttf --with-gdbm --with-gettext --with-iconv --with-png-dir=/usr/local/libpng2 --with-freetype-dir=/usr/local/freetype2 --with-libxml --with-zlib --with-zlib-dir=/usr/local/zlib2 --with-gd --enable-soap --with-curl --with-curlwrappers --with-java=/usr/java/j2sdk1.4.2\_14 --with-mysql=/usr/local/mysql/

注意--with-mysql=/usr/local/mysql/,我之前使用的是—with-mysql这一步过不去,我停掉mysql还是不行,关掉apache也不行,所以就用这个来明确指向mysql的安装目录就搞定了

 

安装PHP

make

make install

报错如下

Installing PHP SAPI module:       apache2handler

make: execvp: /home/yahoon/php-5.0.4/build/shtool: Permission denied

make: \[install-sapi] Error 127 (ignored)

/bin/sh: line 1: /home/yahoon/php-5.0.4/build/shtool: Permission denied

make: \*\** \[install-sapi] Error 126

知道是权限问题,把这个文件提权

\[root@server1 php-5.0.4]# chmod 777 build/shtool

\[root@server1 php-5.0.4]# make install

最后的信息为

Installing PHP SAPI module:       apache2handler

/usr/local/apache22/build/instdso.sh SH\_LIBTOOL='/usr/local/apache22/build/libtool' libphp5.la /usr/local/apache22/modules

/usr/local/apache22/build/libtool --mode=install cp libphp5.la /usr/local/apache22/modules/

cp .libs/libphp5.so /usr/local/apache22/modules/libphp5.so

cp .libs/libphp5.lai /usr/local/apache22/modules/libphp5.la

libtool: install: warning: remember to run \`libtool --finish /home/yahoon/php-5.0.4/libs'

chmod 755 /usr/local/apache22/modules/libphp5.so

\[activating module \`php5' in /usr/local/apache22/conf/httpd.conf]

Installing PHP CLI binary:        /usr/local/php5.04/bin/

Installing PHP CLI man page:      /usr/local/php5.04/man/man1/

Installing PEAR environment:      /usr/local/php5.04/lib/php/

\[PEAR] Archive\_Tar    - installed: 1.1

\[PEAR] Console\_Getopt - installed: 1.2

\[PEAR] PEAR           - installed: 1.3.5

Wrote PEAR system config file at: /usr/local/php5.04/etc/pear.conf

You may want to add: /usr/local/php5.04/lib/php to your php.ini include\_path

\[PEAR] HTML\_Template\_IT- installed: 1.1

\[PEAR] Net\_UserAgent\_Detect- installed: 2.0.1

\[PEAR] XML\_RPC        - installed: 1.2.2

Installing build environment:     /usr/local/php5.04/lib/php/build/

Installing header files:          /usr/local/php5.04/include/php/

Installing helper programs:       /usr/local/php5.04/bin/

  program: phpize

  program: php-config

  program: phpextdist

红色的字说明了像apache的目录写入模块的过程. \[activating module \`php5' in /usr/local/apache22/conf/httpd.conf]这句话引起了我的注意.看来并不用手动添加对php的支持它会自动修改配置文件来激活模块.

到/usr/local/apache22/conf/下看到出现了一个httpd.conf.bak,即它将原来的文件备份了.

这样重启apache就能访问php的页面了.

 

其实这时新安装的php路径/usr/local/php5.04即使删掉这个目录也是可以正常工作的.因为我们需要的so文件已经在apache的modules目录下了