码迷,mamicode.com
首页 > 系统相关 > 详细

源码安装实现lamp的amp分离及Xcache加速

时间:2016-04-22 16:54:21      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:数据库   虚拟机   程序   

LAMP的搭建:

         搭建LAMP的平台,要求apache、mariadb、php三个程序分别实现在三台虚拟机上,实现动静分离。

          1、虚拟机IP 172.18.250.77,安装Apache实现和PHP的交互,httpd与php的交互有三种方式,cgi、fcgi和模块方式,因为是跨主机的交互,所以使用fcgi的方式实现

          2、虚拟机IP 172.18.250.76,安装php和php-fpm,实现对动态页面的处理。

          3、虚拟机IP 172.18.250.10,安装mariadb数据库,实现php对数据库读取数据的处理。

     技术分享

      一、源码安装实现httpd:            

[root@localhost ~]# yum -y groupinstall "Development tools""Desktop Platform Development"
[root@localhost ~]# yum -y install pcre-devel openssl-devel
   //先安装开发工具和按装httpd时所要依赖到的包
[root@localhost ~]# tar xf apr-1.5.0.tar.bz2              //httpd2.4需要apr的版本超过1.4
[root@localhost ~]# cd apr-1.5.0./configure --prefix=/usr/local/apr
[root@localhost ~]# make && make install

[root@localhost ~]# tar xf apr-util-1.5.3.tar.bz2     //httpd2.4需要apr-util的版本超过1.4
[root@localhost ~]# cd apr-util-1.5.3./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr
[root@localhost ~]# make && make install

   1、 编译安装httpd:

[root@localhost ~]# tar -xf httpd-2.4.6.tar.bz2
[root@localhost ~]# ls
anaconda-ks.cfg  httpd-2.4.6  httpd-2.4.6.tar.bz2
[root@localhost ~]# cd httpd-2.4.6
[root@localhost httpd-2.4.6]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --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=prefork --enable-fcgi
[root@localhost ~]# make && make install

  2、链接apache的httpd到环境变量的路径中,让系统能找到httpd

[root@localhost httpd-2.4.6]# ln -s /usr/local/apache/bin/httpd /usr/bin/httpd24

   3、设置启动httpd的脚本

[root@localhost httpd-2.4.6]# cp/usr/local/apache/bin/apachectl /etc/init.d/httpd24
[root@localhost httpd-2.4.6]# vim /etc/init.d/httpd24
#!/bin/sh                    //在/bin/sh下面添加即可
# chkconfig: 35 50 50     //设置服务识别参数,3、5级别启动,启动顺序50,关闭顺序50
# description: Apache     //服务描述信息
[root@localhost httpd-2.4.6]# chkconfig --add httpd24
[root@localhost httpd-2.4.6]# hkconfig --level 35 httpd24 on //开机自启

  4、修改主配置文件,注释DocumentRoot,开启虚拟主机。

[root@localhost httpd24]# vim httpd.conf 
 #DocumentRoot "/usr/local/apache/htdocs"
 # Virtual hosts
 Include /etc/httpd24/extra/httpd-vhosts.conf

 5、设置虚拟主机,开启反向代理,让httpd只处理静态页面

 <VirtualHost 172.18.250.77:80>
    ServerName         //设置虚拟主机名
    DocumentRoot "/www/blog"    //设置虚拟主机URL
   <Directory "/www/blog">      //为URL设置权限
    Options None                //是否设置选项列表
    AllowOverride None          //与访问控制相关的哪些指令可以放在.htaccess文件中
    Require all granted         //设置访问要求 
   </Directory>                  
    ProxyRequests Off           //是否开启正向代理
    ProxyPassMatch ^/(.*\.php)$  fcgi://172.18.250.9:9000/www/blog/$1 //把所有以.php结尾 的文件都转发给php服务器
 </VirtualHost>

6、创建虚拟主机的路径,验证虚拟主机是否正常工作。

[root@localhost httpd24]# apachectl start
[root@localhost httpd24]# ss -tan

LISTEN      0      128       :::80        :::*
[root@localhost blog]# mkdir -p /www/blog/
[root@localhost blog]# cd /www/blog/
[root@localhost blog]# echo "www.b.net" >index.html

 技术分享  出现这个表示httpd正常工作

 二、源码安装PHP

   先解决php安装要依赖到的包

[root@localhost tmp]# yum -y install libmcrypt libmcrypt-devel openssl-devel bzip2-devel

   因为php安装时需要用到数据库的文件,所以先解压一个mariadb,不需要安装

[root@localhost tmp]# tar -xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/
[root@localhost tmp]# ln -s mariadb-5.5.43-linux-x86_64  mysql

   1、编译安装PHP

