Apache-本地主机的域,用于以http://folder.local访问文件夹 [英] Apache - Domain for localhost to access folders as http://folder.local

查看:89
本文介绍了Apache-本地主机的域,用于以http://folder.local访问文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上运行XAMPP,我想为我的项目创建一个虚拟主机,以便将一个tld分配给我的服务器根目录(例如.local),并且在其中的文件夹可以通过URL进行访问,例如http://foldername.local.

I'm running XAMPP on Ubuntu and I'd like to create a virtual host for my projects, so that I have a tld assigned to my server root directory (for example .local) and folders inside it accessible through URLs as http://foldername.local.

而且,使用.htaccess来将http://someotherdomain.local重定向到服务器根目录中的/foldername路径要复杂得多吗?

Also, how much more complicated would it be to use .htaccess to have http://someotherdomain.local redirect to the /foldername path in the server root?

推荐答案

我已经设法自己完成了.可以这样做,但是您需要安装 DNS服务器.

I've managed to do it on my own. It is possible to do it, however you'll need to install a DNS server.

注意:我决定将.dev用作本地域,因此在下面 例如,dev部分将引用我选择的域.保持在

Note: I decided to use .dev as my local domain, so in the following examples, the dev part will refer to my chosen domain. Keep that in mind.

安装和配置DNS服务器

这是哪一个无关紧要,但是您需要知道如何正确配置它.配置取决于您选择的DNS服务器.我去了 dnsmasq .它很轻巧,非常方便.

Install and configure DNS Server

It shouldn't matter which one it is, but you'll need to know how to configure it properly. The configuration depends on which DNS server you chose. I went for dnsmasq. It's lightweight and very handy.

对于 Ubuntu 用户的重要说明是,从Ubuntu 11.10开始, 已经安装了一个名为 dnsmasq-base 的轻量级版本,它将 导致在安装过程中发生冲突.我不会在这里解释如何 绕开这个问题,因为其他地方有很多说明.

An important note for Ubuntu users is that since Ubuntu 11.10 there is already a light version called dnsmasq-base installed, which will cause conflicts during installation. I won't be explaining here how to get around this, because there are many instructions available elsewhere.

一旦安装了DNS服务器,则应将其配置为侦听等于所需域的地址.

Once you have your DNS server installed, you should configure it to listen for the address equal to your desired domain.

在我使用 dnsmasq 的情况下,这意味着打开/etc/dnsmasq.conf并 将第62行更改为此:address=/dev/127.0.1.1

In my case with dnsmasq, that meant opening /etc/dnsmasq.conf and changing line #62 to this: address=/dev/127.0.1.1

配置Web服务器

假设您已经安装了某种服务器软件,则需要进行一些调整.

Configure Web server

Assuming that you already have some kind of Server software installed, you need to make a few tweaks.

首先,您应该编辑hosts文件,以将所需的域映射到本地主机.

First, you should edit your hosts file to map your desired domain to your localhost.

对于在Ubuntu上的Linux XAMPP,这意味着我打开了 /etc/hosts和更改的行

in my case of XAMPP for Linux on Ubuntu, this means I opened /etc/hosts and changed lines

127.0.0.1 localhost
127.0.1.1 tomica-ubuntu

127.0.0.1 localhost
127.0.1.1 tomica-ubuntu dev

这会将http://dev重定向到我的本地服务器.

This will redirect http://dev to my local server.

接下来,使用几个特定选项创建一个新的虚拟主机,如下所示:

Next, create a new virtual host with a couple of specific options, like this:

就我而言,这意味着打开 /opt/lampp/etc/extra/httpd-vhosts.conf并将其添加到 文件:

In my case, that means opening /opt/lampp/etc/extra/httpd-vhosts.conf and adding this at the end of the file:

<VirtualHost *:80>
    DocumentRoot "/opt/lampp/htdocs/dev"
    ServerName dev
    ServerAlias *.dev

    <Directory /opt/lampp/htdocs/dev>
        AllowOverride All
    </Directory>
</VirtualHost>

为了简洁起见,我不会解释这段代码,因为 文档也可用.

For the sake of brevity, I won't explain this piece of code, since documentation is also available.

完成所有操作后,启动 DNS和Web服务器,或者如果它们已经在运行,请重新启动.

After all this is done, start your DNS and Web servers, or restart them if they're already running.

打开新创建的主机的根文件夹.那就是您的中定义的文件夹.就我而言,这是/opt/lampp/htdocs/dev.在其中创建一个.htaccess文件,并将其放入其中:

Open root folder of your newly created host. That's the folder devined in your . In my case, that's /opt/lampp/htdocs/dev. In there, create a .htaccess file and put this in it:

# Specify order of index files; if none exist, show files list
  DirectoryIndex index.php index.html

# Interpret .html files as .php scripts
  AddHandler php5-script .php .html

# THE MAGIC - Redirect subdomains of .dev to their respective folders
  RewriteEngine on
  Options +FollowSymlinks
  RewriteBase /

  RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.dev(.*)?$ [NC]
  RewriteRule !^%2\.dev%3?/$ http://dev/%2%{REQUEST_URI}/ [P]

再次,解释所有这些将需要太多的空间和时间.只需复制/粘贴就可以了,不必担心:)但是不要忘记将我的dev更改为您为域名选择的任何内容.

Again, explaining all this would require too much space and time. Just copy/paste and don't worry :) But don't forget to change my dev to anything you chose for your domain name.

这就是它! 到现在,您应该能够使用http://folder.dev/http://www.folder.devhttp://folder.dev/file.htmlhttp://folder.dev/subfolder/document.txt等地址浏览项目了

AND THAT'S IT! By now you should be able to browse your project using addresses like http://folder.dev/, http://www.folder.dev, http://folder.dev/file.html, http://folder.dev/subfolder/document.txt etc.

作为奖励,我将再添加一个建议.我这样做的原因是为了更轻松地开发Laravel和WordPress项目.但是,对于Laravel,您应该将URL http://lvproject.dev/重定向到/lvproject/public的位置.这是.htaccess文件,可以启用该功能.打开您的/lvproject文件夹,创建一个.htaccess文件并将以下代码放入其中:

As a bonus, I will add just one more advice. The reason why I did all this is so that I could more easily develop my Laravel and WordPress prjects. However, with Laravel, you should redirect the url http://lvproject.dev/ to the location of /lvproject/public. And here is the .htaccess file that enables just that. Open your /lvproject folder, create a .htaccess file and place this code in it:

RewriteBase /lvproject/

RewriteCond %{REQUEST_URI} lvproject/index\.php [NC]
RewriteRule index\.php(.*)$ public/ [L]

该解决方案的两个缺点是:1)需要为每个新项目重新设置RewriteBase规则(即,您需要在每个新项目中手动创建.htaccess); 2)您的项目可以从http://lvproject.dev/http://lvproject.dev/public/进行访问,这并不酷,但是我现在太懒了,无法解决它:)

Two drawbacks of this solution are: 1) RewriteBase rule needs to be set anew for every new project (i.e. you need to manually create .htaccess in each new project); 2) Your project will be available from both http://lvproject.dev/ and http://lvproject.dev/public/, which is not cool, but I'm too lazy at the moment to get it fixed :)

这篇关于Apache-本地主机的域,用于以http://folder.local访问文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