Files
notes/tiddlywiki/httpd Apache with Docker.md

236 lines
6.9 KiB
Markdown
Raw Normal View History

2026-03-12 22:01:38 +01:00
Based on [this article](https://www.middlewareinventory.com/blog/docker-reverse-proxy-example/)
Download HTTPD docker image
---------------------------
Download last httpd image from [Docker Hub](https://hub.docker.com)
```
docker pull httpd
```
To list installed images
```
docker images
```
Costomize image
---------------
Create the directory structure for Apache HTTPD docker application
```
mkdir -p /app/appsdocker/httpd
cd /app/appsdocker/httpd
mkdir vhosts wwwroot logs
```
In order to browse the image and get the `httpd.conf` file, create an auto-remove container in interactive mode and map local `/app/appsdocker/httpd/` diredctory to container `/usr/local/apache2/htdocs/` directory
```
docker run -it --rm -v /app/appsdocker/httpd/:/usr/local/apache2/htdocs/ httpd:latest bash
```
In interactiv shell, copy `httpd.conf` file to `/usr/local/apache2/htdocs` -- this one is pointing to local `/app/appsdocker/httpd/tmp`
```
root@937797441b4b:/usr/local/apache2# cp /usr/local/apache2/conf/httpd.conf /usr/local/apache2/htdocs/
```
Update `httpd.conf`
```
Listen 80
Listen 443
IncludeOptional conf/vhosts/*.conf
LoadModule ssl_module modules/mod_ssl.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule xml2enc_module modules/mod_xml2enc.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
LoadModule proxy_html_module modules/mod_proxy_html.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
```
Create `Dockerfile` under `/app/appsdocker/httpd`
```
# The Base Image used to create this Image
FROM httpd:latest
# Just my name who wrote this file
MAINTAINER Valeriu PLESNILA
# to Copy a file named httpd.conf from present working directory to the /usr/local/apache2/conf inside the container
# I have taken the Standard httpd.conf file and enabled the necassary modules and adding Support for an additional Directory
COPY httpd.conf /usr/local/apache2/conf/httpd.conf
# This is the Additional Directory where we are going to keep our Virtualhost configuraiton files
# You can use the image to create N number of different virtual hosts
RUN mkdir -p /usr/local/apache2/conf/vhosts/
RUN mkdir -p /usr/local/apache2/wwwroot/
# To tell docker to expose this port
EXPOSE 80
EXPOSE 443
# The Base command, This command should be used to start the container
# Remember, A Container is a Process.As long as the base process (started by base cmd) is live the Container will be ALIVE.
CMD ["httpd", "-D", "FOREGROUND"]
```
A simple site
--------------
Create a simple VirtualHost configuration file `/app/appsdocker/httpd/vhosts/gitlab.conf` for the site `gitlab.databasepro.eu`
```
<VirtualHost *:80>
ServerName gitlab.databasepro.eu
ServerAdmin admin@gitlab.databasepro.eu
DocumentRoot /usr/local/apache2/wwwroot/gitlab
<Directory "/usr/local/apache2/wwwroot/gitlab">
Order allow,deny
AllowOverride All
Allow from all
Require all granted
</Directory>
ErrorLog logs/gitlab-error.log
CustomLog logs/gitlab-access.log combined
</VirtualHost>
```
Create a default homepage
```
mkdir /app/appsdocker/httpd/wwwroot/gitlab
echo "Hello, you are on gitlab.databasepro.eu" > /app/appsdocker/httpd/wwwroot/gitlab/index.html
```
Build the image
```
cd /app/appsdocker/httpd
docker build -t my_httpd_image .
```
Create and run the container:
* mapping container `80` port to local `8080` port
* mapping container `443` port to local `8443` port
* mounting container `/usr/local/apache2/conf/vhosts` to local `/app/appsdocker/httpd/vhosts`
* mounting container `/usr/local/apache2/wwwroot` to local `/app/appsdocker/httpd/wwwroot`
* mounting container `/usr/local/apache2/logs` to local `/app/appsdocker/httpd/vhosts`
```
docker container run \
--publish 8080:80 \
--publish 8443:443 \
-d --name my_httpd_server \
-v /app/appsdocker/httpd/vhosts:/usr/local/apache2/conf/vhosts \
-v /app/appsdocker/httpd/wwwroot:/usr/local/apache2/wwwroot \
-v /app/appsdocker/httpd/logs:/usr/local/apache2/logs \
my_httpd_image
```
> In my example I used NAT port mzpping from my Livebox as:
* external port 80 mapped to internal myvm:8080
* external port 443 mapped to internal myvm:8443
>
Add SSL
-------
We will use `certboot` client from [Let's encrypt](https://letsencrypt.org)
```
dnf install -y certbot.noarch
certbot certonly --webroot --webroot-path /app/appsdocker/httpd/wwwroot/gitlab -d gitlab.databasepro.eu
```
Certificate and chain will be saved in `/etc/letsencrypt/`
Destroy container and builded image in order to recreate them for SSL.
```
-- list all container
docker ps -a
-- stop a container
docker stop <container_id/container_name>
-- start a container
docker start <container_id/container_name>
-- restart a container
docker restart <container_id/container_name>
-- remove a container
docker rm <container_id/container_name>
-- logs for a container
docker logs <container_id/container_name>
-- list images
docker images
-- to delete an image
docker rmi <image_id/image_name>
```
Update VirtualHost configuration file `/app/appsdocker/httpd/vhosts/gitlab.conf` for the site `gitlab.databasepro.eu`
```
<VirtualHost *:80>
ServerName gitlab.databasepro.eu
ServerAdmin admin@gitlab.databasepro.eu
DocumentRoot /usr/local/apache2/wwwroot/gitlab
<Directory "/usr/local/apache2/wwwroot/gitlab">
Order allow,deny
AllowOverride All
Allow from all
Require all granted
</Directory>
ErrorLog logs/gitlab-error.log
CustomLog logs/gitlab-access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName gitlab.databasepro.eu
ServerAdmin admin@gitlab.databasepro.eu
DocumentRoot /usr/local/apache2/wwwroot/gitlab
<Directory "/usr/local/apache2/wwwroot/gitlab">
Order allow,deny
AllowOverride All
Allow from all
Require all granted
</Directory>
SSLEngine On
ErrorLog logs/gitlab-error.log
CustomLog logs/gitlab-access.log combined
SSLCertificateFile "/etc/letsencrypt/live/gitlab.databasepro.eu/fullchain.pem"
SSLCertificateKeyFile "/etc/letsencrypt/live/gitlab.databasepro.eu/privkey.pem"
</VirtualHost>
```
Recreate a container mapping also `/etc/letsencrypt`
```
docker container run \
--publish 8080:80 \
--publish 8443:443 \
-d --name my_httpd_server \
-v /etc/letsencrypt:/etc/letsencrypt \
-v /app/appsdocker/httpd/vhosts:/usr/local/apache2/conf/vhosts \
-v /app/appsdocker/httpd/wwwroot:/usr/local/apache2/wwwroot \
-v /app/appsdocker/httpd/logs:/usr/local/apache2/logs \
my_httpd_image
```
Optionally using docker-compose
-------------------------------
`docker-compose.yaml` file:
```
my_httpd_server:
image: my_httpd_image
restart: always
ports:
- 8080:80
- 8443:443
volumes:
- /etc/letsencrypt:/etc/letsencrypt
- /app/appsdocker/httpd/vhosts:/usr/local/apache2/conf/vhosts
- /app/appsdocker/httpd/wwwroot:/usr/local/apache2/wwwroot
- /app/appsdocker/httpd/logs:/usr/local/apache2/logs
```