[root@localhost tmp]# tar -xf php-5.4.36.tar.bz2
[root@localhost tmp]# cd php-5.4.36
[root@localhost tmp]# ./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 --enable-maintainer-zts

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

 2、为php提供配置文件

[root@localhost php-5.4.36]# cp php.ini-production /etc/php.ini

 3、为php-fpm提供配置文件

[root@localhost php-5.4.36]# cd /usr/local/php/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf

 4、为php-fpm提供启动脚本

[root@localhost php-5.4.36]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@localhost php-5.4.36]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-5.4.36]# chkconfig --add php-fpm

  5、编辑php-fpm,设置参数

[root@localhost php-5.4.36]# vim /usr/local/php/etc/php-fpm.conf
pid = /usr/local/php/var/run/php-fpm.pid      //设置PID
listen = 172.18.250.9:9000                    //设置php的监听端口
listen.allowed_clients = 172.18.250.77        //设置httpd所在的IP地址
pm.max_children = 50                          //设置最大子进程数量
pm.start_servers = 5                          //程序启动开启的子进程数
pm.min_spare_servers = 2                      //最小空闲子进程
pm.max_spare_servers = 8                      //最大空闲子进程

 6、启动php-fpm程序,查看监听端口

[root@localhost php-5.4.36]# service php-fpm start
[root@lsj etc]# ss -tan
LISTEN      0      128        172.18.250.9:9000       *:*

 7、修改httpd的配置文件,开启fcgi,开启模块,添加两语句让httpd支持php语言脚本

[root@localhost httpd24]# vim httpd.conf 
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so   //取消注释
LoadModule proxy_module modules/mod_proxy.so             //取消注释

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps     //让httpd识别php代码

 8、创建php的访问目录,和httpd上的一致

[root@localhost tmp]# mkdir /www/blog/
[root@localhost tmp]# cd /www/blog/
[root@localhost blog]# vim index.php
  <?php
    phpinfo();
  ?>

 9、测试httpd能不能识别.php的文件并转发给php服务器。

技术分享

  OK,出现这个页面表示能正常转发

 三、源码安装通用二进制数据库mariadb.

[root@localhost ~]# tar -xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/
[root@localhost local]# ln -s mariadb-5.5.43-linux-x86_64/ mysql

  1、先创建mysql系统用户和mysqlx系统组,让mariadb运行在mysql上,保证安全性

[root@localhost mysql]# groupadd -r mysql
[root@localhost mysql]# useradd -r -g mysql -M -s /sbin/nologin mysql

  2、创建mysql存放数据的目录 

[root@localhost mysql]# mkdir /data/mydata
[root@localhost mysql]# chown -R mysql:mysql /data/mydata  //设置权限

  3、复制mysql的配置文件到/etc下

[root@localhost support-files]# cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
注:  my-huge.cnf : 用于高端产品服务器,包括1到2GB RAM,主要运行mysql  
      my-innodb-heavy-4G.ini : 用于只有innodb的安装,最多有4GB RAM,支持大的查询和低流量
      my-large.cnf : 用于中等规模的产品服务器,包括大约512M RAM
      my-medium.cnf : 用于低端产品服务器,包括很少内存(少于128M)
      my-small.cnf : 用于最低设备的服务器,只有一点内存(少于512M)
[root@localhost support-files]#
[mysqld]
skip_name_resolve = ON           //禁止mysql反解主机名
datadir = /data/mydata           //设置存储数据目录
innodb_file_per_table = ON       //修改InnoDB为独立表空间模式

 4、设置mysql启动脚本

[root@localhost support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost support-files]# chkconfig --add mysql

 5、初始化mysql ,启动mysql

[root@localhost support-files]# /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql
[root@localhost support-files]#  service mysqld start
Starting MySQL.. SUCCESS!

 6、对数据库进行安全加固

[root@localhost support-files]# /usr/local/mysql/bin/mysql_secure_installation
  Enter current password for root (enter for none):     //直接回车,因为密码为空
  Set root password? [Y/n] y                           //设置新密码
  New password: 
  Remove anonymous users? [Y/n] y                      //删除其他没用的用户
  ... Success!
  Disallow root login remotely? [Y/n] n               //禁止远程登录mysql,建议禁止
   ... skipping
  Remove test database and access to it? [Y/n] y      //是否删除测试数据库
  - Dropping test database...
  Reload privilege tables now? [Y/n] y               //是否重新加载权限列表
  ... Success!

  7、授权一个远程账号,让PHP能连接上mysql,并创建个数据库

MariaDB [(none)]> create mytest;
MariaDB [(none)]> grant all on mytest.* to admin@‘172.18.250.10‘ identified by "admin"

 8、测试php能否连接上mysql 

