在 Vagrant Server 中创建虚拟主机

Create Virtual Hosts in Vagrant Server

我是 运行 带有 vagrant 的 Apache 服务器,我想从主机访问多个网站。当前设置如下所示:

Vagrantfile:

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/trusty64"
    config.ssh.forward_agent = true
    config.vm.network "forwarded_port", guest: 80, host: 80, host_ip: "127.0.0.1", id: 'apache_http'
    config.vm.network "forwarded_port", guest: 443, host: 443, host_ip: "127.0.0.1", id: 'apache_https'
    config.vm.network "forwarded_port", guest: 22, host: 22, host_ip: "127.0.0.1", id: 'ssh'
    config.vm.network "private_network", ip: "192.168.33.10"
    config.vm.synced_folder ".", "/var/www/"
end

/etc/hosts

127.0.0.1 localhost
192.168.33.10 website.com
192.168.33.10 a.website.com
192.168.33.10 b.website.com

/etc/apache2/sites-available/website.conf

<VirtualHost *:80>
    ServerAdmin example@gmail.com
    ServerName website.com
    ServerAlias www.website.com
    DocumentRoot /var/www/website/public_html
    ErrorLog /var/www/website/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost _default_:443>
    ServerName website.com
    ServerAlias www.website.com
    DocumentRoot /var/www/website/public_html
    ErrorLog /var/www/website/error.log
    SSLEngine on
    SSLCertificateKeyFile /etc/apache2/ssl/website.key
    SSLCertificateFile /etc/apache2/ssl/website.cert
</VirtualHost>

我可以通过以下方式从 vagrant box 中访问该网站:curl https://website.com -k。我怎样才能将它暴露给主机?

步骤正确。

  1. 在您的 Vagrantfile 中,如果您使用静态 IP,则无需添加转发端口,因为您将直接使用该 IP 访问您的VM/guest

  2. 您需要从主机更新主机文件。在 windows 上,文件位于 C:\Windows\System32\drivers\etc

您现在可以直接从主机

访问 https://website.com