PHP CLI has a few seconds delay

2019-07-21 21:23发布

问题:

When I run PHP in the CLI mode (PHP 5.6.6 under CentOS 6.5 running as VM using VirtualBox) it has a few seconds of delay even if I only check version and there is php.ini file disabled:

time php -n -v
PHP 5.6.6 (cli) (built: Apr  2 2015 14:18:24) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

real    0m5.066s
user    0m0.012s
sys     0m0.006s

回答1:

The problem is related to DNS call PHP is doing. See here and here.

The solution is to add hostname

[root@dev-machine ~]# hostname
dev-machine.com

to /etc/hosts:

127.0.0.1 dev-machine.com

Result:

time php -n -v
PHP 5.6.6 (cli) (built: Apr  2 2015 14:18:24) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

real    0m0.018s
user    0m0.012s
sys     0m0.004s