在Ubuntu上配置Nginx以使用SSL證書,可以按照以下步驟進行操作。這里假設你已經有一個SSL證書文件(例如your_certificate.crt
)和私鑰文件(例如your_private.key
)。如果你還沒有這些文件,你可以通過Let’s Encrypt免費獲取。
如果你還沒有安裝Nginx,可以使用以下命令進行安裝:
sudo apt update
sudo apt install nginx
如果你還沒有SSL證書,可以使用Let’s Encrypt免費獲取。以下是使用Certbot獲取證書的示例:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成證書的獲取和配置。
如果你已經有了SSL證書文件,可以手動配置Nginx。編輯Nginx的配置文件,通常位于/etc/nginx/sites-available/
目錄下。你可以創建一個新的配置文件或編輯默認的配置文件。
sudo nano /etc/nginx/sites-available/yourdomain.com
在配置文件中添加以下內容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /path/to/your_certificate.crt;
ssl_certificate_key /path/to/your_private.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
root /var/www/yourdomain.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根據你的PHP版本調整
}
location ~ /\.ht {
deny all;
}
}
保存并關閉文件后,啟用該配置文件:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
測試Nginx配置是否正確:
sudo nginx -t
如果沒有錯誤,重啟Nginx以應用更改:
sudo systemctl restart nginx
打開瀏覽器并訪問https://yourdomain.com
,你應該能夠看到你的網站,并且瀏覽器地址欄會顯示安全鎖標志。
通過以上步驟,你就可以在Ubuntu上成功配置Nginx以使用SSL證書了。