使用 Vagrant,无法从主机操作系统的 Web 浏览器访问 Apache 虚拟主机

使用 Vagrant,无法从主机操作系统的 Web 浏览器访问 Apache 虚拟主机

我在 Windows 7 主机上使用 Vagrant 在 Virtualbox 中安装了 Ubuntu Server 16.04.1 Xenial Xerus(32 位)。Vagrantfile 内容如下

Vagrant.configure("2") do |config|
    config.vm.box = "boxcutter/ubuntu1604-i386"
    config.vm.network "forwarded_port", guest: 80, host: 8080
    config.vm.hostname = "vagrant.templavoilaplus"
    config.vm.network "private_network", ip: "192.168.33.148"
    config.vm.synced_folder "E:/xampp/htdocs/github/templavoilaplus/", "/var/www/templavoilaplus/public_html"
    config.vm.provision "shell", path: "setup_for_ubuntu_box.sh"
end

当运行 vagrant up 时显示

E:\Virtual Machines\vagrant>vagrant up
==> default: Checking if box 'boxcutter/ubuntu1604-i386' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 (guest) => 8080 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions seems to be installed (5.1.22) correctly, but not 
runni
ng.
: Inappropriate ioctl for device
==> default: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => E:/Virtual Machines/vagrant
    default: /var/www/templavoilaplus/public_html => 
E:/xampp/htdocs/github/templavoilaplus
==> default: Machine already provisioned. Run `vagrant provision` or use the 
`--
provision`
==> default: flag to force provisioning. Provisioners marked to run always will
still run.

E:\Virtual Machines\vagrant>

Windows hosts 文件的内容是

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
216.98.48.18 127.0.0.1
216.98.48.53 127.0.0.1
216.98.48.57 127.0.0.1
216.98.48.133 127.0.0.1
216.98.48.134 127.0.0.1

#adaugat 28/5/2017 pentru vhost xampp github
127.0.0.1 localhost
127.0.0.1 github.localhost

#adaugat 2/6/2017 pentru vagrant private network
192.168.33.148 vagrant.templavoilaplus

我在虚拟机中提供了 apache2 和 php 模块(通过 shell 脚本)并创建了一个虚拟主机,方法是将 000-default.conf 复制并修改为 templavoilaplus.conf。文件内容如下

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName vagrant.templavoilaplus
    DocumentRoot /var/www/templavoilaplus/public_html
    ErrorLog ${APACHE_LOG_DIR}/templavoilaplus_error.log
    CustomLog ${APACHE_LOG_DIR}/templavoilaplus_access.log combined
</VirtualHost>

在 public_html 文件夹中挂载了主机的一个文件夹。之后我运行了 a2ensite 并重新加载了 apache。/etc/host 的内容如下

127.0.0.1    vagrant
127.0.0.1    localhost
127.0.0.1    vagrant
::1    localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters

##vagrant-hostmanager-start
192.168.33.148 vagrant.templavoilaplus

##vagrant-hostmanager-end

如果我在主机中打开浏览器并输入 192.168.33.148,我将获得 Apache2 Ubuntu 默认页面(成功)。此外,如果我在主机中 ping

ping vagrant.templavoilaplus

结果是

Ping statistics for 192.168.33.148:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 2ms, Average = 2ms

如果我 ping 访客(通过 putty)

ping vagrant.templavoilaplus

它显示类似的内容(192.168.33.148)。

问题是这样的:如果我在主机的浏览器地址栏中输入

vagrant.templavoilaplus

我得到的是空白页(mozilla)或 google 搜索结果(chrome)。页面 index.php(虚拟主机重定向后)应显示“成功”。

答案1

我发现问题在于我同时使用了端口转发和私有网络。我坚持使用端口转发。

相关内容