Apache Virtual Host without Servername

Asked

Viewed 427 times

1


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

So on my Ubuntu server 15.10 I rode as follows:

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


No /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>


No /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 Apache help and checked the existence of Servername, but I don’t have DNS. So can I set this up? Where’s my bug?


Thank you very much

  • I have had problems with Ubuntu 15.10 to configure Apache Servers. Have you tried using 14.04? Despite being an older version, it has already been well tested and approved.

2 answers

1


Reading a little more on the internet and joining with the reply of @miguel-batista, what worked was to put the two applications on the same virtual host, differentiating 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>

1

It is possible to 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 redirect referring to vhost that 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 edits 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>

to the 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>

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

then execute the commands

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

to certify that the two 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 it in production use the IP de acesso ao servidor online/site1 or IP de acesso ao servidor online/site2 hope I’ve helped.

  • I think the solution is really out there, but it still hasn’t worked out for my need. I ended up not putting some details and by the way it makes a difference. My application interprets the URL does the routing. For example site1/Users/new calls the new action inside the user controller... I think we’re still missing the apache configuration to make it interpret the URL. It would be . htaccess?

  • 1

    Did you enable the rewrite module? with the sudo a2enmod rewrite command and service apache2 Restart, for URL interpretation must be with the active rewrite module and whenever using the link put the full url or <a href="/user/new">new</a>

  • I enabled yes. When entering site1 everything is fine. When entering site2 it informs that the Url does not exist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.