Nginx中、PHP配置、nginx结合php-fpm、memcache模块、openresty模块
一、PHP的源码编译
1、软件包下载
https://www.php.net/
2、编译php
configure
./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 libpng-devel -y
oniguruma-devel
rpm -ivh oniguruma-6.8.2-1.el7.x86_64.rpm
rpm -ivh oniguruma-devel-6.8.2-1.el7.x86_64.rpm
configure完成 编译 make 安装 make install 
二、拷贝php-fpm配置文件
1、php-fpm.conf
2、www.conf 进入php-fpm.conf文件,将注释打开 

3、php.ini
 设置地区时间 4、php-fpm.service,读取并开启服务  注释 
三、nginx中使用php-fpm
1、修改nginx配置文件
进入nginx的配置目录,编辑配置文件,注释之前的设定,取消php的注释
cd /usr/local/nginx
vim conf/nginx.conf
nginx -s reload
 
在html中编写php发布页面,重新启动服务,在真机浏览器访问http://172.25.73.1/index.php
  
2、添加环境变量

四、php添加memcache功能模块
1、软件包准备
http://pecl.php.net/package/memcache

2、软件安装
解压软件包进入目录,执行phpize,提醒缺少依赖。phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块。  安装依赖,重新phpize
yum install autoconf -y
yum install automake.noarch -y
phpize
对memcache进行源码编译、make、mkae install
./configure --enable-debug
make
make install
 编辑php.ini ,然后重启服务,执行php -m可以看到memcache
cd /usr/local/lnmp/php/etc/
ls
vim php.ini
///
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
938 extension=memcache
///
php -m | grep memcache
systemctl reload php-fpm.service
php -m
 
3、构建nginx高速缓存,添加memcache功能模块
使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制。先安装memcached,并开启服务,查看端口。
yum install -y memcached
systemctl start memcached.service
netstat -antlp
cat /etc/sysconfig/memcached
  cd memcache目录,拷贝文件并编译,最后重启服务
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
///
define('ADMIN_PASSWORD','root'); // Admin Password
$MEMCACHE_SERVERS[] = '172.25.73.1:11211'; // add more as an array
///
nginx -s reload
systemctl start php-fpm.service
systemctl start memcached.service

此时服务配置成功,访问172.25.73.1/memcache.php。使用 用户名,密码 登陆

五、配置php加载模块openresty
基于openresty(构建高效透明的缓存机制) 访问,能将缓存放在nginx中,速度更快
1、准备
nginx -s stop
https://openresty.org/cn/

2、软件安装
tar zxf openresty-1.19.3.1.tar.gz
ls
cd openresty-1.19.3.1/
ls
./configure --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make
make install
3、软件配置
cd /usr/local/openresty/nginx.conf
vim nginx.conf
///
user nginx;
worker_processes auto;
events {
worker_connections 65535;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
///
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
ERROR 
/usr/local/openresty/nginx/sbin/nginx -s reload
报错 nginx: [error] invalid PID number "" in "/usr/local/openresty/nginx/logs/nginx.pid"
解决:使用nginx -c的参数指定nginx.conf文件的位置,conf文件的位置在nginx -t的返回信息中
/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
/usr/local/openresty/nginx/sbin/nginx -s reload
更改配置改进一步来提升性能
cd /usr/local/openresty/nginx/conf
vim nginx.conf
///
http {
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
include mime.types;
default_type application/octet-stream;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
///
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload
 在真机进行压力测试
ab -c10 -n 5000 http://172.25.73.1/example.php
///
Complete requests: 5000
Failed requests: 0
Total transferred: 1425000 bytes
HTML transferred: 580000 bytes
///
ab -c10 -n 5000 http://172.25.73.1/index.php
///
Complete requests: 5000
Failed requests: 492
(Connect: 0, Receive: 0, Length: 492, Exceptions: 0)
Total transferred: 358824457 bytes
HTML transferred: 357979457 bytes
///
|