阿里云/腾讯云 VPS 部署 LNMP/LAMP 与 SSL/TLS 证书配置实战(CentOS 迁移修订版)
问题背景与根因:旧 LNMP 部署文章为什么必须重写
原文《阿里云 腾讯云 网站环境部署搭建 lnmp主机配置 linux Centos转发》以服务清单为主,列出 LNMP、LAMP、wdcp/kloxo/AMH 面板、VPN、CDN、监控等,但缺少可复现的版本、命令、TLS 终止配置和验证步骤。真正导致新手部署失败的根因通常不是 Linux 不会装,而是版本与边界条件过时:CentOS 7 已于 2024-06-30 EOL,CentOS 8 已于 2021-12-31 EOL,新购阿里云/腾讯云 VPS 应优先选 Rocky Linux 9、AlmaLinux 9、Ubuntu 22.04/24.04;公开信任 TLS 证书最长有效期已按 CA/Browser Forum 规则限制为 398 天,Let’s Encrypt 证书固定 90 天,必须自动续期;LNMPA 虽然兼顾静态与动态,但内存占用高,1C2G 机器上容易 OOM;宝塔、wdcp 等面板会额外占用 200-500MB 内存并开放面板端口,安全组若只放行 22 而忘记 80/443,Certbot HTTP-01 验证会直接失败。另一个高频根因是 PHP-FPM 与 Nginx 的 socket 权限不一致,报错为 connect() to unix:/run/php-fpm/www.sock failed (13: Permission denied)。初始化前先执行 cat /etc/os-release、getenforce、firewall-cmd –state,确认系统、SELinux、防火墙状态;阿里云安全组和腾讯云防火墙都要放行 TCP 22、80、443,SSH 改端口后还要同步放行新端口。本文保留阿里云、腾讯云、LNMP、LAMP、CentOS、VPS、面板、安全配置等核心关键词,但把部署流程改写为可直接执行的版本,并补上 SSL/TLS 证书申请、Nginx/Apache 配置、错误排查和效果验证。
系统环境选择与 VPS 初始化:先定 LNMP、LAMP 还是 LNMPA
选型先看内存和页面类型:1C1G/1C2G 的阿里云/腾讯云轻量 VPS 优先 LNMP,Nginx 处理静态文件,PHP-FPM 跑动态;2C4G 且以 WordPress、Discuz 等动态页面为主可选 LAMP,Apache 的 .htaccess 兼容性更好;4C8G 以上或独立服务器才考虑 LNMPA,由 Nginx 抗静态与并发、Apache 跑动态,但内存占用最高。面板不是刚需:宝塔、aaPanel、wdcp 可视化强,但会常驻 Python/面板服务并监听 8888 等端口,小内存机器建议纯命令行。初始化命令以 Rocky/AlmaLinux 9 为例,CentOS 7 仅作迁移参考:hostnamectl set-hostname web01;dnf update -y;timedatectl set-timezone Asia/Shanghai;dnf install -y epel-release vim curl wget tar unzip firewalld policycoreutils-python-utils;systemctl enable –now firewalld;useradd -m deploy;usermod -aG wheel deploy;sed -i ‘s/^#PermitRootLogin.*/PermitRootLogin no/’ /etc/ssh/sshd_config;sed -i ‘s/^#Port 22/Port 22222/’ /etc/ssh/sshd_config;firewall-cmd –permanent –add-port=22222/tcp;firewall-cmd –permanent –add-service=http;firewall-cmd –permanent –add-service=https;firewall-cmd –reload;systemctl restart sshd。改 SSH 端口前先在云安全组放行 22222,避免锁死。SELinux 不要直接 setenforce 0 了事,先 getenforce 记录状态,后续用 semanage、restorecon 放行网站目录和端口。VPS 初始化还要检查磁盘:df -h、lsblk,数据盘挂载到 /var/www 时用 mount /dev/vdb1 /var/www 并写入 /etc/fstab,避免重启后网站目录丢失。完成后再安装 Web 栈,否则 MySQL 数据目录和网站目录规划会返工。
LNMP 完整安装实操:Nginx、PHP-FPM、MySQL 8 可直接执行
以下命令适用于 Rocky Linux 9/AlmaLinux 9,CentOS Stream 9 同理。安装组件:dnf install -y nginx mariadb-server php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-json php-curl php-zip;systemctl enable –now nginx mariadb php-fpm;mysql_secure_installation。创建数据库与用户:mysql -uroot -p 后执行 CREATE DATABASE wp DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER ‘wpuser’@’localhost’ IDENTIFIED BY ‘StrongPass!2026’; GRANT ALL PRIVILEGES ON wp.* TO ‘wpuser’@’localhost’; FLUSH PRIVILEGES;。MySQL 8 默认 caching_sha2_password,PHP 7.4 及以上可用;PHP 5.6/7.0 会报 SQLSTATE[HY000] [2054],应升级 PHP 或仅对旧程序临时执行 ALTER USER ‘wpuser’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘StrongPass!2026’;。Nginx 站点配置写入 /etc/nginx/conf.d/site.conf:server { listen 80; server_name example.com www.example.com; root /var/www/site; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ [.]php$ { fastcgi_pass unix:/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }。创建目录:mkdir -p /var/www/site;echo ‘<?php phpinfo();’ > /var/www/site/index.php;chown -R nginx:nginx /var/www/site;restorecon -Rv /var/www/site。PHP-FPM 关键项在 /etc/php-fpm.d/www.conf:user = nginx、group = nginx、listen.owner = nginx、listen.group = nginx、listen.mode = 0660;执行 sed -i ‘s/^user = apache/user = nginx/’ /etc/php-fpm.d/www.conf;sed -i ‘s/^group = apache/group = nginx/’ /etc/php-fpm.d/www.conf;systemctl restart php-fpm nginx;nginx -t。若 socket 不存在,检查 /run/php-fpm 目录和 systemctl status php-fpm。测试 curl -I http://example.com/index.php,返回 200 且出现 PHP Version 即 LNMP 打通。生产环境删除 phpinfo,设置 php.ini 的 expose_php = Off、upload_max_filesize = 64M、post_max_size = 64M、memory_limit = 256M。
SSL/TLS 证书申请与 Nginx/Apache 配置示例
先做 DNS:example.com 和 www.example.com 的 A 记录指向 VPS 公网 IP,阿里云/腾讯云安全组放行 TCP 80、443。Let’s Encrypt 证书有效期 90 天,公开信任商业 TLS 证书最长 398 天,超过 398 天的证书会被主流浏览器拒绝。Nginx 站点可用 Certbot:dnf install -y certbot python3-certbot-nginx;certbot –nginx -d example.com -d www.example.com –email ops@example.com –agree-tos –no-eff-email –redirect。Apache 用:dnf install -y certbot python3-certbot-apache;certbot –apache -d example.com -d www.example.com。申请失败先查 80 端口:firewall-cmd –list-services;curl -I http://example.com/.well-known/acme-challenge/test。商业证书先生成 CSR:openssl req -new -newkey rsa:2048 -nodes -keyout example.com.key -out example.com.csr -subj ‘/C=CN/ST=Shanghai/L=Shanghai/O=Example/CN=example.com’;把 CSR 提交 CA,下载 fullchain.crt 与私钥。Nginx 手动配置:server { listen 443 ssl http2; server_name example.com www.example.com; ssl_certificate /etc/nginx/ssl/fullchain.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; add_header Strict-Transport-Security max-age=31536000; root /var/www/site; index index.php; location ~ [.]php$ { fastcgi_pass unix:/run/php-fpm/www.sock; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }。80 端口 server 保留并 return 301 https://$host$request_uri;。Apache 配置:<VirtualHost *:443> ServerName example.com:443 SSLEngine on SSLCertificateFile /etc/httpd/ssl/fullchain.crt SSLCertificateKeyFile /etc/httpd/ssl/example.com.key SSLCertificateChainFile /etc/httpd/ssl/chain.crt </VirtualHost>,执行 apachectl configtest。Certbot 自动续期:systemctl enable –now certbot-renew.timer;certbot renew –dry-run。证书路径权限要允许 nginx 或 apache 读取,私钥建议 chmod 600、chown root:root,通过组读权限或复制到受控目录解决。
环境与证书方案对比表格:LNMP、LAMP、LNMPA 和证书类型怎么选
原文只给优缺点,缺少可量化对比。下表按 1C2G、2C4G、4C8G 典型 VPS 给出选型建议。LNMP 的 Nginx 事件模型静态性能好,PHP-FPM 按进程处理动态,内存约 300-600MB 可跑小站;LAMP 的 Apache prefork/event 对 .htaccess、rewrite 兼容好,动态稳定但内存通常多 200-400MB;LNMPA 让 Nginx 反代 Apache,静态与动态兼顾,但两套 Web 服务常驻,内存占用最高。面板方面,宝塔/aaPanel 可降低门槛,但面板服务、计划任务、Python 环境会额外占内存,且面板端口必须限制来源 IP。证书方面,Let’s Encrypt 是 DV 免费证书,90 天,自动化最好;商业 DV 可到 398 天,适合不想频繁续期的静态站;OV/EV 需组织验证,证书有效期同样受 398 天限制,EV 的浏览器绿色地址栏展示已弱化,不应作为唯一选型依据。阿里云、腾讯云控制台的免费证书政策会调整,以控制台实际有效期为准,不要按旧文章的 1 年或 2 年规划。表格中的命令和参数可直接落地:nginx -V 看模块,httpd -M 看 Apache 模块,openssl version 看 TLS 能力。若使用 CDN,源站证书和 CDN 边缘证书要分别配置,回源 443 需放行 CDN 回源 IP。
| 方案 | 静态性能 | 动态稳定性 | 内存占用 | 适用场景 |
|---|---|---|---|---|
| LNMP | 高 | 中 | 低,约 300-600MB | 1C2G 轻量 VPS、静态站、缓存型 WordPress |
| LAMP | 中 | 高 | 中,约 600-1000MB | 2C4G 动态站、需要 .htaccess 的程序 |
| LNMPA | 高 | 高 | 高,约 1GB 以上 | 4C8G 以上、独立服务器、复杂动态站 |
| Let’s Encrypt DV | 90 天 | 免费 | 自动续期 | 大多数站点,Certbot 管理 |
| 商业 DV | 最长 398 天 | 付费 | 手动续期 | 不想频繁续期、需要商业支持 |
| OV/EV | 最长 398 天 | 付费 | 需组织验证 | 金融、政企展示,EV UI 优势弱化 |
安全加固与防火墙、SELinux、SSH 配置
安全配置不能只写修改 SSH 端口、禁用 root 登录,要给出可执行顺序。先放行新 SSH 端口再改配置:firewall-cmd –permanent –add-port=22222/tcp;firewall-cmd –reload;sed -i ‘s/^#Port 22/Port 22222/’ /etc/ssh/sshd_config;sed -i ‘s/^#PermitRootLogin yes/PermitRootLogin no/’ /etc/ssh/sshd_config;sed -i ‘s/^#PasswordAuthentication yes/PasswordAuthentication no/’ /etc/ssh/sshd_config;systemctl restart sshd;ss -lntp | grep 22222。确认 deploy 用户可用 sudo 后再退出 root。防火墙只放行业务端口:firewall-cmd –permanent –add-service=http;firewall-cmd –permanent –add-service=https;firewall-cmd –reload;firewall-cmd –list-all。阿里云安全组、腾讯云防火墙是另一层,必须同步放行 22222、80、443,数据库 3306 不要对公网开放。SELinux 保持 enforcing 时,网站目录执行 restorecon -Rv /var/www/site;Nginx 反代本地端口需 setsebool -P httpd_can_network_connect 1;Apache 监听非标准端口用 semanage port -a -t http_port_t -p tcp 8080。MySQL 执行 mysql_secure_installation,确认 bind-address=127.0.0.1,禁止 root 远程登录,业务用户只授权单库。PHP 关闭危险函数:在 php.ini 设置 disable_functions = exec,passthru,shell_exec,system,proc_open,popen;expose_php = Off。安装 fail2ban 防 SSH 爆破:dnf install -y fail2ban;systemctl enable –now fail2ban;fail2ban-client status sshd。日志轮转用 logrotate,/etc/logrotate.d/nginx 保留 14 天。最后用 nmap -Pn -p 22,80,443,3306 VPS_IP 从外网确认 3306 不可达,避免只改本机防火墙而云安全组仍开放。
常见错误排查:真实报错与定位命令
1)Nginx 502:connect() to unix:/run/php-fpm/www.sock failed (2: No such file or directory) 或 (13: Permission denied)。执行 systemctl status php-fpm、ls -l /run/php-fpm/www.sock、tail -50 /var/log/php-fpm/www-error.log;确认 www.conf 的 listen、user、group 与 Nginx 运行用户一致,目录权限 chown nginx:nginx /run/php-fpm。2)SSL 私钥与证书不匹配:SSL_CTX_use_certificate_chain_file() failed (SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch)。用 openssl x509 -noout -modulus -in fullchain.crt | openssl md5 和 openssl rsa -noout -modulus -in example.com.key | openssl md5 比对,两个 md5 必须一致;不一致说明上传了旧私钥或证书链顺序错误。3)Certbot 验证失败:Challenge failed for domain example.com: Connection refused。查安全组 80、firewall-cmd –list-services、Nginx server_name 是否包含该域名、CDN 是否开启强制 HTTPS 导致 HTTP-01 被跳转;可临时用 certbot certonly –webroot -w /var/www/site -d example.com 验证。4)Apache 启动失败:AH00526: Syntax error on line 12 of /etc/httpd/conf.d/ssl.conf: SSLCertificateFile: file /etc/httpd/ssl/fullchain.crt does not exist or is empty。检查路径、权限、SELinux 上下文,执行 apachectl configtest。5)MySQL 8 连接报 SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client。升级 PHP 到 7.4+,或 ALTER USER ‘wpuser’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘StrongPass!2026’;。6)网站 403 Forbidden 但文件存在:getenforce 为 Enforcing 时执行 restorecon -Rv /var/www/site;chcon -R -t httpd_sys_content_t /var/www/site;查看 /var/log/audit/audit.log 的 AVC 拒绝记录。排查顺序固定为:nginx -t 或 apachectl configtest → systemctl status → ss -lntp → 防火墙/安全组 → SELinux → 应用日志,避免一上来重装环境。
效果验证与续期监控:证书、端口、日志一次查清
部署完成后按层验证。Web 服务:nginx -t;apachectl configtest;systemctl status nginx php-fpm mariadb;ss -lntp | grep -E ‘:80|:443|:3306’。HTTP 跳转:curl -I http://example.com 应返回 301 且 Location 为 https://example.com/;curl -I https://example.com 应返回 200,响应头包含 strict-transport-security。TLS 证书:echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates;确认 Not After 不超过 398 天,Let’s Encrypt 为 90 天;检查证书链:openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null。续期:certbot renew –dry-run;systemctl list-timers | grep certbot;商业证书在到期前 30 天续签并替换 fullchain.crt 与 key 后 nginx -s reload 或 systemctl reload httpd。监控证书剩余天数可写脚本:openssl x509 -checkend 2592000 -noout -in /etc/letsencrypt/live/example.com/cert.pem || echo 证书30天内到期;接入阿里云云监控/腾讯云监控自定义告警。日志:tail -f /var/log/nginx/error.log /var/log/php-fpm/www-error.log /var/log/httpd/ssl_error_log;用 grep ‘SSL_CTX’、grep ‘ 502 ‘、grep ‘ 403 ‘ 快速定位。最后用 SSL Labs 或 testssl.sh 评级:testssl.sh –severity HIGH https://example.com,目标为 A 级、支持 TLS 1.2/1.3、无 RC4/3DES、证书链完整。若使用 CDN,分别验证边缘证书和回源证书,避免源站证书过期但 CDN 仍正常导致回源 502。
常见问题
阿里云/腾讯云安全组已放行 443,为什么 HTTPS 仍打不开?
先在本机执行 ss -lntp | grep 443 确认 Nginx/Apache 已监听,再查 firewalld:firewall-cmd –list-services 是否含 https;SELinux 是否阻止非标准端口;Nginx server_name 是否匹配域名;CDN 回源端口是否写成 80 而源站只开 443。云安全组、系统防火墙、Web 服务监听、CDN 回源四层都要放行。
Let’s Encrypt 只有 90 天,能不能申请 398 天证书?
不能。Let’s Encrypt 固定签发 90 天 DV 证书,公开信任 TLS 证书最长有效期为 398 天,任何 CA 都不能签发超过 398 天的公开信任证书。正确做法是 systemctl enable –now certbot-renew.timer,并用 certbot renew –dry-run 验证自动续期;商业证书可买 398 天,但仍需在到期前续签替换。
LNMP 和 LAMP 到底怎么选?
1C1G/1C2G 轻量 VPS 选 LNMP,Nginx 静态性能好、内存低;2C4G 且程序依赖 .htaccess、mod_rewrite 选 LAMP;4C8G 以上才考虑 LNMPA。不要在小内存机器上同时跑 Nginx、Apache、MySQL 8、面板和 Docker,容易触发 OOM Killer,用 dmesg -T | grep -i oom 可确认。
部署 SSL 后浏览器仍提示不安全怎么办?
常见原因是混合内容、证书链不全、域名不匹配。用浏览器开发者工具 Console 查 Mixed Content;用 openssl s_client -connect example.com:443 -servername example.com -showcerts 检查是否缺中间证书;确认证书 SAN 包含 www 和非 www;HSTS 开启前先确保全站 HTTPS 可访问,否则 max-age=31536000 会把用户锁在错误配置上。
CentOS 7 还能继续部署 LNMP 吗?
能运行但不建议新购。CentOS 7 已于 2024-06-30 EOL,官方不再提供安全更新;CentOS 8 已于 2021-12-31 EOL。新项目选 Rocky Linux 9、AlmaLinux 9 或 Ubuntu 22.04/24.04;存量 CentOS 7 至少迁移到 vault 源并做隔离,但不要承载新业务。迁移时同步检查 PHP 版本、MySQL 8 认证插件和证书自动续期脚本。
安装宝塔/aaPanel 面板后 8888 端口要不要对公网开放?
不要直接对 0.0.0.0/0 开放。面板会常驻服务并占用内存,正确做法是安全组仅放行办公 IP,面板设置强密码和二次验证,修改默认 8888 端口,绑定域名并启用 HTTPS。1C1G 机器不建议装面板,面板、MySQL、PHP-FPM 同时运行容易内存不足。