[root@localhost etc]# cd /www/blog/
[root@localhost blog]# vim index.php 
<?php
    $conn = mysql_connect(‘172.18.250.76‘,‘admin‘,‘admin‘);
     if ($conn)
       echo "sucess";
     else
       echo "false";
?>

 技术分享

 到此源码安装已结束,httpd能正常处理静态页面,能转发动态页面给PHP,PHP也能连接到数据库获取数据


  四、安装WordPress博客来验证动静分离

    1、在httpd服务器的URL目录和php的URL目录都安装上wordpress

[root@loalhost blog]# ls                           //php服务器
index.php  wordpress  wordpress-4.3.1-zh_CN.zip
[root@localhost blog]# ls
index.html  wordpress  wordpress-4.3.1-zh_CN.zip   //apache服务

  2、在php服务器上修改wordpress文件  

[root@localhost wordpress]# cp wp-config-sample.php wp-config.php
[root@localhost wordpress]# vim wp-config.php
define(‘DB_NAME‘, ‘mytest‘);                   //创建数据库的名称
define(‘DB_USER‘, ‘admin‘);                    //数据库账号
define(‘DB_PASSWORD‘, ‘admin‘);                //数据库密码
define(‘DB_HOST‘, ‘172.18.250.76‘);            //数据库服务器

 3、重启php-fpm服务,验证wordpress能否正常访问

[root@localhost wordpress]# service php-fpm restart

技术分享

出现这个页面表示httpd已经不能识别.php的文件,需要修改httpd的配置文件

[root@localhost blog]# vim /etc/httpd24/httpd.conf 
<IfModule dir_module>
    DirectoryIndex  index.php index.html   //添加index.php
</IfModule>
[root@localhost blog]# /usr/local/apache/bin/apachectl restart

再次刷新页面:

技术分享


登录博客:

技术分享

OK,博客能正常加载


五、对博客进行压力测试

[root@localhost ~]# ab -n100 -c10 http://172.18.250.77/wordpress/index.php

Benchmarking 172.18.250.77 (be patient).....done

Server Software:        Apache/2.4.6
Server Hostname:        172.18.250.77
Server Port:            80

Document Path:          /wordpress/index.php
Document Length:        0 bytes

Concurrency Level:      10
Time taken for tests:   6.370 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      27900 bytes
HTML transferred:       0 bytes
Requests per second:    15.70 [#/sec] (mean)    //每秒请求的个数
Time per request:       636.993 [ms] (mean)
Time per request:       63.699 [ms] (mean, across all concurrent requests)
Transfer rate:          4.28 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:   212  606 121.9    583     939
Waiting:      211  605 121.9    582     938
Total:        212  606 121.8    583     939

Percentage of the requests served within a certain time (ms)
  50%    583
  66%    672
  75%    697
  80%    706
  90%    751
  95%    809
  98%    919
  99%    939
 100%    939 (longest request)

安装Xcache模块对PHP进行加速

[root@localhost src]# tar xf xcache-3.2.0.tar.bz2
[root@localhost src]# cd xcache-3.2.0
[root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize    //检查php的环境版本
[root@localhost xcache-3.2.0]# ./configure --enable-xcache--with-php-config=/usr/local/php5/bin/php-config
[root@localhost xcache-3.2.0]# make && make install

makeinstall完会出现如下信息:

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

让php读取xcache的配置文件

[root@localhost src]# mkdir /etc/php.d
[root@localhost src]# cp xcache.ini /etc/php.d/

   编辑xcache的配置文件,添加下面这段话

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

 重启php-fpm程序,验证Xcache是是否加载上

 技术分享

出现这个表示Xcache已经正常加载,在测试下

[root@localhost ~]# ab -n100 -c10 http://172.18.250.77/wordpress/index.php

Benchmarking 172.18.250.77 (be patient).....done


Server Software:        Apache/2.4.6
Server Hostname:        172.18.250.77
Server Port:            80

Document Path:          /wordpress/index.php
Document Length:        0 bytes

Concurrency Level:      10
Time taken for tests:   2.286 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Non-2xx responses:      100
Total transferred:      27900 bytes
HTML transferred:       0 bytes
Requests per second:    43.74 [#/sec] (mean)         //实现了3倍加速请求
Time per request:       228.603 [ms] (mean)
Time per request:       22.860 [ms] (mean, across all concurrent requests)
Transfer rate:          11.92 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:    65  218  49.0    222     362
Waiting:       64  218  49.0    222     362
Total:         65  218  48.9    222     363

Percentage of the requests served within a certain time (ms)
  50%    222
  66%    235
  75%    248
  80%    253
  90%    280
  95%    323
  98%    337
  99%    363
 100%    363 (longest request)

源码安装实现lamp的amp分离及Xcache加速

标签:数据库   虚拟机   程序   

原文地址:http://lanxianting.blog.51cto.com/7394580/1766610

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!