温馨提示×

phpstorm在Ubuntu上如何配置SSL证书

小樊
56
2025-09-23 14:13:24
栏目: 云计算

PhpStorm本身无需直接配置SSL证书,因为它是本地开发的IDE,主要功能是编写和调试代码。SSL证书的配置通常针对运行PHP的Web服务器(如Apache、Nginx),以确保网站通过HTTPS提供服务。以下是在Ubuntu上为Web服务器配置SSL证书的详细步骤(以Let’s Encrypt免费证书为例):

一、准备工作

  1. 确保Ubuntu系统已更新:
    sudo apt update && sudo apt upgrade -y 
  2. 安装Certbot工具(用于自动化获取和部署Let’s Encrypt证书):
    • 若使用Apache:
      sudo apt install certbot python3-certbot-apache -y 
    • 若使用Nginx:
      sudo apt install certbot python3-certbot-nginx -y 

二、获取SSL证书

1. 自动配置(推荐)

Certbot可自动修改Web服务器配置文件(Apache/Nginx),无需手动编辑。

  • Apache
    sudo certbot --apache -d yourdomain.com -d www.yourdomain.com 
  • Nginx
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com 
    执行后会提示选择SSL配置选项(如强制HTTPS重定向),按需选择即可。

2. 手动配置(可选)

若需自定义配置,可手动获取证书并指定路径:

sudo certbot certonly --manual -d yourdomain.com -d www.yourdomain.com 

证书文件会保存至/etc/letsencrypt/live/yourdomain.com/目录,包含:

  • fullchain.pem(证书链,需配置给Web服务器)
  • privkey.pem(私钥,需严格保密)

三、配置Web服务器

1. Apache配置

Certbot自动修改Apache配置文件(位于/etc/apache2/sites-available/),需确保以下指令存在:

<VirtualHost *:443> ServerName yourdomain.com DocumentRoot /var/www/html SSLEngine on SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem # 可选:强制HTTP重定向至HTTPS RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </VirtualHost> 

保存后测试配置语法并重启Apache:

sudo apache2ctl configtest && sudo systemctl restart apache2 

2. Nginx配置

Certbot自动修改Nginx配置文件(位于/etc/nginx/sites-available/),需确保以下指令存在:

server { listen 443 ssl; server_name yourdomain.com www.yourdomain.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # 可选:强制HTTP重定向至HTTPS if ($scheme != "https") { return 301 https://$host$request_uri; } root /var/www/html; index index.php index.html; } 

保存后测试配置语法并重启Nginx:

sudo nginx -t && sudo systemctl restart nginx 

四、验证SSL配置

  1. 浏览器检查:访问https://yourdomain.com,确认地址栏显示锁形图标(无安全警告)。
  2. 命令行检查:使用OpenSSL验证证书链:
    sudo openssl s_client -connect yourdomain.com:443 -servername yourdomain.com | openssl x509 -noout -dates 
    输出应显示证书有效期(如notBeforenotAfter)。

五、设置自动续期

Let’s Encrypt证书有效期为90天,Certbot会自动创建定时任务(certbot.timer)续期。可手动测试续期流程:

sudo certbot renew --dry-run 

若输出显示“Congratulations, all renewals succeeded”,则续期正常。

六、PhpStorm相关配置(可选)

若需在PhpStorm中调试HTTPS请求(如使用Guzzle、cURL),需确保PHP的openssl扩展已启用:

  1. 编辑PHP配置文件(根据PHP版本调整路径,如/etc/php/8.1/apache2/php.ini):
    extension=openssl 
  2. 重启Web服务器使配置生效:
    sudo systemctl restart apache2 # 或nginx 

通过以上步骤,Ubuntu上的Web服务器将成功配置SSL证书,PhpStorm可通过HTTPS与服务器通信。若需进一步优化SSL性能(如启用OCSP Stapling),可参考Web服务器官方文档调整配置。

0