Search code examples
apachedockerdnsreverse-proxywhm

Forwarding subdomain to a port of localhost using WHM and apache2


In brief, I can access my site using example.com:3000 but I want to access it using sub.example.com.

I am using WHM to manage my server using the example.com domain. I have added a DNS Zone using the WHM control panel for sub.example.com. It works fine and redirects to sub.example.com/cgi-sys/defaultwebpage.cgi.

I also tried to make a reverse proxy using VirtualHost by adding the following lines to the /etc/apache2/conf/httpd.conf file as mentioned in here:

<VirtualHost *:80>
    ServerName sub.example.com
    ProxyPreserveHost on
    ProxyPass / http://localhost:3000/
</VirtualHost>

Then I restarted the Apache server using sudo systemctl restart httpd. However the subdomain still redirects to the sub.example.com/cgi-sys/defaultwebpage.cgi path rather than forwarding to the port:3000. Could you please help me resolve this problem?

It is worth mentioning that the server is using WHM and CPanel (version 84.0.21) on CentOS (version 7.7) as well as Apache (version 2.4.41) and the port:3000 is connected to a docker image (gitea) via docker-compose.


Solution

  • I had the same issue with the same setup and was able to solve it by using this configuration:

    <VirtualHost sub.example.com:80>
    
        ServerName sub.example.com
        ServerAlias sub.example.com
    
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPass / http://localhost:3000/
        ProxyPassReverse / http://localhost:3000/
    
    </VirtualHost>
    

    Adding it to Apache's include directive in WHM: https://docs.cpanel.net/whm/service-configuration/include-editor/

    For SSL port 443 you have to use the IP instead of domain:

    <VirtualHost 1.1.1.1:443>