PHP环境配置与优化(Ubuntu16.04/PHP7)

参考:Modern PHP.Josh Lockhart.中国电力出版社.第7章.配置

 

[配置]


PHP-FPM

用于管理PHP进程池的软件,用于接收和处理来自Web服务器的请求。

全局配置(/etc/php/7.0/fpm/php-fpm.conf)

//这两个配置是在指定时间内有指定个子进程失效,让PHP-FPM重启。
emergency_restart_threshold = 10 emergency_restart_interval = 1m

配置进程池(/etc/php/7.0/fpm/pool.d/*.conf)

PHP-FPM进程池中是一系列相关的PHP子进程。

//拥有这个子进程的系统用户
user = example group = deploy

//监听地址和端口号 listen
= 127.0.0.1:9000 listen.allowed_slients = 127.0.0.1

//进程池中最多能有多少个进程 pm.max_children = 50

//初始化准备进程数 pm.start_servers = 3

//空闲时最少进程数和最大进程数 pm.min_spare_servers = 2 pm.max_spare_servers = 4

//最多能处理的进程数 pm.max_requests = 1000

//记录超过指定时间的请求,这个日志能看到超时前最后具体的调用方法,两个设置必须配合使用 slowlog = /path/to/slowlog.log request_slowlog_timeout = 5s

虚拟主机

//简单列出平时调试的虚拟主机,仅供参考
server {

      listen 80 default_server;
      listen [::]:80 default_server;

//主机名

        server_name dev.company *.dev.company;
        set $sub 'dev';
        if ($host ~ "^(.*).dev.company") {
                set $sub $1;
        }
        set $web '/web';
//文档根目录 root
/home/yangqi/www/$sub$web; sendfile off;

//访问日志与错误日志,请确认路经是否存在 access_log
/home/yangqi/www/logs/$sub.access.log; error_log /home/yangqi/www/logs/$sub.error.log;
//默认寻找的文件 index index.php index.html index.htm adminer.php; try_files $uri $uri
/ /index.php?$args;
//告诉nginx如何处理匹配指定URL模式(try_files)的HTTP请求
location ~ .php$ { fastcgi_split_path_info ^(.+.php)(/.+)$; # # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }

[调优]


所有调整都是在php.ini文件中进行(/etc/php/7.0/fpm/php.ini)

内存

//设置单个PHP进程可以使用的内存最大值
memory_limit

//考虑这个值应该思考几个问题
一共能分配多少内存给PHP?
单个PHP进程平均消耗多少内存?
能负担得起多少个PHP-FPM进程?

Zend Opcach

//Zend Opcach 用于缓存操作码(PHP读取脚本后编译得到)

//为操作码缓存分配的内存量
opcache.memory_consumption = 64

//用来存储驻留字符串 opcache.interned_strings_buffer = 16

//最多可以缓存的PHP脚本数量(一定要比PHP应用的文件数量大)
opcache.max_accelerated_files
= 4000
//是否检查PHP脚本的变化,频率由revalidate_freq决定
opcache.validate_timestamps
= 1 //生产环境应设为0 opcache.revalidate_freq = 0

//把对象析构和内存释放交给Zend Engine的内存管理器完成 opcache.fast_shutdown = 1

文件上传

file_uploads = 1
upload_max_filesize = 10M
max_file_uploads = 3

最长执行时间

max_execution_time = 5

处理会话

//使用文件存储session会占用磁盘,而且不便于服务器扩展,使用Redis/Memcached统一更合适
session.save_handler = 'memcached' session.save_part = '127.0.0.2:11211'

缓冲输出

//在较少的片段中把数据返回给请求者的浏览器,4096作为一个片段,可以减少一次发送的片段总数
output_buffering = 4096 lmplict_flush = false

真实路经缓存

//可以使用在脚本末尾使用realpath_cache_size()查看实际大小
realpath_cache_size = 64k
原文地址:https://www.cnblogs.com/yangqi7/p/6633059.html