2.2 lnmp架构_开源软件 nginx MySQL PHP的整合

1. nginx + php-fpm

cgi VS fastcgi

cgi:没有后台,所以,改完参数,需要重启apache(当我们去访问apache时,apache会调用cgi的进程,apache – > cgi)
fcgi:有独立的后台,始终运行在后台,不管我们是否调用它,它都在。,所以,改完参数,直接reload就可以
fcgi更好,响应更快,我们不需要单独创建进程

  1. 访问PHP,需要和nginx结合
    我们要告诉NGINX,当它接收到用户的动态请求时,如何去调用后端的PHP进行处理
    在nginx主配置文件的server的语句块中,定位到location,去掉它的注释
    PHP使用的是9000端口
    location index定义了网站首页
    必须要注释掉一行,因为源码会自动编译,fsatcgi.conf里面已经有了这一行参数
[root@server1 php-7.4.12]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf		
######
它们属于NGINX的server localhost的语句块
        location / {			//定义默认网站的主页
            root   html;
            index  index.php index.html index.htm;	//现在我想要网站的首页是.php
        }

        location ~ \.php$ {		//通配符,表示所有以.php结尾的文件
            root           html;		//默认的发布目录是/usr/local/nginx/html
            fastcgi_pass   127.0.0.1:9000;	//默认的端口
            fastcgi_index  index.php;			//默认首页
           # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  //让NGINX和我们的PHP的默认发布目录保持同步
            include        fastcgi.conf;		//让PHP直接include这个文件,这个文件与默认发布目录有关
        }
[root@server1 conf]# nginx -s reload
  1. 完善NGINX,设置nginx开机自启
    百度官网查找到了内容: nginx systemd
    将网站内容数据写入到/usr/lib/systemd/system/nginx.sevice
    这部分内容要记得要修改路径,pid的位置(和安装的路径相同即可,nginx的默认发布目录下usr/local/nginx
    可以通过脚本开机启动
[root@server1 system]# cd /usr/lib/systemd/system
[root@server1 system]# pwd
/usr/lib/systemd/system
[root@server1 system]# vim nginx.service
######
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t		//检测语法
ExecStart=/usr/local/nginx/sbin/nginx		//启动
ExecReload=/usr/local/nginx/sbin/nginx -s reload		//重载
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@server1 system]# ll /usr/local/nginx/logs/nginx.pid 		//PID存放位置正确
-rw-r--r-- 1 root root 6 Apr  3 21:15 /usr/local/nginx/logs/nginx.pid
[root@server1 system]# systemctl daemon-reload 			//刷新,让systemd可以识别到修改
[root@server1 system]# nginx -s stop
[root@server1 system]# systemctl start nginx		//通过脚本启动
[root@server1 system]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
  1. 回到PHP
    (看一看,可以不做这一步,这种启动方式不推荐)
    init.d的启动方式需要用到chkconfig
    chkconfig这是过去经常用到的,现在用systemd更多些
    level:运行级
    3和5:有无图形
[root@server1 system]# cd /etc/init.d/
[root@server1 init.d]# chkconfig --level 35 php-fpm on		//开机自启
[root@server1 init.d]# chkconfig --list php-fpm 

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

php-fpm        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
  1. init方式在之前打开正常,但是换了一种方式chkconfig后,再次使用 init方式就会出问题
    解决办法:注释/usr/lib/systemd/system/php-fpm.service的21行
[root@server1 init.d]# /etc/init.d/php-fpm stop
Gracefully shutting down php-fpm . done
[root@server1 init.d]# cd /test/php-7.4.12/
[root@server1 php-7.4.12]# cd sapi/fpm/
[root@server1 fpm]# cp php-fpm.service /usr/lib/systemd/system
[root@server1 fpm]# cd /usr/lib/systemd/system
[root@server1 system]# vim php-fpm.service 
#####
21 #ProtectSystem=full
注释21行的内容,禁止保护模式
#####
[root@server1 system]# systemctl daemon-reload		//php启动正常
[root@server1 system]# systemctl start php-fpm
  1. 写动态语言,php函数到默认发布页面
    最好进行测试:Firefox:172.25.23.1
[root@server1 system]# cd /usr/local/nginx/html/
[root@server1 html]# vim index.php
####
<?php
phpinfo()
?>
[root@server1 html]# systemctl  start php-fpm
[root@server1 html]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      24619/php-fpm: mast 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      24445/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3266/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3349/master         
tcp        0      0 172.25.23.1:22          172.25.23.250:35246     ESTABLISHED 3513/sshd: root@pts 
tcp6       0      0 :::22                   :::*                    LISTEN      3266/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      3349/master         
  1. 如果修改了php的配置(改个时区)
    之后需要reload
    最好进行测试:Firefox:172.25.23.1
    在这里插入图片描述
[root@server1 conf]# cd /usr/local/php/etc/
[root@server1 etc]# vim php.ini
#####
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;

[CLI Server]
; Whether the CLI web server uses ANSI color coding in its terminal output.
cli_server.color = On

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone =Asia/Shanghai

[root@server1 etc]# systemctl reload php-fpm		//平滑重载

在这里插入图片描述在这里插入图片描述

2. nginx + php-fpm + mysql

  1. 记得先开启数据库
[root@server1 ~]# /etc/init.d/mysqld start
  1. 改环境变量之后,直接在命令行键入php就会有结果
    键入tuphp -m,可以获取php的模块
[root@server1 ~]# vim ~/.bash_profile 
[root@server1 ~]# source ~/.bash_profile 
[root@server1 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin:/root/bin:/usr/local/nginx/sbin:/usr/local/mysql/bin:/usr/local/php/bin
[root@server1 ~]# php -m

在这里插入图片描述

  1. 进入PHP的配置目录,编辑PHP的主配置文件
    编写默认socket的位置
    数据库装在哪里,默认指向它的socket
[root@server1 ~]# cd /usr/local/php/etc/
[root@server1 etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d  php.ini
[root@server1 etc]# vim php.ini
####
mysqli和mysqlnd,修改2处
[Pdo_mysql]
; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
pdo_mysql.default_socket=/usr/local/mysql/data/mysql.sock

mysqli.default_port = 3306

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
; http://php.net/mysqli.default-socket
mysqli.default_socket =/usr/local/mysql/data/mysql.sock

####
[root@server1 etc]# ll /usr/local/mysql/data/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Mar 30 11:08 /usr/local/mysql/data/mysql.sock
[root@server1 etc]# systemctl restart php-fpm.service 				//如果没有启动php服务的话
[root@server1 etc]# systemctl reload php-fpm		//php的平滑加载
  1. 测试MySQL和PHP已经整合完成
    检测:PHP可以将数据放到MySQL数据库里
    这里使用PHP论坛程序,但是论坛程序版本低,无法用于测试;
    我们改用phpMyAdmin
[root@server1 test]# yum install -y unzip
[root@server1 test]# unzip Discuz_X3.2_SC_UTF8.zip 
[root@server1 test]# ll
total 89956
-rw-r--r--  1 root  root  12486177 Apr  5 12:30 Discuz_X3.2_SC_UTF8.zip
drwxr-xr-x 38  7161 31415     4096 Mar 30 10:41 mysql-5.7.31
-rw-r--r--  1 root  root  51382559 Mar 30 08:36 mysql-boost-5.7.31.tar.gz
drwxr-xr-x  9 mysql mysql      186 Mar 29 20:30 nginx-1.18.0
-rw-r--r--  1 root  root   1039530 Mar 29 20:24 nginx-1.18.0.tar.gz
-rw-r--r--  1 root  root    185197 Apr  2 22:36 oniguruma-6.8.2-1.el7.x86_64.rpm
-rw-r--r--  1 root  root     42189 Apr  2 22:36 oniguruma-devel-6.8.2-1.el7.x86_64.rpm
drwxrwxr-x 19 root  root      4096 Apr  2 22:39 php-7.4.12
-rw-r--r--  1 root  root  12754630 Mar 30 11:21 php-7.4.12.tar.bz2
-rw-r--r--  1 root  root  14199213 Apr  5 12:29 phpMyAdmin-5.0.2-all-languages.zip
drwxr-xr-x  2 root  root       102 Dec  8  2015 readme				//Discuz
drwxr-xr-x 12 root  root      4096 Dec  8  2015 upload		//Discuz
drwxr-xr-x  4 root  root        72 Dec  8  2015 utility			//Discuz
[root@server1 test]# cd upload/
[root@server1 upload]# ls
admin.php  connect.php      forum.php  member.php  search.php  uc_server
api        cp.php           group.php  misc.php    source      userapp.php
api.php    crossdomain.xml  home.php   plugin.php  static
archiver   data             index.php  portal.php  template
config     favicon.ico      install    robots.txt  uc_client

将论坛的数据移动并改名到/usr/local/nginx/html/bbs

[root@server1 test]# mv upload /usr/local/nginx/html/bbs
[root@server1 test]# cd /usr/local/nginx/html/bbs/
[root@server1 bbs]# ls
admin.php  connect.php      forum.php  member.php  search.php  uc_server
api        cp.php           group.php  misc.php    source      userapp.php
api.php    crossdomain.xml  home.php   plugin.php  static
archiver   data             index.php  portal.php  template
config     favicon.ico      install    robots.txt  uc_client
测试:firfoc:/bbs/install

因为我们选择论坛程序版本太低,无法完成测试,所以,我们改用phpMyAdmin进行测试
移动phpMyAdmin的目录到/usr/local/nginx/html/php(并改名为php)

[root@server1 test]# unzip phpMyAdmin-5.0.2-all-languages.zip 
[root@server1 test]# mv phpMyAdmin-5.0.2-all-languages /usr/local/nginx/html/php

测试:firefox:172.25.23.1/php/index.php,测试失败,因为没有给data目录权限,其他人不能访问
在这里插入图片描述

[root@server1 test]# cd /usr/local/mysql/
[root@server1 mysql]# ll
total 300
drwxr-xr-x  2 root  root    4096 Mar 30 10:40 bin
drwxr-x---  5 mysql mysql   4096 Mar 30 11:08 data
drwxr-xr-x  2 root  root      55 Mar 30 10:40 docs
drwxr-xr-x  3 root  root    4096 Mar 30 10:40 include
drwxr-xr-x  4 root  root     192 Mar 30 10:40 lib
-rw-r--r--  1 root  root  275393 Jun  2  2020 LICENSE
drwxr-xr-x  4 root  root      30 Mar 30 10:40 man
drwxr-xr-x 10 root  root    4096 Mar 30 10:41 mysql-test
-rw-r--r--  1 root  root     587 Jun  2  2020 README
-rw-r--r--  1 root  root     587 Jun  2  2020 README-test
drwxr-xr-x 28 root  root    4096 Mar 30 10:41 share
drwxr-xr-x  2 root  root      90 Mar 30 10:41 support-files
[root@server1 mysql]# chmod 755 /usr/local/mysql/data/		//改权限
[root@server1 mysql]# ls -ld /usr/local/mysql/data/
drwxr-xr-x 5 mysql mysql 4096 Mar 30 11:08 /usr/local/mysql/data/
[root@server1 mysql]# ls -l /usr/local/mysql/data/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Mar 30 11:08 /usr/local/mysql/data/mysql.sock
[root@server1 mysql]# systemctl reload php-fpm.service 

此时测试依旧失败,找自己的问题,发现MySQL的端口3306没有打开

[root@server1 mysql]# netstat -antuple | grep mysql
[root@server1 mysql]# systemctl restart mysql					//同时也要记得打开myql的端口,否则,测试失败
[root@server1 mysql]# netstat -antuple | grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      1001       34477      4131/mysqld         

测试:firefox:172.25.23.1/php/index.php,测试成功
在这里插入图片描述
总结几个容易出问题的点:
①如果是配置文件,/usr/local/php/etc/php.ini写的有问题,仔细检查,并且,在最后需要
systemctl reload php-fpm;
②检查端口;
③目录data的权限;
④火墙是否关闭;
⑤selinux

  1. 最开始编译PHP,缺少模块。怎么添加模块
    重新编译PHP代价高
    yum list php-*(外部模块)
    不要装rpm包,否则,系统两个版本冲突,系统会先找rpm包
    比如,解压好memcache之后,进入它,会发现没有configure,执行命令phpize
[root@server1 test]# tar zxf memcache-4.0.5.2.tgz 
[root@server1 test]# cd memcache-4.0.5.2/
[root@server1 memcache-4.0.5.2]# ls
cloudbuild.yaml  config.m4   CREDITS  Dockerfile   LICENSE       php7    tests
config9.m4       config.w32  docker   example.php  memcache.php  README
[root@server1 memcache-4.0.5.2]# phpize		//没有找到autoconf
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

[root@server1 memcache-4.0.5.2]# yum install -y autoconf
[root@server1 memcache-4.0.5.2]# phpize
Configuring for:
PHP Api Version:         20190902
Zend Module Api No:      20190902
Zend Extension Api No:   320190902
[root@server1 memcache-4.0.5.2]# ls
autom4te.cache   config.h.in   config.w32  example.php   README
build            config.m4     CREDITS     LICENSE       run-tests.php
cloudbuild.yaml  configure     docker      memcache.php  tests
config9.m4       configure.ac  Dockerfile  php7

添加没有的模块

[root@server1 memcache-4.0.5.2]# ./configure --enable-memcache

编译
安装的模块在/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
PHP可以memcache的模块操纵memcached的后台服务
(动态程序都是通过相应的模块来完成这些步骤的)

[root@server1 memcache-4.0.5.2]# make
[root@server1 memcache-4.0.5.2]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
[root@server1 memcache-4.0.5.2]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
memcache.so  opcache.a  opcache.so
[root@server1 memcache-4.0.5.2]# php -m | grep mem	//此时没有memcache的模块,还没有生效
[root@server1 memcache-4.0.5.2]# cd /usr/local/php/etc/	//主配置目录
[root@server1 etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php-fpm.d  php.ini
[root@server1 etc]# vim php.ini 
####
写入913行
extension=memcache	//php自动扫描,找到路径
####
[root@server1 etc]# systemctl reload php-fpm.service 	//平滑加载
[root@server1 etc]# php -m | grep memcache
memcache
  1. 安装memcached的后台服务,端口是11211
    完成好部署之后,还要测试firefox:172.25.23.1/example.php
    /usr/local/nginx/html/是默认发布目录,将memcached准备好的测试文件拷贝到默认发布目录里面
[root@server1 test]# yum install -y memcached.x86_64 
[root@server1 test]# systemctl start memcached.service 
[root@server1 test]# netstat -antuple | grep memcached
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      998        38066      7145/memcached      
tcp6       0      0 :::11211                :::*                    LISTEN      998        38067      7145/memcached      
udp        0      0 0.0.0.0:11211           0.0.0.0:*                           998        38070      7145/memcached      
udp6       0      0 :::11211                :::*                                998        38071      7145/memcached      
[root@server1 test]# ll /etc/sysconfig/memcached 
-rw-r--r-- 1 root root 71 Nov  7  2016 /etc/sysconfig/memcached

这个程序默认占用系统的64兆内存

[root@server1 test]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@server1 test]# ll /etc/sysconfig/memcached 
-rw-r--r-- 1 root root 71 Nov  7  2016 /etc/sysconfig/memcached
[root@server1 test]# cat /etc/sysconfig/memcached 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@server1 test]# cd /test/memcache-4.0.5.2/
[root@server1 memcache-4.0.5.2]# cp example.php /usr/local/nginx/html/

火狐测试:http://172.25.21.2/example.php

[root@server1 html]# curl http://172.25.23.1/example.php
string(28) "String to store in memcached"
int(123)
object(stdClass)#3 (1) {
  ["attribute"]=>
  string(4) "test"
}
[root@server1 html]# cd -
/test/memcache-4.0.5.2

在这里插入图片描述

memcached还准备了一个监控页面,还需要再去测试
火狐测试:http://172.25.21.2/memcache.php

[root@server1 memcache-4.0.5.2]# cp memcache.php /usr/local/nginx/html/
[root@server1 memcache-4.0.5.2]# vim /usr/local/nginx/html/memcache.php 
####
$VERSION='$Id$';

define('ADMIN_USERNAME','admin');       // Admin Username
define('ADMIN_PASSWORD','westos');      // Admin Password
define('DATE_FORMAT','Y/m/d H:i:s');
define('GRAPH_SIZE',200);
define('MAX_ITEM_DUMP',50);

$MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
####

在这里插入图片描述在这里插入图片描述

用户通过nginx去访问,nginx通过fastcgi_pass递交给php-fpm:9000,借助php-memcache模块去使用memcached:11211去处理数据

3. 构建nginx高速缓存

让nginx直接交互memcache
需要给nginx加缓存,要加上2个模块memc和srcache
在这里插入图片描述
在这里插入图片描述

  1. 解压
[root@server1 test]# tar zxf openresty-1.19.3.1.tar.gz 
  1. 直接编译,不需要添加参数模块
    同时,也不需要改路径,因为它的路径不会把之前的nginx覆盖/usr/local/openresty
[root@server1 test]# cd openresty-1.19.3.1/
[root@server1 openresty-1.19.3.1]# ls
bundle  configure  COPYRIGHT  patches  README.markdown  README-windows.txt  util
[root@server1 openresty-1.19.3.1]# ./configure 
[root@server1 openresty-1.19.3.1]# gmake
[root@server1 openresty-1.19.3.1]# gmake install
  1. 停掉之前的nginx,确定没有80端口
[root@server1 openresty-1.19.3.1]# cd /usr/local/openresty/
[root@server1 openresty]# ls
bin  COPYRIGHT  luajit  lualib  nginx  pod  resty.index  site
[root@server1 openresty]# cd nginx/
[root@server1 nginx]# ls
conf  html  logs  sbin
[root@server1 nginx]# nginx -s stop
[root@server1 nginx]# netstat -antuple | grep nginx		//关闭成功
[root@server1 nginx]# /usr/local/openresty/nginx/sbin/nginx 
[root@server1 nginx]# netstat -antuple | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          42605      16238/nginx: master
  1. 修改配置文件
[root@server1 nginx]# pwd
/usr/local/openresty/nginx
[root@server1 nginx]# vim /usr/local/openresty/nginx/conf/nginx.conf
####
user  nginx nginx;
worker_processes  auto;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        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;
        }
