Php: Custom docroot

Created on 23 Jun 2016  路  23Comments  路  Source: docker-library/php

I think it'd be handy to have a custom workdir and docroot since for instance a project I'm working on has a path /app and a docroot path of /app/www - often it's "public" or "public_html" as well. Perhaps this can be set using an env?

Most helpful comment

The following simple Dockerfile works to set DocumentRoot via environment variable:

FROM php:7.1-apache

ENV APACHE_DOCUMENT_ROOT /var/www/html

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

All 23 comments

I'd recommend installing a custom configuration file either in place of the supplied /etc/apache2/apache2.conf, or as an add-on in one of the "included" directories:

root@f423f304324a:/etc/apache2# grep Include apache2.conf
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

Something like:

FROM php:7.0-apache
COPY some-vhost.conf /etc/apache2/sites-enabled/

(combined with either docker-compose.yml's build:, automated builds/repository links to keep the image up to date on Docker Hub, or a Jenkins/CD pipeline of some kind)

(another semi-simple option depending on your needs would be something like RUN sed -i 's!/var/www/html!/app/www!g' /etc/apache2/apache2.conf)

I realise I could do these things with an extension of the docker image. I was just wondering if an env could be introduce to save time if that's all one wants to do, for a quick short-term thing.

Like:
docker run -e APACHE_DOCROOT=/app -v $PWD:/app -w /app --rm -it php:apache

I'm in agreement with @dandart - having a way to define the directory with an environment variable makes a lot of sense.

I'm also looking at how most PHP projects and frameworks have a single index.php in the web root and the remaining files above the web root. Ideally it would be great to have a way to easily configure this type of behavior, unless I am missing it?

I am -1 on the idea of an environment variable to set the DocumentRoot. Once we add some Apache or PHP config generation we will end up adding even more "common" config and generate more and more of the config and I'd rather not complicate the image and its maintenance with an Apache config generator.

So now we are back to zero... ;)

However, that is a very valid point and one that argues its not worth considering not implementing very well... Still on the fence. My only downside is now when we need a little bit of customization we can't use off the shelf image and we will continue to see the vendor-apache plethora of slightly one off images...

Can someone explain in simple terms what should be done in order to have a documentroot that is in a subfolder (web) of the web app (like it's the case for silex web apps but certainly other frameworks as well)?
Is there any simple command to run afterward?
Is that just impossible ? Is the only solution to rely on a manual change?
Sounds weird to me that it's not possible to quickly run a web site with a framework (using the subfolder documentroot approach) without relying on some tricky stuff.

Because I'm not a PHP guy basically, I went and checked the folder/directory structure of some popular frameworks: Laravel and Symfony.
And guess what, they are also using a subfolder as the document root.
So, unless I'm missing something, this image can't be used as is with 90% of PHP apps around.
If the maintainers are insisting on not providing a environment variable for documentroot _(which I do not really agree considering setting documentroot seems necessary for most of the php framework out there)_ at least provide some unambiguous approach to make the change starting from your image.
Maybe a ready to use dockerfile?

Does a ready to use Dockerfile make sense? I think a lot of projects are shipping with a docker-compose.yml file to specify their containers. Also, a Dockerfile is more appropriate for a single container - most PHP applications require a database as well.

Hoping there is a solid recommendation for working around this soon, I would prefer not to have to extend their image just to hack it up a little bit for what seems to be a minor modification...

@yosifkit Can you comment on this ? Am I missing something or is this image useless for 90% of the apps based on most successful PHP framework? It may seem a bold statement but I would be pleased to be proved wrong.
What's your recommendation?
Thanks in advance.

For symfony/standard-edition all you need to do is mv web html and change the value extra.symfony-web-dir in composer.json to html.

@hairmare euh ... where should I do this? In the container? Anyway to kind of automate this? How does it fit in the container based dev workflow?
Besides that as far as I can see, you are renaming the web folder to html ... how does it go with the fact that the documentroot is expected to be in a sub-folder of the web app. Maybe I just don't get you explanations: I'm not into PHP stuff...

Hi,

I use Symfony on a Docker container, and I still experience this problem.
Could you manage to eventually use a custom path?

My Dockerfile was based on a php:5.4-apache, and the documentroot in my apache2.conf was working well. Now, I have upgraded to php:5.6.30-apache and I need to link to "http://myurl/web/app_dev.php" in my web browser to have it work, whatever documentroot I put in my cutom apache2.conf.

Thanks,
Nicolas

@nicolaskern
Don't waste your time. For some reason, they are not interested in providing a clear answer on this matter.
90% of the framework have the same approach with a specific document root but that's not their problem. Not even to provide a clearly documented workaround based on their image.
Frustrating, I have to admit, but that's how it is.

Hm interesting. Did you manage to find a workaround? Or are you just putting /web in your urls?

Basically I'm using this image now https://hub.docker.com/r/ruslangetmansky/docker-apache-php/
It provides an APACHE_DOC_ROOT env variable.
Obviously it would certainly be better for maintenance purposes to use the official image but I'm not really into apache/php and the more I was digging into this problem for a workaround the more different/ackward/unclear solution I could find.
I said to myself, that if nobody could really come up with a clear and simple workaround/image extension or whatever, there was certainly a reason as surprising as it could be (surprising in the sense of how can this be so difficult to get there starting from the official image when 90% of the PHP framework seem to have the same approach???). And maybe the guy came up with a complex build for a reason as well ...

Reading the code of the image you provided, it looks like it's just an env variable used to set a DocumentRoot for an apache from ubuntu trusty.
So the solution would be to build another Dockerfile using php with a debian:jessie and see if the problem comes from debian or the apache version used : https://github.com/docker-library/php/blob/master/5.6/apache/Dockerfile

I found a working solution forcing the path in the virtualhost, which I wasn't overriding until now.

The Ideal solution would be to have both a sample dockerfile and docker-compose file that shows how to get a specific documentroot starting from the official php image.
I can't see why this is so difficult and why each time somebody comes up with a different solution? But maybe that's me.

The following simple Dockerfile works to set DocumentRoot via environment variable:

FROM php:7.1-apache

ENV APACHE_DOCUMENT_ROOT /var/www/html

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

@FredericLatour I'm sure the contributors would gladly accept a PR that added additional documentation on how to setup a Dockerfile like @tianon demonstrated to the README.

UPDATE: I decided to just make the PR...

Apache config supports loading env variables, so we do not need to use generating, just rewrite apache.conf to this:

<VirtualHost *:80>
    DocumentRoot ${DOCROOT}
</VirtualHost>

and Dockerfile to have default falue for backward compatibility.

It is tested in this base image.

@yosifkit Will be pull request with this accepted?

The APACHE_DOCUMENT_ROOT it's available for the php:5.4.45-apache ...???

Was this page helpful?
0 / 5 - 0 ratings