skip to Main Content

I’m moving 2 wordpress websites (one of them is a subdomain) to my local machine under docker containers for development.

I would like to access them in my development machine by their domain names using the hosts file, but I’m struggling to get my apache virtual hosts to work.

Let’s say:
Site 1 is ‘www.example.com’
Site 2 is ‘sub.example.com’
Each have their own MySQL database and wordpress code base, setup on different docker-compose containers.

So:
Site 1: wwww.example.com on http://localhost:8000
Site 2: sub.example.com on http://localhost:8001
both work fine via localhost:8000 and localhost:8001

I altered my local hosts file to have:

127.0.0.1       localhost  
127.0.0.1       example.com www.example.com sub.example.com

I’m struggling to setup the apache virtual hosts for this situation,
this is what I’ve tried:

<VirtualHost *:80>
    ServerName www.example.com
    ProxyPass /  http://localhost:8000/
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
</VirtualHost>

<VirtualHost *:80>
    ServerName sub.example.com
    ProxyPass /  http://localhost:8001/
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
</VirtualHost>

Going directly to http://localhost:8000 and http://localhost:8001 works.

When I go to http://www.example.com I get the following error:

Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.25 (Debian) Server at www.example.com Port 80

and on http://sub.example.com:

Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Apache/2.4.25 (Debian) Server at sub.example.com Port 80

Can someone point me in the right direction on how to setup the virtual hosts please?

Thank you

2

Answers


  1. Chosen as BEST ANSWER

    I understand that my question may be off-topic here, but since I managed to solve my problem, I hope you will allow my answer to remain here so that it may help someone else.

    I failed to mention that the apache server was running in its own Docker container.

    This is what I did to solve the problem:

    In the host machine:

    Include www.example.com, example.com and sub.example.com in the hosts file:

    127.0.0.1       localhost  
    127.0.0.1       example.com www.example.com sub.example.com
    

    In the apache docker container:

    Since apache is running in its own docker container, localhost:8000 points to apache docker container's localhost which is incorrect here.
    It needs to point to the host machine's ip address, which in my case is 192.168.1.100.

    <VirtualHost *:80>
        ServerName www.example.com
        ProxyPass /  http://192.168.1.100:8000/
        ProxyPassReverse /  http://192.168.1.100:8000/
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName sub.example.com
        ProxyPass /  http://192.168.1.100:8001/
        ProxyPassReverse /  http://192.168.1.100:8001/
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
    </VirtualHost>
    

    Restart apache

    On each Worpdress Site:

    The last thing to do is to change the 'site_url' and 'home' settings to point to the host machine's ip address (192.168.1.100 here). This can be done by 2 ways:
    Either by going to the wordpress database, table wp_options and modifying:
    site_url and home to http://192.168.1.100:8000 for www.example.com and example.com; and
    site_url and home to http://192.168.1.100:8001 for sub.example.com

    Or by logging into your Wordpress Dashboard (/wp-admin) then going to Settings and modifying:
    WordPress Address (URL) and Site Address (URL) to http://192.168.1.100:8000 for www.example.com and example.com; and
    WordPress Address (URL) and Site Address (URL) to http://192.168.1.100:8001 for sub.example.com

    You shouldn't need to restart the Wordpress containers, but if you get any errors, restart them and check again.


  2. You can try checking the logs located in var/log/httpd(You can also try sudo locate access.log) for the actual error that is happening.

    The one thing you are missing is ProxyPassReverse which will rewrite the URL if your website is redirecting. With it your configuration will look something like this

    <VirtualHost *:80>
        ServerName www.example.com
        ProxyPass /  http://localhost:8000/
        ProxyPassReverse /  http://localhost:8000/
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName sub.example.com
        ProxyPass /  http://localhost:8001/
        ProxyPassReverse /  http://localhost:8001/
        <Proxy *>
            Order deny,allow
            Allow from all
        </Proxy>
    </VirtualHost>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search