Jump to content



Photo

VirtualHosts (apache) on CentOS issues


  • Please log in to reply
5 replies to this topic

#1 goodbytes

goodbytes

    Just below average Joe

  • Tech Issues Solved: 1
  • Joined: 07-May 04
  • Location: England

Posted 12 May 2014 - 16:36

Hi, i think i'm a little confused with VirtualHosts on CentOS using Vagrant. For those that don't know Vagrant is just a wrapper for a virtual machine (Virtual Box) i'm using CentOS 6.5 within Virtualbox which has a local IP.
 
My server DocumentRoot in httpd.conf is set to /home and then i've created sites like /home/username/public_html/ so that it's an exact copy of my production server.
 
I then setup a localhost in the /etc/hosts file:
 
192.168.33.21   dev.local
 
then i can do things like http://dev.local/username/public_html/ (dirty i know, but for quickness it works great for smaller projects).
 
All is working great, however, I'm working on a larger project that would benefit from having it's own VirtualHost so i can access it from http://username.local so off i went into my httpd.conf and added a new virtual host:
 
<VirtualHost *:80>
     ServerAdmin server@admin.com
     DocumentRoot /home/username/public_html
     ServerName username.local
     ServerAlias username.local
     ErrorLog /home/username/public_html/error.log
     CustomLog /home/username/public_html/requests.log common
</VirtualHost>
 
and then back into /etc/hosts i added the new alias:
 
192.168.33.21   dev.local
192.168.33.21   username.local
 
But now both domains username.local and dev.local are going straight to the new folder /home/username/public_html .. so basically dev.local is being overwritten.. any ideas where i am going wrong?



#2 Sikh

Sikh

    Neowin Addict!

  • Tech Issues Solved: 2
  • Joined: 11-March 07
  • Location: localhost
  • OS: Windows 7 / 10.8 / Ubuntu Server
  • Phone: Nexus 5 PA 4.4.2 / iPhone 5

Posted 12 May 2014 - 16:44

When using virtual hosts, you shouldnt have to edit the hosts file. Virtual hosts is running off of the same IP / domain, so editing your hosts file to include the other domain is wrong I believe. I've always just added a virtual host, restarted apache and boom done. There's no need to edit hosts file aside from telling it who localhost is.

So I would say, get rid of the "username.local" definition in the hosts file, restart networking / apache to load the new configs and try it again.



#3 OP goodbytes

goodbytes

    Just below average Joe

  • Tech Issues Solved: 1
  • Joined: 07-May 04
  • Location: England

Posted 12 May 2014 - 16:46

When using virtual hosts, you shouldnt have to edit the hosts file. Virtual hosts is running off of the same IP / domain, so editing your hosts file to include the other domain is wrong I believe. I've always just added a virtual host, restarted apache and boom done. There's no need to edit hosts file aside from telling it who localhost is.

So I would say, get rid of the "username.local" definition in the hosts file, restart networking / apache to load the new configs and try it again.

 

If i remove username.local from the hosts file it goes straight to Google as the computer doesn't know what to do with it.



#4 Sikh

Sikh

    Neowin Addict!

  • Tech Issues Solved: 2
  • Joined: 11-March 07
  • Location: localhost
  • OS: Windows 7 / 10.8 / Ubuntu Server
  • Phone: Nexus 5 PA 4.4.2 / iPhone 5

Posted 12 May 2014 - 16:49

Do you have a entry for 127.0.0.1 localhost



#5 OP goodbytes

goodbytes

    Just below average Joe

  • Tech Issues Solved: 1
  • Joined: 07-May 04
  • Location: England

Posted 12 May 2014 - 16:54

Nevermind i've fixed it, found this in the apache docs:

 

"If you are adding virtual hosts to an existing web server, you must also create a block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host."

 

​Makes perfect sense, can't believe I didn't think of it before hand.



#6 Sikh

Sikh

    Neowin Addict!

  • Tech Issues Solved: 2
  • Joined: 11-March 07
  • Location: localhost
  • OS: Windows 7 / 10.8 / Ubuntu Server
  • Phone: Nexus 5 PA 4.4.2 / iPhone 5

Posted 12 May 2014 - 17:00

And you are correct. I based it on the assumption that you had already defined your default host. 

I usually copy the default virtual host file and then make changes to it, so my default host is always defined. Good catch thou, something for me to pay attention to next time.

 

Its easy to forget / look over things when they are always automated or done for you. Sometimes I feel like I want to make/compile every package myself so I know I need to create everything by hand instead of assuming its always there when installing it via a package manager





Click here to login or here to register to remove this ad, it's free!