Search code examples
apache.htaccessubuntudnssubdomain

Setup subdomain in Apache server ubuntu 16.04 lts


How can I setup a subdomain in Apache on an ubuntu server.

My apache.conf file:

<VirtualHost *:80>
        ServerName www.hello.com
        DocumentRoot /var/www/html/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:81>
        ServerName bye.hello.com
        DocumentRoot /var/www/html/other
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now my access is by IP: 10.20.20.34:80 (www.hello.com) and 10.20.20.34:81 (bye.hello.com). The 80 port works great, but I can't set up a subdomain for 81 port.

We don't need public access, everything is over the intranet. We modified DNS record in our router.

IMPORTANT: I have to keep both access: 10.20.20.34:81 and bye.hello.com

Here is a summary of what happens:

  • 10.20.20.34:80 - OK.
  • 10.20.20.34:81 - OK.
  • www.hello.com - OK.
  • bye.hello.com - FAIL, show me www.hello.com

Solution

  • You will need to add a listen directive to your /etc/apache2/ports.conf. In that file you will see:

    Listen 80
    

    Add a second line underneath:

    Listen 80
    Listen 81
    

    Then restart Apache:

    apache2ctl restart
    

    Otherwise, I do not see any problems with your Virtual Host settings.

    Update

    Apache won't do this for you through the DNS. You can read this to learn more. The best thing for you to do is change the second Virtual Host directive to:

    <VirtualHost *:80>
            ServerName bye.hello.com
    

    And it should work fine. There is no advantage to using two ports. If you have an architectural limitation, then the link above had a few advanced solutions.