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:
[[email protected] ~]# yum -y groupinstall "Development tools""Desktop Platform Development" [[email protected] ~]# yum -y install pcre-devel openssl-devel //先安装开发工具和按装httpd时所要依赖到的包
[[email protected] ~]# tar xf apr-1.5.0.tar.bz2 //httpd2.4需要apr的版本超过1.4 [[email protected] ~]# cd apr-1.5.0./configure --prefix=/usr/local/apr [[email protected] ~]# make && make install [[email protected] ~]# tar xf apr-util-1.5.3.tar.bz2 //httpd2.4需要apr-util的版本超过1.4 [[email protected] ~]# cd apr-util-1.5.3./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr [[email protected] ~]# make && make install
1、 编译安装httpd:
[[email protected] ~]# tar -xf httpd-2.4.6.tar.bz2 [[email protected] ~]# ls anaconda-ks.cfg httpd-2.4.6 httpd-2.4.6.tar.bz2 [[email protected] ~]# cd httpd-2.4.6 [[email protected] 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 [[email protected] ~]# make && make install
2、链接apache的httpd到环境变量的路径中,让系统能找到httpd
[[email protected] httpd-2.4.6]# ln -s /usr/local/apache/bin/httpd /usr/bin/httpd24
3、设置启动httpd的脚本
[[email protected] httpd-2.4.6]# cp/usr/local/apache/bin/apachectl /etc/init.d/httpd24 [[email protected] httpd-2.4.6]# vim /etc/init.d/httpd24 #!/bin/sh //在/bin/sh下面添加即可 # chkconfig: 35 50 50 //设置服务识别参数,3、5级别启动,启动顺序50,关闭顺序50 # description: Apache //服务描述信息 [[email protected] httpd-2.4.6]# chkconfig --add httpd24 [[email protected] httpd-2.4.6]# hkconfig --level 35 httpd24 on //开机自启
4、修改主配置文件,注释DocumentRoot,开启虚拟主机。
[[email protected] 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、创建虚拟主机的路径,验证虚拟主机是否正常工作。
[[email protected] httpd24]# apachectl start [[email protected] httpd24]# ss -tan LISTEN 0 128 :::80 :::* [[email protected] blog]# mkdir -p /www/blog/ [[email protected] blog]# cd /www/blog/ [[email protected] blog]# echo "www.b.net" >index.html
出现这个表示httpd正常工作
二、源码安装PHP
先解决php安装要依赖到的包
[[email protected] tmp]# yum -y install libmcrypt libmcrypt-devel openssl-devel bzip2-devel
因为php安装时需要用到数据库的文件,所以先解压一个mariadb,不需要安装
[[email protected] tmp]# tar -xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/ [[email protected] tmp]# ln -s mariadb-5.5.43-linux-x86_64 mysql
1、编译安装PHP
[[email protected] tmp]# tar -xf php-5.4.36.tar.bz2 [[email protected] tmp]# cd php-5.4.36 [[email protected] 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提供配置文件
[[email protected] php-5.4.36]# cp php.ini-production /etc/php.ini
3、为php-fpm提供配置文件
[[email protected] php-5.4.36]# cd /usr/local/php/etc/ [[email protected] etc]# cp php-fpm.conf.default php-fpm.conf
4、为php-fpm提供启动脚本
[[email protected] php-5.4.36]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [[email protected] php-5.4.36]# chmod +x /etc/rc.d/init.d/php-fpm [[email protected] php-5.4.36]# chkconfig --add php-fpm
5、编辑php-fpm,设置参数
[[email protected] 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程序,查看监听端口
[[email protected] php-5.4.36]# service php-fpm start [[email protected] etc]# ss -tan LISTEN 0 128 172.18.250.9:9000 *:*
7、修改httpd的配置文件,开启fcgi,开启模块,添加两语句让httpd支持php语言脚本
[[email protected] 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上的一致
[[email protected] tmp]# mkdir /www/blog/ [[email protected] tmp]# cd /www/blog/ [[email protected] blog]# vim index.php <?php phpinfo(); ?>
9、测试httpd能不能识别.php的文件并转发给php服务器。
OK,出现这个页面表示能正常转发
三、源码安装通用二进制数据库mariadb.
[[email protected] ~]# tar -xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/ [[email protected] local]# ln -s mariadb-5.5.43-linux-x86_64/ mysql
1、先创建mysql系统用户和mysqlx系统组,让mariadb运行在mysql上,保证安全性
[[email protected] mysql]# groupadd -r mysql [[email protected] mysql]# useradd -r -g mysql -M -s /sbin/nologin mysql
2、创建mysql存放数据的目录
[[email protected] mysql]# mkdir /data/mydata [[email protected] mysql]# chown -R mysql:mysql /data/mydata //设置权限
3、复制mysql的配置文件到/etc下
[[email protected] 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) [[email protected] support-files]# [mysqld] skip_name_resolve = ON //禁止mysql反解主机名 datadir = /data/mydata //设置存储数据目录 innodb_file_per_table = ON //修改InnoDB为独立表空间模式
4、设置mysql启动脚本
[[email protected] support-files]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [[email protected] support-files]# chkconfig --add mysql
5、初始化mysql ,启动mysql
[[email protected] support-files]# /usr/local/mysql/scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql [[email protected] support-files]# service mysqld start Starting MySQL.. SUCCESS!
6、对数据库进行安全加固
[[email protected] 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 [email protected]‘172.18.250.10‘ identified by "admin"
8、测试php能否连接上mysql
[[email protected] etc]# cd /www/blog/ [[email protected] 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
[[email protected] blog]# ls //php服务器 index.php wordpress wordpress-4.3.1-zh_CN.zip [[email protected] blog]# ls index.html wordpress wordpress-4.3.1-zh_CN.zip //apache服务
2、在php服务器上修改wordpress文件
[[email protected] wordpress]# cp wp-config-sample.php wp-config.php [[email protected] 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能否正常访问
[[email protected] wordpress]# service php-fpm restart
出现这个页面表示httpd已经不能识别.php的文件,需要修改httpd的配置文件
[[email protected] blog]# vim /etc/httpd24/httpd.conf <IfModule dir_module> DirectoryIndex index.php index.html //添加index.php </IfModule> [[email protected] blog]# /usr/local/apache/bin/apachectl restart
再次刷新页面:
登录博客:
OK,博客能正常加载
五、对博客进行压力测试
[[email protected] ~]# 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进行加速
[[email protected] src]# tar xf xcache-3.2.0.tar.bz2 [[email protected] src]# cd xcache-3.2.0 [[email protected] xcache-3.2.0]# /usr/local/php/bin/phpize //检查php的环境版本 [[email protected] xcache-3.2.0]# ./configure --enable-xcache--with-php-config=/usr/local/php5/bin/php-config [[email protected] xcache-3.2.0]# make && make install
makeinstall完会出现如下信息:
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/
让php读取xcache的配置文件
[[email protected] src]# mkdir /etc/php.d [[email protected] 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已经正常加载,在测试下
[[email protected] ~]# 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)