Apache Virtual Host without ServerName

1


    I want to set up an environment where I can put my production applications (site1) and approval (site2).     

    

    Then on my Ubuntu 15.10 server I mounted it as follows:     

    - /var/www/html/site1
    - /var/www/html/site2


    In /etc/apache2/sites-enabled/site1.conf:     

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/site1
        Alias /site1 /var/www/html/site1

        <Directory /var/www/html>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>


    In /etc/apache2/sites-enabled/site2.conf:     

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/site2
        Alias /site2 /var/www/html/site2

        <Directory /var/www/html>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>


    Whenever I type in the localhost / site1 URL, it works perfectly. But when I type localhost / site2, it indicates that it was not found. I looked at the Apache help and checked for ServerName, but I do not have DNS. So I can set this up? Where is my mistake?


Thank you very much

    
asked by anonymous 16.06.2016 / 21:25

2 answers

1

By reading a little more on the internet and joining with @ miguel-batista's response, what worked was putting the two applications on the same virtual host, differentiating it by Alias .

/etc/apache2/sites-enabled/site.conf



<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    Alias /site1 /var/www/html/site1
    Alias /site2 /var/www/html/site2

    <Directory /var/www/html>
            Options Indexes FollowSymlinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
    
20.06.2016 / 16:31
1

You can solve your problem as follows:

open the file hosts using the command sudo nano /etc/hosts

and add the following lines in the file to create the vhost redirect you want to create:

127.0.1.1  site1
127.0.1.1  site2

Then open the file sudo nano /etc/apache2/sites-enabled/site1.conf and edit the data as follows

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName site1
        DocumentRoot /var/www/html/site1

        <Directory /var/www/html/site1>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>

for site2

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName site2
        DocumentRoot /var/www/html/site2

        <Directory /var/www/html/site2>
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>

In this way you are creating a vhost for each one and doing the pointing to the folders.

Then run the commands

a2ensite /etc/apache2/sites-enabled/site1.conf
a2ensite /etc/apache2/sites-enabled/site2.conf

To make sure that both vhosts are enabled in apache, and then

service apache2 restart

To access the url you will not need to use localhost

only site1/ or site2/ you do not need a DNS to create a local server using ServerName. If you want to do in production use IP de acesso ao servidor online/site1 or IP de acesso ao servidor online/site2 I hope I have helped.

    
17.06.2016 / 14:28