installer add letsencrypt, ssl manager proper vhost for ssl, update-php-versionalso update ssl vhost if present

This commit is contained in:
Crivion
2025-10-21 13:55:58 +03:00
parent ced6389f32
commit 583c8f9135
3 changed files with 59 additions and 36 deletions

View File

@@ -133,6 +133,20 @@ echo "--------------------------------------------------------------------------
echo -e "\033[0m"
a2enmod headers
echo -e "\033[34m"
echo "--------------------------------------------------------------------------------"
echo "Enabling ssl apache module"
echo "--------------------------------------------------------------------------------"
echo -e "\033[0m"
a2enmod ssl
echo -e "\033[34m"
echo "--------------------------------------------------------------------------------"
echo "Installing certbot"
echo "--------------------------------------------------------------------------------"
echo -e "\033[0m"
apt -y install certbot python3-certbot-apache
echo -e "\033[34m"
echo "--------------------------------------------------------------------------------"

View File

@@ -99,44 +99,45 @@ generate_ssl_certificate() {
create_ssl_vhost() {
local domain=$1
local document_root=$2
print_status "Creating SSL-enabled virtual host for $domain..."
local vhost_file="$APACHE_SITES_PATH/$domain-ssl.conf"
cat > "$vhost_file" << EOF
<VirtualHost *:443>
ServerName $domain
DocumentRoot $document_root
SSLEngine on
SSLCertificateFile $SSL_CERTS_PATH/$domain/fullchain.pem
SSLCertificateKeyFile $SSL_CERTS_PATH/$domain/privkey.pem
# Include SSL configuration
Include /etc/apache2/conf-available/ssl-params.conf
# Security headers
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options DENY
Header always set X-XSS-Protection "1; mode=block"
# Logging
ErrorLog \${APACHE_LOG_DIR}/$domain-ssl_error.log
CustomLog \${APACHE_LOG_DIR}/$domain-ssl_access.log combined
</VirtualHost>
# Redirect HTTP to HTTPS
<VirtualHost *:80>
ServerName $domain
Redirect permanent / https://$domain/
</VirtualHost>
EOF
# Enable the site
print_status "Creating SSL-enabled virtual host for $domain..."
local non_ssl_vhost="$APACHE_SITES_PATH/$domain.conf"
local vhost_file="$APACHE_SITES_PATH/$domain-ssl.conf"
if [[ ! -f "$non_ssl_vhost" ]]; then
print_error "Non-SSL vhost file not found: $non_ssl_vhost"
return 1
fi
# Extract everything between <VirtualHost> and </VirtualHost>
local inner_content
inner_content=$(awk '
/<VirtualHost/{flag=1; next}
/<\/VirtualHost>/{flag=0}
flag
' "$non_ssl_vhost")
{
echo "<VirtualHost *:443>"
echo " ServerName $domain"
echo " SSLEngine on"
echo " SSLCertificateFile \$SSL_CERTS_PATH/$domain/fullchain.pem"
echo " SSLCertificateKeyFile \$SSL_CERTS_PATH/$domain/privkey.pem"
echo
echo "$inner_content" | sed 's/^/ /'
echo "</VirtualHost>"
echo
echo "# Redirect HTTP to HTTPS"
echo "<VirtualHost *:80>"
echo " ServerName $domain"
echo " Redirect permanent / https://$domain/"
echo "</VirtualHost>"
} > "$vhost_file"
# Enable the SSL site
a2ensite "$domain-ssl.conf"
# Test Apache configuration
if apache2ctl configtest; then
systemctl reload apache2
@@ -148,6 +149,7 @@ EOF
fi
}
# Function to remove SSL certificate
remove_ssl_certificate() {
local domain=$1

View File

@@ -16,6 +16,13 @@ VHOST_FILE=$(cat "$VHOST_FILE")
# replace {user} and {version} in template file
VHOST_FILE=$(echo "$VHOST_FILE" | sed "s#$CURRENT_PHP_VERSION#$NEW_PHP_VERSION#g")
# read ssl-vhost file
SSL_VHOST_FILE="/etc/apache2/sites-available/$DOMAIN-ssl.conf"
SSL_VHOST_FILE=$(cat "$SSL_VHOST_FILE")
# replace {user} and {version} in SSL template file
SSL_VHOST_FILE=$(echo "$SSL_VHOST_FILE" | sed "s#$CURRENT_PHP_VERSION#$NEW_PHP_VERSION#g")
# write template file to /etc/apache2/sites-available/{domain}.conf
echo "$VHOST_FILE" > "/etc/apache2/sites-available/$DOMAIN.conf"
echo "Currently on $CURRENT_PHP_VERSION"