在Vagrant中使用简单的LAMP实现多个虚拟主机

6
我正在尝试通过Vagrant创建一个简单的LAMP,该LAMP可以支持存储在/var/www中的多个网站。这可以通过虚拟主机(vhosts)完成。
每个项目应以.dev结尾。
不希望为每个项目创建一个支持数据库的构建版本。
我无法理解Puppet或Chef。我宁愿避免使用它们,但很乐意从repo克隆。
我需要一些建议或指引。
谢谢。
3个回答

15


Vagrant LAMP 多个虚拟主机 - 更新!


请查看下面的更新信息

我一直在寻找提高我的 Web 开发工作流程的方法。目前,我正在使用各种工具和应用程序,如 LAMP、Webmin 和 Filezilla 等来进行主题项目等工作。然后我发现了这个神奇的工具——Vagrant,在测试了它与我的项目之后,我爱上了它,并决定将其整合到当前的本地 Web 开发环境中。因此,我进一步挖掘并寻找可能性,以使用我选择的基础镜像 Ubuntu 12.04 Precise Pangolin 32-bit 来创建和设置一个使用Vagrant的多个虚拟主机设置。在 Google 上搜索返回了许多结果,但我决定单击来自 stackoverflow 的链接,并在此问题中得到解答。我遵循了 Dimitri Kouvdis 回答中提到的链接,这是一个 Github 仓库。我已经测试过了,并遇到了与 Dimitri Kouvdis 遇到的非常相似的问题。但现在已经解决了,感谢他的评论,我让它工作了。但是在寻找我的理想具有多个虚拟支持的 Vagrant Box的过程中,我从 Github 上搜索和测试了几个仓库,直到找到适合我的一个。之所以追寻这些仓库,是因为我还不知道如何使用 Puppet 和 Chef 来提供自己的 Dev 服务器。

我找到了另一个适合我的 Github 仓库

感谢这个仓库

onema / vagrant-lamp-development

https://github.com/onema/vagrant-lamp-development

