LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   CentOS (https://www.linuxquestions.org/questions/centos-111/)
-   -   Testing virtual host creation (https://www.linuxquestions.org/questions/centos-111/testing-virtual-host-creation-4175589196/)

anaigini45 09-11-2016 12:43 AM

Testing virtual host creation
 
Hi,

I have created 3 virtual hosts within my virtual machine.
The DocumentRoot for two of the virtual machines are located in
/var/www/vhosts. The name of these two virtual hosts (ServerName) are beta.example.com & example.com. The index.html contents for both these servers are "COFFEE!" and "COKE!" respectively.

The 3rd virtual host is located in a directory called /website. I created an index.html for this virtual host in /website with the content "OMG SO NON STANDARD".

The SELinux setting for the /website directory was originally :
Code:

# ls -Z / | grep website
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 website
# ls -Z /website/
-rw-r--r--. root root unconfined_u:object_r:default_t:s0 index.html

Changed it to :
Code:

# chcon --reference=/var/www /website/
# ls -Z / | grep website
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 website
# chcon --reference=/var/www/vhosts/example.com/index.html /website/index.html

Even after doing these changes, I am not able to see any output when I do :
Code:

# curl non.example.com
It produces error like 'host not found'.

I then add all three virtual hosts beta.example.com, example.com and non.example.com into /etc/hosts, the same line as localhost.
I then restart httpd, and when I do the 'curl' command :
Code:

# curl non.example.com
COFFEE!

It produces output "COFFEE!". I then move the beta.example.com & example.com to a different directory and restart httpd, and then I get the output desired from curl :

Code:

# curl non.example.com
OMG SO NON STANDARD

Today, I remove the beta.example.com, example.com and non.example.com from /etc/hosts, and restart httpd, and I still get the output same as above :

Code:

# curl non.example.com
OMG SO NON STANDARD

My question is, what is the correct way to get virtual hosts to produce the output desired from index.html without getting any errors. Is it really required to add the VirtualHost "ServerName" (in this case example.com, beta.example.com, non.example.com) in /etc/hosts?

If so then why does using curl on non.example.com give the output COFFEE! which is the content of beta.example.com?

TenTenths 09-12-2016 09:59 AM

Quote:

Originally Posted by anaigini45 (Post 5603568)
Is it really required to add the VirtualHost "ServerName" (in this case example.com, beta.example.com, non.example.com) in /etc/hosts?

It's not necessary but it's by far the easiest method for testing. There is a method using curl that allows you to pass the name of the required host as a parameter for example:
Code:

curl --header 'Host: beta.example.com' http://localhost
or
Code:

curl --resolve beta.example.com:80:127.0.0.1 http://beta.example.com/
So you can see it's easier for testing to just add the hosts to /etc/hosts and have names resolve "correctly"


All times are GMT -5. The time now is 12:06 AM.