Setting up multiple virtual hosts with domain names

March 19, 2017 708 views
Apache Ubuntu 16.04

I'm trying to get my virtual hosts to work on Apache. I followed the tutorials here on this website, but for some reason both my domain names keep going to the standard Apache page instead of their own folders. I disables the 000-default.conf and enabled my own.

Here are my two configuration files:

<VirtualHost *:80>
    ServerAdmin info@insiteweb.xyz
    ServerName insiteweb.xyz
    ServerAlias www.insiteweb.xyz
    DocumentRoot /var/www/insiteweb.xyz/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin info@moviezzz.nl
    ServerName moviezzz.nl
    ServerAlias www.moviezzz.nl
    DocumentRoot /var/www/moviezzz.nl/public_html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Inside the folders /var/www/moviezzz.nl/publichtml and /var/www/insiteweb.xyz/publichtml are both an index.html file. Yet I stil get send to the standard " It works !

If you're seeing this page via a web browser, it means you've setup Tomcat successfully. Congratulations!" page of apache.

Does anyone know what the problem might be?

9 Answers

Where are your vhost files located? Did you restart apache? What does apache2ctl -S show?

The host files are located in /etc/apache2/sites-available/ I enabled them so now they're also in sites-enabled. I did restart apache, always do after a change.

The command shows the following:
https://gyazo.com/b001e7e2b96158a2233808c687f8e3f6

Hi @martijnjansen
I'm not sure why you're seeing something about Tomcat, since that's a Java web server, so I think you have both Tomcat and Apache running at the same time.
Can you run this tail -50 /var/log/apache/error.log and paste that?
Remember to use the @ symbol to make sure to notify people about your response.

@hansen @Woet ,

Thanks for the @ tip, I'm fairly new here.

You were right, Tomcat was still running so I stopped it. I'm getting another message now when I try to go to the website's "ERRCONNECTIONREFUSED". It says the website is unavailable. The error.log shows the following:

https://gyazo.com/ba99adf2fa685f31ed585bb14bb5d9cb

Btw this is in the folder apache2, because the folder apache doesn't exist for me.

  • @martijnjansen But is Apache even running and listening on that interface?
    If you're not using Tomcat, then remove it.

    • @hansen

      Eventually I want to use Tomcat as well, but I understand it is not possible to use both Tomcat and Apache2? I need multiple virtual hosts and on some I want to deploy a WAR file. Is this possible to achieve with Tomcat as well? Because I don't believe Apache2 can deploy Java applications, right?

      • I would probably recommend doing a reverse proxy from Apache and have Tomcat using another port internally.
        But let's focus on one thing at the time. Is Apache running and listening on that interface?

        • @hansen
          Right now I have them both running and I the domain names are connected to the virtual hosts of Tomcat. Is it possible to get php running using Tomcat as well? I can only find it running with Apache, and right now the browser just shows the php code (i have PHP7 installed).

          When I run systemctl status apache2 is see this:
          https://gyazo.com/c35431bc39ccd99a03cdf5e29f21f986

          • @martijnjansen
            I don't quite understand that, because you cannot have two services listening on the same port.
            So if Tomcat is running on port 80 (the regular HTTP port), then Apache cannot be running on that port.
            And since your php code is being served as plain text and not run on the server, it is very likely it's being served by Tomcat, which doesn't support php (I think) like Apache does.

          • @hansen

            That's what I believe as well. Right now the virtual hosts are from Tomcat, not Apache.

            So if I understand correctly, I need Apache2 to listen to port 80, and then let Tomcat listen to another port. When I do this, I can use the virtual hosts of Apache en run PHP scripts on it. Will it still be possible to have war files deployed as well?

@martijnjansen I'll start a new thread, since we maxed out the other one.