####
[root@server1 nginx]# /usr/local/openresty/nginx/sbin/nginx -t	//拥抱绝对路劲检测
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# netstat -antuple		//80端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      0          33010      3716/php-fpm: maste 
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      998        38066      7145/memcached      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          42605      16238/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          24022      3252/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      0          24481      3354/master         
tcp        0      0 172.25.23.1:22          172.25.23.250:51398     ESTABLISHED 0          28189      3530/sshd: root@pts 
tcp6       0      0 :::3306                 :::*                    LISTEN      1001       34477      4131/mysqld         
tcp6       0      0 :::11211                :::*                    LISTEN      998        38067      7145/memcached      
tcp6       0      0 :::22                   :::*                    LISTEN      0          24024      3252/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      0          24482      3354/master         
udp        0      0 0.0.0.0:11211           0.0.0.0:*                           998        38070      7145/memcached      
udp6       0      0 :::11211                :::*                                998        38071      7145/memcached      
[root@server1 nginx]# curl http://172.25.23.1
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Welcome to OpenResty!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to OpenResty!</h1>
<p>If you see this page, the OpenResty web platform is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to our
<a href="https://openresty.org/">openresty.org</a> site<br/>
Commercial support is available at
<a href="https://openresty.com/">openresty.com</a>.</p>
<p>We have articles on troubleshooting issues like <a href="https://blog.openresty.com/en/lua-cpu-flame-graph/?src=wb">high CPU usage</a> and
<a href="https://blog.openresty.com/en/how-or-alloc-mem/">large memory usage</a> on <a href="https://blog.openresty.com/">our official blog site</a>.
<p><em>Thank you for flying <a href="https://openresty.org/">OpenResty</a>.</em></p>
</body>
</html>

测试:firefox:172.25.23.1

  1. 修改主配置文件,构建高效缓存
[root@server1 nginx]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# vim nginx.conf
####

只作为内部调用
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;
        }

        location ~ \.php$ {			//处理请求
                set $key $uri$args;
                srcache_fetch GET /memc $key;		//到memc模块去调用key哈希值
                srcache_store PUT /memc $key;
            root           html;		//没有的话,去这里取数据钥匙,存放到mem模块
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
####
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t	//出现错误,是因为我少写了负载均衡
nginx: [emerg] no port in upstream "memcache" in /usr/local/openresty/nginx/conf/nginx.conf:55
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test failed
[root@server1 conf]# cp /test/memcache-4.0.5.2/example.php /usr/local/openresty/nginx/html/
[root@server1 conf]# vim nginx.conf
####

http {
upstream memcache {		//nginx的负载均衡,定义名字是memcache
        server 127.0.0.1:11211;
        keepalive 512;		//持续连接
        }
    include       mime.types;
    default_type  application/octet-stream;

####
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
  1. 换成高校缓存模式后,我们进行压力测试
    模拟10个并发,处理10000个请求,没有错误
[root@westos ~]# ab -c 10 -n 10000 http://172.25.23.1/example.php
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.25.23.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        openresty/1.19.3.1
Server Hostname:        172.25.23.1
Server Port:            80

Document Path:          /example.php
Document Length:        509 bytes

Concurrency Level:      10
Time taken for tests:   0.526 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      7630000 bytes
HTML transferred:       5090000 bytes
Requests per second:    19019.36 [#/sec] (mean)	///
Time per request:       0.526 [ms] (mean)
Time per request:       0.053 [ms] (mean, across all concurrent requests)
Transfer rate:          14171.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.3      0       1
Waiting:        0    0   0.3      0       1
Total:          0    1   0.3      0       2
ERROR: The median and mean for the total time are more than twice the standard
       deviation apart. These results are NOT reliable.

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      2 (longest request)

LNMP架构基本完成
以下进行优化完善

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值