在Linux下开启SSL的方法有很多,这里我们主要介绍两种常见的方法:使用Apache或Nginx服务器以及使用Let’s Encrypt免费证书,下面我们将详细介绍这两种方法的步骤和注意事项。
方法一:使用Apache或Nginx服务器
1. 安装Apache或Nginx服务器
在Linux下安装Apache或Nginx服务器的方法有很多,这里以Ubuntu系统为例,使用以下命令安装:
sudo apt-get update sudo apt-get install apache2
或者
sudo apt-get update sudo apt-get install nginx
2. 配置Apache或Nginx服务器支持SSL
对于Apache服务器,我们需要编辑其配置文件(通常位于/etc/apache2/sites-available/000-default.conf
),在<VirtualHost *:443>
标签内添加以下内容:
SSLEngine on SSLCertificateFile /path/to/your/certificate.crt SSLCertificateKeyFile /path/to/your/privatekey.key
对于Nginx服务器,我们需要编辑其配置文件(通常位于/etc/nginx/sites-available/default
),在server { listen 443; }
块内添加以下内容:
ssl_certificate /path/to/your/certificate.crt; ssl_certificate_key /path/to/your/privatekey.key;
然后重启Apache或Nginx服务器以使配置生效:
对于Apache服务器:
sudo service apache2 restart
对于Nginx服务器:
sudo service nginx restart
3. 测试SSL是否生效
打开浏览器,访问你的网站(假设域名为example.com
,IP地址为192.168.1.100
),在地址栏中查看是否有绿色的锁标志,如果有,说明SSL已经生效。
方法二:使用Let’s Encrypt免费证书
1. 安装Certbot工具
Certbot是一个用于自动获取Let’s Encrypt免费证书的工具,首先需要安装它:
sudo apt-get update sudo apt-get install certbot python3-certbot-apache 对于Apache服务器 sudo apt-get install certbot python3-certbot-nginx 对于Nginx服务器
2. 申请Let’s Encrypt证书并自动配置Apache或Nginx服务器(以Apache为例)
运行以下命令申请证书:
sudo certbot --apache --agree-tos --redirect --renew-by-default --email your@email.com --webroot -w /var/www/html example.com 将example.com替换为你的域名,将your@email.com替换为你的邮箱地址,将/var/www/html替换为你的实际网站根目录路径
按照提示操作,完成证书申请过程,Certbot会自动配置Apache服务器的虚拟主机,使其支持SSL,你可以通过访问https://example.com
来查看证书是否生效,如果一切正常,你会看到一个类似于以下的输出:
Your certificate has been installed successfully! Your domain name (example.com) has been validated by Let's Encrypt. This means that it is now secure and trusted by all major browsers! The SSL certificate was installed successfully at 2022-01-01T00:00:00Z. Your certificate will expire on 2023-01-01T00:00:00Z. To renew or redeploy your certificate later using this command, please run again with the--renewal-hook
option followed by a path to the renewal hook script which should be located in the/etc/letsencrypt/renewal-hooks/$LE_DOMAIN_ENVIRONMENT directory. For more information about renewal hooks, see https://letsencrypt.org/docs/getting-started-with-the-apache-renewer/automatically-renewing-your-certificates. If you want to keep your existing certificate after this command completes, simply run
exit`. If you need help or have questions, please visit https://letsencrypt.org or email us at support@letsencrypt.org. Thank you! -The ACME Server (support@acme.sh) (administrator) [id=e5a9fbb7-c7b8-4f5d-8e7e-6e5fbc3b7d5a] (using fqdn) -Please consider supporting the ACME Project by signing up for an account at https://acme.sh if you plan to reuse this configuration in the future. Thank you!"
至此,你已经成功地在Linux下启用了SSL,现在你可以放心地将你的网站部署到这个安全的环境中。
本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/472059.html
如有侵犯您的合法权益请发邮件951076433@qq.com联系删除