目录
一.PHP源码编译
软件下载:https://www.php.net/
# tar jxf php-7.4.6.tar.bz2 #解压软件包
软件编译:
# cd php-7.4.6
# ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
此时会提示缺少依赖性,需要下载
yum install -y systemd-devel
yum install -y libxml2-devel
yum install -y sqlite-devel
yum install -y libcurl-devel
yum install -y libpng-devel
yum install -y oniguruma-6.8.2-1.el7.x86_64.rpm oniguruma-devel-6.8.2-1.el7.x86_64.rpm
安装完依赖性重新编译./configure
,再执行make`:
./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring --enable-bcmath --with-fpm-systemd
make
make install
二.拷贝php-fpm配置文件
1、php-fpm.conf
cd /usr/local/
cd php
cd etc/
ls
cp php-fpm.conf.default php-fpm.conf
ls
vim php-fpm.conf #更改配置文件
2. www.conf
cd php-fpm.d/
ls
cp www.conf.default www.conf
ls
3.php.ini
cd
cd php-7.4.12/
ll php.ini-production
cp php.ini-production /usr/local/php/etc/php.ini
cd /usr/local/lamp/php/etc/
ls
vim php.ini #更改时区
4.php-fpm.service
cd
cd php-7.4.12/
ls
cd sapi/
ls
cd fpm/
cp php-fpm.service /usr/lib/systemd/system
systemctl daemon-reload #重载
systemctl start php-fpm #开启服务
启动服务报错,则需要注释掉php-fpm.service中的#ProtectSystem=full
三.nginx结合php-fpm
修改nginx配置文件:
vim /usr/local/nginx/conf/nginx.conf
///
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
/// ###取消注释这几行
编写php
发布文件内容,在网页上能够显示。
cd /usr/local/nginx/html
vim index.php
///
<?php
phpinfo()
?>
///
nginx -s reload
在真机中打开浏览器访问:http://172.25.101.1/index.php
四.添加memcache功能模块
1、安装memcache模块
在server1中:
cd
lftp 172.25.254.250
>ls
>cd pub/docs/lamp
>get memcache-4.0.5.2.tgz
>exit
ls
tar zxf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2/
ls
phpize #扩展模块,如果执行失败则需要添加环境变量
cd
vim .bash_profile #添加环境变量
source .bash_profile #引用环境变量
which phpize
编译之前需要安装以下两个依赖软件:
yum install autoconf -y
yum install automake.noarch -y
phpize #重新执行扩展
编译:
./configure --enable-debug
make
make install
编辑php.ini
,添加memcache
模块,然后重启服务,执行php -m可以看到memcache。
vim php.ini
systemctl reload php-fpm #刷新服务
php -m | grep memcache
2 .添加memcache模块,构建nginx高速缓存
1.添加memcache模块,需要先安装memcached,并开启服务,查看端口:
yum install -y memcached
systemctl start memcached.service
netstat -antlp ##11211端口
cat /etc/sysconfig/memcached
cd
cd memcache-4.0.5.2/
ls
cp example.php /usr/local/nginx/html/
cp memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/
ls
vim memcache.php #修改密码和server地址
nginx -s reload ##刷新nginx
systemctl start php-fpm.service ##开启服务
systemctl start memcached.service ##开启服务
在真机中打开浏览器访问:http://http://172.25.101.1/example.php,会从缓存中取数据
接下来访问172.25.101.1/memcache.php。用户名为上面配置文件中的memcache,密码westos。
传统模式的访问太慢,在真机中执行压力测试命令,对其进行优化:
刷新页面,信息处理率达到百分百。
基于openresty(构建高效透明的缓存机制) 访问,能将缓存放在nginx中,速度更快
安装openresty:
https://openresty.org/download/openresty-1.19.3.1.tar.gz
tar zxf openresty-1.19.3.1.tar.gz
ls
cd openresty-1.19.3.1/
ls
./configure
#编译
make && make install
关闭nginx
systemctl stop nginx
netstat -antlp
参考之前配置的nginx.conf文件
cd /usr/local/openresty/nginx/conf
vim nginx.conf
当所请求的url以“.php”结尾时,首先到memcache中查询有没有以
u
r
l
url
urlargs为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前以
u
r
i
uri
uriargs为key,将输入结果存入memcache。
为了提高性能,继续添加以下内容:
cd ..
cd html/
cp /usr/local/nginx/html/example.php .
cp /usr/local/nginx/html/index.php .
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload
注意: 出现如图报错时,说明该路径下找不到nginx服务,通过nginx -c 指定 nginx.conf的位置
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf//nginx.conf
/usr/local/openresty/nginx/sbin/nginx -s reload
在真机进行压力测试:
ab -c10 -n 5000 http://172.25.101.1/example.php
传统模式:
新模式:
相比于传统模式,新模式的传输速率快了很多。