3

My aim is to be able to create 2 virtual hosts where one corresponds to the dev environment and another to the test environment in my local ubuntu 16.04 machine. One thing that is particular to this application is that it has to support dynamic subdomains. Some of them are fixed like login and admin subdomain, but also each user will have their own subdomain. This virtual host works fine for the dev environment:

<VirtualHost *:80>
        ServerName myapp.local
        ServerAlias *.myapp.local
        DocumentRoot /var/www/myapp/web
        <Directory /var/www/myapp/web/>
            AllowOverride All
            Require local
        </Directory>
        ErrorLog /var/www/myapp/logs/error.log
        CustomLog /var/www/myapp/logs/access.log combined
</VirtualHost>

Now I want to be able to have a virtual host for the test version and access it by this url: login.test.myapp.local, admin.test.myapp.local, ... Here is the virtual host that I have that is still not working because the request always falls in the first virtual host at this time:

<VirtualHost *:80>
        ServerName test.myapp.local
        ServerAlias *.test.myapp.local
        DocumentRoot /var/www/test.myapp/web
        <Directory /var/www/test.myapp/web/>
            AllowOverride All
            Require local
        </Directory>
        ErrorLog /var/www/test.myapp/logs/error.log
        CustomLog /var/www/test.myapp/logs/access.log combined
</VirtualHost>

I know that I need to tell the first virtual host to ignore urls that start with "test." and "*.test." but I do not know how to do that.

Thanks

Karim Mtl
  • 287

1 Answers1

4

Apache interprets its configuration in the order in which it is recorded. For example we put on first place the deny directives and on the second the allow directives.

That you need is to put your virtual host configurations in a correct order. For example place the test virtual host configuration in a conf file named 001-test.myapp.local.conf and the regular virtual host in a file named 002-myapp.local.conf. Or if the both configurations are in one file place the test one first:

$ cat /etc/apache2/sites-available/myapp.local.conf 

<VirtualHost *:80>
        ServerName test.myapp.local
        ServerAlias *.test.myapp.local
        DocumentRoot /var/www/test.myapp/web
        <Directory /var/www/test.myapp/web/>
            AllowOverride All
            Require local
        </Directory>
</VirtualHost>

<VirtualHost *:80>
        ServerName myapp.local
        ServerAlias *.myapp.local
        DocumentRoot /var/www/myapp/web
        <Directory /var/www/myapp/web/>
            AllowOverride All
            Require local
        </Directory>
</VirtualHost>

Thus the requests that doesn't match to the first virtual host will fall into the second. Here is how it's working on Ubuntu 18.04 with Apache/2.4.29:

enter image description here

pa4080
  • 29,831
  • I just tested by putting the test virtual host before the other one in the same .conf file and it works now. Thank you! – Karim Mtl Sep 12 '18 at 22:19
  • You are welcome, @KarimMtl. – pa4080 Sep 12 '18 at 22:36
  • @pa4080 there are any other settings need to do? like in /etc/hosts or any other.

    Because im getting an error like IP address could not be found.

    I have set my DocumentRoot directory to /var/www/html/my-project/public/, is it right or need to do some other changes for root.

    – Gomzy Sep 16 '19 at 06:54
  • @Gomzy, yes, you need to add all domains in /etc/hosts if there is not registered domain that points to your server. See the link at the bottom of the updated answer. – pa4080 Sep 16 '19 at 07:17
  • @pa4080 Here if you are adding any dynamic value for host is pointing your defined directory? because as i am checking you are entering only those host which you have defined in hosts file. Ex : if you enter xyz.myapp.local will works without adds in hosts file? – Gomzy Sep 16 '19 at 08:38
  • @Gomzy, yes. This was the requirement :) – pa4080 Sep 16 '19 at 08:42
  • @pa4080 if I want to access xyz.myapp.local, then it is compulsory to add it to the /etc/hosts ? – Gomzy Sep 16 '19 at 09:14
  • Yes, then you will be able to access it via the same machine. @Gomzy : https://askubuntu.com/a/1024979/566421 and https://askubuntu.com/a/941305/566421 – pa4080 Sep 16 '19 at 09:21
  • @pa4080 Is it possible to create dynamic host like Blogspot, like in Blogspot I can access my blog with my username, like suppose my username is gomzy then I can access my blog with gomzy.blogspot.com. it is possible to do without adds in the hosts file? – Gomzy Sep 16 '19 at 09:47
  • @Gomzy, yes it is possible, but this is too broad for this format. Cheers! – pa4080 Sep 16 '19 at 09:51