debian

Debian Apache SSL证书配置

小樊
39
2025-10-13 13:49:32
栏目: 云计算

Debian Apache SSL Certificate Configuration

Configuring SSL/TLS for Apache on Debian involves installing necessary packages, obtaining a certificate (self-signed or from a trusted CA), and adjusting Apache configurations. Below is a structured guide covering both self-signed (for testing) and trusted CA certificates (for production).

1. Install Required Packages

First, ensure Apache and SSL tools are installed on your Debian system:

sudo apt update
sudo apt install apache2 openssl

This installs Apache2 (web server) and OpenSSL (toolkit for SSL/TLS certificates).

2. Obtain an SSL Certificate

You can either generate a self-signed certificate (for testing/internal use) or obtain one from a trusted Certificate Authority (CA) like Let’s Encrypt (for production).

Option A: Generate a Self-Signed Certificate

Use OpenSSL 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

Option B: Obtain a Trusted CA Certificate (Recommended for Production)

For public-facing sites, use Let’s Encrypt (free) via Certbot:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

3. Configure Apache for SSL

For Self-Signed Certificates

Edit the default SSL site configuration:

sudo nano /etc/apache2/sites-available/default-ssl.conf

Modify the following directives to point to your certificate/key files:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
        SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
        <Directory /var/www/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
</IfModule>

For Trusted CA Certificates (Certbot)

Certbot automatically creates a configuration file (e.g., /etc/apache2/sites-available/yourdomain-le-ssl.conf) with the correct paths. Verify the file includes:

<VirtualHost *:443>
    ServerName yourdomain.com
    DocumentRoot /var/www/yourdomain
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
    # Optional: Add security headers
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
</VirtualHost>

4. Enable SSL and Restart Apache

Enable the SSL module and the SSL site configuration:

sudo a2enmod ssl  # Enable SSL module (if not already enabled)
sudo a2ensite default-ssl  # Enable the default SSL site (or your custom config)
sudo systemctl restart apache2  # Apply changes

For Certbot configurations, enabling the site is handled automatically during certificate issuance.

5. Optional: Force HTTP to HTTPS Redirection

To ensure all traffic uses HTTPS, edit the default HTTP site configuration:

sudo nano /etc/apache2/sites-available/000-default.conf

Add a Redirect directive inside the <VirtualHost *:80> block:

<VirtualHost *:80>
    ServerName yourdomain.com
    Redirect permanent / https://yourdomain.com/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Restart Apache to apply the redirection:

sudo systemctl restart apache2

6. Verify the Configuration

Check Apache Syntax

Before restarting Apache, validate the configuration for errors:

sudo apache2ctl configtest

Test SSL Connection

7. Maintain Certificates (Trusted CA Only)

For Let’s Encrypt certificates, Certbot automatically sets up a cron job to renew certificates every 60 days. To manually test renewal:

sudo certbot renew --dry-run

By following these steps, you can secure your Apache web server on Debian with SSL/TLS—either for testing (self-signed) or production (trusted CA). Always prefer trusted CA certificates for public-facing sites to avoid browser warnings.

0
看了该问题的人还看了