Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Last active May 5, 2024 06:33
Show Gist options
  • Select an option

  • Save devinci-it/068e77aee450d6308acd36b3d28ea3d1 to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/068e77aee450d6308acd36b3d28ea3d1 to your computer and use it in GitHub Desktop.
This is a template for Apache virtual host configuration with HTTPS redirection and self-signed certificate.
#!/bin/bash
# deploy_web_app Function:
# Copies web application files to the Apache directory and ensures appropriate permissions.
# Parameters: None
deploy_web_app() {
# Copy web application files to Apache directory
sudo cp -r /path/to/your/web/app /var/www/html/
# Ensure appropriate permissions
sudo chown -R www-data:www-data /var/www/html/
}
# generate_self_signed_ssl Function:
# Generates a self-signed SSL certificate.
# Parameters: None
generate_self_signed_ssl() {
# Generate private key
sudo openssl genrsa -out /etc/ssl/private/phprookie.key 2048
# Generate certificate signing request (CSR)
sudo openssl req -new -key /etc/ssl/private/phprookie.key -out /tmp/phprookie.csr
# Generate self-signed certificate
sudo openssl x509 -req -days 365 -in /tmp/phprookie.csr -signkey /etc/ssl/private/phprookie.key -out /etc/ssl/certs/phprookie.crt
}
# configure_apache_ssl_certbot Function:
# Configures Apache for SSL using Let's Encrypt certificate.
# Parameters: None
configure_apache_ssl_certbot() {
# Install Certbot
sudo apt-get update
sudo apt-get install certbot python3-certbot-apache -y
# Obtain Let's Encrypt certificate
sudo certbot --apache -d yourdomain.com
# Certbot sets up auto-renewal automatically
}
# fill_placeholders Function:
# Prompts the user to fill in placeholders interactively.
# Parameters: None
fill_placeholders() {
# Prompt user for server name
read -p "Enter server name: " server_name
# Prompt user for server alias
read -p "Enter server alias (optional, press Enter to skip): " server_alias
# Prompt user for SSL certificate file path
read -p "Enter path to SSL certificate file: " ssl_certificate_file
# Prompt user for SSL certificate key file path
read -p "Enter path to SSL certificate key file: " ssl_certificate_key_file
}
# generate_apache_config Function:
# Generates Apache virtual host configuration.
# Parameters: None
generate_apache_config() {
# Fill in placeholders in the template
sed -e "s|{{ server_name }}|${server_name}|g" \
-e "s|{{ server_alias }}|${server_alias}|g" \
-e "s|{{ ssl_certificate_file }}|${ssl_certificate_file}|g" \
-e "s|{{ ssl_certificate_key_file }}|${ssl_certificate_key_file}|g" \
template.conf > apache_config.conf
}
# Summary:
# This is a template for Apache virtual host configuration with HTTPS redirection and self-signed certificate.
#
# Placeholders:
# - {{ server_name }}: Placeholder for the server name.
# - {{ server_alias }}: Placeholder for the server alias.
# - {{ ssl_certificate_file }}: Placeholder for the path to the SSL certificate file.
# - {{ ssl_certificate_key_file }}: Placeholder for the path to the SSL certificate key file.
#
# Self-signed Certificate Creation:
# Use the following command to create a self-signed certificate and private key:
# sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
# Adjust the paths as needed.
#
# Permissions:
# After creating the certificate and key files, set the permissions as follows:
# - Set the permissions of the certificate file to 644 (-rw-r--r--).
# - Set the permissions of the private key file to 600 (-rw-------).
# This ensures that the certificate file is readable by everyone and the private key file is only readable by the owner.
#
# Deployment:
# Replace {{ ssl_certificate_file }} and {{ ssl_certificate_key_file }} in the template with the actual paths to these files,
# and deploy the configuration to your Apache server.
# Template for Apache virtual host configuration with HTTPS redirection and self-signed certificate
<VirtualHost *:80>
# ServerName: The primary domain name of the server
ServerName {{ server_name }}
# ServerAlias: Additional domain names that the server responds to
ServerAlias {{ server_alias }}
# Redirect all HTTP traffic to HTTPS
Redirect permanent / https://{{ server_name }}/
# ErrorLog: Location of the error log file for this virtual host
ErrorLog ${APACHE_LOG_DIR}/error.log
# CustomLog: Location of the access log file for this virtual host
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
# ServerName: The primary domain name of the server
ServerName {{ server_name }}
# ServerAlias: Additional domain names that the server responds to
ServerAlias {{ server_alias }}
# DocumentRoot: The directory containing the website's files
DocumentRoot /var/www/html
# Enable SSL/TLS
SSLEngine on
# SSLCertificateFile: Path to the SSL certificate file
SSLCertificateFile {{ ssl_certificate_file }}
# SSLCertificateKeyFile: Path to the SSL certificate key file
SSLCertificateKeyFile {{ ssl_certificate_key_file }}
# Additional SSL configurations like SSLProtocol, SSLCipherSuite, etc. can be added here if needed
# ErrorLog: Location of the error log file for this virtual host
ErrorLog ${APACHE_LOG_DIR}/error.log
# CustomLog: Location of the access log file for this virtual host
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment