在CentOS上配置LNMP(Linux, Nginx, MySQL, PHP)以实现SSL加密,可以按照以下步骤进行:
Certbot是一个用于获取和部署Let’s Encrypt SSL证书的工具。你可以使用以下命令安装它:
sudo yum install epel-release
sudo yum install certbot python2-certbot-nginx
使用Certbot获取SSL证书。假设你的域名是example.com
,你可以运行以下命令:
sudo certbot --nginx -d example.com -d www.example.com
Certbot会自动配置Nginx以使用SSL,并将证书文件放在适当的位置。
Certbot会自动修改你的Nginx配置文件,通常位于/etc/nginx/conf.d/example.com.conf
。你可以手动检查并确保配置如下:
server {
listen 80;
server_name example.com www.example.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.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$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ =404;
}
}
保存配置文件后,重启Nginx以应用更改:
sudo systemctl restart nginx
你可以使用以下命令测试你的SSL配置是否正确:
sudo openssl s_client -connect example.com:443 -servername example.com
如果一切正常,你应该会看到SSL握手成功的信息。
Let’s Encrypt的证书有效期为90天,因此你需要设置自动续期。Certbot会自动为你设置一个cron任务来处理续期。你可以手动测试续期过程:
sudo certbot renew --dry-run
如果没有错误信息,说明续期配置正确。
通过以上步骤,你就可以在CentOS上成功配置LNMP以实现SSL加密。