将文件夹动态重定向到 vagrant vm 上的子域
dynamically redirect folders to subdomains on a vagrant vm
我已经按照 these 说明使用 vagrant 设置了一个开发环境。
这会将我的主机文件夹“./”(192.168.22.33/projectname/... 或 user/myname/sites/mysite)同步到 VM 上的假来宾文件夹:“/var/www/html”
作为命名文件夹(例如 mysite)的 LAMP 堆栈,它工作正常,但我想允许每个文件夹作为子域访问,例如foldername.ip.xip.io 映射到 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
,这很有效,但我希望它是
隐形,我知道这不是最佳做法。
- 我试过使用文档根目录。
- 我尝试在我的
install.sh
文件中编辑 vhosts
并启用
vhost_alias
- 但我无法让它工作。
- 我只能对命名文件夹执行此操作,例如在我的 vagrant 文件中列出一些 ips。我不想这样做,因为我想设置虚拟机,这样如果我创建一个新文件夹(例如通过拉 git 回购协议)它将自动工作,而不必每次都重新配置流浪汉创建了一个新的子域。
我认为 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! ---"
我已经解决了。
编辑虚拟主机设置如下,
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
我已经按照 these 说明使用 vagrant 设置了一个开发环境。
这会将我的主机文件夹“./”(192.168.22.33/projectname/... 或 user/myname/sites/mysite)同步到 VM 上的假来宾文件夹:“/var/www/html”
作为命名文件夹(例如 mysite)的 LAMP 堆栈,它工作正常,但我想允许每个文件夹作为子域访问,例如foldername.ip.xip.io 映射到 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
,这很有效,但我希望它是 隐形,我知道这不是最佳做法。 - 我试过使用文档根目录。
- 我尝试在我的
install.sh
文件中编辑vhosts
并启用vhost_alias
- 但我无法让它工作。 - 我只能对命名文件夹执行此操作,例如在我的 vagrant 文件中列出一些 ips。我不想这样做,因为我想设置虚拟机,这样如果我创建一个新文件夹(例如通过拉 git 回购协议)它将自动工作,而不必每次都重新配置流浪汉创建了一个新的子域。
我认为 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! ---"
我已经解决了。
编辑虚拟主机设置如下,
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