4
votes

I've been struggling trying to connect to a centos 6.4 vm using Vagrant. I'm using salt as a provisioning agent and I have installed apache,php,mysql packages successfully. When i ssh into the box apache is running fine. I added an index.html file in /var/www and I get the contents back when I curl localhost:80

Vagrant.configure("2") do |config|
  ## Chose your base box
  config.vm.box = "centos63"
  config.vm.box_url = ""

  ## For masterless, mount your salt file root
  config.vm.synced_folder "salt/roots/", "/srv/"


  ## Use all the defaults:
  config.vm.provision :salt do |salt|
    salt.verbose = true
    salt.run_highstate = true
    salt.minion_config = "salt/minion"
 end
end

Vagrant::Config.run do |config|
 config.vm.forward_port 80, 8080
 config.vm.share_folder "mypath", "/var/www/leo", "."
end

I ran sudo lsof -i :8080 on my local machine and gave me no results. I also cleared the iptable config in the guest machine with iptables -F. When I curl the guest machine

 curl -v 'localhost:8080'                            
* About to connect() to localhost port 8080 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connection refused
*   Trying fe80::1...
* Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

Do I need guest additions installed? I looked around on how to install this but I'm not sure if it has to be installed on the host or the guest. Not sure what else to try.

2

2 Answers

3
votes

What you are trying to do here isn't possible just using vagrant without running vagrant as root. You can run Vagrant as root i believe but VirtualBox won't agree with that. You can continue to use a port number or if you want or need to use port 80 there is a way.

I had this issue when a client of mine asked me to do a Wordpress Multisite setup. With Wordpress MS you can't have port numbers in the URL b/c some of the URL mapping will not work correctly. I was surprised when I found that out and didn't want to go back to using a program like MAMP.

Anyway here are two ways to achieve this goal (neither are very hard). I'm a Mac user so these are Mac specific Answers, I will see if there is a Windows version and update my answer when I can test it to make sure (see below, there is a way).

Way #1 (Mac IP Firewall Utility):

In your vagrant file

config.vm.forward_port 80, 8080
config.vm.forward_port 443, 8443

Thats pretty normal.

Now open up terminal and you can use the ipfw utility

sudo ipfw add 100 fwd 127.0.0.1,8080 tcp from any to me 80
sudo ipfw add 101 fwd 127.0.0.1,8443 tcp from any to me 443

Now that cmd is not permanent so you would have to re-run the cmd if you restarted your machine. You can make it permanent though and I am including a link below that will explain the rest of way #1.

Web Development on Port 80 and 443 in Vagrant

Way #2 (Mac POW and Anvil):

If you don't have Pow yet, get it! It's a really cool app. Install Pow and Anvil, you can find Anvil there and you can find Pow there.

You can read the docs on how to set those up but don't pay attention to the "static" and "rack" sites part, you need this part.

You will be using Port Proxying through Pow to take the incoming traffic from mycoolsite.dev and forward it to the virtual machine like mycoolsite.dev:8080 and then the virtual machine will forward 8080 to 80 and back up the line your content will come.

After you install Anvil/Pow and get them set up run this line:

echo 8080 > ~/.pow/mycoolsite

Then click Anvil in the task bar (you may have to refresh it or close and reopen) and turn the site on, thats it, what?? Really? Pow and Anvil rock!!

So there are two ways I have found, I'm sure that there are some things you can do with your Hosts file and I used to do that a bunch. However, these other ways that are available really make it easy to forget about that pesky hosts file.

Note for Windows Users (and Mac users that don't like the first 2 ways): You can use Vagrant Host Manager, you can find out how to set it up here on github. It is a vagrant plugin and basically will edit your hosts file for you, all you do is your vagrantfile config and you are good to go after that. I just tested it on Windows 7 and it worked there so it should be good, if you have any issues just look through the docs on github or file an issue for the Vagrant Host Manager team to review.

0
votes

I changed the guest port to 5656 and it worked. When running lsof -i :8080 I didn't see any results so i figured nothing was using that port maybe I was wrong.