CentOS LNMP 最佳实践
一 基础部署与目录规范
yum update -y && yum install -y epel-release wgetfirewall-cmd --permanent --zone=public --add-service=http --add-service=https && firewall-cmd --reloadyum install -y nginx && systemctl enable --now nginxyum install -y mariadb-server mariadb && systemctl enable --now mariadb && mysql_secure_installationyum install -y php php-fpm php-mysqlnd php-gd php-mbstring php-xml php-opcache/var/www/your_domain/var/log/nginx、/var/log/php-fpmnginx:nginx,目录属主:chown -R nginx:nginx /var/www/your_domainecho "<?php phpinfo(); ?>" > /var/www/your_domain/info.phphttp://your_ip/info.php 确认 Nginx + PHP 正常。二 关键配置优化
worker_processes auto; worker_connections 1024;(按 CPU 与内存调优)gzip on; 并为图片、CSS、JS 设置 expires 与 Cache-Controlmap $request_time $loggable { 1.000+ slow; default main; } 将 >1s 请求单独记录到 slow.log,便于定位瓶颈pm = dynamic; pm.max_children = 50; pm.start_servers = 5; pm.min_spare_servers = 5; pm.max_spare_servers = 35; pm.max_requests = 1000request_slowlog_timeout = 1s; slowlog = /var/log/php-fpm/www-slow.logrlimit_files = 65535; env[PATH]=/usr/local/bin:/usr/bin:/bin; security.limit_extensions = .phpopcache.enable=1; opcache.memory_consumption=128; opcache.interned_strings_buffer=16; opcache.max_accelerated_files=10000; opcache.validate_timestamps=1; opcache.revalidate_freq=60memory_limit=128M; max_execution_time=30; post_max_size=16M; upload_max_filesize=10Mdefault_charset="utf-8"; log_errors=On; error_log=/var/log/php-fpm/error.logmax_connections=500~2000(视内存与业务而定)innodb_buffer_pool_size(建议为可用内存的 50%~70%,数据库专用机可更高)slow_query_log=1; long_query_time=1; tmp_table_size=64M; max_heap_table_size=64M[client] [mysql] [mysqld] 段设置 default-character-set=utf8mb4三 安全加固与日常运维
open_basedirdisable_functions = exec,passthru,shell_exec,system,proc_open,popen,show_source四 高可用与扩展
五 性能压测与持续优化
ab/wrk/siege 进行基线压测;关注 P95/P99、RPS、错误率、带宽、CPU/IOpt-query-digest 定位 TOP SQL;补充索引、改写 SQL、避免全表扫描vm.swappiness、dirty_ratio/background_ratio);合理分区与文件系统(如 XFS)