PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] How to set up Apache with 3 sites: Two name based virtual hosts and one via the IP address

 September 20, 2022     apache, virtualhost     No comments   

Issue

I have 2 sites linked to domains already.

For this I have 2 conf files into /etc/apache2/sites-available/:

<domain1>.conf
<domain2>.conf

With DocumentRoot /var/www/domain1 and DocumentRoot /var/www/domain2.

In addition, I need to setup the 3rd site direct linked to server IP.

For this I created conf file: IP.conf (IP is IP of the server):

<VirtualHost *:80>
    ServerAdmin <email>
    ServerName <IP>
    ServerAlias <IP>
    DocumentRoot /var/www/html/wordpress
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Run:

sudo a2ensite <IP>.conf

And:

restart apache service.

But it doesn't help.

Could you advise how to configure routing?


Solution

The issue is the default Apache config always uses the IP address of the server. So that IP.conf will never be loaded.

You need to edit the default Apache config — located in sites-available — to make your changes.

Looking at your config, you are indicating the raw IP address for the ServerName and ServerAlias that will — effectively — defeat the purpose of setting up name based virtual hosts:

<VirtualHost *:80>
    ServerAdmin <email>
    ServerName <IP>
    ServerAlias <IP>
    DocumentRoot /var/www/html/wordpress
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

What happens in a case like this is the configs will be ignored because the default Apache setup will always defer to the IP address of the machine you are on. Heck, it would even use all network interfaces if your server has multiple IP addresses.

For name based virtual hosting to work, you must use the domain/host name in your config. Something like this for domain1:

<VirtualHost *:80>
    ServerAdmin <email>
    ServerName <domain1>
    ServerAlias <domain1>
    DocumentRoot /var/www/domain1
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And this for domain2

<VirtualHost *:80>
    ServerAdmin <email>
    ServerName <domain2>
    ServerAlias <domain2>
    DocumentRoot /var/www/domain2
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Now for the IP address host config, you should go into your Apache config directory — /etc/apache2/ on Debian/Ubuntu or /etc/httpd/ on CentOS/RedHat — and look inside the sites-available directory. There should be a file named 000-default.conf:

/etc/apache2/sites-available/000-default.conf

And make the changes you are showing in your IP.conf in there. At a most basic level just change the DocumentRoot to be this:

DocumentRoot /var/www/html/wordpress

Then restart Apache and it should be working as expected.



Answered By - Giacomo1968
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing