在CentOS上优化PHP设置可以通过多个方面来实现,包括安装最新的PHP版本、启用和配置OPcache、调整PHP配置文件(php.ini)参数、以及系统级优化等。以下是详细的步骤和建议:
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php83
yum install -y php php-cli php-fpm php-devel php-gd php-mcrypt php-mbstring php-mysqlnd php-pdo php-xml php-process php-curl php-bcmath php-json php-opcache php-zip php-pear php-redis
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=64
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.fast_shutdown=1
memory_limit = 256M
max_execution_time = 30
file_uploads = On
max_file_uploads = 10
upload_max_filesize = 10M
session.save_handler = memcached
session.save_path = 127.0.0.1:11211
disable_functions = system, exec, shell_exec, passthru, popen, php, php_eval, eval, assert
expose_php = Off
echo 'vm.swappiness = 10' >> /etc/sysctl.conf
sysctl -p
echo '8192' > /sys/block/sda/queue/read_ahead_kb
sudo sysctl -w vm.nr_hugepages=128
修改php配置文件:opcache.huge_code_pages=1
重新启动php-fpm:sudo service php-fpm restart
ab -n 1000 -c 50 http://yourserver.com/
zend_extension=/path/to/xdebug.so
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/tmp"
通过这些优化措施,可以显著提高PHP应用程序在CentOS上的性能和安全性。每个应用程序的需求和环境不同,因此建议在开发和测试环境中进行充分测试后再应用到生产环境。