首页主机资讯Ubuntu PHP配置与SSL证书安装

Ubuntu PHP配置与SSL证书安装

时间2025-11-25 18:29:04发布访客分类主机资讯浏览1104
导读:Ubuntu PHP 配置与 SSL 证书安装 一 准备与版本确认 更新系统并安装基础组件(以 Ubuntu 20.04/22.04 为例,按需选择 Apache 或 Nginx 与 PHP 7.4/8.x): 安装 Apache 或...

Ubuntu PHP 配置与 SSL 证书安装

一 准备与版本确认

  • 更新系统并安装基础组件(以 Ubuntu 20.04/22.04 为例,按需选择 ApacheNginxPHP 7.4/8.x):
    • 安装 Apache 或 Nginx
      • sudo apt update
      • sudo apt install apache2 # 或 sudo apt install nginx
    • 安装 PHP 与常用扩展(示例为 PHP 8.1,请按实际替换为你的版本)
      • sudo apt install php php-cli php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip
    • 验证版本
      • php -v
      • 查看 php.ini 路径:php --ini
  • 防火墙放行端口(如使用 UFW
    • sudo ufw allow 80,443/tcp
    • sudo ufw enable & & sudo ufw status
  • 将域名 A 记录指向服务器公网 IP,确保可通过 http://yourdomain.com 访问。

二 使用 Let’s Encrypt 获取并安装 SSL 证书

  • 安装 Certbot 及对应插件
    • Apache:sudo apt install certbot python3-certbot-apache
    • Nginx:sudo apt install certbot python3-certbot-nginx
  • 获取并自动配置证书(将 yourdomain.com 替换为你的域名)
    • Apache:sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
    • Nginx:sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • 证书自动续期测试
    • sudo certbot renew --dry-run
  • 说明
    • Let’s Encrypt 证书有效期 90 天;Certbot 通常会自动创建定时任务或 systemd 定时器进行续期。
    • 若使用云厂商 CDN/负载均衡,请在对应平台上传证书或在源站终止 TLS。

三 手动安装自有 SSL 证书

  • Apache 手动配置
    • 启用 SSL 模块与站点
      • sudo a2enmod ssl
      • sudo a2ensite default-ssl.conf # 或你的站点配置
    • 放置证书(示例路径)
      • sudo mkdir -p /etc/apache2/ssl
      • 将证书文件(如 server.crt / server.key / ca_bundle.crtfullchain.pem / privkey.pem)复制到 /etc/apache2/ssl
    • 编辑 /etc/apache2/sites-available/default-ssl.conf(或你的虚拟主机)
      • SSLEngine on
      • SSLCertificateFile /etc/apache2/ssl/fullchain.pem # 或 server.crt
      • SSLCertificateKeyFile /etc/apache2/ssl/privkey.pem # 或 server.key
      • SSLCertificateChainFile /etc/apache2/ssl/ca_bundle.crt # 若使用中间证书
    • 检查并重启
      • sudo apache2ctl configtest
      • sudo systemctl restart apache2
  • Nginx 手动配置
    • 放置证书(示例路径)
      • sudo mkdir -p /etc/nginx/ssl
      • fullchain.pemprivkey.pem 复制到 /etc/nginx/ssl
    • 在 server 配置中启用 SSL(示例)
      • listen 443 ssl;
      • server_name yourdomain.com www.yourdomain.com;
      • ssl_certificate /etc/nginx/ssl/fullchain.pem;
      • ssl_certificate_key /etc/nginx/ssl/privkey.pem;
      • include /etc/letsencrypt/options-ssl-nginx.conf; # 可选:使用 Certbot 提供的通用配置
      • ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # 可选:若已生成
    • 检查并重启
      • sudo nginx -t
      • sudo systemctl restart nginx
  • 注意
    • 证书路径、文件名与链文件请根据你的 CA 实际文件为准;若使用 SNI 多域名,请为每个域名配置对应的 server 块。

四 PHP 与安全配置要点

  • 找到并编辑正确的 php.ini(CLI 与 FPM/Apache 可能不同)
    • php --ini
    • 常见路径示例:/etc/php/8.1/apache2/php.ini 或 /etc/php/8.1/fpm/php.ini
  • 常用与安全相关设置(按应用需求调整)
    • allow_url_fopen = On(多数应用需要;仅在可信来源时使用)
    • allow_url_include = Off(更安全的默认)
    • expose_php = Off
    • cgi.fix_pathinfo = 0(防止路径解析漏洞)
    • 若应用发起 HTTPS 外部请求,建议设置 CA 证书
      • openssl.cafile = /etc/ssl/certs/ca-certificates.crt
  • 重启服务使配置生效
    • Apache:sudo systemctl restart apache2
    • PHP-FPM:sudo systemctl restart php8.1-fpm(按实际版本)
    • Nginx:sudo systemctl restart nginx
  • 可选优化
    • 启用 OPcache(生产环境强烈建议)
      • sudo apt install php-opcache
      • 在 php.ini 中:opcache.enable=1;可按需调整 opcache.memory_consumption、opcache.max_accelerated_files。

五 验证与常见问题

  • 访问与检测
    • 浏览器访问 https://yourdomain.com,确认地址栏显示锁标识
    • 使用 SSL Labs 在线检测站点评分与链配置
  • HTTP 到 HTTPS 重定向
    • Apache(在 80 端口虚拟主机中)
      • Redirect permanent / https://yourdomain.com/
    • Nginx
      • server { listen 80; server_name yourdomain.com; return 301 https://$host$request_uri; }
  • 常见问题排查
    • 证书不匹配或未生效:核对域名、证书路径与权限(证书文件应为 600644,私钥 600
    • 端口未放行:确认 80/443 已在防火墙与云安全组放行
    • 502/504 错误:检查 PHP-FPM 是否运行、监听套接字路径是否与 Nginx 配置一致
    • 混合内容:确保页面资源(CSS/JS/图片)均使用 https:// 或协议相对路径
    • 续期失败:检查域名解析、端口可达性、定时任务/定时器是否正常工作。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu PHP配置与SSL证书安装
本文地址: https://pptw.com/jishu/755803.html
Ubuntu PHP配置缓存设置 Ubuntu PHP配置与Web服务器集成

游客 回复需填写必要信息