php 如何在 XAMPP 上创建虚拟主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27268205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create virtual host on XAMPP
提问by Paras Arora
I am sure this question is being asked many times but I am not encounter with a problem. I am using XAMPP where I configure Zend framework.
我相信这个问题已经被问过很多次了,但我没有遇到问题。我正在使用 XAMPP 配置 Zend 框架。
XAMPP is running on port 8081as 80is being occupied by some Windows process I need to use virtual host for that I configure with following code in C:/xampp/apache/config/extra/httpd-vhosts.config
(or C:/xampp/apache/conf/extra/httpd-vhosts.conf
in newer releases).
XAMPP 在端口 8081上运行,因为80被一些 Windows 进程占用,我需要使用虚拟主机,我在C:/xampp/apache/config/extra/httpd-vhosts.config
(或C:/xampp/apache/conf/extra/httpd-vhosts.conf
在较新版本中)配置了以下代码。
<VirtualHost *:80>
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/CommunicationApp/public"
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
and also update the hosts file with 127.0.0.1 comm-app.local
and try to re-start apache but it is showing error.
并更新主机文件127.0.0.1 comm-app.local
并尝试重新启动 apache,但它显示错误。
15:03:01 [Apache] Error: Apache shutdown unexpectedly.
15:03:01 [Apache] This may be due to a blocked port, missing dependencies,
15:03:01 [Apache] improper privileges, a crash, or a shutdown by another method.
15:03:01 [Apache] Press the Logs button to view error logs and check
15:03:01 [Apache] the Windows Event Viewer for more clues
15:03:01 [Apache] If you need more help, copy and post this
15:03:01 [Apache] entire log window on the forums
采纳答案by Steini
I see two errors:
我看到两个错误:
<VirtualHost *:80> -> Fix to :8081, your POrt the server runs on
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/CommunicationApp/public" -> This is probably why it crashes, missing >
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
-> MIssing close container: </VirtualHost>
Fixed version:
固定版本:
<VirtualHost *:8081>
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/CommunicationApp/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
One thing to mention:
有一件事要提:
You can always try and run command:
您可以随时尝试运行命令:
service apache2 configtest
This will tell you when you got a malformed configuration and maybe even can tell you where the problem is.
这会告诉您什么时候出现了格式错误的配置,甚至可能会告诉您问题出在哪里。
Furthermore it helps avoid unavailability in a LIVE system:
此外,它还有助于避免在 LIVE 系统中不可用:
service apache2 restart
will shutdown and then fail to start, this configtest you know beforehand "oops I did something wrong, I should fix this first" but the apache itself is still running with old configuration. :)
将关闭然后无法启动,这个配置测试你事先知道“哎呀我做错了什么,我应该先解决这个问题”但 apache 本身仍在以旧配置运行。:)
回答by Amit Naraniwal
Step 1)C:\WINDOWS\system32\drivers\etc\ Open the "hosts" file :
步骤 1)C:\WINDOWS\system32\drivers\etc\ 打开“hosts”文件:
127.0.0.1 localhost
127.0.0.1 test.com
127.0.0.1 example.com
Step 2)xampp\apache\conf\extra\httpd-vhosts.conf
步骤 2)xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs/test/
ServerName www.test.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs/example/
ServerName www.example.com
</VirtualHost>
Step 3)C:\xampp\apache\conf\httpd.conf. Scroll down to the Supplemental configuration section at the end, and locate the following section (around line 500), Remove the # from the beginning of the second line so the section now looks like this:
步骤 3)C:\xampp\apache\conf\httpd.conf。向下滚动到最后的 Supplemental configuration 部分,并找到以下部分(大约第 500 行),从第二行的开头删除 #,因此该部分现在看起来像这样:
#Virtual hosts
Include conf/extra/httpd-vhosts.conf
Step 4)Restart XAMPP and now run in your browser :
步骤 4)重新启动 XAMPP 并在您的浏览器中运行:
www.example.com or www.test.com
回答by Ashish pathak
Add this Code in C:\xampp\apache\conf\extra\httpd-vhosts.conf
将此代码添加到 C:\xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName qa-staging.com
ServerAlias www.qa-staging.com
<Directory "c:/xampp/htdocs">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now Add your virtual host name in bellow file.
现在在波纹管文件中添加您的虚拟主机名。
C:\Windows\System32\drivers\etc\hosts
127.0.0.1 qa-staging.com
127.0.0.1 qa-staging.com
If you are not able to save this code in host file then right click on notpad select Run as administrator and then you can able to save your custom code now restart your XAMP
如果您无法将此代码保存在主机文件中,则右键单击记事本选择以管理员身份运行,然后您可以保存自定义代码,现在重新启动 XAMP
回答by Asarudeen
Write these codes end of the C:\xampp\apache\conf\extra\httpd-vhosts.conf file,
将这些代码写在 C:\xampp\apache\conf\extra\httpd-vhosts.conf 文件的末尾,
DocumentRoot "D:/xampp/htdocs/foldername"
ServerName www.siteurl.com
ServerAlias www.siteurl.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
between the virtual host tag.
虚拟主机标签之间。
and edit the file System32/Drivers/etc/hosts use notepad as administrator
并编辑文件 System32/Drivers/etc/hosts 以管理员身份使用记事本
add bottom of the file
添加文件底部
127.0.0.1 www.siteurl.com
回答by Pupil
Just change the port to 8081
and following virtual host will work:
只需将端口更改为8081
以下虚拟主机即可:
<VirtualHost *:8081>
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/CommunicationApp/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
回答by Bhutto Hafeez
回答by develway
In Your disk drive:\xampp\apache\conf\extra\httpd-vhosts.conf exists an example and you could edit it with your configuration:
在您的磁盘驱动器中:\xampp\apache\conf\extra\httpd-vhosts.conf 中存在一个示例,您可以使用您的配置对其进行编辑:
##<VirtualHost *:80>
##ServerAdmin [email protected]
##DocumentRoot "C:/xampp/htdocs/dummy-host.example.com"
##ServerName dummy-host.example.com
##ServerAlias www.dummy-host.example.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
##</VirtualHost>
It would be like this, as example and don't forget to add VirtualHost for localhost itself to have posibility run phpmyadmin and other project at the same time on the port 80, as example I will show with store.local project:
就像这样,作为示例,不要忘记为 localhost 本身添加 VirtualHost,以便在端口 80 上同时运行 phpmyadmin 和其他项目,作为示例,我将使用 store.local 项目显示:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "c:/xampp/htdocs/store.local/public"
ServerName www.store.local
ServerAlias store.local
<Directory C:/xampp/htdocs/store.local>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
then as mentioned above you must add in: C:\windows\system32\drivers\hosts to the bottom of the file
然后如上所述,您必须在文件底部添加: C:\windows\system32\drivers\hosts
127.0.0.1 store.local
127.0.0.1 www.store.local
restart Apache and try in the browser:
重启 Apache 并在浏览器中尝试:
store.local or www.store.local
maybe at the first time you must to add like this:
也许在第一次你必须像这样添加:
http://store.local or http://www.store.local
to use other ports, you must add follows, before your VirtualHost:
要使用其他端口,您必须在 VirtualHost 之前添加以下内容:
Listen 8081 or another which you prefer
then just use the port for your VirtualHost like this:
然后像这样使用 VirtualHost 的端口:
<VirtualHost *:8081>
ServerAdmin [email protected]
DocumentRoot "c:/xampp/htdocs/store.local/public"
ServerName store.local
ServerAlias www.store.local
<Directory C:/xampp/htdocs/store.local>
AllowOverride All
Require all granted
</Directory>
then restart Apache and try in the browser
然后重启Apache并在浏览器中尝试
store.local:8081 or www.store.local:8081
and only project for which you add the port will running on this port, for example other projects and phpmyadmin will be still running on port 80
并且只有您为其添加端口的项目才会在此端口上运行,例如其他项目和 phpmyadmin 仍将在端口 80 上运行
回答by Yash
Apache Virtual Host documentationSetting up a virtual host (vhost) provides several benefits:
Apache 虚拟主机文档设置虚拟主机 (vhost) 有几个好处:
- Virtual Hosts make URLs cleaner – localhost/mysite vs mysite.local.
- Virtual Hosts make permissions easier – restrict access for a single vhost on a local network vs permitting access to all sites on your local network.
- Some applications require a “.” in the URL (ahem Magento). While you can setup localhost.com/mysite by editing the Windows hosts file, creating a vhost is a better solution.
- 虚拟主机使 URL 更清晰——localhost/mysite 与 mysite.local。
- 虚拟主机使权限更容易——限制对本地网络上单个虚拟主机的访问与允许访问本地网络上的所有站点。
- 某些应用程序需要“.” 在 URL 中(咳咳 Magento)。虽然您可以通过编辑 Windows 主机文件来设置 localhost.com/mysite,但创建 vhost 是更好的解决方案。
VirtualHost DirectiveContains directives that apply only to a specific hostname or IP address
Location DirectiveApplies the enclosed directives only to matching URLs
VirtualHost 指令包含仅适用于特定主机名或 IP 地址的指令
位置指令仅将包含的指令应用于匹配的 URL
Examplechanges over configuration file - D:\xampp\apache\conf\extra\httpd-vhosts.conf
配置文件更改示例-D:\xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin localhost
DocumentRoot "D:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost localhost:80>
ServerAdmin [email protected]
DocumentRoot "/www/docs/host.example.com"
#DocumentRoot "D:\xampp\htdocs\phpPages"
ServerName host.example.com
ErrorLog "logs/host.example.com-error_log"
TransferLog "logs/host.example.com-access_log"
</VirtualHost>
# To get view of PHP application in the Browser.
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "D:\xampp\htdocs\app1"
ServerName app1.yash.com
ServerAlias app1.yash.com
ErrorLog "logs/app1.yash.com-error.log"
CustomLog "logs/app1.yash.com-access.log" combined
# App1 communication proxy call to Java War applications from XAMP
<Location /ServletApp1>
ProxyPass http://app1.yashJava.com:8080/ServletApp1
ProxyPassReverse http://app1.yashJava.com:8080/ServletApp1
Order Allow,Deny
Allow from all
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "D:\xampp\htdocs\app2"
ServerName app2.yash.com
ErrorLog "logs/app2.yash.com-error.log"
CustomLog "logs/app2.yash.com-access.log" combined
# App1 communication proxy call to Java War applications from XAMP
<Location /ServletApp2>
ProxyPass http://app1.yashJava.com:8080/ServletApp2
ProxyPassReverse http://app1.yashJava.com:8080/ServletApp2
Order Allow,Deny
Allow from all
</Location>
</VirtualHost>
Update Your Windows Hosts File ? Open your Windows hosts file located in C:\Windows\System32\drivers\etc\hosts.
更新您的 Windows 主机文件?打开位于以下位置的 Windows 主机文件C:\Windows\System32\drivers\etc\hosts.
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 test.com
127.0.0.1 example.com
127.0.0.1 myssl.yash.com
D:\xampp\apache\conf\httpd.conf, [httpd-ssl.conf](http://httpd.apache.org/docs/2.2/mod/mod_ssl.html)
D:\xampp\apache\conf\httpd.conf, [httpd-ssl.conf](http://httpd.apache.org/docs/2.2/mod/mod_ssl.html)
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost> directive.
# Listen 0.0.0.0:80 | [::]:80
Listen 80
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule speling_module modules/mod_speling.so
# ServerAdmin: Your address, where problems with the server should be e-mailed.
# This address appears on some server-generated pages, such as error documents.
# e.g. [email protected]
ServerAdmin postmaster@localhost
ServerName localhost:80
DocumentRoot "D:/xampp/htdocs"
<Directory "D:/xampp/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"
# ===== httpd-ssl.conf - SSL Virtual Host Context =====
# Note: Configurations that use IPv6 but not IPv4-mapped addresses need two
# Listen directives: "Listen [::]:443" and "Listen 0.0.0.0:443"
Listen 443
## SSL Virtual Host Context
<VirtualHost _default_:443>
DocumentRoot "D:\xampp\htdocs\projectFolderSSL"
ServerName myssl.yash.com:443
ServerAlias myssl.yash.com:443
ServerAdmin webmaster@localhost
ErrorLog "logs/error.log"
<IfModule log_config_module>
CustomLog "logs/access.log" combined
</IfModule>
## Redirecting URL from Web server to Application server over different machine.
# myssl.yash.com:443/ServletWebApp
<Location /path>
ProxyPass http://java.yash2.com:8444/ServletWebApp
ProxyPassReverse http://java.yash2.com:8444/ServletWebApp
Order Allow,Deny
Allow from all
</Location>
#SSLCertificateFile "conf/ssl.crt/server.crt"
SSLCertificateFile "D:\SSL_Vendor\yash.crt"
#SSLCertificateKeyFile "conf/ssl.key/server.key"
SSLCertificateKeyFile "D:\SSL_Vendor\private-key.key"
#SSLCertificateChainFile "conf/ssl.crt/server-ca.crt"
SSLCertificateChainFile "D:\SSL_Vendor\intermediate.crt"
</VirtualHost>
# ===== httpd-ssl.conf - SSL Virtual Host Context =====
@see
@看
回答by Jakir Hosen Khan
I fixed it using following configuration.
我使用以下配置修复了它。
Listen 85
<VirtualHost *:85>
DocumentRoot "C:/xampp/htdocs/LaraBlog/public"
<Directory "C:/xampp/htdocs/CommunicationApp/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
回答by Yaakov Ainspan
I'm a little late to the party, but I wrote this little bash script for Mac that creates a VirtualHost through the terminal:
我参加聚会有点晚了,但是我为 Mac 编写了这个小 bash 脚本,它通过终端创建了一个 VirtualHost:
#!/bin/bash
echo "Welcome to the VirtualHostCreator! Press <RETURN> to continue."
read
echo "Enter the name the VirtualHost you would like to create. No spaces or dashes, please."
read hostname
echo "Enter the document root of the VirtualHost."
read doc_root
echo "Creating VirtualHost \"$hostname\". You may be prompted for your password."
hosts_file="/etc/hosts"
vhosts_file="/Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf"
restart_command="sudo /Applications/XAMPP/xamppfiles/xampp restart"
cat >> $vhosts_file << EndOfMessage
<VirtualHost ${hostname}>
ServerName ${hostname}
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/${doc_root}"
</VirtualHost>
EndOfMessage
sudo sh -c "echo \"127.0.0.1 $hostname\" >> $hosts_file"
$restart_command
I'm sure there are a few improvements that can be made, and it only has the two required options for the vhost (server name and document root), but it does the job much more quickly and efficiently than opening and editing all the files manually, and also automatically restarts XAMPP afterwards.
我确定可以进行一些改进,并且它只有两个必需的 vhost 选项(服务器名称和文档根目录),但它比打开和编辑所有文件更快、更有效地完成工作手动,之后也会自动重新启动 XAMPP。
This assumes that you have the default installation location for XAMPP, which can all be changed.
这假设您拥有 XAMPP 的默认安装位置,所有这些位置都可以更改。