在测试 Dimitri Kouvdis 提到的 Github 仓库时,我遇到了几个问题,并且在那段时间内停止了工作,决定从 Github 中寻找类似的仓库。我发现了几个并测试了其中一些,包括 Nino Paolo 的仓库(https://github.com/paolooo/vagrant-lamp)。我再次遇到了几个问题,尤其是在 vagrant up 期间。我决定再次寻找另一个,直到我找到并成功设置了 onema 的仓库。我最终决定使用并将这个解决方案整合到我的 Web 开发实践环境中。我开始爱上 onema 的仓库,因为;

  • 这个软件很容易设置,因为指南易于遵循,特别是对于像我这样的新手和自学者。
  • 它有几个文档和示例设置。
  • 最重要的是,与我测试过的其他软件相比,添加新的虚拟主机非常容易。
  • 您可以选择并设置本地驱动器中任何文件夹作为源站点/项目文件夹。
  • 它只是简单的,没有不必要的附加组件(如node.js,less等)。我只需要LAMP堆栈,加上vhost支持和phpmyadmin。

如何设置 - 对于初学者和像我这样的自学者

正如我之前提到的那样,通过阅读并按照在其Github Repo页面上找到的说明进行设置(特别是对于新手和像我这样的自学者)非常容易。- https://github.com/onema/vagrant-lamp-development。您可以选择使用下载的zip文件或选择使用git clone https://github.com/onema/vagrant-lamp-developmentVagrant文件复制到本地驱动器。

这就是我如何设置它(在我的*nix机器Ubuntu 12.04 x64中);

  1. Download the files to your local-drive, either by using the downloaded zip files or git and do the basic Vagrant and VirtualBox setup. Read instruction from the Github Page, plus do the below steps.


  2. With my setup, I've edited the Vagrantile a bit. In line: 70 of the orig Vagranfle file, I disabled NFS by putting a comment # to

    config.vm.synced_folder "~/Sites", "/vagrant", nfs: true


    so, it now looks like this...

    #config.vm.synced_folder "~/Sites", "/vagrant", nfs: true

    and then commenting out...

    #config.vm.synced_folder "~/Sites", "/vagrant"

    line: 140 from the orig Vagrantfile

    so, it now looks like this...

    config.vm.synced_folder "~/Sites", "/vagrant"

    I am doing this because in my machine, during vagrant up, it throws several NFS related errors that I do not understand.

  3. Now, for the sake of this example, and from the example from it's Repo Page, create a folder and you must name it Sites. This should be located in your User Account's folder root or your Home Folder.

    /home/your-user-account-home-folder/Sites

    In my case, (take note of that capital S)

    /home/gary/Sites

    • A.

      Create your sample dev-site/vhost and create a new folder named wordpress.dev and make this as the document root of your vhost wordpress.dev Then add your project files here. See example below;

      /home/gary/Sites/wordpress.dev



    • B.

      Now, reate a simple splash page for your newly created vhost so you can confirm that your configuration is redirecting you to the destination vhost root folder, when you visit the dev site from your browser. Of course, when your configurations are correct, you shall see the splash page. This is how I made my splash page.

      I created an index.php file and put some codes inside it,like so;

      <?php echo "Success!!! Your wordpress.dev looks fine"; ?>

      So when you successfully reached your vhost, you'll be greeted by the message Success!!! Your wordpress.dev looks fine



  4. Now, add your Vagrant Box's IP Address to your hosts file and mapping your dev site wordpress.dev to it, like so;

    192.168.50.4 wordpress.dev

    Note: 192.168.50.4 is the default configured IP Address of the Vagrant Box, you may change this to your liking and update your hosts file.

    In my case, I put it like so,

    • A

      10.10.10.10 wordpress.dev - in my hosts file, while

    • B

      config.vm.network "private_network", ip: "10.10.10.10"

      in my Vagrantfile.

      You can find this settings by looking inside your Vagrantfile and navigating just below this line

      # Host-Only networking required for nfs shares

      Then change the ip: settings from there and update your hosts file to match the IP.


  5. Edit your Vagrantfile again and add your vhost settings, to point to your dev site folder, add these code block like so;

    :wordpress => {
       :name => "wordpress",
       :host => "wordpress.dev", 
       :aliases => ["wordpress.dev"],
       :docroot => "/wordpress.dev"
    }
    



    When you added the code to the orig Vagrantfile, it should look like this;

     :vhost => {
        :localhost => {
        :name => "localhost",
        :host => "localhost", 
        :aliases => ["localhost.web", "dev.localhost-static.web"],
        :docroot => ""
      },
        :wordpress => {
        :name => "wordpress",
        :host => "wordpress.dev", 
        :aliases => ["wordpress.dev"],
        :docroot => "/wordpress.dev"
      }
    


    So your Vagrantfile shall now look like this one below; take note on the comma , right above the w of the :wordpress and right of closing curly brace }.

    There should be a comma in there, when you add another vhost, you should add another comma to the right of the closing curly brace } of wordpress.

      Vagrant.configure("2") do |config|
      config.vm.box = "precise32"
      config.vm.box_url = "http://files.vagrantup.com/precise32.box"
      config.vm.host_name = "localhost"
      config.vm.provision "chef_solo"  do |chef|
        chef.cookbooks_path = "cookbooks"    
        chef.add_recipe "vagrant_main"
        #####################################
        # MONGODB
        # https://github.com/edelight/chef-cookbooks
        #####################################
        chef.add_recipe "mongodb::10gen_repo"
        chef.add_recipe "mongodb::default"
        #####################################
        # REDIS
        # https://github.com/phlipper/chef-redis
        #####################################
        chef.add_recipe "redis"
        chef.json.merge!({
          :mysql => {
            :server_root_password => "root",
            :server_debian_password => "root",
            :server_repl_password => "root"
          },
          #####################################
          # YOU WILL NEED TO ADD THESE DOMAINS 
          # TO THE LIST OF HOSTS IN YOUR LOCAL 
          # ENVIRONMENT FOR THESE TO BE PROPERLY 
          # ROUTED
          #####################################
          :vhost => {
            :localhost => {
                :name => "localhost",
                :host => "localhost", 
                :aliases => ["localhost.web", "dev.localhost-static.web"],
                :docroot => ""
            },
            :symfony => {
                    :name => "symfony",
                    :host => "symfony.web", 
                    :aliases => ["symfony"],
                    :docroot => "/symfony/web"
            },
            :wordpress => {
                :name => "wordpress",
                :host => "wordpress.dev", 
                :aliases => ["wordpress"],
                :docroot => "/wordpress.dev"
            }
          }
        })
      end
      config.vm.network "forwarded_port", guest: 80, host: 8080
      config.vm.network "forwarded_port", guest: 3306, host: 3307
    
      ##########################################################################
      # UNCOMMENT IF NFS IS DISABLED
      ##########################################################################
      config.vm.synced_folder "~/Sites", "/vagrant"
    
      ##########################################################################
      # NFS 
      # Enable if you have performance issues with large projects. 
      # see the following links for more info:
      # http://forum.symfony-project.org/viewtopic.php?t=52241&p=167041#p147056
      # http://docs.vagrantup.com/v2/synced-folders/nfs.html
      # http://www.phase2technology.com/blog/vagrant-and-nfs/
      ###########################################################################
      # Host-Only networking required for nfs shares
      config.vm.network "private_network", ip: "10.10.10.10"
      #config.vm.synced_folder "~/Sites", "/vagrant", nfs: true
    
    
      config.vm.provider :virtualbox do |vb|
        #   # Don't boot with headless mode
        #   vb.gui = true
        #
        #   # Use VBoxManage to customize the VM. For example to change memory:
        vb.customize ["modifyvm", :id, "--memory", "512"]
      end
    end
    
  6. You are now ready to start your Vagrant Box, open your terminal window

    ctrl + alt + tand type in

    cd vagrant-lamp-development to change directory to vagrant-lamp-development folder and type in

    vagrant up

    so you can start the Vagrant Box VM and then wait for it to boot-up completely. It will take several mintues for the FIRST INITIAL BOOT UP. Upon successful bootup, open up your browser and navigate to

    http://wordrpress.dev

    You should see a web splash message, that you made earlier in Step 3 B.

  7. To Add New Vhosts, just create a new folder inside /home/your-home-folder/Sites/new-project-folder and then...

    • Repeat Steps 3B, 4A, 5

    • If you made the changes or the addition of Vhosts to your Vagrantfle while the VM Box is Running, type..

      vagrant provision

      in your terminal to make the changes take effect.

    • If you made the changes or the addition of vhosts to your Vagrantfile while the VM Box is OFF, type...

      vagrant up --provision

      in your terminal to make the changes take effect.

      NOTE: The Vagrantfile used in this guide will download a 64-bit Precise Pangolin Basebox Base Box, you could change it to a 32-bit Precise Pangolin Basebox by changing the config from the Vagrantfile.

      I did this in my case, because I already downloaded my 32-bit base box. So I changed it to 32-bit, so I don't have to download again.

      Change...

      Vagrant.configure("2") do |config|
           config.vm.box = "precise64"
           config.vm.box_url = "http://files.vagrantup.com/precise64.box"
      


      To

      Vagrant.configure("2") do |config|
              config.vm.box = "precise32"
              config.vm.box_url = "http://files.vagrantup.com/precise32.box"
      

      DONE!


