LAMP——httpd 2.4.20 + mysql-5.6.26 + php-5.6.22编译安装过程

httpd 2.4.20 + mysql-5.6.26 + php-5.6.22编译安装过程:
php

资源准备html

系统:centos6.5
mysql

PHP:wget http://docs.php.net/distributions/php-5.6.22.tar.gzlinux

MySQL:wget http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.26.tar.gz正则表达式

APACHE:wget http://mirrors.noc.im/apache//httpd/httpd-2.4.20.tar.gzsql

Xcache:wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz数据库

APR: wget http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gzapache

        wget http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.gzvim

1、编译安装apachecentos

一、解决依赖关系

httpd-2.4.20须要较新版本的apr和apr-util,所以须要事先对其进行升级。升级方式有两种,一种是经过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行

wget http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz

wget http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.gz


(1) 编译安装apr

# tar zxf apr-1.5.2.tar.gz

# cd apr-1.5.2

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

# make && make install

(2) 编译安装apr-util

# tar zxf apr-util-1.5.4.tar.gz

# cd apr-util-1.5.4

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

# make && make install

附:apache官方对APR的介绍:

The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.

(3) httpd-2.4.20编译过程也要依赖于pcre-devel软件包,须要事先安装。此软件包系统光盘自带,所以,找到并安装便可。

二、编译安装httpd-2.4.20

首先下载httpd-2.4.20到本地然后执行以下命令进行编译安装过程:

# tar zxf httpd-2.4.20.tar.gz

# cd httpd-2.4.20

# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-deflate--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

# make && make install


httpd编译选项:

--prefix=/usr/local/apache

--sysconfdir=/etc/httpd/

--enable-so(支持动态共享模块,没有此项的话php没法以模块方式与httpd结合工做)

--enable-ssl(支持https

--enable-deflate(响应client时将结果压缩,节约带宽)

--enable-proxy-fcgi(若httpdphp结合使用FastCGI此项要启用)

--enable-mpm-shared=all

--with-mpm=event(设置默认是哪一种,不指默认是event

--enable-cgidthread MPMs

--enable-cgi(非thread

--enable-rewrite(支持URL重写)

--enable-modules=most(编译FastCGI,后续要手工启用)

--enable-mods-shared=most

--with-apr=/usr/local/apr

--with-apr-util=/usr/local/apr-util



错误:configure: error: pcre-config for libpcre not found

解决:

#yum  -y  install pcre-devel(Perl Compatible Regular Expressions是一个Perl库,包括 perl兼容的正则表达式库,devel包含普通包,只比普通包多了头文件,动态连接库的话两种包都有,编译的时候若是须要用到这个库,那么须要安装这个库的devel,由于须要头文件)

错误:configure: error: MPM most does not support dynamic loading.

解决:--enable-mpm-shared=all

错误:启动服务时undefined symbol:apr……

--with-included-apr --with-apr=/usr/local/apr/bin/apr-1-config--with-apr-util=/usr/local/apr-util/bin/apu-1-config(指向具体位置)



补充:

(1)构建MPM为静态模块

在所有平台中,MPM均可以构建为静态模块。在构建时选择一种MPM,连接到服务器中。若是要改变MPM,必须从新构建。为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可使用 ./httpd -l 来肯定选择的MPM。 此命令会列出编译到服务器程序中的全部模块,包括 MPM。

(2)构建 MPM 为动态模块

在Unix或相似平台中,MPM能够构建为动态模块,与其它动态模块同样在运行时加载。 构建 MPM 为动态模块容许经过修改LoadModule指令内容来改变MPM,而不用从新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项便可启用此特性。当给出的参数为all时,全部此平台支持的MPM模块都会被安装。还能够在参数中给出模块列表。默认MPM,能够自动选择或者在执行configure脚本时经过--with-mpm选项来指定,而后出如今生成的服务器配置文件中。编辑LoadModule指令内容能够选择不一样的MPM。

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

编辑vim /etc/httpd/httpd.conf,添加以下行便可:

PidFile  "/var/run/httpd.pid"

四、提供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

接下来就能够启动服务进行测试了。

2、安装mysql-5.5.26

一、准备数据存放的文件系统

新建一个逻辑卷,并将其挂载至特定目录便可。这里再也不给出过程。

这里假设其逻辑卷的挂载目录为/mydata,然后须要建立/mydata/data目录作为mysql数据的存放目录。

二、新建用户以安全方式运行进程:

# groupadd -r mysql

# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

# chown -R mysql:mysql /mydata/data

三、安装并初始化mysql-5.5.26

首先下载平台对应的mysql版本至本地,这里是32位平台,所以,选择的为mysql-5.5.28-linux2.6-86.tar.gz

# tar xf mysql-5.5.28-linux2.6-86.tar.gz -C /usr/local

# cd /usr/local/

# ln -sv mysql-5.5.28-linux2.6-i686  mysql

# cd mysql 

#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1


#chown -R mysql:mysql  .

# scripts/mysql_install_db --user=mysql --datadir=/mydata/data  --basedir=/usr/local/mysql

# chown -R root  .

四、为mysql提供主配置文件:

# cd /usr/local/mysql

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

并修改此文件中thread_concurrency的值为你的CPU个数乘以2,好比这里使用以下行:

thread_concurrency = 2

另外还须要添加以下行指定mysql数据文件的存放位置:

datadir = /mydata/data

五、为mysql提供sysv服务脚本:

# cd /usr/local/mysql

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

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

添加至服务列表:

# chkconfig --add mysqld

# chkconfig mysqld on

vim /etc/my.cnf

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

# These are commonly set, remove the # and set as required.

basedir = /usr/local/mysql

datadir = /mydata/data

port = 3306

# server_id = .....

socket = /var/run/mysqld/mysqld.pid

去掉如上行的注释

[root@localhost mysql]# service mysqld start

Starting MySQL....... SUCCESS!

然后就能够启动服务测试使用了。

为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还须要进行以下步骤:

六、输出mysql的man手册至man命令的查找路径:

编辑/etc/man.config,添加以下行便可:

MANPATH  /usr/local/mysql/man

七、输出mysql的头文件至系统头文件路径/usr/include:

这能够经过简单的建立连接实现:

# ln -sv /usr/local/mysql/include  /usr/include/mysql

八、输出mysql的库文件给系统库查找路径:

# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

然后让系统从新载入系统库:

# ldconfig

九、修改PATH环境变量,让系统能够直接使用mysql的相关命令。具体实现过程这里再也不给出。

3、编译安装php-5.6.22

一、解决依赖关系:

请配置好yum源(能够是本地系统光盘)后执行以下命令:

# yum -y groupinstall "X Software Development" 

若是想让编译的php支持mcrypt扩展,此处还须要下载以下包并安装之:

yum -y install libmcrypt* bzip2* libjpeg* pcre* png*

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

二、编译安装php-5.6.22

首先下载源码包至本地目录,下载位置:wget http://docs.php.net/distributions/php-5.6.22.tar.gz

# tar zvxf php-5.6.22.tar.gz

# cd php-5.6.22

# ./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 --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2  --enable-maintainer-zts --with-bz2 --with-curl --with-gd   --with-mhash --with-mcrypt --enable-fpm --enable-sockets --enable-sysvshm --enable-fpm --enable-sockets --enable-sysvshm  --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd


报错:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

解决代码以下:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install


说明:

一、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。

二、若是使用PHP5.3以上版本,为了连接MySQL数据库,能够指定mysqlnd,这样在本机就不须要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,能够编译时绑定到它(而不用和具体的MySQL客户端库绑定造成依赖),但从PHP 5.4开始它就是默认设置了。

# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd


# make

# make test

# make intall

为php提供配置文件:

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

三、 编辑apache配置文件httpd.conf,以apache支持php

 # vim /etc/httpd/httpd.conf

 一、添加以下二行

AddType application/x-httpd-php  .php

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

 二、定位至DirectoryIndex index.html 

   修改成:

    DirectoryIndex  index.php  index.html

然后从新启动httpd,或让其从新载入配置文件便可测试php是否已经能够正常使用。

4、安装xcache,为php加速:

一、安装

# tar xf xcache-3.2.0..tar.gz

# cd xcache-3.2.0

# /usr/local/php/bin/phpize

#phpize --clean && phpize

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

# make && make install

安装结束时,会出现相似以下行:

Build complete.

Don't forget to run 'make test'.

Installing shared extensions:     /usr/lib64/php/modules/


二、编辑php.ini,整合php和xcache:

首先将xcache提供的样例配置导入php.ini

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

说明:xcache.ini文件在xcache的源码目录中。

接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改成以下行:

zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

注意:若是php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。

5、启用服务器状态

mod_status模块可让管理员查看服务器的执行状态,它经过一个HTML页面展现了当前服务器的统计数据。这些数据一般包括但不限于:

(1) 处于工做状态的worker进程数;

(2) 空闲状态的worker进程数;

(3) 每一个worker的状态,包括此worker已经响应的请求数,及由此worker发送的内容的字节数;

(4) 当前服务器总共发送的字节数;

(5) 服务器自上次启动或重启以来至当前的时长;

(6) 平均每秒钟响应的请求数、平均每秒钟发送的字节数、平均每一个请求所请求内容的字节数;

启用状态页面的方法很简单,只须要在主配置文件中添加以下内容便可:

<Location /server-status>

    SetHandler server-status

    Require all granted

</Location>

须要提醒的是,这里的状态信息不该该被全部人随意访问,所以,应该限制仅容许某些特定地址的客户端查看。好比使用Require ip 172.16.0.0/16来限制仅容许指定网段的主机查看此页面。

第二部分、配置apache-2.4.20与fpm方式的php-5.6.22

1、apache、MySQL的安装与前一部分相同;请根据其进行安装;

2、编译安装php-5.6.22

一、解决依赖关系:

yum -y install php-fpm

请配置好yum源(能够是本地系统光盘)后执行以下命令:

若是想让编译的php支持mcrypt扩展下载包并安装:

libmcrypt-2.5.7-5.el5.i386.rpm

libmcrypt-devel-2.5.7-5.el5.i386.rpm

mhash-0.9.9-1.el5.centos.i386.rpm

mhash-devel-0.9.9-1.el5.centos.i386.rpm

二、编译安装php-5.6.22

首先下载源码包至本地目录

# tar xf php-5.6.22.tar.gz

# cd php-5.6.22

# ./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

说明:若是使用PHP5.3以上版本,为了连接MySQL数据库,能够指定mysqlnd,这样在本机就不须要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,能够编译时绑定到它(而不用和具体的MySQL客户端库绑定造成依赖),但从PHP 5.4开始它就是默认设置了。

# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

# make

# make intall

为php提供配置文件:

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

三、配置php-fpm

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

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

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

# chkconfig --add php-fpm

# chkconfig php-fpm on

为php-fpm提供配置文件:

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

最新版本PHP在sapi/fpm/php-fpm.conf已经存在不须要改动

编辑php-fpm的配置文件:

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

配置fpm的相关选项为你所须要的值,并启用pid文件(以下最后一行):

pm.max_children = 50

pm.start_servers = 5

pm.min_spare_servers = 2

pm.max_spare_servers = 8

pid = /usr/local/php/var/run/php-fpm.pid 

接下来就能够启动php-fpm了:

# service php-fpm start

使用以下命令来验正(若是此命令输出有中几个php-fpm进程就说明启动成功了):

# ps aux | grep php-fpm

默认状况下,fpm监听在127.0.0.1的9000端口,也可使用以下命令验正其是否已经监听在相应的套接字。

# netstat -tnlp | grep php-fpm

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      689/php-fpm 


3、配置httpd-2.4.20

一、启用httpd的相关模块

vim /etc/httpd/httpd.conf

在Apache httpd 2.4之后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它实际上是做为mod_proxy.so模块的扩充,所以,这两个模块都要加载

119行

LoadModule proxy_module modules/mod_proxy.so

123行

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

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

在相应的虚拟主机中添加相似以下两行。

    ProxyRequests Off

    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

例如:

<VirtualHost *:80>

    DocumentRoot "/www/magedu.com"

    ServerName magedu.com

    ServerAlias www.geek3.com

    ProxyRequests Off

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

    <Directory "/www/magedu.com">

        Options none

        AllowOverride none

        Require all granted

    </Directory>

</VirtualHost>

ProxyRequests Off:关闭正向代理

ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少须要知道运行的目录和URI,因此这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不须要手动指定。

三、编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持php格式的主页

 # vim /etc/httpd/httpd.conf

 一、添加以下二行

   AddType application/x-httpd-php  .php

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

 二、定位至DirectoryIndex index.html 

   修改成:

    DirectoryIndex  index.php  index.html

补充:Apache httpd 2.4之前的版本中,要么把PHP做为Apache的模块运行,要么添加一个第三方模块支持PHP-FPM实现。

相关文章
相关标签/搜索