# Linux实战笔记:WordPress文创项目四项优化结合Ansible ROLE

📅 2026/7/2 14:35:57
# Linux实战笔记:WordPress文创项目四项优化结合Ansible ROLE
本文是另一篇博客# Linux实战笔记WordPress文创项目部署全流程记录的内容补充将第一阶段的四项性能优化融入一键部署lnmp的ROLE之中方便后续批量部署。一、LNMP Role 融入性能优化的详细修改步骤本次配置全程在zop控制节点主机进行操作。步骤1定位Role目录确认基础结构确保Role目录结构完整本次修改涉及vars/main.yml、templates/nginx.conf.j2、templates/www.conf.j2、tasks/main.yml四个核心文件先进入Role目录[rootzop ansible]# cd roles/lnmp/步骤2修改变量文件vars/main.yml—— 新增性能优化相关变量为统一管理优化参数后续调整无需改模板/任务在vars/main.yml中新增Nginx、PHP-FPM、OPcache的优化变量编辑vars/main.yml[rootzop lnmp]# vim vars/main.yml新增以下变量追加到原有内容后# 性能优化 - Nginx 相关 # Nginx工作进程数auto匹配CPU核心nginx_worker_processes:auto# Nginx单进程最大连接数nginx_worker_connections:1024# Nginx事件模型epoll为Linux高性能模型nginx_events_use:epoll# Nginx是否开启多连接接收nginx_multi_accept:on# 性能优化 - PHP-FPM 相关 # PHP-FPM进程管理模式dynamic动态php_fpm_pm:dynamic# PHP-FPM最大进程数2G内存建议204G建议40php_fpm_max_children:20# PHP-FPM启动初始进程数php_fpm_start_servers:5# PHP-FPM空闲最小进程数php_fpm_min_spare_servers:2# PHP-FPM空闲最大进程数php_fpm_max_spare_servers:8# PHP-FPM单进程最大请求数防止内存泄漏php_fpm_max_requests:1000# 性能优化 - PHP OPcache 相关 # 是否开启OPcachephp_opcache_enable:1# CLI模式是否开启OPcachephp_opcache_enable_cli:1# OPcache共享内存大小MBphp_opcache_memory_consumption:128# OPcache最大缓存脚本数php_opcache_max_accelerated_files:10000# OPcache脚本更新检查频率秒开发期60生产期3600php_opcache_revalidate_freq:60# 是否开启OPcache快速关闭php_opcache_fast_shutdown:1# 是否保存注释兼容WordPress插件php_opcache_save_comments:1步骤3修改Nginx配置模板templates/nginx.conf.j2—— 融入Nginx进程/连接数、静态资源缓存优化编辑nginx.conf.j2模板[rootzop lnmp]# vim templates/nginx.conf.j2替换/新增核心优化内容全局块引用变量设置worker_processesevents块新增epoll事件模型、multi_accept多连接接收server块新增静态资源缓存的location规则。步骤4修改PHP-FPM配置模板templates/www.conf.j2—— 融入PHP-FPM进程数优化编辑www.conf.j2模板[rootzop lnmp]# vim templates/www.conf.j2将原有硬编码的PM进程数配置替换为变量引用适配不同服务器资源场景。步骤5修改任务文件tasks/main.yml—— 新增OPcache安装与配置任务编辑tasks/main.yml[rootzop lnmp]# vim tasks/main.yml在“启动 PHP-FPM 服务”任务前新增安装php-opcache和配置OPcache参数的任务通过lineinfile模块写入php.ini确保幂等性。步骤6验证Handlershandlers/main.yml—— 确保服务重载/重启生效原有handlers已包含“重载Nginx”“重启PHP-FPM”无需修改仅需确认内容完整。步骤7运行Playbook验证优化生效执行原有deploy_lnmp.yml剧本批量部署包含性能优化的LNMP[rootzop ansible]# ansible-playbook deploy_lnmp.yml二、修改后LNMP Role 所有核心配置文件完整内容1. vars/main.yml完整内容# 路径与版本变量nginx_version:1.22.1nginx_install_path:/usr/local/nginxnginx_src_package:/root/nginx-{{ nginx_version }}.tar.gznginx_user:nginxweb_root:{{ nginx_install_path }}/html# PHP 基础配置php_fpm_listen:127.0.0.1:9000# MariaDB 配置mariadb_service:mariadb# 性能优化 - Nginx 相关 nginx_worker_processes:autonginx_worker_connections:1024nginx_events_use:epollnginx_multi_accept:on# 性能优化 - PHP-FPM 相关 php_fpm_pm:dynamicphp_fpm_max_children:20php_fpm_start_servers:5php_fpm_min_spare_servers:2php_fpm_max_spare_servers:8php_fpm_max_requests:1000# 性能优化 - PHP OPcache 相关 php_opcache_enable:1php_opcache_enable_cli:1php_opcache_memory_consumption:128php_opcache_max_accelerated_files:10000php_opcache_revalidate_freq:60php_opcache_fast_shutdown:1php_opcache_save_comments:12. templates/nginx.conf.j2完整内容# 全局块引用变量设置工作进程数 worker_processes {{ nginx_worker_processes }}; # events块性能优化配置 events { worker_connections {{ nginx_worker_connections }}; use {{ nginx_events_use }}; multi_accept {{ nginx_multi_accept }}; } http { include mime.types; default_type application/octet-stream; server_tokens off;# 安全加固隐藏版本号 sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root {{ web_root }}; index index.php index.html index.htm; location / { root html; index index.php index.html index.htm; } # 性能优化静态资源缓存规则 location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot|svg)$ { expires 7d; add_header Cache-Control public, no-transform; access_log off; } error_page 500 502 503 504 /50x.html; location /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass {{ php_fpm_listen }}; fastcgi_index index.php; include fastcgi.conf; } } }3. templates/www.conf.j2完整内容[www] ; 基础配置 listen {{ php_fpm_listen }} listen.allowed_clients 127.0.0.1 listen.acl_users apache,nginx user apache group apache ; 性能优化PHP-FPM进程数配置 pm {{ php_fpm_pm }} pm.max_children {{ php_fpm_max_children }} pm.start_servers {{ php_fpm_start_servers }} pm.min_spare_servers {{ php_fpm_min_spare_servers }} pm.max_spare_servers {{ php_fpm_max_spare_servers }} pm.max_requests {{ php_fpm_max_requests }} ; 日志与会话配置默认保留 slowlog /var/log/php-fpm/www-slow.log php_admin_value[error_log] /var/log/php-fpm/www-error.log php_admin_flag[log_errors] on php_value[session.save_handler] files php_value[session.save_path] /var/lib/php/session php_value[soap.wsdl_cache_dir] /var/lib/php/wsdlcache4. tasks/main.yml完整内容---# 1. 安装系统依赖Nginx编译依赖 PHP/MariaDB 运行依赖-name:安装 LNMP 全量依赖包yum:name:-gcc-make-pcre-devel-openssl-devel-php-php-fpm-php-mysqlnd-php-json-mariadb-mariadb-server-mariadb-develstate:present# 2. 创建 Nginx 运行用户-name:创建 Nginx 系统用户user:name:{{ nginx_user }}shell:/sbin/nologincreate_home:nostate:present# 3. 拷贝 Nginx 源码包到被管理节点-name:拷贝 Nginx 源码包copy:src:{{ nginx_src_package }}dest:/root/mode:0644# 4. 解压源码包-name:解压 Nginx 源码包unarchive:src:/root/nginx-{{ nginx_version }}.tar.gzdest:/root/remote_src:yescreates:/root/nginx-{{ nginx_version }}# 已解压则跳过# 5. 编译安装 Nginx已安装则跳过实现幂等-name:编译安装 Nginxshell:|./configure --prefix{{ nginx_install_path }} \ --user{{ nginx_user }} --group{{ nginx_user }} \ --with-http_ssl_module --with-http_stub_status_module make make installargs:chdir:/root/nginx-{{ nginx_version }}creates:{{ nginx_install_path }}/sbin/nginx# 已存在则不执行编译# 6. 渲染 Nginx 主配置文件模板方式替代 sed 改行号-name:生成 Nginx 主配置文件template:src:nginx.conf.j2dest:{{ nginx_install_path }}/conf/nginx.confmode:0644notify:重载 Nginx# 配置变更才触发重载# 7. 渲染 PHP-FPM 配置文件-name:配置 PHP-FPM 监听端口template:src:www.conf.j2dest:/etc/php-fpm.d/www.confmode:0644notify:重启 PHP-FPM# 性能优化安装并配置PHP OPcache -name:安装 PHP OPcache 扩展yum:name:php-opcachestate:present-name:配置 PHP OPcache 优化参数lineinfile:path:/etc/php.iniline:|[opcache] opcache.enable {{ php_opcache_enable }} opcache.enable_cli {{ php_opcache_enable_cli }} opcache.memory_consumption {{ php_opcache_memory_consumption }} opcache.max_accelerated_files {{ php_opcache_max_accelerated_files }} opcache.revalidate_freq {{ php_opcache_revalidate_freq }} opcache.fast_shutdown {{ php_opcache_fast_shutdown }} opcache.save_comments {{ php_opcache_save_comments }}regexp:^\[opcache\]# 匹配[opcache]开头行避免重复添加create:yes# 若php.ini无opcache段则创建notify:重启 PHP-FPM# 8. 启动 MariaDB 服务并设置开机自启-name:启动 MariaDB 服务service:name:{{ mariadb_service }}state:startedenabled:yes# 9. 启动 PHP-FPM 服务并设置开机自启-name:启动 PHP-FPM 服务service:name:php-fpmstate:startedenabled:yes# 10. 设置 Nginx 开机自启写入 rc.local兼容原有习惯-name:设置 Nginx 开机自启lineinfile:path:/etc/rc.d/rc.localline:{{ nginx_install_path }}/sbin/nginxstate:present-name:赋予 rc.local 执行权限file:path:/etc/rc.d/rc.localmode:0755# 11. 启动 Nginx 服务幂等已启动则不重复操作-name:启动 Nginx 服务shell:{{ nginx_install_path }}/sbin/nginxargs:creates:/var/run/nginx.pid5. handlers/main.yml完整内容---# handlers file for roles/lnmp-name:重载 Nginxshell:{{ nginx_install_path }}/sbin/nginx -s reload-name:重启 PHP-FPMservice:name:php-fpmstate:restarted-name:重启 MariaDBservice:name:{{ mariadb_service }}state:restarted三、验证优化生效的方法Nginx 优化验证# 查看Nginx优化参数是否加载ansible webserver-mshell-a/usr/local/nginx/sbin/nginx -T | grep -E worker_processes|worker_connections|epoll|multi_accept# 验证静态资源缓存curl-Ihttp://Web节点IP/wp-includes/css/dashicons.min.css|grep-EExpires|Cache-ControlPHP-FPM 优化验证# 查看PHP-FPM进程数配置ansible webserver-mshell-agrep -E pm.max_children|pm.start_servers /etc/php-fpm.d/www.conf# 查看PHP-FPM实际进程数ansible webserver-mshell-aps aux | grep php-fpm | wc -lOPcache 优化验证# 创建phpinfo文件ansible webserver-mcopy-acontent?php phpinfo(); ? dest/usr/local/nginx/html/opcache.php mode0644# 访问验证OPcache状态curlhttp://Web节点IP/opcache.php|grep-iopcache|head-20