PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 21, 2022

[FIXED] Where do I copy my VirtualHost directive in my Apache docker image?

 September 21, 2022     apache, django, docker, virtualhost     No comments   

Issue

I'm trying to Dockerize my Apache/Django project locally. On my local machine (running Mac Sierra) I have this file (maps.conf) in my /etc/apache2/other/ directory ...

<VirtualHost *:80>
    ServerName maps.example.com

    Alias /static /Library/WebServer/Documents/maps/maps/static
    <Directory /Library/WebServer/Documents/maps/maps/static>
        Require all granted
    </Directory>

    # Next, add the following directory block
    <Directory /Library/WebServer/Documents/maps/maps_project>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess maps python-home=/Library/WebServer/Documents/maps/venv python-path=/Library/WebServer/Documents/maps
    WSGIProcessGroup maps
    WSGIScriptAlias / /Library/WebServer/Documents/maps/maps_project/wsgi.py

</VirtualHost>

I found this Apache docker file ...

FROM httpd:2.4
COPY ./public-html/ /usr/local/apache2/htdocs/

Where am I supposed to copy my VirtualHost directive above so that my Apache docker image connects properly?


Solution

TL;DR

It's at /usr/local/apache2/conf/extra/httpd-vhosts.conf, but you'll need to do more than just copying yours there because a) Apache won't know about it; and b) your macOS paths won't work.

Long version

The Docker docs for the official Apache image show how to extract the main Apache config:

docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf

Now you have a my-httpd.conf locally, outside the container. Open it up and have a look - it is just a standard Apache config, and towards the bottom you'll see the usual:

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

So now we can see both how to include a vhost config, and where to put it.

First, uncomment that Include line in your local copy of my-httpd.conf so that the vhosts config will actually be included. We'll copy that edited version into the container, overwriting the original, so it will be used at runtime.

Now we need to update your existing vhosts config file (maps.conf) so it is suitable for use in the container. The macOS-specific paths you're currently using in that file won't exist inside the container, so the file won't work as-is.

You can see the main Docroot in the main my-httpd.conf file:

DocumentRoot "/usr/local/apache2/htdocs"

For simplicity let's put your content under that main Docroot, say in a dir called maps/. Copy your maps.conf to a new file (say my-vhosts.conf in the same dir as my-httpd.conf), updating your macOS /Library/WebServer/Documents/maps paths with the container path /usr/local/apache2/htdocs/maps. I can't replicate your app/config, but for the sake of demonstration I used this super simple my-vhosts.conf:

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache2/htdocs/maps"
</VirtualHost>

Next put it all together in the Dockerfile. It needs to copy in the 2 new config files, as well as your actual content. Note that you can't use a an absolute path for the source file in a COPY command, so you'll need to move or copy your local maps/ content into the current directory first.

FROM httpd:2.4
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./my-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
COPY ./maps /usr/local/apache2/htdocs/maps

Now just build it and run it. Again using the commands shown in the Docker docs for Apache:

docker build -t my-apache2 .
docker run -dit --name my-running-app -p 8080:80 my-apache2

Finally, for the sake of completeness, for local development where you might not want to keep recreating and running the image as you work on your app or tune your config files, you could map those things into the container as volumes, rather than just copy them in once at build-time. That way you can work and see your changes live in the container.

Easiest approach for that would be to use docker-compose, and a docker-compose.yml:

version: '3'

services:
    apache:
        image: httpd:2.4 
        container_name: apache
        ports: 
            - "8080:80"
        volumes:
            - ./my-httpd.conf:/usr/local/apache2/conf/httpd.conf
            - ./my-vhosts.conf:/usr/local/apache2/conf/extra/httpd-vhosts.conf
            - ./maps:/usr/local/apache2/htdocs/maps

Then docker-compose up to run it.



Answered By - Don't Panic
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing