Linux企业化运维--(3)PHP配置、nginx结合php-fpm、memcache模块、openresty模块以及高速缓存

Linux企业化运维

实验所用系统为Redhat-rhel7.6。

Linux企业化运维–PHP配置、nginx结合php-fpm、memcache模块、openresty模块以及高速缓存

一、PHP源码编译

1、软件下载

https://www.php.net/		##网址下载

或者

cd
lftp 172.25.254.250
> ls
> cd pub/docs/lamp/
> get php-7.4.12.tar.bz2 
> exit
tar jxf php-7.4.12.tar.bz2

请添加图片描述

2、软件编译

还是configure--makefile--make 三步曲,首先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
yum install oniguruma-devel -y

请添加图片描述

请添加图片描述

请添加图片描述
请添加图片描述

请添加图片描述

切入php目录,重新configuremakemake install

cd php-7.4.12/
./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/lnmp/
cd php
cd etc/
ls
cp php-fpm.conf.default php-fpm.conf	
ls

请添加图片描述

2、www.conf

cd php-fpm.d/
ls
cp www.conf.default www.conf		
ls

请添加图片描述

对此配置文件进行更改。

cd ..
vim php-fpm.conf
///
pid = run/php-fpm.pid	##取消注释
///

请添加图片描述

3、php.ini

cd
cd php-7.4.12/
cp php.ini-production /usr/local/lnmp/php/etc/php.ini	
cd /usr/local/lnmp/php/etc/
vim php.ini
///
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai	##取消注释
///

请添加图片描述
请添加图片描述

4、php-fpm.service,读取并开启服务

cd
cd php-7.4.12/
cd sapi/
ls
cd fpm/
cp php-fpm.service /usr/lib/systemd/system	
cd /usr/lib/systemd/system
vim php-fpm.service 		##php-fpm启动文件
///
#ProtectSystem=full			##注释掉
///
systemctl daemon-reload 
systemctl start php-fpm.service

请添加图片描述

请添加图片描述

三、nginx结合php-fpm

1、修改nginx配置文件

切入配置目录,编辑配置文件,注释之前的设定,取消php的注释。

cd /usr/local/nginx/conf/
vim nginx.conf
///
        limit_conn_zone $binary_remote_addr zone=addr:10m;
        limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
        #return 500;
        #rewrite ^(.*) http://www.westos.org permanent;

        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;
        }
        }
        location / {
                root    html;
                index   index.php index.html index.htm;
        }
///

请添加图片描述
请添加图片描述
请添加图片描述

编写一个php发布文件,重启服务。

cd ..(nginx)
cd html/
vim index.php
///
<?php
phpinfo()
?>
///
nginx -s reload

请添加图片描述
请添加图片描述

在真机浏览器访问server1

在真机浏览器访问
http://172.25.24.1/index.php

请添加图片描述

2、添加环境变量

cd /usr/local/lnmp/php/sbin/
ls -> php-fpm
cd ..(php)
cd bin/
ls
pwd							##/usr/local/lnmp/php/bin
cd
vim .bash_profile			##在.bash_profile文件添加文件路径
///
PATH=$PATH:$HOME/bin:/usr/local/lnmp/php/bin
///
source .bash_profile 		##使其生效
which php					##/usr/local/lnmp/php/bin/php
which phpize				##/usr/local/lnmp/php/bin/phpize
echo $PATH

请添加图片描述
请添加图片描述

四、php添加memcache功能模块

1、软件下载

http://pecl.php.net/package/memcache	##网址下载

或者

lftp 172.25.254.250
> ls
> cd pub/docs/lamp/
> get memcache-4.0.5.2.tgz
> exit

请添加图片描述

2、软件安装

解压软件包,切入目录,执行phpize,提醒缺少依赖。phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块。

tar zxf memcache-4.0.5.2.tgz
cd memcache-4.0.5.2/
ls
phpize		##phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,提醒缺少依赖autoconf

请添加图片描述
安装依赖,重新执行phpize

yum install autoconf -y
yum install automake.noarch -y
phpize			##扩展成功

请添加图片描述
请添加图片描述
请添加图片描述

memcache进行源码编译,configure--make--make 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与memcache服务
///
php -m | grep memcache				##出现memcache表示添加成功
systemctl reload php-fpm.service 
php -m								##测试php服务是否正常,可以看到memcache

请添加图片描述
请添加图片描述
请添加图片描述请添加图片描述

请添加图片描述

3、添加memcache功能模块

先安装memcached,并开启服务,查看端口。

yum install -y memcached
systemctl start memcached.service
netstat -antlp					##11211端口
cat /etc/sysconfig/memcached	##11211端口

请添加图片描述
请添加图片描述
请添加图片描述
切入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','westos');      // Admin Password						##设定密码
$MEMCACHE_SERVERS[] = '172.25.24.1:11211'; // add more as an array				##server1IP
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array		##注释
///
nginx -s reload							##重启nginx
systemctl start php-fpm.service			##开启服务
systemctl start memcached.service		##开启服务

请添加图片描述
请添加图片描述

在真机浏览器中试访问,172.25.24.1/example.php
将在缓存中读取数据。
请添加图片描述
此时服务配置成功,访问172.25.24.1/memcache.php。用户名memcache,密码westos
请添加图片描述
请添加图片描述
但此时我们可以观察到,当前的信息处理率不能达到百分百,需要进行优化。在真机中执行压力测试命令,对其进行优化。

###真机
ab -c20 -n 1000 http://172.25.24.1/example.php

请添加图片描述
刷新页面,可以看到信息处理率达到百分百。
请添加图片描述

五、配置php加载模块openresty,构建nginx高速缓存

基于openresty(构建高效透明的缓存机制) 访问,能将缓存放在nginx中,速度更快。
使用memc-nginxsrcache-nginx模块构建高效透明的缓存机制。
如果需要做到高速缓存,nginx可以跳过php直接向memcache存储,但是只能做静态存储 ,如果需要动态存储,还是要调用php,因此两种缓存策略时同时在进行的。
请添加图片描述

nginx -s stop		##先停止nginx服务

1、下载软件

https://openresty.org/cn/

或者

cd
lftp 172.25.254.250
> ls
> cd pub/docs/lamp
> get openresty-1.19.3.1.tar.gz
> exit

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  1;

events {
    worker_connections  65535;
}

        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;
        }
///
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

请添加图片描述请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
进一步配置来提升性能。

cd /usr/local/openresty/nginx/conf
vim nginx.conf
///
http {
        upstream memcache {
        server 127.0.0.1:11211;
        keepalive 512;					##保持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;	##使用内置的$query_string来作为key
        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.24.1/example.php
///
Complete requests:      5000
Failed requests:        0
Total transferred:      1425000 bytes
HTML transferred:       580000 bytes
///
##可以看到压测速度很快,且没有报错,速度很快。
ab -c10 -n 5000 http://172.25.24.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
///
##传输量大幅度提升

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

[注意]

/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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值