简体   繁体   English

是否有cmd提示快速更改httpd.conf DocumentRoot目录

[英]Are there cmd prompts to quickly change httpd.conf DocumentRoot directories

I have multiple websites in folders that I need to switch back and forth too constantly. 我在文件夹中有多个网站,我需要来回切换。 Each time I have to go to the httpd.conf file to find them and change DocumentRoot and Directory lines. 每次我都要去httpd.conf文件找到它们并更改DocumentRootDirectory行。 I was wondering if there is a faster way to do this, maybe with a cmd prompt? 我想知道是否有更快的方法来执行此操作,可能是使用cmd提示符? Or maybe a php inc file I can create. 或者也许我可以创建一个php inc文件。

Thanks. 谢谢。

You would be much better off setting all your sites up as Virtual Hosts . 将所有网站设置为Virtual Hosts会更好。 They are always available and you dont have to change anything other than the url to access each individually. 它们总是可用的,您不必更改除URL之外的任何内容来单独访问每个URL。

HowTo: Create Virtual Hosts in WAMP HowTo:在WAMP中创建虚拟主机

BEFORE DOING ANY OF THIS PLEASE ENSURE APACHE AND MYSQL ARE WORKING PROPERLY FIRST!!! 在做任何事情之前,请确保APACHE和MYSQL正常工作!

Create a new folder outside the wamp directory structure. 在wamp目录结构之外创建一个新文件夹。 For example 例如

C:\websites\www

but this can be on any disk drive visible to the PC running wamp 但这可以在运行wamp的PC可见的任何磁盘驱动器上

Create a subfolder in c:\\websites for each site you want to create. 在c:\\ sites中为要创建的每个站点创建一个子文件夹。 for example: 例如:

C:\websites\www\site1
C:\websites\www\site2

Edit the file C:\\wamp\\bin\\apache\\apachex.yz\\conf\\extra\\httpd-vhosts.conf where x,y and z are the version numbers of apache that you actually have installed. 编辑文件C:\\ wamp \\ bin \\ apache \\ apachex.yz \\ conf \\ extra \\ httpd-vhosts.conf其中x,y和z是您实际安装的apache的版本号。

NOTE: If you are switching between 2 or more versions of apache this will have to be done to all your versions of apache in turn. 注意:如果要在两个或更多版本的apache之间切换,则必须依次对所有版本的apache进行此操作。

SUGGESTION: I like to use the format sitename.dev to make it obvious to me that I am dealing with my localhost development copy of a site, you may prefer another notation, thats ok, the word dev has no actual defined meaning in this case, its just my way of naming my development versions of a live site. 建议:我喜欢使用格式sitename.dev让我明白我正在处理我的网站的localhost开发副本,你可能更喜欢另一种表示法,没关系,在这种情况下,单词dev没有实际定义的含义,它只是我命名我的开发版本的实时网站的方式。

