简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM