Sign in to follow this  
Followers 0
game_over

VirtualHosts (apache) on CentOS issues

6 posts in this topic

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Do you have a entry for 127.0.0.1 localhost

Share this post


Link to post
Share on other sites

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.

3 people like this

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.