LAMP架构之httpd+(php-fpm)+mariadb
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LAMP架构之httpd+(php-fpm)+mariadb相关的知识,希望对你有一定的参考价值。
前言:
FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail-Over特性等等。
CentOS 7:
httpd-2.4:rpm包默认编译支持了fcgi模块;
php-fpm包:专用于将php运行于fpm模式;
============================================================
CentOS 7, lamp(php-fpm);
实现:
(1) 三者分离于三台主机;
(2) 一个虚拟主机用于提供phpMyAdmin;另一个虚拟主机用于提供wordpress;
(3) 为php提供xcache;
ON http(172.16.17.71):
1)安装httpd程序包
[[email protected] ~]# yum -y install httpd
2)编辑httpd的配置文件
ServerName www.example.com:80 //启用ServerName,否则httpd会反解ip地址,可能会报错 #DocumentRoot "/var/www/html" //注释掉默认的中心主机 <IfModule dir_module> DirectoryIndex index.html index.php //添加对index.php主页的支持 </IfModule> AddType application/x-httpd-php .php //添加对.php页面文件的支持 AddType application/x-httpd-php-source .phps
3)新建/etc/httpd/conf.d/vhosts.conf,提供虚拟主机www.pma.com以及www.wordpress.com
<VirtualHost *:80> ServerName www.pma.com DocumentRoot "/web/pma/htdocs" ProxyRequests Off //httpd以反向代理方式跟后端的php-fpm建立连接,所以这里将正向代理关闭从而启用反向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.17.72:9000/web/pma/htdocs/$1 //定义将以.php结尾的URL请求通过fcgi协议转发至后端的php-fpm,注意fcgi后跟的是本地文件路径,$1就表示^/(.*\.php)$ <Directory "/web/pma/htdocs"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName www.wordpress.com DocumentRoot "/web/wordpress/htdocs" ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.17.72:9000/web/wordpress/htdocs/$1 <Directory "/web/wordpress/htdocs"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
4)安装配置PHPmyadmin程序
[[email protected] ~]# mkdir -pv /web/{pma,wordpress} [[email protected] ~]# unzip phpMyAdmin-4.4.14.1-all-languages.zip -d /web/pma/ [[email protected] ~]# mv /web/pma/phpMyAdmin-4.4.14.1-all-languages/ /web/pma/htdocs [[email protected] ~]# cd /web/pma/htdocs [[email protected] htdocs]# cp config.sample.inc.php config.inc.php [[email protected] htdocs]# openssl rand -base64 20 5Cfn69rDk+6I3Twd4KtgLfT/v5k= 编辑config.inc.php文件,将生成的随机数复制到里面,然后更改数据库连接主机: $cfg[‘blowfish_secret‘] = ‘5Cfn69rDk+6I3Twd4KtgLfT/v5k‘; $cfg[‘Servers‘][$i][‘host‘] = ‘172.16.17.73‘;
5)安装配置wordpress程序
[[email protected] ~]# unzip wordpress-4.3.1-zh_CN.zip -d /web/wordpress/ [[email protected] ~]# mv /web/wordpress/wordpress/ /web/wordpress/htdocs [[email protected] ~]# cd /web/wordpress/htdocs [[email protected] 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.73‘);
6)启动httpd服务
[[email protected] ~]# httpd -t Syntax OK [[email protected] ~]# systemctl start httpd.service
ON php(172.16.17.72):
1)安装php-fpm相关程序包
[[email protected] ~]# yum -y install php-fpm php-mysql php-mbstring
2)编辑配置文件,更监听地址及允许请求的客户端
[[email protected] ~]# vim /etc/php-fpm.d/ listen = 172.16.17.72:9000 listen.allowed_clients = 172.16.17.71
3)创建session目录,并确保运行php-fpm进程的用户对此目录有读写权限
[[email protected] ~]# mkdir /var/lib/php/session
[[email protected] ~]# chown -R apache.apache /var/lib/php/session
4)复制web服务器的网页某到php服务器上一份,客户端访问时,获取静态资源会访问到web服务器,访问动态资源将访问到php服务器
[[email protected] ~]# scp -r [email protected]:/web /
5)启动php-fpm进程
[[email protected] ~]# service php-fpm start [[email protected] ~]# ss -tnl | grep 9000 LISTEN 0 128 172.16.17.72:9000 *:* [[email protected] ~]# ps aux | grep php-fpm root 28850 0.3 2.1 323968 10292 ? Ss 06:39 0:00 php-fpm: master process (/etc/php-fpm.conf) apache 28852 0.0 1.0 326052 4940 ? S 06:39 0:00 php-fpm: pool www apache 28853 0.0 1.0 326052 4944 ? S 06:39 0:00 php-fpm: pool www apache 28854 0.0 1.0 326052 4944 ? S 06:39 0:00 php-fpm: pool www apache 28855 0.0 1.0 326052 4944 ? S 06:39 0:00 php-fpm: pool www apache 28856 0.0 1.0 326052 4944 ? S 06:39 0:00 php-fpm: pool www root 28875 0.0 0.1 112640 960 pts/0 R+ 06:40 0:00 grep --color=auto php-fpm
ON mariadb(172.16.17.73):
1)创建运行mariadb的用户
[[email protected] ~]# groupadd -r mysql [[email protected] ~]# useradd -r -g mysql mysql
2)创建数据库存放目录
[[email protected] ~]# mkdir -pv /data/mysql [[email protected] ~]# chown -R mysql.mysql /data/mysql/
3)解压mariadb程序包至/usr/local,并创建mysql软链接
[[email protected] ~]# tar xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/ [[email protected] ~]# cd /usr/local/ [[email protected] local]# ln -sv mariadb-5.5.46-linux-x86_64/ mysql
4)初始化数据库
[[email protected] local]# chown -R root.mysql mysql/ [[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
5)提供mysql主配置文件并编辑之
[[email protected] mysql]# mkdir /etc/mysql [[email protected] 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服务脚本
[[email protected] mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [[email protected] mysql]# chmod +x /etc/rc.d/init.d/mysqld [[email protected] mysql]# chkconfig --add mysqld [[email protected] mysql]# chkconfig mysqld on
7)启动mysqld服务
[[email protected] ~]# service mysqld start
8)创建wpdb库,授权wpuser用户连接
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 ‘[email protected]#‘; MariaDB [(none)]> FLUSH PRIVILEGES;
加本地hosts文件解析,打开浏览器添访问网站(配置好php-fpm后需要重启一下httpd服务):
客户端使用ab命令对服务器进行压力测试:
[[email protected] ~]# ab -c 100 -n 1000 http://www.pma.com/index.php
ON http(172.16.17.72):为php服务器编译安装xcache
1)安装php-devel包及开发工具包组
[[email protected] ~]# yum -y install php-devel [[email protected] ~]# yum -y groupinstall "Development Tools" "Server Platform Development"
2)解压xcache源码包至/usr/local下
[[email protected] ~]# tar xf xcache-3.2.0.tar.bz2 -C /usr/local/ [[email protected] ~]# mv /usr/local/xcache-3.2.0/ /usr/local/xcache
3)生成configure脚本
[[email protected] xcache]# phpize
4)执行编译安装
[[email protected] xcache]# ./configure --enable-xcache --with-php-config=/usr/bin/php-config [[email protected] xcache]# make -j 4 && make install
5)复制xcache的ini文件至/etc/php.d目录下
[[email protected] xcache]# cp xcache.ini /etc/php.d/
6)重启php-fpm服务
[[email protected] ~]# systemctl restart php-fpm.service
再次在客户端使用ab命令对服务器进行压力测试:
[[email protected] ~]# ab -c 100 -n 1000 http://www.pma.com/index.php
以上是关于LAMP架构之httpd+(php-fpm)+mariadb的主要内容,如果未能解决你的问题,请参考以下文章