; lamp平台 php解析器基于模块和php-fpm | Linux运维部落

lamp平台 php解析器基于模块和php-fpm

首先,我先介绍一下实验环境:

http服务器:192.168.236.128(php解析器基于modules)

mysql服务器:192.168.236.129

编译和配置http服务器,http版本是2.4以上的。

由于http依赖于apr apr-util这两个包,但是我们系统上的rpm包版本比较低,我们也需要下载这两个源码包来编译,解决依赖关系。

还要一些开发包组,所以,这一些都要在编译时做好!!

yum groupinstall Desktop Platform Development  Server Platform Development -y(这个步骤要两个机子上都做好!)

编译apr:

tar xf apr-1.4.6.tar.bz2

cd apr-1.4.6

./configure –prefix=/usr/local/apr

make && make install

编译apr-util:

tar xf apr-util-1.4.1.tar.bz2

cd apr-util-1.4.1

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

make && make install

编译httpd:

tar xf httpd-2.4.6.tar.bz2

cd httpd-2.4.6

./configure –prefix=/usr/local/apache –sysconfdir=/etc/http24 –enable-so –enable-modules=most –enable-mods-shared=most –enable-proxy –enable-proxy-fcgi –enable-mpms-shared=all –enable-cgi –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –with-z –with-ssl –with-mpm=event –enable-rewrite

编译时,出现了错误,缺少了pcre的库文件,这时,需要安装这些库文件。

configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

yum install pcre-devel -y 

安装后,再来一次上面的命令,好了之后:

make && make install

编译完成后,我们进行配置http的配置文件:

vim /etc/http24/httpd.conf

在配置文件中增加一下参数:

DirectoryIndex index.php index.html

AddType application/x-httpd-php .php

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

然后为http提供服务脚本:

#!/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/init.d/httpd24

chkconfig –add httpd24

service httpd24 start

查看一下监听端口,检测http服务是否开启了:

[root@www httpd-2.4.6]# ss -tnlp

State       Recv-Q Send-Q                                          Local Address:Port                                            Peer Address:Port 

LISTEN      0      128                                                        :::111                                                       :::*      users:(("rpcbind",1172,11))

LISTEN      0      128                                                         *:111                                                        *:*      users:(("rpcbind",1172,8))

LISTEN      0      128                                                        :::80                                                        :::*      users:(("httpd",32097,4))

好了,我们切换到192.168.236.129的主机上安装mariadb,mariadb是基于二进制包安装的:

tar zxvf mariadb-5.5.36-linux-x86_64.tar.gz

mv mariadb-5.5.36-linux-x86_64 /usr/local/

[root@www ~]# ln -sv /usr/local/mariadb-5.5.36-linux-x86_64 /usr/local/mysql

`/usr/local/mysql' -> `/usr/local/mariadb-5.5.36-linux-x86_64'

mkdir /etc/mysql

cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf

编译这个配置文件,加入 datadir=/data

然后,我们需要创建这个数据库的数据目录:

mkdir /data

groupadd -r mysql

useradd -r -g mysql mysql

chown mysql:mysql /data

cd /usr/local/mysql

chown -R mysql:mysql ./

然后,我们就要进行数据库的初始化:

./scripts/mysql_install_db –user=mysql –datadir=/data –basedir=/usr/local/mysql

我们要为数据库提供服务脚本:

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

把这个脚本放到服务脚本中去:

chmod +x /etc/init.d/mysqld

chkconfig –add mysqld

service mysqld start

检查服务是否开启了:

[root@www mysql]# ss -tnlp

LISTEN      0      50                                                                               *:3306                                                                            *:*      users:(("mysqld",29215,14))

我们还需要需要修改一下mysql的PATH环境变量:

vim /etc/profile.d/mysql.sh

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

然后,我们重新login进来

[root@www ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 2

Server version: 5.5.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

表明我们已经可以用mysql客户端登进服务器端了,我们为了等一下的测试先创建一个用户:

MariaDB [(none)]> grant all on *.* to 'bwei'@192.168.236.128 identified by 'bwei';

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

然后,我们可以退出了!

ok了,我们切换为http服务的主机编译php:

开始之前,也是需要安装一些开发包:

yum -y install bzip2-devel libmcrypt-devel

tar xf php-5.4.19.tar.bz2

cd php-5.4.19

./configure –prefix=/usr/local/php –with-apxs2=/usr/local/apache/bin/apxs –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-libxml-dir=/usr –with-zlib –with-bz2 –enable-xml –with-jpeg-dir –with-png-dir –with-freetype-dir –enable-mbstring –with-mcrypt –enable-sockets –with-mysql=mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd –enable-maintainer-zts

出现了问题,解决方法也是一样,安装libxml2-devel就可以了:

configure: error: xml2-config not found. Please check your libxml2 installation.

yum install -y libxml2-devel

然后,就按上面的方法再来一次,最后,make && make install

为php解析器提供配置文件:

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

我们可以对php解析器进行测试:

vim /usr/local/apache/htdocs/index.php

<?php

    phpinfo();

?>

对是否正常连接mysql测试:

<?php

    $con=mysql_connect('192.168.236.129','bwei','bwei');

    if($con)

    echo "ok!!";

    else

    echo "false!!";

    mysql_close();

?>

测试的时候要把iptables关闭!!

好了,我们给php解析器加上组件xcache,先编译xcache:

tar xf xcache-3.1.0.tar.bz2

cd xcache-3.1.0

[root@www xcache-3.1.0]# /usr/local/php/bin/phpize 

Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

这是为了xcache提供configure文件。

./configure –enable-xcache –with-php-config=/usr/local/php/bin/php-config –sysconfdir=/etc/php.d

make && make install

安装后,会出现这个 Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

我们把这个路径复制一下,然后,把这个路径下的模块写到xcacahe的配置文件下:

mkdir /etc/php.d

cp xcache.ini /etc/php.d/

vim /etc/php.d/xcache.ini

修改下面项:

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

重启一下服务器:service httpd24 restart

然后,我们在上面安装一个phpadmin:

unzip phpMyAdmin-4.0.5-all-languages.zip

mv phpMyAdmin-4.0.5-all-languages /usr/local/apache/htdocs/phpadmin

cd /usr/local/apache/htdocs/phpadmin/

提供phpadmin的配置文件:

cp config.sample.inc.php config.inc.php

编辑这个配置文件,修改这一项:

$cfg['Servers'][$i]['host'] = '192.168.236.129';

`T)W$56@LLN2W7HNHRW4]1C.png

我们打开浏览器,测试一下,说明我们的配置没有问题的!!

下一部分,我要做php解析器是基于fpm的,我会把上面的php解析器基于http模块的功能去除,然后,我们再到mysql的主机上编译php。

http服务器:192.168.236.128

php解析器 mysql服务器:192.168.236.129

下面我们开始切换到192.168.236.129主机上编译php:

tar xf php-5.4.19.tar.bz2

cd php-5.4.19

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

make && make install

给fpm提供服务脚本:

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

chmod +x /etc/init.d/fpm

chkconfig –add fpm

给php解析器提供配置文件:

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

为fpm提供服务配置文件:

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

编辑这个配置文件,修改以下几项:

listen = 192.168.236.129:9000

pm.max_children = 30

pm.start_servers = 5

pm.min_spare_servers = 5

pm.max_spare_servers = 8

启动服务:service fpm start

ss -tnlp 查看服务是否开启:

users:(("master",1947,12))

LISTEN      0      128                                                                192.168.236.129:9000                                                                            *:*      users:(("php-fpm",122099,7),("php-fpm",122100,0),("php-fpm",122101,0),("php-fpm",122102,0),("php-fpm",122103,0),("php-fpm",122104,0))

好了,我们再为php解析器提供xcache:

tar xf xcache-3.1.0.tar.bz2

cd xcache-3.1.0

[root@www xcache-3.1.0]# /usr/local/php/bin/phpize 

Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

这是为了xcache提供configure文件。

./configure –enable-xcache –with-php-config=/usr/local/php/bin/php-config –sysconfdir=/etc/php.d

make && make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

我们把这个路径复制一下,然后,把这个路径下的模块写到xcacahe的配置文件下:

mkdir /etc/php.d

cp xcache.ini /etc/php.d/

vim /etc/php.d/xcache.ini

修改下面项:

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

重启一下服务:

service fpm restartservice fpm restart

配置http主机(192.168.236.128)

vim /etc/http24/httpd.conf

把这项注释(表示不启用php解析器作为http功能里):

#LoadModule php5_module        modules/libphp5.so

把下面的功能启动:

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

然后在后面的配置文件中增加以下配置:

ProxyRequests Off

ProxyPassMatch  ^/(.*\.php)$ fcgi://192.168.236.129:9000/php/$1

切换到php解析器的主机,为这个主机提供phpadmin作为测试:

mkdir /php

mv phpMyAdmin-4.0.5-all-languages /php/phpadmin

cd /php/phpadmin/

提供phpadmin的配置文件:

cp config.sample.inc.php config.inc.php

好了!我们去数据库创建一个本地用户,然后,我们打开浏览器测试一下:

ENP3`)L[(U8R~D}W4(Q37ST.png

实验完毕!!

原创文章,作者:13-广州-杨过,如若转载,请注明出处:/7705

评论列表(2条)

  • stanley
    stanley 2015-08-31 10:43

    代码段最好能格式化,样式会有较大改观

  • bluesli 2015-09-01 07:55

    最好能讲下什么是php-fpm, 跟apache里的php模块有什么区别.

联系我们

400-080-6560

在线咨询:点击这里给我发消息

邮件:1660809109@qq.com

工作时间:周一至周五,9:30-18:30,节假日同时也值班

友情链接:测试  万达注册  万达直属QQ  万达娱乐登录  万达娱乐直属QQ  万达招商QQ  万达娱乐招商  万达开户  万达娱乐直属