I would recommend that you run Apache publicly (*:80) and then do a reverse proxy to Tomcat (which will be running on a local port - let's say 127.0.0.1:8080). That way your visitors don't have to use a special port to access Tomcat, but will actually access Apache, which would just relay/proxy the connection to Tomcat.
You only need to have Tomcat for WAR, so you'll only use it for that.

  • Ok, so for now I stopped Tomcat, Apache is running and in the ports.conf:

    Listen 80
    
    <IfModule ssl_module>
        Listen 443
    </IfModule>
    
    <IfModule mod_gnutls.c>
        Listen 443
    </IfModule>
    

    The other conf files are:

    <VirtualHost *:80>
        ServerAdmin info@moviezzz.nl
        ServerName moviezzz.nl
        ServerAlias www.moviezzz.nl
        DocumentRoot /var/www/moviezzz.nl/public_html
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin info@insiteweb.xyz
        ServerName insiteweb.xyz
        ServerAlias www.insiteweb.xyz
        DocumentRoot /var/www/insiteweb.xyz/public_html
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    In both public_html folders is an index.html test file. Yet when I go to my domain names is says the website is unavailable. Probably because Apache is not listening to port 80?

    • Correct, it seems like Apache is not listening on the interface, because it's set to listen on port 80.
      Or maybe you just need to restart Apache.
      If you run lsof -P -iTCP -sTCP:LISTEN will show all active listening ports, what service and many other details.

      • @hansen
        Ok, now I got Apache2 working. It was listening to port 80 with a correct interface. So I started tomcat again to see to what port that was listening to. It was 8080. Then I remembered that a while ago I redirected port 8080 to 80. Now I let Apache listen to port 8080 to and stopped Tomcat again, and now it works (including the php code).

        I redirected it with the following code: sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8080. Is it possible to change this back? So I can just let Apache listen to port 80 without a redirection?

        • Run this to get the rule number for that redirect:
          sudo iptables --list PREROUTING
          Then run this to delete that rule (where xxxx is the rule number):
          sudo iptables --delete PREROUTING xxxx

          • That's weird. When I run the first command it says:

            iptables: No chain/target/match by that name.
            

            Will it still be possible to get Tomcat working with Apache listening to 8080 with a reverse proxy the way you suggested?

          • @martijnjansen
            Try this sudo iptables --list and it should show all chains.

  • @martijnjansen
    Okay, then you must have done the redirect somewhere else.
    Is this a new droplet or old, meaning do you mind reinstalling it?
    Because you're going to end up in a mess if you're running on different ports, doing redirects - if something goes wrong later on, it will be very difficult to debug.

  • @martijnjansen
    PS. Don't use screenshots for console stuff, just copy the text and click the Code-button in this text editor to insert it with formatting.

@hansen
Ok, I will use copying in the future.

And it's a new droplet so I can reinstall it. You mean rebuild to Ubuntu 16.04 Base Image?

  • @martijnjansen
    Yes, rebuild (I think it's called in the DO control panel). Then only install Apache and PHP to start with. We'll install Tomcat later after Apache is working.

    • @hansen

      Ok, I rebuild my droplet and have Apache and PHP installed and both work with virtual hosts. Now I don't really understand yet what you meant with a reversed proxy for Tomcat. What do I have to do to make it possible for me to use Tomcat for some website's?

      • @martijnjansen
        You need to setup Tomcat on localhost on port 8080, then you can have Apache do the connection internally to Tomcat, so the visitor is just using the regular port 80.
        https://wiki.apache.org/httpd/TomcatReverseProxy

        • @hansen
          I installed Tomcat as well which is now listening to port 8080. I have deployed a war file on Tomcat which is working properly. But I can't seem to get the reversed proxy right. I have the following code in proxy.conf

          # mod_proxy setup.
          ProxyRequests Off
          ProxyPass http://insiteweb.xyz/insite-login http://localhost:8080/insite-1.5.1.RELEASE
          ProxyPassReverse http://insiteweb.xyz/insite-login http://localhost:8080/insite-1.5.1.RELEASE
          
          <Location "http://insiteweb.xyz/insite-login">
            # Configurations specific to this location. Add what you need.
            # For instance, you can add mod_proxy_html directives to fix
            # links in the HTML code. See link at end of this page about using
            # mod_proxy_html.
          
            # Allow access to this proxied URL location for everyone.
            Order allow,deny
            Allow from all
          </Location>
          

          So what I want is when i go in the browser to http://insiteweb.xyz/insite-login, you actually see what is on http://localhost:8080/insite-1.5.1.RELEASE.

          Do you know what I'm doing wrong? Because when I go to http://insiteweb.xyz/insite-login I just get a 404 Not Found.

          • @martijnjansen
            I haven't used Apache for many years, but try this:

            ProxyRequests Off
            ProxyPass /insite-login http://localhost:8080/insite-1.5.1.RELEASE
            ProxyPassReverse /insite-login http://localhost:8080/insite-1.5.1.RELEASE
            <Location "/insite-login">
              Order allow,deny
              Allow from all
            </Location>
            

            I'm not sure if you can use http://localhost:8080/insite-1.5.1.RELEASE or if you have to use http://localhost:8080 and then do the redirection on Tomcat.

@hansen

I think you're right that I have to do the redirection on Tomcat. Your code still gives a 404 and the link changes to http://insiteweb.xyz/insite-1.5.1.RELEASE. When I try the redirect without /insite-1.5.1.RELEASE it works, but it only goes to the regular Tomcat page.

For the redirect on Tomcat, do I have to set a redirect in the index.jsp of ROOT or is there another better way?

  • @martijnjansen Hmmm, you've caught me with my pants down, I don't know :-)
    I've never worked with Tomcat, but why don't you put insite-1.5.1.RELEASE in the root?
    If you want to have separate releases, I would run another Tomcat vhost, which listens on localhost:8081 and that could be your insite-1.6.0.DEVELOPMENT
    And then you'll setup that reverse proxy in Apache.

    • @hansen
      Haha ok. :P

      I guess I could put that app in the root. I'm a freelance webdeveloper and I'm using this droplet for the website's of my clients. The app I have installed right now is the one where they can log in, so it's ok if that's the root and is accesible from all the domain names by adding /insite-login at the end.

      The only problem would be, maybe in the future I want to have multiple WAR files deployed and add one domain name to it. How can I use another Tomcat vhost? I can't install Tomcat again, can I?

      • @martijnjansen :-)

        You can have multiple WAR files deployed. Just think of Tomcat as a container for each project.
        Do not run multiple Tomcats, just use a vhost in Tomcat to separate the projects.
        And if you want to run multiple WAR files in the same project, then they don't overlap, so you would use /insite-login and /insite-something for another.

        Play around with it, but you're probably better off by asking in a Tomcat-only forum about specifics like that.

Have another answer? Share your knowledge.