编译安装php-5.4.40

 

场景一:php编译为httpd的模块;


编译安装httpd-2.4:

apr,apr-util

httpd-2.4


mariadb-5.5.42

注意:不用使用5.5.40除非自己去打补丁;与php结合会有问题;


安装php:

额外安装:libxml2-devel, libmcrypt-devel, bzip-devel

方法一:

我挂载的是CentOS-6.6-x86_64-bin-DVD1 这个安装光盘;

# mount /dev/cdrom /media

# cd /media/Package

# rpm -ivh libxml2-devel-2.6.26-2.1.2.1.x86_64.rpm

# rpm -ivh zlib-devel-1.2.3-3.x86_64.rpm

 

# yum install -y  bzip2-devel

# yum -y install  libmcrypt-devel 

方法二:

# yum  install -y  libxml2-devellibmcrypt-devel  bzip-devel

 

注意:如果httpd使用的mpm为worker或event,还应该额外使用 --enable-maintainer-zts选项

 

# tar xf php-5.4.40.tar.bz2

# cd php-5.4.40

 

#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config

--with-apxs2=/usr/local/apache/bin/apxs --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr 

--enable-xml --enable-sockets --with-mcrypt --with-bz2

--with-config-file-path=/etc/php/php.ini --with-config-file-scan-dir=/etc/php.d/

 

#make

 

wKioL1VF-PjDuvHpAAE3JlFDw0M112.jpg

 

#make install

 

wKiom1VF95XQVtcgAAOti7zit6Y601.jpg

 

 

# mkdir /etc/php

# ls /etc/php

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

 

# vim /etc/php/php.ini  

找到跟时区相关的项目:修改为一下内容

date.timezone = Asia/Shanghai

 

# service httpd24 restart 

# httpd -M  可以看到模块会被加载进来;

 

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

添加如下内容:

<?php

   phpinfo();

?>

 

# vim /etc/httpd24/httpd.conf

在AddType 附加添加下面类容:

AddType application/x-httpd-php .php

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

 

<IfModule dir_module>

添加成这样:DirectoryIndex  index.php  index.html

这是有优先顺序的,把index.php 放在第一项;

 

# service httpd24 reload

 

wKioL1VF-ReSrQ8GAAOb0onVqB0370.jpg 

 

 

联系MYSQL:

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

<?php

     $link = mysql_connect('127.0.0.1','root','');

     if ($link)

            echo "OK";

     else

            echo "Failure";

     mysql_close()

?>

 

 

# service mysqld stop

 

# service mysqld start

 

 

 

安装:phpMyAdmin

# unzip phpMyAdmin-4.0.5-all-languages.zip

# mv phpMyAdmin-4.0.5-all-languages /usr/local/apache/htdocs/pma

 

# cd /usr/local/apache/htdocs/pma

 

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


#vim config.inc.php

$cfg['blowfish_secret'] = '最好生成一个随机串放进来(随便写点什么都可以)'



此时就可以在浏览器中访问pma了;

wKiom1VF98aDyIJCAAGpzptPoVM255.jpg

 

 

此时Mysql服务器root用户的密码是空;需要设定密码才能登陆pma;

# mysql

>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mgeedu');

>SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('mgeedu');

 

> SHOW GLOBAL VARIABLES LIKE '%skip%';

 

因为MySQL 服务器会进行反向解析;所有最好设定两个密码;

 

如果不想让MySQL 服务器进行反向解析 可以使用下面的方法,而且方向解析会降低Mysql服务器的性能,所有最好把它关闭;

 

# vim /etc/mysql/my.cnf

 

在 [mysqld] 下面增加一行:


skip_name_resolve = on   就可以不进行反向解析了;


# service mysqld restart  

 

但是现在登录mysql 服务器是不能直接登录的;

[root@localhost pma]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

 

只能使用下面的方法登录:

[root@localhost pma]# mysql -uroot -p

Enter password: 

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

Your MariaDB connection id is 3

Server version: 5.5.43-MariaDB-log MariaDB Server

 

此时就可以用root用户访问pma.

 

wKioL1VF-USiFNfdAADJKJ9iM_k823.jpg

 

wKiom1VF9-jTbf1-AAURw7lnJ6M172.jpg

 

 

 

 

现在可以进行一下压力测试:(用ab命令做压力测试)

 

[root@localhost pma]# ab -n 1000 -c 50 http://172.16.8.101/pam/index.php

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 172.16.8.101 (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Completed 600 requests

Completed 700 requests

Completed 800 requests

Completed 900 requests

Completed 1000 requests

Finished 1000 requests

 

 

Server Software:        Apache/2.4.10

Server Hostname:        172.16.8.101

Server Port:            80

 

Document Path:          /pam/index.php

Document Length:        211 bytes

 

Concurrency Level:      50

Time taken for tests:   0.247 seconds

Complete requests:      1000

Failed requests:        0

Non-2xx responses:      1000

Total transferred:      420000 bytes

HTML transferred:       211000 bytes

Requests per second:    4044.11 [#/sec] (mean)

Time per request:       12.364 [ms] (mean)

Time per request:       0.247 [ms] (mean, across all concurrent requests)

Transfer rate:          1658.72 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    5   2.3      5      11

Processing:     2    7   7.6      6      87

Waiting:        0    6   7.5      4      86

Total:          3   12   8.0     11      92

 

Percentage of the requests served within a certain time (ms)

  50%     11

  66%     12

  75%     13

  80%     14

  90%     16

  95%     18

  98%     40

  99%     57

 100%     92 (longest request)

 

 

这是访问php页面的速度; 一般访问静态页面的速度会更加快;

 

 

 

现在安装xcache 加速php的访问速度:

 

# tar xf xcache-3.2.0.tar.bz2 

# cd xcache-3.2.0

xcache 很独特,解压后是没有 config 文件的,需要如下步骤把config配置文件解压出来;

 

# /usr/local/php/bin/phpize  

Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

 

# ll      现在就可以看到config 文件了;

 

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

# make && make install

 

会生成这个文件:

wKioL1VF-WKBb1LvAAD7ZtqDxmw434.jpg

 

 

# mkdir /etc/php.d

# cp xcache.ini /etc/php.d

 

# cd /etc/php.d

# vim xcache.ini 

修改为如下内容:

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

 

# service httpd24 restart

 

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

修改为如下内容:

<php

Phpinfo();

?>

 

# service httpd24 restart

 

wKioL1VF-XHjtI46AAU8j2SNvrk632.jpg

 

 

Xcache模块已经被加载进了PHP,现在来测试是否加速了php页面的访问速度读:

 

 

[root@localhost xcache-3.2.0]# ab -n 1000 -c 50 http://172.16.8.101/pam/index.php

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 172.16.8.101 (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Completed 600 requests

Completed 700 requests

Completed 800 requests

Completed 900 requests

Completed 1000 requests

Finished 1000 requests

 

 

Server Software:        Apache/2.4.10

Server Hostname:        172.16.8.101

Server Port:            80

 

Document Path:          /pam/index.php

Document Length:        211 bytes

 

Concurrency Level:      50

Time taken for tests:   0.160 seconds

Complete requests:      1000

Failed requests:        0

Non-2xx responses:      1000

Total transferred:      420000 bytes

HTML transferred:       211000 bytes

Requests per second:    6235.31 [#/sec] (mean)

Time per request:       8.019 [ms] (mean)

Time per request:       0.160 [ms] (mean, across all concurrent requests)

Transfer rate:          2557.45 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    2   1.3      2       8

Processing:     0    5   2.1      5      13

Waiting:        0    4   1.8      4      10

Total:          2    8   2.0      8      14

 

Percentage of the requests served within a certain time (ms)

  50%      8

  66%      9

  75%      9

  80%     10

  90%     10

  95%     11

  98%     11

  99%     13

 100%     14 (longest request)

 

xcache 会给php的访问速度带来性能的提升;

 

 

 

 

 

 

场景二:php以fpm工作为独立守护进程


(还是在上面的实验环境中进行,所以需要修改一下)

 

# service httpd24 stop

 

# cd /etc/httpd24/

[root@localhost httpd24]# ls

extra  httpd.conf  httpd.conf.bak  magic  mime.types  original  ssl

 

# mv  httpd.conf  httpd.conf.php如果以后还想使用,还可以还原回来;

# cp httpd.conf.bak httpd.conf将原来备份的配置文件复制回来(备份很重要)

 

# service  httpd24  start

 

 

现在重新编译php:

# mv  php-5.4.40 php-5.4.40.module   以后还要使用的话,可以还原回来;


#rm -rf /usr/local/php/ /etc/php /etc/php.d/删除原来使用的文件;


# tar xf php-5.4.40.tar.bz2 

# cd php-5.4.40

 

# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config

--enable-fpm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir

--with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-bz2

--with-config-file-path=/etc/php/php.ini --with-config-file-scan-dir=/etc/php.d/

 

与php安装mysql模块不同的是:

--with-mysqli=/usr/local/mysql/bin/mysql_config这一项换成了--enable-fpm

 

wKioL1VF-YGARGniAAHavZprRBc608.jpg

 

 

# make && make install 

# mkdir  /etc/php  /etc/php.d

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

 

为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

 

# cd /usr/local/php/etc/


# cp php-fpm.conf.default php-fpm.conf


# vim php-fpm.conf

修改如下配置:

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


error_log = /var/log/php-fpm.log


要启动的最大并发进程:

pm.max_children = 200

刚刚启动服务时候启动多少个进程:

pm.start_server = 5

最小空闲几个服务进程:

pm.min_space_server = 2

最多空闲几个服务进程:

pm.max_space_servers = 5   这个最起码要大于pm.start_server = 5 的个数

 

 

# service php-fpm start

Starting php-fpm  done

 

# ss -tnl   此时 9000 端口已经打开了;

 

wKiom1VF-CHB9umyAAGvnzhyMCM935.jpg

 

 

配置httpd 服务:


# cd /etc/httpd24/

# vim httpd.conf


启动httpd的相关模块:

在apache httpd 2.4 以后已经专门有一个模块针对FastCGI的实现,此模块

为mod_proxy_fcgi.so ,它其实是作为mod_proxy.so模块的扩充,因此两个模块都要加载。

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

 

 

配置虚拟主机支持使用fcgi:

在相应的虚拟主机中添加类似如下两行;(在最后添加一下2行)

ProxyRequests Off关闭正向代理;

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1

 

================================================================

如果使用的是虚拟主机:要把下面2项 放入<VirtualHost *:80> 下即可

ProxyRequests Off关闭正向代理;

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

================================================================

 

 

# httpd -t

# service httpd24 reload

# httpd -M


# cd /usr/local/apache/htdocs/

# vim index.php

添加如下内容:

<?

phpinfo();

?>


# vim /etc/httpd24/httpd.conf

增加此内容:

DirectoryIndex index.php index.html


# service httpd24 reload

 

wKiom1VF-DLjOKWeAAO-4d0tdLI701.jpg 

 

wKioL1VF-a6SZJxrAAIgKk_klLA578.jpg

 

 

wKiom1VF-FGwJgjwAASaY2grr6A365.jpg

 

 

用AB压力测试工具进行压力测试:

 

[root@localhost htdocs]# ab -n 100 -c 5 http://172.16.8.101/pam/index.php

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 172.16.8.101 (be patient).....done

 

 

Server Software:        Apache/2.4.10

Server Hostname:        172.16.8.101

Server Port:            80

 

Document Path:          /pam/index.php

Document Length:        16 bytes

 

Concurrency Level:      5

Time taken for tests:   0.046 seconds

Complete requests:      100

Failed requests:        0

Non-2xx responses:      100

Total transferred:      19900 bytes

HTML transferred:       1600 bytes

Requests per second:    2180.22 [#/sec] (mean)

Time per request:       2.293 [ms] (mean)

Time per request:       0.459 [ms] (mean, across all concurrent requests)

Transfer rate:          423.69 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    1   0.5      0       2

Processing:     1    2   0.7      1       4

Waiting:        0    1   0.5      1       3

Total:          1    2   0.7      2       5

ERROR: The median and mean for the initial connection time are more than twice the standard

       deviation apart. These results are NOT reliable.

WARNING: The median and mean for the processing time are not within a normal deviation

        These results are probably not that reliable.

 

Percentage of the requests served within a certain time (ms)

  50%      2

  66%      2

  75%      3

  80%      3

  90%      3

  95%      4

  98%      5

  99%      5

 100%      5 (longest request)

 

 

需要把服务放置在不通主机上,这样才能看到真正的性能;

 

 

 

下面配置xcache 加速php页面的访问速度:

 

删除场景一时候使用的xcache

# rm  -rf  xcache-3.2.0

 

# tar xf xcache-3.2.0.tar.bz2 

# cd xcache-3.2.0

# /usr/local/php/bin/phpize

Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

 

 

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

 

# make && make install

 

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

# cd /etc/php.d/

 

# vim xcache.ini

修改如下配置:

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

 

# service php-fpm restart

 

 

wKioL1VF-dHSC-yHAAZqJ74Bvvk814.jpg 

 

 

使用了xcache 做测试:

 

[root@localhost pma]# ab -n 1000 -c 50 http://172.16.8.101/pam/index.php

This is ApacheBench, Version 2.3 <$Revision: 1604373 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

 

Benchmarking 172.16.8.101 (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Completed 600 requests

Completed 700 requests

Completed 800 requests

Completed 900 requests

Completed 1000 requests

Finished 1000 requests

 

 

Server Software:        Apache/2.4.10

Server Hostname:        172.16.8.101

Server Port:            80

 

Document Path:          /pam/index.php

Document Length:        16 bytes

 

Concurrency Level:      50

Time taken for tests:   0.435 seconds

Complete requests:      1000

Failed requests:        0

Non-2xx responses:      1000

Total transferred:      199000 bytes

HTML transferred:       16000 bytes

Requests per second:    2300.24 [#/sec] (mean)

Time per request:       21.737 [ms] (mean)

Time per request:       0.435 [ms] (mean, across all concurrent requests)

Transfer rate:          447.02 [Kbytes/sec] received

 

Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        1    9   4.3      9      20

Processing:     1   12   4.3     12      38

Waiting:        1   10   4.2     10      20

Total:          6   21   3.4     22      39

 

Percentage of the requests served within a certain time (ms)

  50%     22

  66%     22

  75%     23

  80%     23

  90%     24

  95%     25

  98%     30

  99%     35

 100%     39 (longest request)

 

 

这里使用的是 1000个并发请求一共请求50次;比第一次请求100次 共5次请求的速度还要快。