在Ubuntu上配置PHP的SSL证书,通常需要以下几个步骤:
首先,你需要一个Web服务器来托管你的PHP应用。这里以Nginx为例。
sudo apt update
sudo apt install nginx
你可以从Let’s Encrypt免费获取SSL证书。使用Certbot工具可以简化这个过程。
sudo apt install certbot python3-certbot-nginx
然后运行Certbot来获取并安装证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成证书的安装。Certbot会自动修改Nginx配置文件以启用HTTPS。
Certbot会自动创建一个Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com-le-ssl.conf
。确保这个文件中的配置如下:
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;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/html;
index index.php index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
}
location / {
try_files $uri $uri/ =404;
}
}
确保将yourdomain.com
替换为你的实际域名,并根据需要调整PHP-FPM的路径。
创建一个符号链接以启用SSL配置:
sudo ln -s /etc/nginx/sites-available/yourdomain.com-le-ssl.conf /etc/nginx/sites-enabled/
重启Nginx以应用更改:
sudo systemctl restart nginx
确保你的PHP配置文件(通常是/etc/php/7.4/fpm/php.ini
)中启用了必要的SSL选项。例如:
openssl.cafile=/etc/ssl/certs/ca-certificates.crt
打开浏览器并访问https://yourdomain.com
,你应该能够看到你的网站,并且浏览器地址栏会显示安全锁图标。
Certbot会自动设置一个cron任务来定期续期证书。你可以手动测试续期过程:
sudo certbot renew --dry-run
如果没有问题,证书将会自动续期。
通过以上步骤,你就可以在Ubuntu上成功配置PHP的SSL证书了。