LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-27-2018, 04:10 AM   #1
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Rep: Reputation: Disabled
Apache Virtual host issue


Hello,

I am facing a very strange issue while setting a virtual host on apache to setup multiple websites using separate IPs.

Virtual host is setup but when i am browsing the website it display content under /var/www/html and displaying site1 and site2 folder instead of access the content under respective directory as mentioned in virtual host file.

Below is the steps and virtual host configuration i setup

sudo chown -R apache:apache /var/www/html/site1
sudo chown -R apache.apache /var/www/html/site2
chmod -R 755 /var/www

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled

Added IncludeOptional sites-enabled/*.conf line in /etc/httpd/conf/httpd.conf file
vim /etc/httpd/sites-available/site1.conf
vim /etc/httpd/sites-available/site2.conf

ln -s /etc/httpd/sites-available/site1.conf /etc/httpd/sites-enabled/site1.conf
ln -s /etc/httpd/sites-available/site1.conf /etc/httpd/sites-enabled/site2.conf

systemctl restart httpd

Below is configuration added to created apache host

site1.conf
<VirtualHost 192.168.1.11:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/site1
ServerName example.com
ServerAlias www.example.com

<Directory /var/www/html/site1/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

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

</VirtualHost>

site2.conf
<VirtualHost 192.168.1.12:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/site2
ServerName example.com
ServerAlias www.example.com

<Directory /var/www/html/site2/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

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

</VirtualHost>

I am using CentOS7 and I already disabled selinux and firewalld
Please help me out here.
 
Old 08-27-2018, 08:22 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by sunnysthakur View Post
Hello,
I am facing a very strange issue while setting a virtual host on apache to setup multiple websites using separate IPs. Virtual host is setup but when i am browsing the website it display content under /var/www/html and displaying site1 and site2 folder instead of access the content under respective directory as mentioned in virtual host file.

Below is the steps and virtual host configuration i setup

sudo chown -R apache:apache /var/www/html/site1
sudo chown -R apache.apache /var/www/html/site2
chmod -R 755 /var/www

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled

Added IncludeOptional sites-enabled/*.conf line in /etc/httpd/conf/httpd.conf file
vim /etc/httpd/sites-available/site1.conf
vim /etc/httpd/sites-available/site2.conf

ln -s /etc/httpd/sites-available/site1.conf /etc/httpd/sites-enabled/site1.conf
ln -s /etc/httpd/sites-available/site1.conf /etc/httpd/sites-enabled/site2.conf

systemctl restart httpd

Below is configuration added to created apache host I am using CentOS7 and I already disabled selinux and firewalld Please help me out here.
Use CODE tags when posting configs/logs, so its easier to read. There are many guides on how to set up virtual hosts on CentOS:
https://www.digitalocean.com/communi...ts-on-centos-7

...that you can try to follow. You don't say where you got the steps you're posting, but you've missed a step somewhere. You also say when you 'browse the website', it displays folder. Is that when you try to go to http://site1 (or site2), or when you go to the main address of your server? Have you configured both of those IP addresses on your server?

What's in the error and access logs?? After being an administrator for 12 years now, you should know that those log files are the best place to start.
 
Old 08-27-2018, 10:55 AM   #3
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
Hello,

Both sites are hosted on separate IPs say site1 on 192.168.1.100 and site2 on 192.168.1.101. Now when i access site1 using http://192.168.1.100 it land me to /var/www/html where these two folders are created (site1 and site2) and vise versa.
I went through logs as well but there isn't error related to this because apache is accessing /var/www/html instead of mentioned directories.
This is a strange issue for me because same thing is working on one of the server which we setup a year ago.
 
Old 08-27-2018, 11:02 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,484

Rep: Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556Reputation: 1556
With out the full config it would appear that these are configured as Name Virtual Hosts, so using the IP address alone will do nothing as it's the server name that's used to match the Vhost to the relevant DocRoot
 
1 members found this post helpful.
Old 08-27-2018, 11:18 AM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
If you access an Apache web server by IP address, you will always see the first defined host.
What is the DocumentRoot value of your server? Presumably /var/www/html -- what content is in that directory? That's what accessing by IP address will show you.

Your sample VirtualHost entries both show the same domain name. Again, in that case, accessing by domain name will only show the first defined entry.

You do not need two IP addresses to do VirtualHost...we have ~70 domains hosted on a single IP address. Is the web server listening on both IP addresses? Are both addresses configure on the NIC?

I'm not sure how you have domain names on private IP addresses, either, as you can't set those up in DNS.

Also, your config IP addresses and your later example don't match.
 
1 members found this post helpful.
Old 08-27-2018, 11:27 AM   #6
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
Hello

Below is the document root setup in httpd.conf

PHP Code:
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    
AllowOverride None
    
# Allow open access:
    
Require all granted
</Directory
/var/www/html was having default index.html which i deleted and after that it start list directories under /var/www/html on browser.
 
Old 08-27-2018, 11:32 AM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by sunnysthakur View Post
Hello

Below is the document root setup in httpd.conf

Code:
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
/var/www/html was having default index.html which i deleted and after that it start list directories under /var/www/html on browser.
That's the expected and correct behavior for a directory without a index.html And that's what you'll see when accessing by IP address. You're not supplying a domain name, so there's no way for the web server to direct the request to the site1 or site2 directories.
 
1 members found this post helpful.
Old 08-28-2018, 12:31 PM   #8
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
I bind the virtualhosts using IP as i am testing on local machine.

Below is a content displayed on browser.

PHP Code:
Index of /
[
ICO]    Name    Last modified    Size    Description 
However when i change the Documentroot in httpd.conf to /var/www/html/site1 it start working but this is a trick as this will not help to host multiple files.
 
Old 08-28-2018, 02:36 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
What is the Listen directive set to?
Are the domain names for the VirtualHosts different?
What happens when you access by domain instead of IP?

Again, you don't need (I don't think it will even work) to have different IP addresses for each VirtualHost
From the Apache docs:
Quote:
Error condition
Multiple Listen directives for the same ip address and port will result in an Address already in use error message.
VirtualHosts are accessed by the domain name, not by IP address.
Set both VirtualHosts to the IP address specified in the Listen directive.
Make sure the domain names are different (I presume they are)
Use http://domainname.com to access the server.
 
Old 08-28-2018, 02:38 PM   #10
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by sunnysthakur View Post
I bind the virtualhosts using IP as i am testing on local machine.
It's not the IP address in the <VirtualHost> directive that binds the web server, it's the IP address in the Listen directive...and there can only be one IP address per port.
 
Old 08-29-2018, 04:08 AM   #11
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
Below is hosts added to my /etc/hosts

PHP Code:
10.10.1.161     site1.com       site1
10.10.1.162     site2
.com       site2 

Below is my httpd.conf file configuration

PHP Code:
ServerRoot "/etc/httpd"

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80

ServerAdmin root
@localhost

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    
AllowOverride none
    
Require all denied
</Directory>
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    
AllowOverride None
    
# Allow open access:
    
Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/var/www/html">
    
#
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    
Options Indexes FollowSymLinks

    
#
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    
AllowOverride None

    
#
    # Controls who can get stuff from this server.
    #
    
Require all granted
</Directory>
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
IncludeOptional sites-enabled/*.conf 
Below is my virtualhost file configuration for each host

PHP Code:
/etc/httpd/sites-enabled/site1.conf

<VirtualHost 10.10.1.161:80>
    
ServerName www.site1.com
    ServerAlias site1
.com
    DocumentRoot 
/var/www/site1.com/public_html
    ErrorLog 
/var/www/site1.com/error.log
    CustomLog 
/var/www/site1.com/requests.log combined
</VirtualHost>

/
etc/httpd/sites-enabled/site2.conf

<VirtualHost 10.10.1.162:80>
    
ServerName www.site2.com
    ServerAlias site2
.com
    DocumentRoot 
/var/www/site2.com/public_html
    ErrorLog 
/var/www/site2.com/error.log
    CustomLog 
/var/www/site2.com/requests.log combined
</VirtualHost
Note :- Actually this configuration work on my local machine but when i am doing the same thing on AWS instance it is not working and by accessing the virtualhost by IP it pointed to apache default index page.
 
Old 08-29-2018, 08:35 AM   #12
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by sunnysthakur View Post
Note :- Actually this configuration work on my local machine but when i am doing the same thing on AWS instance it is not working and by accessing the virtualhost by IP it pointed to apache default index page.
virtualhost by IP will always show you the default configuration. Apache VirtualHost doesn't work by IP, it works by domain name.

Again,
  • Set the VirtualHost to the IP address of the server...they will both be the same
  • Access the server with the domain name defined in the VirtualHost (and yes, you will need the hosts file on the calling desktop configured to relate the domain name(s) to the single IP address, if they are not "real" , DNS resolveable domains)

Please try that and let us know what happens.
 
Old 09-07-2018, 08:14 AM   #13
sunnysthakur
Member
 
Registered: Aug 2012
Posts: 92

Original Poster
Rep: Reputation: Disabled
I will try using hostname and will let you know in case of any issue.
 
Old 09-10-2018, 02:00 AM   #14
kabaddi
LQ Newbie
 
Registered: Sep 2005
Posts: 2

Rep: Reputation: 0
I'd be interested to see the output of
Code:
ip addr
to see what ip address/es this machine might have. As said before, it's usually best to have one when working with Apache VirtualHosts.
 
  


Reply

Tags
apache, httpd, virtual host



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Apache Virtual Host Issue psoheil Linux - Server 15 12-24-2012 01:36 PM
Apache Virtual Host issue ciphyre Linux - Server 3 10-24-2008 11:29 AM
Apache: virtual host issue Zeno McDohl Linux - Server 17 12-21-2006 02:59 PM
Virtual Host issue with apache gauge73 Linux - Networking 1 07-12-2005 02:00 PM
Apache Virtual Host problem - DNS or DHCP issue? costrevs Linux - Software 16 03-23-2005 07:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration