-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_ssl.sh
44 lines (37 loc) · 1.44 KB
/
install_ssl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# Check if the script is running as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
# Request website domain
read -p "Enter your website domain (if not, a certificate will not be obtained): " domain
# Check if Certbot is installed
if [ -f /usr/bin/letsencrypt ]; then
echo "Letsencrypt is installed"
else
echo "Letsencrypt is not installed"
echo "Installing Letsencrypt..."
apt update
apt install certbot python3-certbot-nginx -y
fi
# Paths to configuration files
template_conf="config/nginx_config.conf"
nginx_conf="/etc/nginx/sites-available/$domain.conf"
# Check if the Nginx configuration for the domain exists
if [ ! -f "$nginx_conf" ]; then
echo "Creating Nginx configuration for $domain..."
# Copy the template configuration file to the Nginx sites-available directory
cp "$template_conf" "$nginx_conf"
# Replace placeholders with the actual domain name
sed -i "s/{{user_domain}}/$domain/g" "$nginx_conf"
# Create a symbolic link in the sites-enabled directory
ln -s "$nginx_conf" /etc/nginx/sites-enabled/
# Test Nginx configuration and reload if successful
nginx -t && systemctl reload nginx
fi
echo 'Creating SSL certificate...'
# Obtain an SSL certificate for the domain and www.domain
certbot --nginx -d $domain
# add certificate renewal to crontab (every day at midnight)
(crontab -l 2>/dev/null; echo "0 0 * * * /usr/bin/certbot renew --quiet") | crontab -