动态地将文件夹重定向到vagrant vm上的子域

时间:2016-03-01 16:21:32

标签: apache .htaccess vagrant development-environment vhosts

我按照these说明使用vagrant建立了一个开发环境。

这会将我的主机文件夹'./'(192.168.22.33/projectname / ...或user / myname / sites / mysite)同步到虚拟机上的假访客文件夹:“/ var / www / html”

它作为命名文件夹(例如mysite)的LAMP堆栈工作正常,但我想允许每个文件夹作为子域进行访问,例如: for foldername.ip.xip.io to map to ip / foldername

我希望能够在'sites'中创建一个新文件夹,然后访问:

mysite.local.dev or mysite.192.168.22.33 accessing /user/myname/sites/mysite
mysite2.local.dev or mysite2.192.168.22.33 accessing the mysite2 folder.
  • 我尝试过使用.htaccess,但是我很喜欢它 看不见,我明白这不是最佳做法。
  • 我尝试过使用文档根目录。
  • 我尝试在vhosts文件中编辑install.sh并启用 vhost_alias - 但我无法让它发挥作用。
  • 我只能为命名文件夹执行此操作,例如在我的vagrant文​​件中列出了一些ips。我不想这样做,因为我想设置vm,如果我创建一个新文件夹(例如通过拉一个git repo)它将自动工作,而不必每次创建一个新的子域时重新配置vagrant

我认为vhosts必须是正确的方法,但我不能让它工作 - 我在下面错过了什么?

谢谢!

我的vagrantfile包含:

    # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
        config.vm.box = "precise32"

        config.vm.box_url = "http://files.vagrantup.com/precise32.box"

        config.vm.network :forwarded_port, guest: 80, host: 8080 
        config.vm.network :forwarded_port, guest: 443, host: 8443 

        config.vm.provision :shell, :path => "install.sh"

        config.vm.synced_folder ".", "/vagrant", :mount_options => ["dmode=777", "fmode=666"]
        #config.vm.synced_folder ".", "/var/www", :mount_options => ["dmode=777", "fmode=666"]

        # If true, then any SSH connections made will enable agent forwarding.
        # Default value: false
        # config.ssh.forward_agent = true

        # Share an additional folder to the guest VM. The first argument is
        # the path on the host to the actual folder. The second argument is
        # the path on the guest to mount the folder. And the optional third
        # argument is a set of non-required options.
         config.vm.synced_folder "./myproject", "/var/www/html"
    end

我的install.sh文件包含:

#!/usr/bin/env bash

echo "--- Installing now. ---"

echo "--- Fixing the TTY STDIN error ---"
sed -i 's/^mesg n$/tty -s \&\& mesg n/g' /root/.profile

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- MySQL time ---"
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'

echo "--- Installing base packages ---"
sudo apt-get install -y vim curl python-software-properties

echo "--- Installing PHP5 ---"
sudo add-apt-repository -y ppa:ondrej/php5

echo "--- Updating packages list ---"
sudo apt-get update

echo "--- Installing PHP-specific packages ---"
sudo apt-get install -y php5 apache2 libapache2-mod-php5 php5-curl php5-gd php5-mcrypt mysql-server-5.5 php5-mysql git-core

echo "--- Installing and configuring Xdebug ---"
sudo apt-get install -y php5-xdebug

cat << EOF | sudo tee -a /etc/php5/mods-available/xdebug.ini
xdebug.scream=1
xdebug.cli_color=1
xdebug.show_local_vars=1
EOF


# echo "--- Configuring Hosts ---"
# sudo a2enmod vhost_alias
# # setup hosts file
# VHOST=$(cat <<EOF
# <VirtualHost *:80>
#   ServerName vhosts.fqdn
#   ServerAlias *.local.dev
#   VirtualDocumentRoot /var/www/%1+
# </VirtualHost>
# EOF
# )
# echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf

echo "--- Enabling mod-rewrite ---"
sudo a2enmod rewrite

echo "--- Setting document root ---"
#sudo mkdir "/var/www"
sudo rm -rf /var/www/html
sudo ln -fs /vagrant/public/ /var/www/html
#sed -i "s#DocumentRoot /var/www/html#DocumentRoot /var/www#g" /etc/apache2/sites-available/000-default.conf

echo "--- Turn errors on ---"
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php5/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php5/apache2/php.ini

sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

echo "--- Set php short open tags to on. ---"
sed -i "s/short_open_tag = .*/short_open_tag = On/" /etc/php5/apache2/php.ini

echo "--- Restarting Apache ---"
sudo service apache2 restart

echo "--- Set up composer ---"
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Framework stuff here

echo "--- All set to go! ---"

1 个答案:

答案 0 :(得分:0)

我已经解决了。

编辑vhosts设置如下,

 echo "--- Configuring Hosts ---"
 #Must be enabled to edit vhosts.
 sudo a2enmod vhost_alias

echo "---Set up home.dev and foldername.dev ---"
    # The first virtualhost redirects home.dev local.dev and localhost.dev to the root dir
    #. The second redirects foldername.dev to site/foldername.
    # %-2 refers to the second parameter from the right of the string.

 # setup hosts file
VHOST=$(cat <<EOF
UseCanonicalName Off
<VirtualHost *:80>
    ServerAlias home.dev local.dev localhost.dev
    VirtualDocumentRoot /var/www/html/
</VirtualHost>

<VirtualHost *:80>
    ServerAlias *.dev
    VirtualDocumentRoot /var/www/html/%-2
</VirtualHost>

EOF
)
echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf


#The below is no longer necessary - commented out!
#echo "--- Enabling mod-rewrite ---"
#sudo a2enmod rewrite

#echo "--- Setting document root ---"
#sudo mkdir "/var/www"
#sudo rm -rf /var/www/html
#sudo ln -fs /vagrant/public/ /var/www/html
#sed -i "s#DocumentRoot /var/www/html#DocumentRoot /var/www#g" /etc/apache2/sites-available/000-default.conf