NOTE: Remove or better still comment out ( using the # in column 1 ) the lines that already exists in this file. 注意:删除或更好地注释掉(使用第1列中的#)此文件中已存在的行。 They are just examples. 它们只是一个例子。

example contents: 示例内容:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

## must be first so the the wamp menu page loads when you use just localhost as the domain name
## Also NEVER change the security to anything other than Allow from 127.0.0.1 localhost ::1
## Then a drive by Ip address hack should return a 403 denied access
<VirtualHost *:80>
   DocumentRoot "C:/wamp/www"
   ServerName localhost
   ServerAlias localhost
   **Using Apache 2.2 syntax**
   <Directory  "C:/wamp/www">
      Order Deny,Allow
      Deny from all
      Allow from 127.0.0.1 localhost ::1
      ## For every ip in the subnet, just use the first 3 numbers of the subnet
      ## Check you actual subnet for the actual values to use here
      Allow from 192.168.0
   </Directory>

  **Using Apache 2.4 syntax**
  <Directory  "C:/wamp/www">
      Require local
      ## And possibly allow access from you local network
      ## Check you subnet for the actual values to use here
      Require ip 192.168.0
  </Directory>
</VirtualHost>


<VirtualHost *:80>
    DocumentRoot "C:/websites/www/site1"
    ServerName site1.dev
    ServerAlias www.site1.dev
    Options Indexes FollowSymLinks
    **Using Apache 2.2 syntax**
    <Directory "C:/websites/www/site1">
        AllowOverride All
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1 localhost ::1
        #If you want to allow access from your internal network
        # For specific ip addresses add one line per ip address
        #Allow from 192.168.0.nnn
        # For every ip in the subnet, just use the first 3 numbers of the subnet
        #Allow from 192.168.0
        # If you want to allow access to everyone
        #Allow from all
    </Directory>

    **Using Apache 2.4 syntax**
    <Directory "C:/websites/www/site1">
        AllowOverride All
        Require local
        #If you want to allow access from your internal network
        # For specific ip addresses add one line per ip address
        #Require ip 192.168.0.nnn
        # For every ip in the subnet, just use the first 3 numbers of the subnet
        #Require ip 192.168.0
        # If you want to allow access to everyone
        #Require all granted
    </Directory>

</VirtualHost>

Add as many as you require so each of your sites have one, changing the DocumentRoot, ServerName and any other of the parameters as appropriate. 添加所需数量,以便每个站点都有一个,根据需要更改DocumentRoot,ServerName和任何其他参数。

This also allows you to make SITE SPECIFIC changes to the configuration. 这也允许您对配置进行SITE SPECIFIC更改。

NOTE: This will make the wamp manager "Put Online" function no longer have any effect on these new vhost'ed sites as the security for each one is now part of the vhost definition, so leave WAMP, OFFLINE. 注意:这将使wamp管理器“Put Online”功能不再对这些新的vhost'ed站点产生任何影响,因为每个站点的安全性现在都是vhost定义的一部分,因此请离开WAMP,OFFLINE。

If you want to put one or more sites online you will have to change the Allow commands MANUALLY in the httpd-vhosts.conf file. 如果要将一个或多个站点联机,则必须在httpd-vhosts.conf文件中手动更改“允许命令”。

To check your subnet do the following: Launch a command window, and run 要检查子网,请执行以下操作:启动命令窗口,然后运行

>ipconfig

Look for the line "Default Gateway" in the output and use the third number in your Allow commands. 在输出中查找“Default Gateway”行,并在Allow命令中使用第三个数字。

Edit your httpd.conf file and search for these lines, they are near the bottom of the file. 编辑您的httpd.conf文件并搜索这些行,它们位于文件的底部。

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Remove the '#' comment character on this line to Include your newly changed vhosts, this will cause apache to register their existance. 删除此行上的“#”注释字符以包含新更改的vhost,这将导致apache注册它们的存在。 eg 例如

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

While still editing your httpd.conf file search for 在编辑httpd.conf文件时仍在搜索

#   onlineoffline tag - don't remove
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1

</Directory>

DO NOT CHANGE THESE LINES! 不要改变这些线!

Add the following after the <\\Directory> tag to secure your new C:\\websites folder. 在<\\ Directory>标记后添加以下内容以保护新的C:\\ websites文件夹。

<Directory "C:/websites/">
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
</Directory>

This is to set security on your new directory structure so that access to these new sites is only allowed from 127.0.0.1 (localhost) unless amended from within a specific VHOST. 这是为了在新目录结构上设置安全性,以便只允许从127.0.0.1(localhost)访问这些新站点,除非在特定VHOST中进行修改。 PS Your internal network will probably be something like 192.168.xy, check your system first! PS您的内部网络可能类似于192.168.xy,请先检查您的系统!

Now in order for your browser to know how to get to these new domain names ie site1.dev and site2.dev, we need to tell windows what IP address they are located on. 现在,为了让您的浏览器知道如何访问这些新域名,例如site1.dev和site2.dev,我们需要告诉Windows它们所在的IP地址。 There is a file called HOSTS that is a hangover from the days before Domain Name Servers (DNS) were invented. 有一个名为HOSTS的文件是域名服务器(DNS)发明前几天的宿醉。 It is a way of giving nice easy rememberable names to IP address's, which of course is what DNS Servers do for us all now. 这是一种为IP地址提供简单易记的名称的方法,这当然是DNS服务器现在为我们所做的事情。

Edit your HOSTS file, this can be found in C:\\windows\\system32\\drivers\\etc , the file does not have an extension. 编辑你的HOSTS文件,这可以在C:\\ windows \\ system32 \\ drivers \\ etc中找到,该文件没有扩展名。

Windows protects this file so you must be an Administrator to be allowed to save changes to this file. Windows保护此文件,因此您必须是管理员才能保存对此文件的更改。

If you are using VISTA or Windows7/8 you may think you are an Administrator BUT YOU ARE NOT!!!! 如果您使用的是VISTA或Windows7 / 8,您可能会认为您是管理员,但您不是!

So to edit this file you must launch your editor, or Notepad in a specific way to gain Administrator rights. 因此,要编辑此文件,您必须以特定方式启动编辑器或记事本以获得管理员权限。 To do this find your editors icon and launch it using the following key strokes: 要执行此操作,请找到您的编辑器图标并使用以下击键启动它:

Shift + Right Click over its icon, this will display a menu, click the item "Run as Administrator", and click "Allow" on the challenge dialog that will appear. Shift +右键单击其图标,将显示一个菜单,单击“以管理员身份运行”项,然后在将出现的挑战对话框上单击“允许”。

Now you are ready to edit the hosts file so navigate your editor to c:\\windows\\system32\\drivers\\etc\\hosts 现在您已准备好编辑hosts文件,因此将编辑器导航到c:\\ windows \\ system32 \\ drivers \\ etc \\ hosts

Add the following lines to this file 将以下行添加到此文件中

127.0.0.1 site1.dev 127.0.0.1 site2.dev 127.0.0.1 site1.dev 127.0.0.1 site2.dev

NOTE: You will need to add one line in this file for each of your new virtual hosts. 注意:您需要在此文件中为每个新虚拟主机添加一行。

Once you have saved these changes you need to make windows refresh its 'domain name - ipaddress cross reference' cache. 保存这些更改后,您需要让Windows刷新其“域名 - ipaddress交叉引用”缓存。

To do this launch a command window as an Administrator ( Shift + Left Click over the command window icon ) and run these 2 commands. 要执行此操作,请以管理员身份启动命令窗口(Shift +左键单击命令窗口图标)并运行这两个命令。

>net stop "DNS Client"
>net start "DNS Client"

Note: The quotes are required as there is a space in the services name. 注意:引号是必需的,因为服务名称中有空格。

In order for Apache to pick up these changes you must bounce ( restart ) apache. 为了让Apache获取这些更改,您必须反弹(重启)apache。

DO this by: Wamp manager -> Apache -> Service -> Restart Service 请执行此操作:Wamp manager - > Apache - > Service - > Restart Service

You should now be able to use the address site1.dev in your browser to get to your new sites. 您现在应该可以在浏览器中使用地址site1.dev来访问新网站。 Copy your sites code into the "C:/websites/xxxx" folder if you already have a site coded or, place a quick and simple index.php file into the "c:\\websites\\xxxx" folder to proove it all works. 如果您已经对站点进行了编码,则将站点代码复制到“C:/ websites / xxxx”文件夹中,或者将快速简单的index.php文件放入“c:\\ websites \\ xxxx”文件夹中以证明其一切正常。 example: 例:

   <!DOCTYPE html>
   <html lang="en-US">
   <head>
   <meta charset="UTF-8">
   <title>SITE1</title>
   </head>
   <body>
   <?php
        echo '<div style="background-color:red;color;white;text-align:center;font-size:18px">HELLO FROM Site1</div>';
   ?>
   </body>
   </html>

TROUBLE SHOOTING: 故障排除:

If you have used the new domain name ( site1.dev ) and it has not found the site. 如果您使用了新域名(site1.dev)并且未找到该站点。 a. 一种。 Check the changes to the hosts file. 检查hosts文件的更改。 b. Restart the "DNS Service" that runs in windows. 重新启动在Windows中运行的“DNS服务”。 This caches all doman names that you use in a browser so that the browser does not have to query a DNS Server each time you re-use a domain name. 这会缓存您在浏览器中使用的所有doman名称,以便每次重新使用域名时浏览器都不必查询DNS服务器。 This may have cached your failed attempt but a restart is easy and should solve the problem and is quicker that re-booting windows, which should also work. 这可能已经缓存了你失败的尝试,但重启很容易,应该解决问题,并且更快启动窗口,这也应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM