yum安装实现lamp分离及Xcache加速
LAMP的搭建:
准备前提:
虚拟机:Centos7 172.18.250.77 安装httpd,通过Fcgi模块来跟php互动,只处理静态网页。
虚拟机:Centos7 172.18.250.78 安装php-fpm程序,php-5.3.3的版本已经包含了php-fpm,不需要在打补丁,但CentOS6上官方源没有php-fpm包,所以用CentOS 7 实验。
虚拟机:CentOS6 172.18.250.76 安装mysql,实现php跟mysql的互动。
httpd提供两个虚拟主机,分别安装wordpress博客及discuz论坛。
一、yum安装httpd
[root@localhost local]# yum -y install httpd
1、编辑主配置文件,注释DocumentRoot
[root@localhost httpd]# vim conf/httpd.conf#DocumentRoot "/var/www/html"
2、创建虚拟主机文件
[root@localhost httpd]# cd /etc/httpd/conf.d/[root@localhost conf.d]# vim vhost.confServerName www.a.com DocumentRoot "/www/blog" Options None Allowoverride None Require all granted ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.18.250.78:9000/www/blog/$1 ServerName www.b.net DocumentRoot "/www/php" Options None Allowoverride None Require all granted ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.18.250.78:9000/www/php/$1
3、创建虚拟主机需要的目录
[root@localhost conf.d]# mkdir -p /www/blog[root@localhost conf.d]# mkdir -p /www/php
4、启动httpd服务,查看监听端口
[root@localhost conf.d]# systemctl start httpd.service[root@localhost conf.d]# ss -tanLISTEN 0 128 :::80 :::*
5、在虚拟主机目录创建测试页,看httpd是否正常
[root@localhost conf.d]# echo "www.a.com" >/www/blog/index.html[root@localhost conf.d]# echo "www.b.net" >/www/php/index.html
OK,虚拟主机正常
二、yum安装PHP
[root@localhost ~]# yum -y install php-fpm
PHP-FPM是一个PHPFastCGI管理器,是只用于PHP的,httpd通过Fcgi模块来把php的文件传送给php-fpm进行处理
1、编辑php-fpm文件
[root@localhost ~]# vim /etc/php-fpm.d/www.conflisten = 172.18.250.78:9000 //本机IPlisten.allowed_clients = 172.18.250.77 //httpd端IP
2、重启php-fpm程序,查看监听端口
[root@localhost ~]# systemctl start php-fpm.service[root@localhost ~]# ss -tanLISTEN 0 128 172.18.250.78:9000 *:*
3、在php端上创建和httpd上一样的目录
[root@localhost ~]# mkdir -p /www/blog[root@localhost ~]# mkdir -p /www/php
4、创建测试页,验证看httpd能不能转发过来
[root@localhost ~]# mkdir -p /www/blog[root@localhost ~]# mkdir -p /www/php[root@localhost ~]# vim /www/blog/index.php[root@localhost ~]# vim /www/php/index.php
OK,httpd能读到.php结尾的文件并转发给php处理
三、yum安装mysql
[root@localhost ~]# yum -y install mysql-server
1、启动mysql,并对数据库就行安全加固
[root@localhost ~]# service mysqld start[root@localhost ~]# mysql_secure_installationEnter current password for root (enter for none): //默认数据库密码为空Set root password? [Y/n] y //设置新密码Remove anonymous users? [Y/n] y //移除匿名用户Disallow root login remotely? [Y/n] n //是否禁止用户远程登录Remove test database and access to it? [Y/n] y //是否移除test数据库Reload privilege tables now? [Y/n] y //是否重启加载权限列表
2、创建用户和数据库,并授权php端能远程登录
mysql> grant all on *.* to 'admin'@'172.18.250.78' identified by "admin";Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> create database mytest;Query OK, 1 row affected (0.00 sec)mysql> create database bbs;Query OK, 1 row affected (0.01 sec)
3、测试php能不能登录数据库
[root@localhost ~]# yum -y install php-mysql //安装mysql驱动[root@localhost ~]# vim /www/blog/index.php [root@localhost ~]# vim /www/php/index.php[root@localhost ~]# systemctl restart php-fpm.service
OK,测试正常,可以安装wordpress和Discuz论坛
1、分别在php服务器上和httpd上解压wordpress包
[root@localhost blog]# unzip wordpress-4.3.1-zh_CN.zip[root@localhost blog]# unzip wordpress-4.3.1-zh_CN.zip [root@localhost blog]# cd wordpress //只需在php上改动[root@localhost wordpress]# cp wp-config-sample.php wp-config.php /** WordPress数据库的名称 */define('DB_NAME', 'mytest');/** MySQL数据库用户名 */define('DB_USER', 'admin');/** MySQL数据库密码 */define('DB_PASSWORD', 'admin');/** MySQL主机 */define('DB_HOST', '172.18.250.76');
出现这个页面的话只需要修改httpd主配置文件,让httpd能识别php的文件代码
[root@localhost blog]# vim /etc/httpd/conf/httpd.confAddType application/x-httpd-php .php //添加下面的语句就行AddType aaplication/x-httpd-source .phpsDirectoryIndex index.php index.html
重启httpd服务,再次测试下
登录wordPress博客:
OK,WordPress博客能正常加载
1、分别在php服务器上和httpd上解压Discuz包
[root@localhost php]# unzip Discuz_X3.2_SC_UTF8.zip [root@localhost php]# unzip Discuz_X3.2_SC_UTF8.zip[root@localhost php]# cd upload //在php服务器上
修改目录的权限即可:
[root@localhost upload]# chown -R apache config/[root@localhost upload]# chown -R apache uc_client/ //httpd和php上都需要改[root@localhost upload]# chown -R apache data[root@localhost upload]# chown -R apache template/[root@localhost upload]# chown -R apache uc_server/
根据步骤一步步执行就行:
注册完后登录论坛:
。。。。。发现论坛只有文字,这是因为php安装了Discuz后修改了程序的文件导致的,这时httpd上的静态程序文件跟php上的动态程序文件不一致,所以只需要同步下就行。
[root@localhost php]# scp -r upload/* root@172.18.250.77:/www/php/upload/
再次刷新:OK
如果想要实现自动同步的话,可以使用initory+rsync、sersync等工具。
四:安装Xcahe对php进行加速
[root@localhost src]# tar -xf xcache-3.2.0.tar.bz2 //只需在PHP服务器上[root@localhost src]# lsxcache-3.2.0 xcache-3.2.0.tar.bz2[root@localhost src]# cd xcache-3.2.0 [root@localhost xcache-3.2.0]# yum -y install php-devel //安装php开发工具包[root@localhost xcache-3.2.0]# phpize //检查php的版本及api接口Configuring for:PHP Api Version: 20100412Zend Module Api No: 20100525Zend Extension Api No: 220100525[root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/bin/php-config[root@localhost xcache-3.2.0]# make && make install[root@localhost xcache-3.2.0]# cp xcache.ini /etc/php.d/
[root@localhost upload]# ab -n100 -c10 http://172.18.250.77/wordpress/index.phpBenchmarking 172.18.250.77 (be patient).....doneServer Software: Apache/2.4.6Server Hostname: 172.18.250.77Server Port: 80Document Path: /wordpress/index.phpDocument Length: 0 bytesConcurrency Level: 10Time taken for tests: 3.908 secondsComplete requests: 100Failed requests: 0Write errors: 0Non-2xx responses: 100Total transferred: 34900 bytesHTML transferred: 0 bytesRequests per second: 25.59 [#/sec] (mean) //ab压力测试每秒请求个数Time per request: 390.849 [ms] (mean)Time per request: 39.085 [ms] (mean, across all concurrent requests)Transfer rate: 8.72 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 0.3 0 1Processing: 104 369 95.7 368 589Waiting: 104 367 94.8 367 589Total: 105 369 95.7 368 589Percentage of the requests served within a certain time (ms) 50% 368 66% 410 75% 443 80% 455 90% 502 95% 536 98% 589 99% 589 100% 589 (longest request)
重启php-fpm服务,看xcache有没有加载:
[root@localhost blog]# php -m [Zend Modules] //表明xcache已经开启XCacheXCache Cacher
再一次做ab压力测试:
[root@localhost upload]# ab -n100 -c10 http://172.18.250.77/wordpress/index.phpBenchmarking 172.18.250.77 (be patient).....doneServer Software: Apache/2.4.6Server Hostname: 172.18.250.77Server Port: 80Document Path: /wordpress/index.phpDocument Length: 0 bytesConcurrency Level: 10Time taken for tests: 1.489 secondsComplete requests: 100Failed requests: 0Write errors: 0Non-2xx responses: 100Total transferred: 34900 bytesHTML transferred: 0 bytesRequests per second: 67.18 [#/sec] (mean) //加载xcache后有了提升Time per request: 148.853 [ms] (mean)Time per request: 14.885 [ms] (mean, across all concurrent requests)Transfer rate: 22.90 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 0.2 0 1Processing: 38 141 31.0 139 240Waiting: 37 141 31.0 138 240Total: 38 141 31.0 139 240Percentage of the requests served within a certain time (ms) 50% 139 66% 152 75% 160 80% 168 90% 179 95% 194 98% 216 99% 240 100% 240 (longest request)