Running virtual hosts in apache docker container

34,384

Solution 1

Thanks to @David Maze I found the problem I added the listen directives to the top of my apache configuration and changed the port numbers.

docker-compose.yml

version: '3.2'
services:
  php-apache:
    build:
      context: ./apache-php
    ports:
      - 80:80
      - 8060:8060

    volumes:
      - ./DocumentRoot:/var/www/html:z

Apache config

Listen 80
Listen 8060

<VirtualHost *:8060>
    DocumentRoot /var/www/html/api
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Directory structure

apache-php
   ¬ sqlite3_ext
   ¬ 000-default.conf (Apache config)
   ¬ Dockerfile
   ¬ php.ini
docker-compose.yml

Dockerfile

FROM php:7.2.1-apache
RUN docker-php-ext-install pdo pdo_mysql mysqli

# Enable apache rewrite
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf

#Install spatialite and create symlink for libproj.so.0
COPY sqlite3_ext /etc/sqlite3_ext
RUN apt-get update && apt-get -y install gdal-bin
RUN ln -s /usr/lib/x86_64-linux-gnu/libproj.so.12 /usr/lib/x86_64-linux-gnu/libproj.so.0

#Install gd library for images
RUN apt-get install libpng-dev libfreetype6-dev libjpeg62-turbo-dev -qy \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd

#Copy php ini
COPY php.ini /usr/local/etc/php/php.ini
RUN a2enmod rewrite

Solution 2

Here is a solution -- Step by Step:

Step 1:

Add/Update extra_hosts, hostname, domainname and in docker-compose.yml and in my case I'm running it over PORT 80.

This is how my php service looks like in docker-compose.yml file.

php:
   build: .
   image: php:7.2-apache
   working_dir: /var/www/html
   volumes:
     - ./:/var/www/html
     - ./php.ini:/usr/local/etc/php/php.ini
   extra_hosts:
     - "lara.local:127.0.0.1"
   hostname: lara.local
   domainname: local 
   ports:
     - 80:80  
   environment:
     - "DB_PORT=3306"
     - "DB_HOST=database"

Step 2:
Update your hosts file with following line:

127.0.0.1 lara.local

Step 3: Test our hostname by running this command

docker exec -it <your-php-container-name> hostname

If you see output lara.local then you are good to go!

Step 4: Rebuild app

docker-compose build

Step 5: Start the services and check the app is running at http://lara.local

docker-compose up -d

Note: If you are using a different port for example 8080 then it would be http://lara.local:8080

PS. If you want to change the DocumentRoot then ssh to your container and cd /etc/apache2/sites-available/000-default.conf and change DocumentRoot path as per your needs.

Share:
34,384
Luke.T
Author by

Luke.T

Updated on October 12, 2020

Comments

  • Luke.T
    Luke.T over 3 years

    I have two php applications in the same apache container and I'm trying to run one of them on a port since it needs to be accessible via a root domain and not a subfolder.

    I want to run the application on port 8060 which I've tried doing using apache virtual hosts but it won't load the page (http://192.168.99.100:8060/) it just says connection refused. However the normal root ip - http://192.168.99.100 works fine. My docker file is as follows

    version: '3.2'
      services:
        php-apache:
          build:
            context: ./apache-php
          ports:
            - 80:80
            - 8060:8060
    
          expose:
            - '8060'
          volumes:
            - ./DocumentRoot:/var/www/html:z
    

    My apache configuration

    <VirtualHost *:60>
        DocumentRoot /var/www/html/api
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    Any help would be greatly appreciated.

  • Asad Khan
    Asad Khan almost 5 years
    Sir really noob to docker can you please help me step by step that how to install virtual host?
  • Asad Khan
    Asad Khan almost 5 years
    Sir I have added the folder name vhost docker file is in vhost folder. vhost has an another folder name www for my domains. where do I place the apache configuration file
  • Luke.T
    Luke.T almost 5 years
    @AsadKhan Not sure what you're docker setup is but I have a Dockerfile referenced by "context: ./apache-php" in my docker compose.yml as you can see above. Then in my Dockerfile I've added on line 5 (see above) a command to replace my custom apache config with the containers default apache config. I've updated my answer hopefully that'll help.
  • Luke.T
    Luke.T almost 5 years
    @AsadKhan I'm not very well aquainted with magento but it runs on apache still so I'd have a look at this article linode.com/docs/web-servers/apache-tips-and-tricks/…. Use the code above to copy your apache config it may not be called 000-default.conf, but if you login to the container and have a look in /etc you should be able to find it's name in the apache folder.
  • Gayan
    Gayan over 3 years
    In my case the step 3 didn't work in the order you suggested. For me step 3 needed to be executed after step 5 and also had to use docker-compose instead of docker. So the command was docker-compose exec <your-php-container-name> hostname
  • TonyInuma
    TonyInuma almost 3 years
    This works only with a domain. And whats up on many domains?
  • And Finally
    And Finally over 2 years
    Thanks @Luke.T, this fixed my problem too!