1. Get an SSL Certificate
There are several ways to obtain an SSL certificate. For simplicity, let's use Let’s Encrypt, a free SSL certificate provider.
Option 1: Use Let’s Encrypt (via Certbot)
Install Certbot on your EC2 instance (assuming you’re using a Linux-based instance, e.g., Ubuntu).
Install Certbot:
Obtain and install the SSL certificate using Certbot: Run the following command, replacing
your-domain.com
with your actual domain name:Certbot will automatically configure the SSL certificate with your Apache or Nginx server and renew the certificate automatically.
If you're using Nginx, replace
--apache
with--nginx
.
Option 2: Use AWS ACM (for Elastic Load Balancer)
If you use AWS services like Elastic Load Balancer (ELB), you can request an SSL certificate through AWS Certificate Manager (ACM).
- Request a Certificate:
- Open the AWS Management Console.
- Go to AWS Certificate Manager.
- Click Request a certificate, choose Public Certificate, and follow the prompts.
- Validate the domain ownership using DNS or Email validation.
- After validation, you’ll get the SSL certificate and its details. You can then use it with an Elastic Load Balancer or CloudFront distribution.
2. Install the SSL Certificate on Your EC2 Instance
If you use a certificate from Let’s Encrypt, it is typically stored in the /etc/letsencrypt/live/your-domain.com/
directory.
To configure your web server:
For Apache:
Enable SSL module (if it’s not already enabled):
Edit the Apache SSL configuration file:
Update the
SSLCertificateFile
,SSLCertificateKeyFile
, andSSLCertificateChainFile
directives with the paths to the SSL certificate files from Let’s Encrypt:Enable the SSL site:
For Nginx:
Open the Nginx configuration file for your site:
Update the
ssl_certificate
andssl_certificate_key
with the correct paths:Reload Nginx:
3. Redirect HTTP to HTTPS
It’s a good practice to redirect all HTTP traffic to HTTPS to ensure secure communication.
For Apache:
Edit the Apache configuration file or
.htaccess
file:Add the following lines to redirect HTTP to HTTPS:
For Nginx:
- Add the following server block to your Nginx config to handle the redirection:
4. Test Your SSL Certificate
Once everything is set up, test your site using a browser or an SSL checker tool to confirm that the SSL certificate is working correctly.
Visit your site in a browser and ensure the padlock icon appears in the address bar (indicating HTTPS is active).
You can also use tools like SSL Labs' SSL Test to check the details and security of your SSL implementation.
5. Automate Certificate Renewal
Let’s Encrypt certificates need to be renewed every 90 days. Certbot can automate this process.
To set up automatic renewal for Certbot, add a cron job:
Add the following line to automatically renew the certificate:
This cron job will check for certificate expiration twice a day and renew it if necessary.
Post a Comment