编译安装LAMP之配置httpd以FastCGI方式与php整合
更新:HHH   时间:2023-1-7


一、编译安装httpd(httpd 2.4.4,相关软件包可到apache官网上下载)

# hwclock -s 将软件时间同步为硬件时间,防止安装软件时出错

1、解决依赖关系

httpd-2.4.4需要较新版本的aprapr-util,因此需要事先对其进行升级。这里使用源码包进行升级(apr-1.5.2apr-util-1.5.4

(1) 编译安装apr

# tar xf apr-1.5.2.tar.bz2

# cd apr-1.5.2

# ./configure --prefix=/usr/local/apr

# make && make install

(2) 编译安装apr-util

# tar xf apr-util-1.5.4.tar.bz2

# cd apr-util-1.5.4

# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

# make && make install

(3) httpd-2.4.4编译过程也要依赖于pcre-devel软件包,需要事先安装。

#yum -y install pcre-devel

(4) 可在编译安装httpd时会报错:checking for OpenSSL version >=0.9.7 ... FAILED

#yum -y install openssl-devel

#yum update openssl

2、编译安装httpd-2.4.4

# tar xf httpd-2.4.4.tar.bz2

# cd httpd-2.4.4

# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event

(含义:--perfix=/usr/local/apache 指定安装路径;--sysconfdir=/etc/httpd 指定配置文件路径;--enable-so  apache核心装载DSO,但实际不编译任何动态模块;--enable-ssl 支持ssl模块;--enable-cgi支持cgi模块;--enable-rewrite 支持url重写;--with-zlib支持zlib库文件;--with-pcre 包含pcre函数;--with-apr=/usr/local/apr 指定apr的路径;--with-apr-util=/usr/local/apr-util 指明apr-util路径;--enable-modules=most 支持模块;支持共享模块;--with-mpm=event加载模块event

# make && make install

3、修改httpd的主配置文件,设置其Pid文件的路径

#vim /etc/httpd/httpd.conf,添加如下行即可:

PidFile  "/var/run/httpd.pid"

4、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:

#!/bin/bash

#

# httpd        Startup script for the Apache HTTP Server

#

# chkconfig: - 85 15

# description: Apache is a World Wide Web server.  It is used to serve \

#       HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

 

# Source function library.

. /etc/rc.d/init.d/functions

 

if [ -f /etc/sysconfig/httpd ]; then

        . /etc/sysconfig/httpd

fi

 

# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"C"}

 

# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""

 

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.

 

# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

 

start() {

        echo -n $"Starting $prog: "

        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && touch ${lockfile}

        return $RETVAL

}

 

stop() {

echo -n $"Stopping $prog: "

killproc -p ${pidfile} -d 10 $httpd

RETVAL=$?

echo

[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

reload() {

    echo -n $"Reloading $prog: "

    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

        RETVAL=$?

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

    fi

    echo

}

 

# See how we were called.

case "$1" in

  start)

start

;;

  stop)

stop

;;

  status)

        status -p ${pidfile} $httpd

RETVAL=$?

;;

  restart)

stop

start

;;

  condrestart)

if [ -f ${pidfile} ] ; then

stop

start

fi

;;

  reload)

        reload

;;

  graceful|help|configtest|fullstatus)

$apachectl $@

RETVAL=$?

;;

  *)

echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

exit 1

esac

 

exit $RETVAL

 

而后为此脚本赋予执行权限并加入服务列表:

# chmod +x /etc/rc.d/init.d/httpd

# chkconfig --add httpd

5.提供执行相关命令所需的环境变量

#vim /etc/profile.d/httpd.sh,添加以下内容:

export PATH=$PATH:/usr/local/apache/bin

6.至此,http服务配置结束,可以启动测试了

#service httpd restart

二、安装MySQL(我这里通过编译安装MySQL-5.6.33(通用二进制格式))

1.将下载好的压缩包解压至/usr/local,并进入此目录

# tar xf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz -C /usr/local

# cd /usr/local/

2.为解压后的目录创建一个链接,并进入此目录

# ln -sv mysql-5.6.33-linux-glibc2.5-x86_64 mysql

# cd mysql

3.创建MySQL用户(使其成为系统用户)和MySQL

# groupadd -r -g 306 mysql

# useradd -g 306 -r -u 306 mysql

4.使mysql下的所有文件都属于mysql用户和mysql

# chown -R mysql.mysql /usr/local/mysql/*

5.提前准备一个分区用来创建逻辑卷(我这里用/dev/sda5),在此分区上创建逻辑卷并使其可以开机自动挂载使用。

# pvcreate /dev/sda5

# vgcreate myvg /dev/sda5

# lvcreate -n mydata -L 512M myvg

# mkfs.ext4 /dev/myvg/mydata

#vim /etc/fstab,添加以下内容:

/dev/myvg/mydata    /mydata   ext4    defaults        0 0

# mkdir /mydata

# mount -a

6.创建数据目录,并使其属于mysql用户和mysql组,其他人无权限

# mkdir /mydata/data

# chown -R mysql:mysql /mydata/data/

# chmod o-rw /mydata/data/

7.准备安装所需的开发环境

# yum -y install perl-Data-Dumper.x86_64

8.准备就绪,开始安装

# scripts/mysql_install_db --user=mysql --datadir=/mydata/data

9.安装完成后为了安全,更改/usr/local/mysql下所有文件的权限

#chown -R root .

10.准备启动脚本,并使其开机自动启动

# cp support-files/mysql.server /etc/init.d/mysqld

# chkconfig --add mysqld

# chkconfig --list mysqld

11.编辑数据库配置文件

# cp support-files/my-default.cnf /etc/my.cnf

#vim /etc/my.cnf,修改和添加以下内容:

datadir = /mydata/data

12.提供执行相关命令所需的环境变量

# vim /etc/profile.d/mysql.sh

添加以下内容:

export PATH=$PATH:/usr/local/mysql/bin

13.至此,MySQL服务配置完成,可以启动测试

# systemctl start mysqld

三、编译安装php-5.5.38:(相关包可以到http://www.php.net/ 下载)

1.安装前准备:

1)想让编译的php支持mcrypt扩展,需要装两个包(这两个包需要额外下载):

# rpm -ivh libmcrypt-2.5.8-13.el7.x86_64.rpm

# rpm -ivh libmcrypt-devel-2.5.8-13.el7.x86_64.rpm

2)如果出现出现configure:error:xml2-config not found错误:

# yum -y install libxml2

#yum -y install libxml2-devel

2.将源码包下载至本地,开始安装

# tar xf php-5.5.38.tar.bz2

#cd php-5.5.38

#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets  --enable-fpm  --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  

(释义:

--prefix=/usr/local/php指定php安装目录;--with-mysql=/usr/local/mysqlmysql安装目录,对mysql的支持;--with-openssl增加openssl的支持;--with-mysqli=/usr/local/mysql/bin/mysql_configmysqlin扩展技术,不仅可以调用MySQL的存储过程,处理MySQL事物,而且可以使访问数据库工作变得稳定;

--enable-mbstring多字节字符串的支持;

--with-freetype-dir打开对freetype字体库的支持;

--with-jpeg-dirjpeg格式图片的支持;

--with-png-dir打开对png图片的支持;

--with-zlib打开对zlib库的支持;

--with-libxml-dir=/usr打开libxml2库的支持;

--enable-xml打开对xml的支持;

--enable-sockets打开socket支;--with-apxs2=/usr/local/apache/bin/apxs;整合apache,apxs功能是使用mod_so中的LoadModule指令,加载指定模块到apache,要求apache 要打开SO模块;

--with-mcrypt算法;

--with-config-file-path=/etc指定php.ini(配置文件)位置;--with-config-file-scan-dir=/etc/php.d是搜索下面的ini文件php.ini一起使用;

--with-bz2打开对bz2文件的支持;

#make

#make install

3.php提供配置文件

# cp php.ini-production /etc/php.ini

4.配置php-fpm

为php-fpm提供Sysv init脚本,并将其添加至服务列表:

# cp sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm

# chmod +x /etc/rc.d/init.d/php-fpm

# chkconfig --add php-fpm

5.为php-fpm提供配置文件:

# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

6.编辑php-fpm的配置文件:

# vim /usr/local/php/etc/php-fpm.conf

配置fpm的相关选项为你所需要的值

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 8

7.接下来就可以启动php-fpm了:

# service php-fpm start

四、配置虚拟主机支持使用fcgi

1、注释中心主机,启用虚拟主机,并启用mod_proxy_fcgi.somod_proxy.so模块

(在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载)

# vim /etc/httpd/httpd.conf

注释:DocumentRoot "/usr/local/apache/htdocs"

启用:Include /etc/httpd/extra/httpd-vhosts.conf

      LoadModule proxy_module modules/mod_proxy.so

  LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

添加:AddType application/x-httpd-php .php

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

修改:DirectoryIndex index.html index.php

2、需要先创建目录:/www/a.org/,并在其中提供测试页面

# mkdir -p /www/a.org

# vim index.php

<h2>my first </h2>

<?php

phpinfo();

?>

3、编辑虚拟主机配置文件,并检查有无语法错误

# vim /etc/httpd/extra/httpd-vhosts.conf

改为如下内容

<VirtualHost *:80>

    DocumentRoot "/www/a.org"

    ServerName www.a.org

    ProxyRequests Off(关闭正向代理功能)

    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/a.org/$1

    <Directory "/www/a.org">

        Options none

        AllowOverride none

        Require all granted

    </Directory>

    ErrorLog "logs/dummy-host.example.com-error_log"

    CustomLog "logs/dummy-host.example.com-access_log" common

</VirtualHost>

# httpd -t

4、现在即可重启httpd,进行测试了!

# systemctl restart httpd

五、安装xcache,为php加速(我这里用xcache-3.1.2)

1.下载源码,解压

# tar xf xcache-3.1.2.tar.bz2

# cd xcache-3.1.2

2.使php加载xcache扩展

# /usr/local/php/bin/phpize

在此处可能遇到报错:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable.Then,return this script

原因:Autoconf是一个用于生成可以自动地配置软件源代码包以适应多种Unix类系统的 shell脚本的工具,需要安装。

解决办法:

# cd /usr/src/

#wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz

# tar -zvxf m4-1.4.9.tar.gz

# cd m4-1.4.9/

# ./configure && make && make install

# cd ../

# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz

# tar -zvxf autoconf-2.62.tar.gz

# cd autoconf-2.62/

# ./configure && make && make install

3.编译安装

#./configure --enable-xcache -with-php-config=/usr/local/php/bin/

php-config

#make

#make install

结束后会生成:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20121212/

4.编辑配置文件

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d/

5.重启php-fpm服务

# service php-fpm restart

此时访问到的页面会有xcache的扩展!


返回web开发教程...