返回列表 发帖

Discuz!X2.5+OPcache+伪静态

笺注:这是在 LNMP一键安装包(lamp_CentOS6.9) 的基础上进行。


[root@localhost ~]# php -v
PHP 7.0.7 (cli) (built: Jun 25 2020 01:53:47) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

图片1.png
2021-2-27 10:17


备注:PHP7系列都不再支持Zend Guard Loader





######

OPcache是Zend开发的闭源但可以免费使用的PHP优化加速缓存组件。

安装OPcache:
[root@localhost ~]# cd lnmp1.3-full
[root@localhost lnmp1.3-full]# ./addons.sh
图片2.png
2021-2-27 10:18



安装或取消安装:
图片3.png
2021-2-27 10:18



OPcache安装成功:
图片4.png
2021-2-27 10:19



服务器本地测试:
[root@localhost ~]# php -v
PHP 7.0.7 (cli) (built: Jun 25 2020 01:53:47) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies


在PHP测试页中,也可以看到OPcache出现了:
图片5.png
2021-2-27 10:19




PHP配置文件中,关于OPcache的配置:(此时OPcache的缓存功能已启用)
[root@localhost ~]# vi /usr/local/php/etc/php.ini
;opcache
[Zend Opcache]
zend_extension="opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1


备注:
opcache.memory_consumption=128  ##为OPcache分配的共享内存,单位为MB;假如服务器的空闲内存比较多,这里的值可以调大一些。


opcache.max_accelerated_files=4000  ##缓存文件的最大数量,这里的值可以调大一些。

网站文件内容更新后,并没有出现延迟;



测试OPcache的缓存功能:
[root@localhost ~]# ab
-bash: ab: command not found

[root@localhost ~]# yum -y install httpd-tools

在没有DNS服务器解析域名的情况下,可以在文件/etc/hosts里绑定:
[root@localhost ~]# tail -1 /etc/hosts
192.168.168.130 www.zhuohua.store

[root@localhost ~]# ab -n 1000 -c 100 http://www.zhuohua.store/forum.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.zhuohua.store (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        www.zhuohua.store
Server Port:            80

Document Path:          /forum.php
Document Length:        12814 bytes

Concurrency Level:      100
Time taken for tests:   7.629 seconds
Complete requests:      1000
Failed requests:        977
   (Connect: 0, Receive: 0, Length: 977, Exceptions: 0)
Write errors:           0
Total transferred:      13637742 bytes
HTML transferred:       12825836 bytes
Requests per second:    131.08 [#/sec] (mean)
Time per request:       762.920 [ms] (mean)
Time per request:       7.629 [ms] (mean, across all concurrent requests)
Transfer rate:          1745.68 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   7.0      0      35
Processing:    41  732 178.9    737    1487
Waiting:       11  728 178.0    727    1419
Total:         52  734 174.8    737    1487

Percentage of the requests served within a certain time (ms)
  50%    737
  66%    804
  75%    828
  80%    845
  90%    883
  95%    935
  98%   1219
  99%   1294
100%   1487 (longest request)

笺注:可以看到OPcache的缓存功能启用后,每秒最大处理请求次数为131.08次,每个请求响应时间是7.629毫秒。





在PHP配置文件中关闭OPcache的缓存功能:
[root@localhost ~]# vi /usr/local/php/etc/php.ini
加上:  opcache.enable=0
图片6.png
2021-2-27 10:22



图片7.png
2021-2-27 10:22



再次测试:
[root@localhost ~]# ab -n 1000 -c 100 http://www.zhuohua.store/forum.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.zhuohua.store (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        www.zhuohua.store
Server Port:            80

Document Path:          /forum.php
Document Length:        12735 bytes

Concurrency Level:      100
Time taken for tests:   22.810 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      13547060 bytes
HTML transferred:       12735000 bytes
Requests per second:    43.84 [#/sec] (mean)
Time per request:       2280.993 [ms] (mean)
Time per request:       22.810 [ms] (mean, across all concurrent requests)
Transfer rate:          579.99 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.9      0      10
Processing:    41 2247 661.2   2240    4443
Waiting:       22 2149 624.0   2107    4262
Total:         41 2248 659.4   2240    4443

Percentage of the requests served within a certain time (ms)
  50%   2240
  66%   2429
  75%   2647
  80%   2776
  90%   3065
  95%   3229
  98%   3500
  99%   3635
100%   4443 (longest request)

笺注:可以看到OPcache的缓存功能没有启用时,每秒最大处理请求次数为43.845次,每个请求响应时间是22.810毫秒。





在PHP配置文件中再次启用OPcache的缓存功能:
[root@localhost ~]# vi /usr/local/php/etc/php.ini
修改:  opcache.enable=1
图片8.png
2021-2-27 10:23



图片9.png
2021-2-27 10:24



再次测试:
[root@localhost ~]# ab -n 1000 -c 100 http://www.zhuohua.store/forum.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.zhuohua.store (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        Apache
Server Hostname:        www.zhuohua.store
Server Port:            80

Document Path:          /forum.php
Document Length:        12736 bytes

Concurrency Level:      100
Time taken for tests:   7.414 seconds
Complete requests:      1000
Failed requests:        994
   (Connect: 0, Receive: 0, Length: 994, Exceptions: 0)
Write errors:           0
Total transferred:      13560824 bytes
HTML transferred:       12747741 bytes
Requests per second:    134.89 [#/sec] (mean)
Time per request:       741.366 [ms] (mean)
Time per request:       7.414 [ms] (mean, across all concurrent requests)
Transfer rate:          1786.30 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.2      0       8
Processing:    53  718 177.1    692    1324
Waiting:       51  706 170.9    685    1324
Total:         60  718 175.6    692    1324

Percentage of the requests served within a certain time (ms)
  50%    692
  66%    765
  75%    799
  80%    827
  90%    917
  95%    970
  98%   1100
  99%   1233
100%   1324 (longest request)

笺注:可以看到OPcache的缓存功能再次启用后,每秒最大处理请求次数为134.89次,每个请求响应时间是7.414毫秒。












#############
#############

Discuz!是采用PHP语言开发的,动态地址不利于蜘蛛抓取,开启伪静态有利于SEO优化。
其实Discuz!不开启伪静态,也是可以进行蜘蛛抓取的。


Discuz!X2.5开启伪静态:

管理中心》全局》SEO设置》URL静态化:
图片10.png
2021-2-27 10:25


图片11.png
2021-2-27 10:26

注释:全选。


图片12.png
2021-2-27 10:26



提交后,点击右上角的 查看当前的Rewrite规则
图片13.png
2021-2-27 10:26



复制这一段代码:(站点Discuz!使用的是Apache基于域名的虚拟主机,所以这里选择Apache Web Server(虚拟主机用户)
图片14.png
2021-2-27 10:27



笺注:也有Nginx的Rewrite规则:
图片15.png
2021-2-27 10:27



在Discuz!的站点目录下,创建伪静态规则文件:( .htaccess )
[root@localhost zhuohua.store]# pwd
/www/zhuohua.store

[root@localhost zhuohua.store]# vi .htaccess
# 将 RewriteEngine 模式打开
RewriteEngine On

# 修改以下语句中的 /discuz 为您的论坛目录地址,如果程序放在根目录中,请将 /discuz 修改为 /
#RewriteBase /discuz

RewriteBase /

# Rewrite 系统规则请勿修改
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ plugin.php?id=$1:$2&%1


[root@localhost zhuohua.store]# ll .htaccess
-rw-r--r-- 1 root root 1320 2月  26 19:49 .htaccess


保存后,重启一下Apache:
[root@localhost ~]# service httpd restart
restart apache...  done


测试:

伪静态开启后,也不是说所有URL的表现形式都显示为静态页面:
图片16.png
2021-2-27 10:28



图片17.png
2021-2-27 10:29




板块、文章的URL的表现形式都显示为静态页面:
图片18.png
2021-2-27 10:29



图片19.png
2021-2-27 10:29






相关文章:
Discuz!X3.3+OPcache+伪静态
Discuz!7.2+Zend OPcache

返回列表