0

I have two sites which runs independently on my VPS, let's call them site1 and site2.

They are respectively located at /var/www/html/site1 and /var/www/html/site2

I don't have a domain name yet but I can acces my site using server IP, let's say its adress is 8.8.8.8

I currently have two separate apache configuration file for each site.

Both sites are configured as follow, only DocumentRoot and the second Directory block differs.

<VirtualHost *:80>
    ServerName 8.8.8.8
    DocumentRoot /var/www/html/site
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/site>
        AllowOverride All
    </Directory>

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

I can acces both my site by enabling one and disabling the other one.

What I would like to do is to have site1 on http://8.8.8.8 and test site on http://site2.8.8.8.8

I tried the following configuration without success

<VirtualHost *:80>
    ServerName 8.8.8.8
    DocumentRoot /var/www/html/site1
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/site1>
        AllowOverride All
    </Directory>

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

<VirtualHost *:80>
    ServerName site2.8.8.8.8
    DocumentRoot /var/www/html/site2
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/site2>
        AllowOverride All
    </Directory>

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

Do I need DNS records to be able to achieve my goal ?

I not, how should I configure apache and my VPS to be correctly redirected with either sit1 or site2 depending on the IP prefix ?

2

Name Based Virtual Hosting uses the hostname your web browser sends as part of the HTTP request to identify which VirtualHost will be used to answer the request.

Typically you need to set up DNS for that and point both site1.example.com and site2.example.com to the IP-address of your webserver.

For a single user / developer, you can avoid registrering a domain and setting up DNS records by adding those two host names to the hosts file of your workstation.


If you have multiple IP-addresses available (nowadays rarely in the IPv4 world but commonly in IPv6) you can bind each Apache VirtualHost entry to different IP-address and don't need either DNS or the hosts file to serve different content, simply access the correct IP-address

https://httpd.apache.org/docs/current/vhosts/name-based.html#namevip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.