以下基于fastcgi方式
httpd+php结合的方式:
- [ ] module: php
- [x] fastcgi : php-fpm
- CentOS 6:
PHP-5.3.2之前:默认不支持fpm机制;需要自行打补丁并编译安装
httpd-2.2:默认不支持fcgi协议,需要自行编译此模块
解决方案:编译安装httpd-2.4, php-5.3.3+
- CentOS 7:
httpd-2.4:rpm包默认编译支持fcgi模块
php-fpm包:专用于将php运行于fpm模式
配置fastcgi
php配置
配置文件:/etc/php.ini,/etc/php.d/*.ini
Module下,重启Httpd服务
FastCGI模式下,重启php-fpm服务配置文件格式
配置文件格式:[foo]:Section Header
Directive=value
注释符:# 纯粹的注释信息
; 用于注释可启动的指令
说明:在较新的版本中,已经完全使用”;”进行注释
php.ini核心配置的详细说明: http://php.net/manual/zh/ini.core.php
Php.ini配置选项列表: http://php.net/manual/zh/ini.list.php
php网站出现故障时,在客户端会暴露数据库查询信息等重要信息,故而只适合中小型企业,大型企业用java开发,编译为二进制,即使出现故障,客户端看到的也是二进制信息,没有任何意义
1、安装fastcgi的安装包php-fpm
[root@centos7:html]# yum remove php
[root@centos7:~]# yum install php-fpm -y
[root@centos7:~]# rpm -ql php-fpm
/etc/logrotate.d/php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d
/etc/php-fpm.d/www.conf #配置文件
/etc/sysconfig/php-fpm
/run/php-fpm
/usr/lib/systemd/system/php-fpm.service #以独立的服务存在
/usr/lib/tmpfiles.d/php-fpm.conf
/usr/sbin/php-fpm #独立的程序
/usr/share/doc/php-fpm-5.4.16
/usr/share/doc/php-fpm-5.4.16/fpm_LICENSE
/usr/share/doc/php-fpm-5.4.16/php-fpm.conf.default
/usr/share/fpm
/usr/share/fpm/status.html
/usr/share/man/man8/php-fpm.8.gz
/var/log/php-fpm #日志文件
#apache不知道fastcgi的存在,需要配置apache的配置文件,让apache好把php动态页面交给fastcgi处理
[root@centos7:~]# vim /etc/php-fpm.d/www.conf #配置文件
pm.max_children = 50 #实际中调大一点
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
;pm.max_requests = 500
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache
#上述未更改任何参数
[root@centos7:conf.d]# cd /etc/httpd/conf.d/
[root@centos7:conf.d]# ls
autoindex.conf cobbler.conf README userdir.conf welcome.conf
[root@centos7:conf.d]# vim fcgi.conf
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
注意:在HTTPD服务器上必须启用proxy_fcgi_module模块,充当PHP客户端
httpd -M |grep fcgi
cat /etc/httpd/conf.modules.d/00-proxy.conf
[root@centos7:conf.d]# systemctl start php-fpm
[root@centos7:html]# systemctl restart httpd
浏览器打开http://192.168.31.7/upload/ 仍然可以访问
2、实验1
三台主机,httpd服务器,apache之php-fpm服务器,数据库服务器
1、更换php程序
#一般php-fpm和httpd是在一个服务器上,此处实验放在两个主机上了,仅实验
php服务器
[root@cos7-1:~]# yum install php-fpm php-mysql mariadb -y
[root@cos7-1:~]# vim /data/website/mysql1.php
<?php
$conn = mysql_connect('192.168.31.27','phpuser','centos');
if ($conn)
echo "OK";
else
echo "Failure";
#echo mysql_error();
mysql_close();
?>
[root@cos7-2:~ ]# yum install mariadb-server -y
[root@cos7-2:~ ]# systemctl start mariadb
[root@cos7-2:~ ]# mysql -e "grant all on *.* to phpuser@'192.168.31.%' identified by 'centos'";
[root@cos7-2:~ ]# mysql -e "select user from mysql.user" #可以查询到用户phpuser
[root@cos7-1:website]# vim /etc/php-fpm.d/www.conf
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 9000
;listen.allowed_clients = 127.0.0.1 #或者把此行注释掉,默认即为下面一行配置
listen.allowed_clients = any #允许任何主机连接
[root@centos7:html]# systemctl restart php-fpm.service
[root@centos7:html]# ss -ntl #9000端口监听在所有ip上
[root@centos7:~]# vim /etc/httpd/conf.d/fcgi.conf
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.31.17:9000/data/website/$1
[root@centos7:~]# systemctl restart httpd
浏览器输入:http://192.168.31.7/mysql1.php
—————————————————————————————————————
2、测试更换为php-fpm的网站
192.168.31.17当数据库服务器 ,192.168.31.7当网站服务器
php的fastcgi;www.wordpress.com;www.phpmyadmin.com;www.discuz.com
[root@cos7-1:~]# yum install mariadb-server -y
mysql -e "create database wpdb;grant all on wpdb.* to wpuser@'192.168.31.%' identified by 'centos'";
[root@centos7:conf]# mysql -uwpuser -pcentos -h192.168.31.17 #测试可以连接
[root@centos7:~]# yum install httpd mariadb php-mysql -y
[root@centos7:~]# mkdir /data/wordpress
[root@centos7:~]# mkdir /data/discurz
[root@centos7:~]# vim /etc/httpd/conf.d/test.conf
DirectoryIndex index.php #默认主页面
<VirtualHost *:80>
ServerName www.wordpress.com
DocumentRoot /data/wordpress
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
<Directory "/data/wordpress/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.discurz.com
DocumentRoot /data/discurz
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/discurz/$1
<Directory "/data/discurz/">
Require all granted
</Directory>
</VirtualHost>
[root@centos7:~]# systemctl start httpd php-fpm
[root@centos7:data]# ls
wordpress-4.9.4-zh_CN.tar.gz upload
Discuz_X3.3_SC_UTF8.zip utility
wordpress readme
[root@centos7:data]# mv upload discurz
[root@centos7:wordpress]# mv wp-config-sample.php wp-config.php
[root@centos7:wordpress]# vim wp-config.php
define('DB_NAME', 'wpdb');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuesr');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'centos');
/** MySQL主机 */
define('DB_HOST', '192.168.31.17');
[root@centos7:data]# setfacl -R -m u:apache:rwx discurz/
[root@centos6 ~ ]#vim /etc/hosts
192.168.31.7 www.wordpress.com www.discurz.com
图6
实验2 编译安装
编译安装LAMP基于fastcgi模式
各版本信息如下
apr-1.6.3.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.34.tar.bz2
php-7.1.18.tar.bz2
mariadb-10.2.15-linux-x86_64.tar.gz
wordpress-4.9.4-zh_CN.tar.gz
①httpd编译安装
[root@centos7:data]# yum install "development tools" -y
[root@centos7:data]# yum install openssl-devel pcre-devel expat-devel -y
[root@centos7:data]# tar xf httpd-2.4.34.tar.bz2
[root@centos7:data]# tar xf apr-1.6.3.tar.gz
[root@centos7:data]# tar xf apr-util-1.6.1.tar.gz
[root@centos7:data]# mv apr-1.6.3 httpd-2.4.34/srclib/apr
[root@centos7:data]# mv apr-util-1.6.1 httpd-2.4.34/srclib/apr-util
[root@centos7:data]# cd httpd-2.4.34/
[root@centos7:httpd-2.4.34]# ./configure --prefix=/app/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
[root@centos7:httpd-2.4.34]# make -j 4 && make install
[root@centos7:httpd-2.4.34]# echo PATH=/app/httpd24/bin:$PATH > /etc/profile.d/httpd.sh
[root@centos7:httpd-2.4.34]# . /etc/profile.d/httpd.sh
[root@centos7:httpd-2.4.34]# apachectl start
#浏览器输入192.168.31.7显示It works!
[root@centos7:~]#ps aux
root 35833 0.0 0.1 99484 2324 ? Ss 09:23 0:00 /app/httpd24/bin/httpd -k start
daemon 35834 0.0 0.1 101568 2588 ? S 09:23 0:00 /app/httpd24/bin/httpd -k start
daemon 35835 0.0 0.0 101568 1932 ? S 09:23 0:00 /app/httpd24/bin/httpd -k start
daemon 35836 0.0 0.0 101568 1932 ? S 09:23 0:00 /app/httpd24/bin/httpd -k start
daemon 35837 0.0 0.0 101568 1932 ? S 09:23 0:00 /app/httpd24/bin/httpd -k start
daemon 35838 0.0 0.0 101568 1932 ? S 09:23 0:00 /app/httpd24/bin/httpd -k start
daemon 35846 0.0 0.0 101568 1936 ? S 09:24 0:00 /app/httpd24/bin/httpd -k start
[root@centos7:~]# useradd -r -s /sbin/nologin apache
[root@centos7:~]# vim /app/httpd24/conf/httpd.conf
User apache
Group apache
[root@centos7:~]# apachectl stop
[root@centos7:~]# apachectl start
[root@centos7:~]# ps aux #进程账号已经由daemon改为apache
#apache开机自启动
vim /etc/rc.d/rc.local
/app/httpd24/bin/apachectl
②mariadb-server编译安装
[root@centos7:data]# tar xvf mariadb-10.2.15-linux-x86_64.tar.gz -C /usr/local
[root@centos7:local]# pwd
/usr/local
[root@centos7:local]# ln -s mariadb-10.2.15-linux-x86_64/ mysql
[root@centos7:local]# ll mysql/ #目录账号不对,都是root和数字
[root@centos7:local]# useradd -r -s /sbin/nologin mysql
[root@centos7:local]# chown -R mysql.mysql mysql/
[root@centos7:local]# chown mysql.mysql /app/mysql/
[root@centos7:mysql]# pwd
/usr/local/mysql
[root@centos7:mysql]# scripts/mysql_install_db --datadir=/app/mysql --user=mysql
[root@centos7:mysql]# ll /app/mysql/
total 110616
-rw-rw---- 1 root root 16384 Aug 13 09:34 aria_log.00000001
-rw-rw---- 1 root root 52 Aug 13 09:34 aria_log_control
-rw-rw---- 1 root root 860 Aug 13 09:34 ib_buffer_pool
-rw-rw---- 1 root root 12582912 Aug 13 09:34 ibdata1
-rw-rw---- 1 root root 50331648 Aug 13 09:34 ib_logfile0
-rw-rw---- 1 root root 50331648 Aug 13 09:34 ib_logfile1
drwx------ 2 root root 6 Aug 13 09:34 mysql
drwx------ 2 root root 6 Aug 13 09:34 test
[root@centos7:mysql]# ls /etc/my.cnf #系统自带的配置文件,不用
/etc/my.cnf
[root@centos7:mysql]# mkdir /etc/mysql/ #自己创建配置文件
[root@centos7:mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf #拷贝配置文件摸版
[root@centos7:mysql]# vim /etc/mysql/my.cnf
[mysqld]
datadir=/app/mysql
[root@centos7:mysql]# cp support-files/mysql.server /etc/init.d/mysqld #服务脚本
[root@centos7:mysql]# chkconfig --add mysqld #添加服务或者开机启动时会自动加载
[root@centos7:mysql]# chkconfig --list # 查看已经加载进去
[root@centos7:mysql]# service mysqld start
[root@centos7:bin]# pwd
/usr/local/mysql/bin
[root@centos7:bin]# vim /etc/profile.d/httpd.sh #把上面的路径添加进去
[root@centos7:httpd-2.4.34]# . /etc/profile.d/httpd.sh
为wordpress网站准备数据库账号
[root@centos7:mysql]# mysql -e "create database wpdb;grant all on wpdb.* to wpuser@'localhost' identified by 'centos'"
③php-fpm编译安装
[root@centos7:data]# tar xvf php-7.1.18.tar.bz2
[root@centos7:data]# ls
apr-1.6.3.tar.gz httpd-2.4.34.tar.bz2 php-7.1.18.tar.bz2
apr-util-1.6.1.tar.gz mariadb-10.2.15-linux-x86_64.tar.gz wordpress-4.9.4-zh_CN.tar.gz
httpd-2.4.34 php-7.1.18
[root@centos7:data]# yum install libxml2-devel bzip2-devel libmcrypt-devel
[root@centos7:data]# cd php-7.1.18/
[root@centos7:php-7.1.18]# pwd
/data/php-7.1.18
[root@centos7:php-7.1.18]# ./configure --prefix=/app/php --enable-mysqlnd --with-mysqli=mysqlnd --with-openssl --with-pdo-mysql=mysqlnd --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-maintainer-zts --disable-fileinfo
[root@centos7:bin]# pwd
/app/php/bin
[root@centos7:bin]# vim /etc/profile.d/httpd.sh #把上面路径添加进去
[root@centos7:bin]# . /etc/profile.d/httpd.sh
[root@centos7:bin]# cd /data/php-7.1.18/
[root@centos7:php-7.1.18]# cp php.ini-production /etc/php.ini #可以更改时区
[root@centos7:conf]# vim /etc/httpd24/conf/httpd.conf
Include conf/extra/php.conf
[root@centos7:extra]# pwd
/app/httpd24/conf/extra
[root@centos7:extra]# vim php.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.php index.html
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1
[root@centos7:extra]# vim /app/httpd24/conf/httpd.conf
取消下面注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
[root@centos7:extra]# apachectl start
[root@centos7:extra]# ss -ntl #80端口已开
[root@centos7:php-7.1.18]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@centos7:php-7.1.18]# chmod +x /etc/init.d/php-fpm
[root@centos7:php-7.1.18]# chkconfig --add php-fpm
[root@centos7:php-7.1.18]# pwd
/data/php-7.1.18
[root@centos7:etc]# cp php-fpm.conf.default php-fpm.conf
[root@centos7:php-fpm.d]# pwd
/app/php/etc/php-fpm.d
[root@centos7:php-fpm.d]# cp www.conf.default www.conf
[root@centos7:php-fpm.d]# vim www.conf
user = apache
group = apache
[root@centos7:php-fpm.d]# service php-fpm start
[root@centos7:extra]# apachectl stop
[root@centos7:extra]# apachectl start
Starting php-fpm done
[root@centos7:php-fpm.d]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 *:*
[root@centos7:htdocs]# pwd
/app/httpd24/htdocs
[root@centos7:htdocs]# vim index.php
<?php
phpinfo();
?>
④wordpress安装
[root@centos7:data]# tar xvf wordpress-4.9.4-zh_CN.tar.gz
[root@centos7:data]# cp -r wordpress/* /app/httpd24/htdocs/
cp: overwrite ‘/app/httpd24/htdocs/index.php’? y
[root@centos7:htdocs]# ls
index.html wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
[root@centos7:htdocs]# mv wp-config-sample.php wp-config.php
[root@centos7:htdocs]# vim wp-config.php
define('DB_NAME', 'wpdb');
/** MySQL数据库用户名 */
define('DB_USER', 'wpuser');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'centos');
/** MySQL主机 */
define('DB_HOST', 'localhost');
浏览器输入192.168.31.7/index.php才可以打开网站
[root@centos7:extra]# vim php.conf
DirectoryIndex index.php index.html 默认第一个主站点页面未能生效
[root@centos7:conf]# pwd
/app/httpd24/conf
[root@centos7:conf]# vim httpd.conf
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
[root@centos7:conf]# apachectl restart
此时在浏览器输入192.168.31.7即可访问wordpress
⑤性能编译安装
性能测试php7.1.18,官网说明php7的版本性能比以前大有提升,可以不用xchache加速
[root@cos7-2:~ ]# yum install httpd-tools
[root@cos7-2:~ ]# ab -c 10 -n 100 http://192.168.31.7/
Requests per second: 88.94 [#/sec] (mean) #比以前版本性能提升了
实验:编译安装httpd-2.4.34,用脚本实现未做
总结
实验:编译安装LAMP基于fastcgi
环境:
centos7.5
apr-1.6.3.tar.gz
apr-util-1.6.1.tar.gz
httpd-2.4.34.tar.bz2
php-7.1.18.tar.bz2
mariadb-10.2.16-linux-x86_64.tar.gz
wordpress-4.9.4-zh_CN.tar.gz
1 实现HTTPD
yum groupinstall “development tools”
yum install pcre-devel openssl-devel expat-devel
useradd -r -s /sbin/nologin apache
tar xf httpd-2.4.34.tar.bz2
tar xf apr-1.6.3.tar.gz
tar xf apr-util-1.6.1.tar.gz
–
mv apr-1.6.3 httpd-2.4.34/srclib/apr
mv apr-util-1.6.1 httpd-2.4.34/srclib/apr-util
cd httpd-2.4.34/./configure –prefix=/app/httpd24 –enable-so –enable-ssl –enable-cgi –enable-rewrite –with-zlib –with-pcre –enable-modules=most –enable-mpms-shared=all –with-mpm=prefork –with-included-apr
make && make install
_
ls /app/httpd24/
echo ’PATH=/app/httpd24/bin:$PATH‘ > /etc/profile.d/lamp.sh
-
vim /app/httpd24/conf/httpd.conf
user apache
group apache
apachectl
ss -ntl
vim /app/httpd24/conf/httpd.conf
取消下面注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
<\IfModule dir_module>
DirectoryIndex index.php index.html
<\/IfModule>
Include conf/extra/php.conf
vim /app/httpd24/conf/extra/php.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1
apachectl stop
apachectl
开机启动
echo “/app/httpd24/bin/apachectl start” >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
2 实现Maraidb
useradd -r -s /sbin/nologin mysql
tar xvf mariadb-10.2.16-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mariadb-10.2.16-linux-x86_64/ mysql
chown -R mysql.mysql mysql/
mkdir /app/mysql
chown mysql.mysql /app/mysql
cd /usr/local/mysql/
scripts/mysql_install_db –datadir=/app/mysql –user=mysql
mkdir /etc/mysql/
cp support-files/my-huge.cnf /etc/mysql/my.cnf
vim /etc/mysql/my.cnf
datadir=/app/mysql
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig –add mysqld
chkconfig –list
service mysqld start
vim /etc/profile.d/lamp.sh
PATH=/app/httpd24/bin:/usr/local/mysql/bin:$PATH
mysql -e “create database wpdb;grant all on wpdb.* to wpuser@’localhost’ identified by ‘centos’”
3 实现PHP
yum install libxml2-devel bzip2-devel libmcrypt-devel
tar xvf php-7.1.18.tar.bz2
cd php-7.1.18/
./configure –prefix=/app/php –enable-mysqlnd –with-mysqli=mysqlnd –with-openssl –with-pdo-mysql=mysqlnd –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –enable-fpm –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –enable-maintainer-zts –disable-fileinfo
make -j 4 && make install
vim /etc/profile.d/lamp.sh
PATH=/app/php/bin:/app/httpd24/bin:/usr/local/mysql/bin:$PATH
cp php.ini-production /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig –add php-fpm
chkconfig php-fpm on
cd /app/php/etc
cp php-fpm.conf.default php-fpm.conf
cp php-fpm.d/www.conf.default php-fpm.d/www.conf
service php-fpm start
4 wordpress
tar xvf wordpress-4.9.4-zh_CN.tar.gz
cd /app/httpd24/htdocs/
mv wp-config-sample.php wp-config.php
vim wp-config.php