web服务器CGI和fastcgi

CGI

CGI:Common Gateway Interface 公共网关接口
CGI 在2000年或更早的时候用得比较多,以前web服务器一般只处理静态的请求,如果碰到一个动态请
求怎么办呢?web服务器会根据这次请求的内容,然后会 fork 一个新进程来运行外部的 C 程序或者
bash,perl脚本等,这个进程会把处理完的数据返回给web服务器,最后web服务器把内容发送给用户,
刚才fork的进程也随之退出。 如果下次用户还请求改动态脚本,那么web服务器又再次fork一个新进
程,周而复始的进行。
CGI 可以让一个客户端,从网页浏览器通过http服务器向执行在网络服务器上的程序传输数据;CGI描述
了客户端和服务器程序之间传输的一种标准

请求流程:
Client -- (http协议) --> httpd -- (cgi协议) --> application server (program file) -- (mysql协议) --> mysql
范例:httpd 利用CGI运行shell脚本

fastcgi

fastcgi的方式是,web服务器收到一个请求时,不会重新fork一个进程(因为这个进程在web服务器启
动时就开启了,而且不会退出),web服务器直接把内容传递给这个进程(进程间通信,但fastcgi使用
了别的方式,tcp方式通信),这个进程收到请求后进行处理,把结果返回给web服务器,最后自己接着
等待下一个请求的到来,而不是退出

请求流程:
Client -- (http协议) --> httpd -- (fastcgi协议) --> fastcgi服务器 -- (mysql协议) --> mysql

CGI和fastcgi 比较

CGI: 兼职, 一次性的过河拆桥式的服务

FASTCGI: 专职,全周期的持续式的服务

名称 在web服务器方面 在对数据进行处理的进程方面
cgi fork一个新的进程进行处理 读取参数,处理数据,然后就结束生命期
fastcgi 用tcp方式跟远程机子上的进程或本地进程建立连接 要开启tcp端口,进入循环,等待数据的到来,处理数据

PHP 配置

php 的配置文件:/etc/php.ini, /etc/php.d/*.ini

配置文件在php解释器启动时被读取
对配置文件的修改生效方法

Modules:重启httpd服务
FastCGI:重启php-fpm服务

/etc/php.ini配置文件格式:

[foo]:Section Header
directive = value

注释符:
以#开头,纯粹的注释信息
以 ; 开头,用于注释可启用的directive
提示:较新的版本中,已经完全使用 “ ; ” 进行注释

php.ini 配置参考文档:

php.ini的核心配置选项文档: http://php.net/manual/zh/ini.core.php
php.ini配置选项列表:http://php.net/manual/zh/ini.list.php

php常见设置:

expose_php = On #响应报文显示首部字段x-powered-by: PHP/x.y.z,暴露php版本,建议为off
max_execution_time= 30 #最长执行时间30s
memory_limit=128M #生产不够,可调大
display_errors=off #调试使用,不要打开,否则可能暴露重要信息
display_startup_errors=off #建议关闭
post_max_size=8M #最大上传数据大小,生产可能调大,比下面项大
upload_max_filesize =2M #最大上传文件,生产可能要调大
max_file_uploads = 20 #同时上传最多文件数
date.timezone =Asia/Shanghai #指定时区
short_open_tag=on #开启短标签,如: <? phpinfo();?>

范例:

[root@centos7 ~]# yum -y install httpd php
[root@centos7 ~]# systemctl start httpd
[root@centos7 ~]# cat /var/www/html/test.php
<?php
phpinfo();
?>
[root@centos7 ~]#cat /var/www/html/session.php
<?php
session_start();
echo session_id();
?>

[root@centos6 ~]# curl -I 10.0.0.7/test.php
HTTP/1.1 200 OK
date: Thu, 02 Apr 2020 04:02:57 GMT
server: Apache/2.4.6 (CentOS) PHP/5.4.16
x-powered-by: PHP/5.4.16 # 暴露PHP版本信息
content-type: text/html; charset=UTF-8
cache-control: private
[root@centos7 ~]# vim /etc/php.ini
expose_php = Off
[root@centos7 ~]# systemctl restart httpd

PHP 语言格式

php语言有两种使用格式:
格式 1

<?php
echo "<h1>Hello world!</h1>"
?>

格式 2

<h1>
<?php echo "Hello world!" ?>
</h1>

LAMP实现方式

httpd 接收用户的web请求;静态资源则直接响应;动态资源为php脚本,对此类资源的请求将交由php来运行

httpd与php结合的方式
modules :将php编译成为httpd的模块libphp5.so,只有prefork 模式才支持
FastCGI

LAMP架构实现
静态资源:
Client -- http --> httpd

动态资源:
Client -- http --> httpd --> libphp5.so () -- mysql --> MySQL server
Client -- http --> httpd -->fastcgi-- mysql --> MySQL server

利用rpm包实现LAMP安装部署

CentOS 8
Module 模块方式

dnf install httpd mariadb-server php php-mysqlnd

FastCGI 方式

dnf install httpd mariadb-server php-fpm php-mysqlnd

CentOS 7
Module 模块方式

yum install httpd mariadb-server php php-mysql

FastCGI 方式

yum install httpd php-fpm php-mysql mariadb-server

CentOS 6:
Module 模块方式

yum install httpd, php, php-mysql, mysql-server

FastCGI方式:默认不支持

注意:基于module实现,httpd 需要 使用prefork模型

使用mysql扩展连接数据库

使用mysql扩展模块mysql.so连接数据,此方式已经在php 7 版后淘汰
范例:php使用mysql扩展连接数据库的测试代码

<?php
$conn = mysql_connect('mysqlserver','username','password');
if ($conn)
  echo "OK";
else
  echo "Failure";
  #echo mysql_error();
  mysql_close();
?>

使用mysqli扩展连接数据库

使用mysqli扩展模块mysqli.so连接数据,此方式只能连接MySQL数据库,不支持其它数据库
范例:php使用mysqli扩展连接数据库的测试代码

<?php
$mysqli=new mysqli("mysqlserver", "username", "password");
if(mysqli_connect_errno()){
  echo "Failure";
  $mysqli=null;
  exit;
}
echo "OK";
$mysqli->close();
?>

使用PDO(PHP Data Object)扩展连接数据库

使用PDO扩展模块pdo_mysql.so连接数据库,此方式可以支持连接MySQL,Oracle等多种数据库
范例:php使用pdo扩展连接数据库的测试代码1

<?php
$dsn='mysql:host=mysqlhost;port=3306;dbname=mysql';
$username='root';
$passwd='magedu';
$dbh=new PDO($dsn,$username,$passwd);
var_dump($dbh);
?>

范例:php使用pdo扩展连接数据库的测试代码2

<?php
try {
$user='root';
$pass='magedu';
$dbh = new PDO('mysql:host=mysqlhost;port=3306;dbname=mysql', $user, $pass);
foreach($dbh->query('SELECT user,host from user') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>

实现LAMP实战案例

范例:CentOS 8 默认使用factcgi模式,可以按下面步骤修改为httpd的模块方式

[root@centos8 ~]# dnf -y install httpd php php-mysqlnd mariadb-server

#修改为prefork模式支持httpd 模块方式
[root@centos8 ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
#LoadModule mpm_event_module modules/mod_mpm_event.so

#CentOS 8的php 默认是factcgi模式 ,修改为httpd模块方式,此步非必须,是可选做
[root@centos8 html]# vim /etc/httpd/conf.d/php.conf
#<IfModule !mod_php5.c>
# <IfModule !mod_php7.c>
# # Enable http authorization headers
# SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
#
# <FilesMatch \.(php|phar)$>
# SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
# </FilesMatch>
# </IfModule>
#</IfModule>

[root@centos8 ~]# vim /var/www/html/lamp.php
[root@centos8 ~]# cat /var/www/html/lamp.php
<?php
try {
$user='root';
$pass='';
$dbh = new PDO('mysql:host=localhost;dbname=mysql', $user, $pass);
foreach($dbh->query('SELECT user,host from user') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
phpinfo();
?>

[root@centos8 ~]# systemctl start httpd mariadb

打开chrome浏览器,访问 http://httpd服务器IP/lamp.php 可以看到php页面,说明LAMP 搭建成功

范例:CentOS 8 部署 phpMyAdmin-5.0.1

[root@centos8 ~]# yum -y install httpd mariadb-server php php-mysqlnd php-json
php-xml
[root@centos8 ~]# systemctl start httpd mariadb
[root@centos8 ~]# mysql_secure_installation
[root@centos8 ~]# unzip phpMyAdmin-5.0.1-all-languages.zip
[root@centos8 ~]# mv phpMyAdmin-5.0.1-all-languages /var/www/html/pma
#如果出错,可以查看日志
[root@centos8 ~]# tail /var/log/php-fpm/www-error.log
#浏览器访问http://LAMP服务器IP/pma/

范例:CentOS 7 利用清华Yum源安装php 7.4 部署 phpMyAdmin-5.0.2

[root@centos7 ~]# yum -y install
https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
[root@centos7 ~]# yum -y install httpd php74-php php74-php-mbstring php74-phpmysqlnd
mariadb-server unzip
[root@centos7 ~]# systemctl enable --now httpd mariadb
[root@centos7 ~]# mysqladmin password magedu
[root@centos7 ~]# wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-
5.0.2-all-languages.zip
[root@centos7 ~]# unzip phpMyAdmin-5.0.2-all-languages.zip
[root@centos7 ~]# mv phpMyAdmin-5.0.2-all-languages/ /var/www/html/pma
#浏览器访问http://LAMP服务器IP/pma/

范例:CentOS 7 利用清华Yum源安装php 7.2 部署 phpMyAdmin-5.0.1

[root@centos7 ~]# yum -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remirelease-
7.rpm

[root@centos7 ~]# yum -y install httpd php72-php php72-php-mbstring php72-php-mysqlnd mariadbserver
[root@centos7 ~]# systemctl start httpd mariadb
[root@centos7 ~]# wget https://files.phpmyadmin.net/phpMyAdmin/5.0.1/phpMyAdmin-5.0.1-alllanguages.
zip
[root@centos7 ~]# yum install unzip -y
[root@centos7 ~]# unzip phpMyAdmin-5.0.1-all-languages.zip
[root@centos7 ~]# cd phpMyAdmin-5.0.1-all-languages/
[root@centos7 ~]# mv phpMyAdmin-5.0.1-all-languages /var/www/html/pma
#设置mysql的root密码,phpMyadmin不允许空密码登录
[root@centos7 ~]# mysql_secure_installation
#浏览器访问http://LAMP服务器IP/pma/

范例:CentOS 7 利用RPM包部署phpMyadmin4.4

[root@centos7 ~]# yum -y install httpd mariadb-server php php-mysql
[root@centos7 ~]# systemctl start mariadb
[root@centos7 ~]# mysql_secure_installation
#下载:https://www.phpmyadmin.net/downloads/
[root@centos7 ~]# unzip phpMyAdmin-4.4.15.10-all-languages.zip
[root@centos7 ~]# mv phpMyAdmin-4.4.15.10-all-languages /var/www/html/phpmyadmin
[root@centos7 ~]# cd /var/www/html/phpmyadmin/
[root@centos7 ~]# cp config.sample.inc.php config.inc.php
#如果mysql 服务器和phpmyadmin不在同一台主机,还需要修改以下设置
[root@centos7 ~]# vim config.inc.php
$cfg['Servers'][$i]['host'] = 'mysqlserverIP';
[root@centos7 ~]# yum -y install php-mbstring
[root@centos7 ~]# systemctl start httpd
#浏览器访问:http://LAMP服务器IP/phpmyadmin,输入root及口令即可登录

范例:CentOS 8 利用RPM包部署 wordpress

[root@centos8 ~]# dnf -y install httpd php php-json php-mysqlnd mariadb-server
[root@centos8 ~]# systemctl enable --now httpd mariadb
[root@centos8 ~]# mysql
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified
by '123456';
[root@centos8 ~]# wget https://cn.wordpress.org/latest-zh_CN.zip
[root@centos8 ~]# tar xvf wordpress-5.4.2-zh_CN.tar.gz
[root@centos8 ~]# mv wordpress/* /var/www/html/
[root@centos8 ~]# chown -R apache.apache /var/www/html/
#浏览器访问
http://LAMP服务器/wordpress
#如果连接数据库失败,可以用127.0.0.1代替localhost

#安全加固
#用浏览器访问https://api.wordpress.org/secret-key/1.1/salt/,替代下面wp-config.php内
容
[root@centos8 ~]# vim /var/www/html/wp-config.php

范例:CentOS 7 利用RPM包部署wordpress

#下载官网地址:https://cn.wordpress.org/
#解压缩WordPress博客程序到网页站点目录下
[root@centos7 ~]# unzip wordpress-5.1.1-zh_CN.zip
[root@centos7 ~]# mv wordpress /var/www/html/
#新建wpdb库和wpuser用户
mysql> create database wpdb;
mysql> grant all on wpdb.* to wpuser@'%' identified by "wppass";
setfacl –R –m u:apache:rwx /var/www/html/wordpress/
#或者chown –R apache.apache /var/www/html/wordpress
#打开http://LAMP服务器IP/wordpress进行页面安装

范例:CentOS 8 利用RPM包部署Discuz!

[root@centos8 ~]# dnf -y install httpd php php-xml php-mysqlnd mariadb-server
[root@centos8 ~]# unzip Discuz_X3.4_SC_UTF8【20191201】.zip
[root@centos8 ~]# mv upload/ /var/www/html/forum
[root@centos8 ~]# chown -R apache.apache /var/www/html/forum
[root@centos8 ~]# mysql
MariaDB [ultrax]> create database ultrax;
MariaDB [ultrax]> grant all on ultrax.* to ultrax@'localhost' identified by
'123456';

范例:CentOS 7 利用RPM包部署Discuz!

#官网:http://www.discuz.net
#下载源码:
[root@centos7 ~]# wget http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip
[root@centos7 ~]# unzip Discuz_X3.3_SC_UTF8.zip
[root@centos7 ~]# mv upload/ /var/www/html/forum
[root@centos7 ~]# setfacl -R -m u:apache:rwx /var/www/html/forum
mysql> create database discuz;
mysql> grant all on discuz.* to discuz@'172.16.0.%’identified by"123456";
安装向导:http://localhost/forum

实现 PowerDNS 应用部署

PowerDNS 是一个跨平台的开源DNS服务组件,PowerDNS为DNS数据提供纯文本文件或第三方数据库
(如MySQL,PostgreSQL,Microsoft SQL Server,Oracle或Sybase)中存储的数据。PowerDNS同
时有Windows和Linux/Unix的版本。 PowerDNS在Windows下可以使用 Access的mdb文件记录DNS信
息,而在Linux/Unix下则常使用MySQL来记录DNS信息。
Poweradmin 是为 PowerDNS 服务器的提供基于PHP语言实现的 Web 界面的DNS 管理工具。此工具支
持 PowerDNS 的主要功能。

PowerDNS官网:https://www.powerdns.com/
PowerDNS文档:https://doc.powerdns.com/
Poweradmin官网:http://www.poweradmin.org/

范例:CentOS 8 利用RPM包部署PowerDNS(目前20200311,不支持powerdns,缺少相关php包)

[root@centos8 ~]# yum install -y pdns pdns-backend-mysql mariadb-server
[root@centos8 ~]# systemctl start mariadb
[root@centos8 ~]# mysql < pdns.sql
[root@centos8 ~]# vim /etc/pdns/pdns.conf
launch=gmysql
gmysql-host=localhost
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=123456

[root@centos8 ~]# grep -Ev "^#|^$" /etc/pdns/pdns.conf
launch=gmysql #修改此行
gmysql-host=localhost #以下行是增加
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=123456
setgid=pdns #以下两行不变
setuid=pdns
[root@centos8 ~]# systemctl enable --now pdns
[root@centos8 ~]# ss -ntlpu |grep pdns_server
udp UNCONN 0 0 0.0.0.0:53 0.0.0.0:*
users:(("pdns_server",pid=3073,fd=9))
udp UNCONN 0 0

[root@centos8 ~]# yum -y install httpd php php-devel php-gd php-ldap php-mysqlnd
php-odbc php-pear php-xml php-xmlrpc php-mhash gettext
[root@centos8 ~]# systemctl restart httpd

范例:CentOS 7 利用RPM包部署PowerDNS

#安装包:基于EPEL源
[root@centos7 ~]# yum install -y pdns pdns-backend-mysql mariadb-server
[root@centos7 ~]# rpm -ql pdns
/etc/pdns
/etc/pdns/pdns.conf
/usr/bin/pdns_control
/usr/bin/pdns_zone2ldap
...

[root@centos7 ~]# systemctl enable --now mariadb
#准备mariadb中的数据库,表和用户
[root@centos7 ~]# mysql
MariaDB [(none)]> CREATE DATABASE powerdns;
MariaDB [(none)]> GRANT ALL ON powerdns.* TO 'powerdns'@'localhost' IDENTIFIED
BY '123456';
#创建powerdns数据库中的表,参看下面文档实现
#https://doc.powerdns.com/md/authoritative/backend-generic-mysql/

use powerdns;
CREATE TABLE domains (
id INT AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
master VARCHAR(128) DEFAULT NULL,
last_check INT DEFAULT NULL,
type VARCHAR(6) NOT NULL,
notified_serial INT DEFAULT NULL,
account VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE UNIQUE INDEX name_index ON domains(name);

CREATE TABLE records (
id BIGINT AUTO_INCREMENT,
domain_id INT DEFAULT NULL,
name VARCHAR(255) DEFAULT NULL,
type VARCHAR(10) DEFAULT NULL,
content VARCHAR(64000) DEFAULT NULL,
ttl INT DEFAULT NULL,
prio INT DEFAULT NULL,
change_date INT DEFAULT NULL,
disabled TINYINT(1) DEFAULT 0,
ordername VARCHAR(255) BINARY DEFAULT NULL,
auth TINYINT(1) DEFAULT 1,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX nametype_index ON records(name,type);
CREATE INDEX domain_id ON records(domain_id);
CREATE INDEX recordorder ON records (domain_id, ordername);

CREATE TABLE supermasters (
ip VARCHAR(64) NOT NULL,
nameserver VARCHAR(255) NOT NULL,
account VARCHAR(40) NOT NULL,
PRIMARY KEY (ip, nameserver)
) Engine=InnoDB;
CREATE TABLE comments (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
type VARCHAR(10) NOT NULL,
modified_at INT NOT NULL,
account VARCHAR(40) NOT NULL,
comment VARCHAR(64000) NOT NULL,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX comments_domain_id_idx ON comments (domain_id);
CREATE INDEX comments_name_type_idx ON comments (name, type);
CREATE INDEX comments_order_idx ON comments (domain_id, modified_at);
CREATE TABLE domainmetadata (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
kind VARCHAR(32),
content TEXT,
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE INDEX domainmetadata_idx ON domainmetadata (domain_id, kind);
CREATE TABLE cryptokeys (
id INT AUTO_INCREMENT,
domain_id INT NOT NULL,
flags INT NOT NULL,
active BOOL,
content TEXT,
PRIMARY KEY(id)
) Engine=InnoDB;
CREATE INDEX domainidindex ON cryptokeys(domain_id);
CREATE TABLE tsigkeys (
id INT AUTO_INCREMENT,
name VARCHAR(255),
algorithm VARCHAR(50),
secret VARCHAR(255),
PRIMARY KEY (id)
) Engine=InnoDB;
CREATE UNIQUE INDEX namealgoindex ON tsigkeys(name, algorithm);

#配置PowerDNS使用mariadb作为后台数据存储
[root@centos7 ~]# vim /etc/pdns/pdns.conf,查找到包含launch= 的行,修改并添加下面的内容
launch=gmysql #修改此行
gmysql-host=localhost #以下行是增加的
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=123456

[root@centos7 ~]# grep -Ev "^#|^$" /etc/pdns/pdns.conf
launch=gmysql
gmysql-host=localhost
gmysql-port=3306
gmysql-dbname=powerdns
gmysql-user=powerdns
gmysql-password=123456
setgid=pdns
setuid=pdns
#启动服务
[root@centos7 ~]# systemctl enable --now pdns
[root@centos7 ~]# ss -ntlu
Netid State Recv-Q Send-Q Local Address:Port
Peer Address:Port
udp UNCONN 0 0 127.0.0.1:323
...

#安装httpd和php相关包
[root@centos7 ~]# yum -y install httpd php php-devel php-gd php-mcrypt php-imap
php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mhash
gettext
[root@centos7 ~]# systemctl enable --now httpd
#下载poweradmin程序,并解压缩到相应目录
[root@centos7 ~]# wget
http://downloads.sourceforge.net/project/poweradmin/poweradmin-2.1.7.tgz
[root@centos7 ~]# tar xvf poweradmin-2.1.7.tgz -C /var/www/html
[root@centos7 ~]# cd /var/www/html
[root@centos7 ~]# mv poweradmin-2.1.7 poweradmin

访问下面地址,启动PowerAdmin的网页安装向导:

http://powerdns服务器IP/poweradmin/install/

提供先前配置的数据库详情,同时为网站的超级管理员admin用户设置密码为123456

为Poweradmin创建一个受限用户
#说明:
Username:PowerAdmin用户名
Password:上述用户的密码
Hostmaster:当创建SOA记录指定默认主机管理员
Primary nameserver:主域名服务器
Secondary namesever:辅域名服务器

按照下面页面说明,在数据库中创建用户并授权
MariaDB [(none)]> GRANT SELECT, INSERT, UPDATE, DELETE ON powerdns.* TO
'poweradmin'@'localhost‘ IDENTIFIED BY '123456';

按下面页面说明,创建config.in.php文件内容
[root@centos7 ~]# vim /var/www/html/poweradmin/inc/config.inc.php

安装完毕后
删除install目录
rm -rf /var/www/html/poweradmin/install/

登录http://powerdns服务器IP/poweradmin/

实现 xcache 加速 php 5.X

编译php-xcache加速访问,支持php 5.6版以下

官网:http://xcache.lighttpd.net/wiki/ReleaseArchive

案例:CentOS 7上安装清华源的php56,并编译安装 xcache加速

[root@centos7 ~]#  yum -install
https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
[root@centos7 ~]# yum -y install php56-php php56-php-mysqlnd mariadb-server
[root@centos7 ~]# systemctl enable --now httpd mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to
/usr/lib/systemd/system/httpd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service
to /usr/lib/systemd/system/mariadb.service.
[root@centos7 ~]# mysql
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress.* to wordpress@'localhost' identified
by '123456';
[root@centos7 ~]# tar xvf wordpress-5.3.2-zh_CN.tar.gz -C /var/www/html
[root@centos7 ~]# cd /var/www/html
[root@centos7 ~]# chown -R apache.apache wordpress/
[root@centos8 ~]# ab -c 10 -n 100 http://10.0.0.7/wordpress/

#安装编译xcache
[root@centos7 ~]# yum -y install gcc php56-php-devel
#下载并解压缩xcache-3.2.0.tar.bz2
[root@centos7 ~]# tar xf xcache-3.2.0.tar.gz
#生成编译环境
[root@centos7 ~]# cd xcache-3.2.0/
[root@centos7 xcache-3.2.0]# /opt/remi/php56/root/usr/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
[root@centos7 xcache-3.2.0]#./configure --enable-xcache --with-phpconfig=/
opt/remi/php56/root/usr/bin/php-config
[root@centos7 xcache-3.2.0]# make && make install
...省略...
Installing shared extensions: /opt/remi/php56/root/usr/lib64/php/modules/
[root@centos7 xcache-3.2.0]# cat xcache.ini >> /opt/remi/php56/root/etc/php.ini
#安装base源中执行即可cp xcache.ini /etc/php.d/
[root@centos7 ~]# systemctl restart httpd.service
#测试性能
[root@centos8 ~]# ab -c10 -n 100 http://LAMP服务器/wordpress
Requests per second: 7.26

实现 opcache 加速 php 7.X

[root@centos8 ~]# dnf install php-opcache
[root@centos8 ~]# cat /etc/php.ini
[opcache]
zend_extension=opcache.so
opcache.enable=1
[root@centos8 ~]# systemctl restart php-fpm

范例:CentOS 8 实现 opache 加速

[root@centos8 ~]# dnf -y install httpd php php-mysqlnd mariadb-server php-opcache
php-json
[root@centos8 ~]# rpm -ql php-opcache
/etc/php.d/10-opcache.ini
/etc/php.d/opcache-default.blacklist
/usr/lib/.build-id
/usr/lib/.build-id/71
/usr/lib/.build-id/71/55ebb00f7ebcab9d708c1d5c7b7e634cce259c
/usr/lib64/php/modules/opcache.so
[root@centos8 ~]# grep opcache /etc/php.d/10-opcache.ini
zend_extension=opcache
opcache.enable=1
...省略...
#加速前,默认启用,先禁用加速

[root@centos8 ~]# vim /etc/php.d/10-opcache.ini
opcache.enable=0
[root@centos8 ~]# systemctl restart php-fpm
[root@centos7 ~]# ab -c 10 -n 100 http://10.0.0.8/wordpress/
......
Requests per second: 4.31 [#/sec] (mean)
......
#启用加速
[root@centos8 ~]# vim /etc/php.d/10-opcache.ini
opcache.enable=1
[root@centos8 ~]# systemctl restart php-fpm
##加速后
[root@centos7 ~]# ab -c 10 -n 100 http://10.0.0.8/wordpress/
......
Requests per second: 19.76 [#/sec] (mean)......
posted @ 2021-06-01 22:31  空白的旋律  阅读(563)  评论(0编辑  收藏  举报