我测试过的其他可用的Vagrant LAMP存储库 - 但不一定支持多个虚拟主机

其他Vagrant LAMP存储库

如果您想使用R8的Vagrant-LAMP存储库 - 用于多个虚拟主机目的

r8/vagrant-lamp

以下是如何添加新的vhosts

  1. Create a new .json file matching your desired dev-site name, ex: wordpress.dev in
    vagrant-lamp/data_bags/sites

    So it shall look like...
    vagrant-lamp/data_bags/sites/wordpress.json

  2. Edit the newly created file wordpress.json and add the following (for example only);

    {
        "id": "wordpress",
        "host": "wordpress.dev",
        "aliases": [
            "www.wordpress.dev"
        ]
    }
    
  3. Create a new folder named wordpress.dev inside vagrant-lamp/public
    So it will look like...
    vagrant-lamp/public/wordpress.dev

  4. Edit your hosts file to add and map 192.168.33.10``wordpress.dev
    So it shall look like this... 192.168.33.10 wordpress.dev

  5. And you're good to go... Fire up vagrant and type vagrant up --provision, if the box is turned off
    If the box is turned on when you did the procedure, then type vagrant provision instead.

  6. After this, fire up your browser and test your config, browse to http://wordpress.dev.
    Make sure you put some index files in there.

更新:从Github添加了新的存储库。

各位,我的Linux Mint 13(基于Ubuntu 12.04)开发机坏了。这就是为什么我不得不重新安装我的操作系统(我的错...使用Fake RAID并且没有备份),但这一次,我使用了Linux Mint 17,它是基于Ubuntu 14.0.4 LTS的。

所以我从头开始重新安装了所有东西,并快速设置了我的Web开发环境。但结果发现,我的之前与vagrant配合使用的工作环境已经不再起作用了。因此,我搜索了如何设置另一个Web开发环境的解决方案,以使其能够与Ubuntu 14.04 LTS兼容。幸运的是,我找到了一个可行的解决方案,使用了另一个名为CPT Server的Github存储库。

设置

以下是设置步骤:

  • 克隆该存储库:cptserver
    或下载zip文件
  • 浏览到您克隆或下载存储库的位置(解压缩zip文件)
  • 打开config/config.yaml并根据需要进行自定义。在我的情况下,我没有修改任何默认配置,只是添加了我的虚拟主机。
  • 现在从终端中,浏览到您下载或克隆存储库的位置并运行vagrant up

就是这样!第一次启动需要很长时间。

添加新虚拟主机非常容易

只需编辑config/config.yaml并转到文件的最底部。查找vhost:行并像这样插入新的虚拟主机:

 ServerName: mydevsite.dev
    ServerAlias: mydevsite.dev
    DocumentRoot: /var/www/mydevsite.dev
    ServerAdmin: webmaster@localhost

接下来在 www 文件夹内创建一个新文件夹,并将其命名为与 ServerName 或 DocumentRoot 文件夹相匹配的名称 mydevsite.dev

如果在 Vagrant 运行时添加了一个新的主机,则执行以下操作:vagrant provision 如果在 Vagrant box 运行时修改了 config.yaml 的上部分,请执行以下操作:vagrant reload --provision

不要忘记更新主机机器的 hosts 文件,将您的虚拟主机指向 Vagrant 的 IP。

这就是全部内容。


嗨,我看了你的帖子,虽然有些难以理解。我注意到你遇到了NFS错误,NFS对性能至关重要。这是Virtualbox 4.3+的一个bug,你应该在执行vagrant up之前运行sudo launchctl load /Library/LaunchDaemons/org.virtualbox.startup.plist。你的方法比我的复杂得多。我稍后会编辑我的帖子。 - Dimitri Kouvdis
1
其实很简单,我只是用一步一步的过程解释了一下。我的帖子真正想针对的是像我这样的新手和自学者。在等待您的评论时,我测试了几个存储库,并且在r8存储库中遇到了几个问题。而在onema存储库中则没有任何问题。使用onema存储库添加额外的虚拟主机只需要编辑Vagrantfilehosts文件即可。总之,我只是为了我的新手同伴们写了这篇文章。 - GaryP
@ErdemEce,请看这里。https://github.com/mitchellh/vagrant/issues/1673。Mitchellh提到:“是的,这是一个已知的‘问题’。它并不是真正的问题,因为它并不会导致任何问题。实际上,这个错误消息出现的原因是Ubuntu中的一个bug,它没有检查stdin是否是TTY,而只是假设它是。” - GaryP
这可以使用hostmanager完成吗? - Jan Beck
我建议你尝试一下并分享你的结果。;-) - GaryP
显示剩余2条评论

0

你也可以使用这个:

https://github.com/paolooo/vagrant-lamp

你可以去看看。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接