php-fpm:
CentOS 6:
PHP-5.3.2-:默认不支持fpm机制;需要自行打补丁并编译安装;
httpd-2.2:默认不支持fcgi协议,需要自行编译此模块;
解决方案:编译安装httpd-2.4, php-5.3.3+;
CentOS 6, lamp (编译安装http和php-fpm);
实现:
(1) 三者分离于三台主机;
(2) 一个虚拟主机用于提供phpMyAdmin;另一个虚拟主机用于提供wordpress;
(3) 为php提供xcache
(4) 切换mpm为非prefork机制;
On http(172.16.17.61):
1)配置编译环境,安装开发包组
[root@node1 ~]# yum -y groupinstall "Development Tools" "Server Platform Development"
2)安装httpd依赖的apr及apr-util,apr需要先安装
[root@node1 ~]# tar xf apr-1.5.0.tar.bz2 [root@node1 ~]# cd apr-1.5.0 [root@node1 apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@node1 apr-1.5.0]# make -j 4 && make install ============================================================ [root@node1 ~]# tar xf apr-util-1.5.3.tar.bz2 [root@node1 ~]# cd apr-util-1.5.3 [root@node1 apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [root@node1 apr-util-1.5.3]# make -j 4 && make install
3)编译安装httpd程序
[root@node1 ~]# yum -y install pcre-devel openssl-devel [root@node1 ~]# tar xf httpd-2.4.10.tar.bz2 [root@node1 ~]# cd httpd-2.4.10 [root@node1 httpd-2.4.10]# ./configure --prefix=/usr/local/apache24 --sysconfdir=/etc/httpd24 \ > --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all \ > --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-mpm=prefork [root@node1 httpd-2.4.10]# make -j 4 && make install
4)提供httpd服务脚本,复制CentOS6的httpd2.2服务脚本,简单修改即可
[root@node1 ~]# cp /etc/rc.d/init.d/httpd /etc/rc.d/init.d/httpd24 [root@node1 ~]# vim /etc/rc.d/init.d/httpd24 apachectl=/usr/local/apache24/bin/apachectl httpd=${HTTPD-/usr/local/apache24/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache24/logs/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
5)添加httpd的PATH环境变量
[root@node1 ~]# chkconfig --add httpd24 [root@node1 ~]# chkconfig httpd24 on [root@node1 ~]# vim /etc/profile.d/httpd24.sh export PATH=/usr/local/apache24/bin:$PATH [root@node1 ~]# . /etc/profile.d/httpd24.sh
6)启用httpd2.4的fcgi模块
[root@node1 ~]# vim /etc/httpd24/httpd.conf LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
7)添加http对php页面文件的支持,启用虚拟主机
[root@node1 ~]# vim /etc/httpd24/httpd.conf ServerName www.example.com:80 #DocumentRoot "/usr/local/apache24/htdocs" <IfModule dir_module> DirectoryIndex index.html index.php </IfModule> AddType application/x-httpd-php .php AddType application/x-httpd-source .phps Include /etc/httpd24/extra/httpd-vhosts.conf
8)提供虚拟主机www.pma.com及www.wordpress.com
[root@node1 ~]# vim /etc/httpd24/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/web/pma/htdocs" ServerName www.pma.com ServerAlias web.pma.com ErrorLog "logs/pma.com-error_log" CustomLog "logs/pma.com-access_log" combined ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.17.62:9000/web/pma/htdocs <Directory "/web/pma/htdocs"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/web/wordpress/htdocs" ServerName www.wordpress.com ServerAlias web.wordpress.com ErrorLog "logs/wordpress.com-error_log" CustomLog "logs/wordpress.com-access_log" combined ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.17.62:9000/web/wordpress/htdocs <Directory "/web/wordpress/htdocs"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
9)安装配置phpMyAdmin程序
[root@node1 ~]# mkdir -pv /web/{pma,wordpress} [root@node1 ~]# unzip phpMyAdmin-4.4.14.1-all-languages.zip -d /web/pma/ [root@node1 ~]# mv /web/pma/phpMyAdmin-4.4.14.1-all-languages/ /web/pma/htdocs [root@node1 ~]# cd /web/pma/htdocs [root@node1 htdocs]# cp config.sample.inc.php config.inc.php [root@node1 htdocs]# openssl rand -base64 20 vNVkpLUPfn2a4dz/ZKnJkBmvH54= 编辑config.inc.php文件,将生成的随机数复制到里面,然后更改数据库连接主机: $cfg['blowfish_secret'] = 'vNVkpLUPfn2a4dz/ZKnJkBmvH54'; $cfg['Servers'][$i]['host'] = '172.16.17.63';
10)安装配置WordPress程序
[root@node1 ~]# unzip wordpress-4.3.1-zh_CN.zip -d /web/wordpress/ [root@node1 ~]# mv /web/wordpress/wordpress/ /web/wordpress/htdocs [root@node1 ~]# cd /web/wordpress/htdocs [root@node1 htdocs]# cp wp-config-sample.php wp-config.php 编辑wp-config.php文件,配置数据库连接信息: define('DB_NAME', 'wpdb'); define('DB_USER', 'wpuser'); define('DB_PASSWORD', 'wppasswd'); define('DB_HOST', '172.16.17.63');
11)启动httpd服务
[root@node1 ~]# httpd -t Syntax OK [root@node1 ~]# service httpd24 start Starting httpd: [ OK ] [root@node1 ~]# ss -tnl | grep 80 LISTEN 0 128 *:48075 *:* LISTEN 0 128 :::80 :::*
On php(172.16.17.62):
1)配置编译环境,安装开发包组
[root@node2 ~]# yum -y groupinstall "Development Tools" "Server Platform Development" [root@node2 ~]# yum install -y libxml2-devel libmcrypt-devel openssl-devel bzip2-devel
2)编译安装php程序,默认启用php-fpm
[root@node2 ~]# tar xf php-5.6.4.tar.xz -C /usr/local/src [root@node2 ~]# cd /usr/local/src/php-5.6.4 [root@node2 php-5.6.4]# ./configure --prefix=/usr/local/php --sysconfdir=/etc/php \ > --enable-mbstring --enable-xml --enable-fpm --enable-sockets --enable-maintainer-zts \ > --with-openssl --with-freetype-dir --with-png-dir --with-jpeg-dir --with-zlib --with-bz2 \ > --with-libxml-dir=/usr --with-mcrypt --with-config-file-scan-dir=/etc/php.d \ > --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --user=apache [root@node2 php-5.6.4]# make -j 4 && make install
3)为php提供配置文件
[root@node2 php-5.6.4]# cp php.ini-production /etc/php/php.ini
4)配置php-fpm
[root@node2 php-5.6.4]# mkdir /etc/php.d [root@node2 php-5.6.4]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@node2 php-5.6.4]# chmod +x /etc/init.d/php-fpm [root@node2 php-5.6.4]# chkconfig --add php-fpm [root@node2 php-5.6.4]# chkconfig php-fpm on
5)为php-fpm提供配置文件并编辑之
[root@node2 php-5.6.4]# cp /etc/php/php-fpm.conf.default /etc/php/php-fpm.conf [root@node2 php-5.6.4]# vim /etc/php/php-fpm.conf pid = /usr/local/php/var/run/php-fpm.pid listen = 172.16.17.62:9000 listen.allowed_clients = 172.16.17.61 pm.max_children = 60 pm.start_servers = 5 pm.min_spare_servers = 3 pm.max_spare_servers = 8
6)复制web服务器的网页某到php服务器上一份,客户端访问时,获取静态资源会访问到web服务器,访问动态资源将访问到php服务器
[root@node2 ~]# scp -r [email protected]:/web /
7)启动php-fpm
[root@node2 ~]# service php-fpm start Starting php-fpm done [root@node2 ~]# ss -tnl | grep --color "9000" LISTEN 0 128 172.16.17.62:9000 *:* [root@node2 ~]# ps aux | grep --color "php-fpm" root 108087 0.0 1.0 71880 5128 ? Ss 00:07 0:00 php-fpm: master process (/etc/php/php-fpm.conf) apache 108088 0.0 0.9 71880 4436 ? S 00:07 0:00 php-fpm: pool www apache 108089 0.0 0.9 71880 4440 ? S 00:07 0:00 php-fpm: pool www apache 108090 0.0 0.9 71880 4440 ? S 00:07 0:00 php-fpm: pool www apache 108091 0.0 0.9 71880 4440 ? S 00:07 0:00 php-fpm: pool www apache 108092 0.0 0.9 71880 4440 ? S 00:07 0:00 php-fpm: pool www root 108096 0.0 0.1 103304 892 pts/0 S+ 00:08 0:00 grep --color php-fpm
On mariadb(172.16.17.73):
1)创建运行mariadb的用户
[root@node2 ~]# groupadd -r mysql [root@node2 ~]# useradd -r -g mysql mysql
2)创建数据库存放目录
[root@node2 ~]# mkdir -pv /data/mysql [root@node2 ~]# chown -R mysql.mysql /data/mysql/
3)解压mariadb程序包至/usr/local,并创建mysql软链接
[root@node2 ~]# tar xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/ [root@node2 ~]# cd /usr/local/ [root@node2 local]# ln -sv mariadb-5.5.46-linux-x86_64/ mysql
4)初始化数据库
[root@node2 local]# chown -R root.mysql mysql/ [root@node2 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
5)提供mysql主配置文件并编辑之
[root@node2 mysql]# mkdir /etc/mysql [root@node2 mysql]# cp support-files/my-medium.cnf /etc/mysql/my.cnf 在my.cnf配置文件中的[mysqld]端添加以下内容: datadir = /data/mysql //设置mysql的数据存储目录 innodb_file_per_table = ON //设置innodb存储引擎独立存储数据库 skip_name_resolve = ON //禁止mysql进行反解主机名
6)提供mysql服务脚本
[root@node2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@node2 mysql]# chmod +x /etc/rc.d/init.d/mysqld [root@node2 mysql]# chkconfig --add mysqld [root@node2 mysql]# chkconfig mysqld on
7)设置mysql命令的环境变量
[root@node3 ~]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql/bin:$PATH [root@node3 ~]# . /etc/profile.d/mysql.sh
8)导出mysql头文件
[root@node3 ~]# ln -sv /usr/local/mysql/include/mysql/ /usr/include/
9)导出mysql库文件路径
[root@node3 ~]# echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mysql.conf [root@node3 ~]# ldconfig
10)导出mysql的man帮助手册
[root@node3 ~]# vim /etc/man.config MANPATH /usr/local/mysql/man //添加此行
11)启动mysqld服务
[root@node2 ~]# service mysqld start
8)创建wpdb库,授权wpuser用户连接
[root@node3 ~]# mysql MariaDB [(none)]> USE mysql; MariaDB [mysql]> DELETE FROM mysql.user WHERE User=''; MariaDB [mysql]> DELETE FROM mysql.user WHERE Host='node3'; MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123qwe!@#'; MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '123qwe!@#'; MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'::1' IDENTIFIED BY '123qwe!@#'; MariaDB [(none)]> CREATE SCHEMA wpdb; MariaDB [(none)]> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'172.16.%.%' IDENTIFIED BY 'wppasswd'; MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.%.%' IDENTIFIED BY '123qwe!@#'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [mysql]> SELECT Host,User,Password FROM user; +------------+--------+-------------------------------------------+ | Host | User | Password | +------------+--------+-------------------------------------------+ | localhost | root | *A47CB5FB12B47CBF12AB43B7EC865013906E10BD | | 127.0.0.1 | root | *A47CB5FB12B47CBF12AB43B7EC865013906E10BD | | ::1 | root | *A47CB5FB12B47CBF12AB43B7EC865013906E10BD | | 172.16.%.% | wpuser | *7CD61EFBFDDDBD978EA9017F2A26A59DE4589025 | | 172.16.%.% | root | *A47CB5FB12B47CBF12AB43B7EC865013906E10BD | +------------+--------+-------------------------------------------+
重启httpd24服务,添加本地hosts文件解析,打开浏览器添访问网站(配置好php-fpm后需要重启一下httpd服务):
[root@node1 ~]# service httpd24 restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
客户端使用ab命令对服务器进行压力测试:
[root@client ~]# ab -c 100 -n 1000 http://www.pma.com/index.php
ON php(172.16.17.72):为php服务器编译安装xcache
1)解压xcache源码包至/usr/local下
[root@node2 xcache]# tar xf xcache-3.2.0.tar.bz2 -C /usr/local/ [root@node2 xcache]# mv /usr/local/xcache-3.2.0/ /usr/local/xcache
2)生成configure脚本
[root@node2 xcache]# /usr/local/php/bin/phpize
3)执行编译安装
[root@node2 xcache]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
4)复制xcache的ini文件至/etc/php.d目录下
[root@node2 xcache]# cp xcache.ini /etc/php.d/
5)建立php测试页面
[root@node2 ~]# vim /web/pma/htdocs/test.php <?php phpinfo(); ?>
6)重启php-fpm服务
[root@node2 xcache]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
再次在客户端使用ab命令对服务器进行压力测试:
[root@client ~]# ab -c 100 -n 1000 http://www.pma.com/index.php
On http(172.16.17.61):切换mpm为event机制:
[root@node1 ~]# vim /etc/httpd24/httpd.conf Include /etc/httpd24/extra/httpd-mpm.conf #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so LoadModule mpm_event_module modules/mod_mpm_event.so //注释prefork模块,配置event模块 [root@node1 ~]# service httpd24 restart [root@node1 ~]# httpd -M | grep mpm mpm_event_module (shared)