To install mod_ssl in a Docker container, you need to ensure that the mod_ssl module is enabled for the Apache HTTP Server (httpd) within your Docker image. Here's a step-by-step guide to achieve this:
1. Choose a Base Image
First, choose a base image that includes Apache. Common choices are httpd, bitnami/httpd, or any other Apache-based image. For this example, we'll use the official httpd image.
2. Create a Dockerfile
Create a Dockerfile in your project directory. Here’s an example Dockerfile that installs and enables mod_ssl:
# Use the official httpd image from Docker Hub
FROM httpd:2.4
# Install mod_ssl
RUN apt-get update && \
apt-get install -y libssl-dev && \
a2enmod ssl
# Copy the SSL configuration file to the appropriate directory
COPY ssl.conf /usr/local/apache2/conf.d/
# Copy your SSL certificate and key
COPY server.crt /usr/local/apache2/conf/
COPY server.key /usr/local/apache2/conf/
# Expose port 443 for HTTPS
EXPOSE 443
# Default command to run Apache in the foreground
CMD ["httpd", "-D", "FOREGROUND"]
3. Create SSL Configuration File
Create an ssl.conf file in your project directory. This file will configure Apache to use SSL. Here’s a basic example:
Listen 443 https
<VirtualHost _default_:443>
DocumentRoot "/usr/local/apache2/htdocs"
ServerName www.example.com:443
SSLEngine on
# Paths to your SSL certificate and key
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/usr/local/apache2/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
# Other directives as needed
ErrorLog "/proc/self/fd/2"
CustomLog "/proc/self/fd/1" common
</VirtualHost>
4. Prepare SSL Certificate and Key
Place your server.crt (SSL certificate) and server.key (private key) in your project directory. If you don't have an SSL certificate, you can generate a self-signed certificate for testing purposes using OpenSSL:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
When prompted, fill in the required information. For testing, you can use localhost as the Common Name (CN).
5. Build the Docker Image
Navigate to your project directory and build the Docker image:
docker build -t my-apache-ssl .
6. Run the Docker Container
Run the Docker container, mapping port 443 on your host to port 443 in the container:
docker run -d -p 443:443 --name my-apache-ssl my-apache-ssl
7. Verify the Installation
Open your web browser and navigate to https://localhost (or your server's IP address). You should see your website served over HTTPS. If you used a self-signed certificate you will get a warning about the certificate not being trusted you can proceed for testing purposes.
Using a Different Base Image
If you are using a different base image, such as bitnami/httpd, the steps might vary slightly. For bitnami/httpd, mod_ssl is usually already included, and you just need to configure it. Here’s an example Dockerfile for bitnami/httpd:
# Use the bitnami/httpd image from Docker Hub
FROM bitnami/httpd:2
# Copy the SSL configuration file to the appropriate directory
COPY ssl.conf /opt/bitnami/apache/conf/vhosts/
# Copy your SSL certificate and key
COPY server.crt /opt/bitnami/apache/conf/
COPY server.key /opt/bitnami/apache/conf/
# Expose port 443 for HTTPS
EXPOSE 443
And your ssl.conf would be placed in /opt/bitnami/apache/conf/vhosts/
Conclusion
By following these steps you can successfully install and enable mod_ssl in your Docker container, allowing you to serve content over HTTPS.