作者:lvyanbo | 来源:互联网 | 2024-11-22 15:04
本文将指导您如何在 Apache 服务器上配置虚拟主机,以支持多个网站或应用在同一台服务器上运行。这不仅提高了资源利用率,还能有效管理不同项目。
首先,确保您的系统中已安装 Apache 服务器,并且可以通过命令行访问其配置文件。接下来,我们将创建一个新的配置文件来定义虚拟主机:
(export USERHOME=$(dscl . -read /Users/`whoami` NFSHomeDirectory | awk -F":" '{print $2}') ; cat > ~/Sites/httpd-vhosts.conf <#
# 监听端口。
#
#Listen 8080 # 在主 httpd.conf 文件中定义
Listen 8443
#
# 使用基于名称的虚拟主机。
#
NameVirtualHost *:8080
NameVirtualHost *:8443
#
# 为 ~/Sites 中的虚拟主机设置权限。
#
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
Allow from all
# 对于用户 ~/Sites 文件夹中的 http://localhost
ServerName localhost
DocumentRoot "${USERHOME}/Sites"
ServerName localhost
Include "${USERHOME}/Sites/ssl/ssl-shared-cert.inc"
DocumentRoot "${USERHOME}/Sites"
#
# 虚拟主机示例
#
## 手动虚拟主机模板(HTTP 和 HTTPS)
#
# ServerName project.dev
# CustomLog "${USERHOME}/Sites/logs/project.dev-access_log" combined
# ErrorLog "${USERHOME}/Sites/logs/project.dev-error_log"
# DocumentRoot "${USERHOME}/Sites/project.dev"
#
#
# ServerName project.dev
# Include "${USERHOME}/Sites/ssl/ssl-shared-cert.inc"
# CustomLog "${USERHOME}/Sites/logs/project.dev-access_log" combined
# ErrorLog "${USERHOME}/Sites/logs/project.dev-error_log"
# DocumentRoot "${USERHOME}/Sites/project.dev"
#
#
# 自动虚拟主机
#
# 位于 ${USERHOME}/Sites/webroot 的目录可以通过 http://webroot.dev 访问
# 在 Drupal 中,取消注释包含 RewriteBase / 的行
#
# 此日志格式将在每条日志记录的开头显示虚拟主机名称
LogFormat "%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedmassvhost
# 带 .dev 的自动虚拟主机
ServerName dev
ServerAlias *.dev
CustomLog "${USERHOME}/Sites/logs/dev-access_log" combinedmassvhost
ErrorLog "${USERHOME}/Sites/logs/dev-error_log"
VirtualDocumentRoot ${USERHOME}/Sites/%-2+
ServerName dev
ServerAlias *.dev
Include "${USERHOME}/Sites/ssl/ssl-shared-cert.inc"
CustomLog "${USERHOME}/Sites/logs/dev-access_log" combinedmassvhost
ErrorLog "${USERHOME}/Sites/logs/dev-error_log"
VirtualDocumentRoot ${USERHOME}/Sites/%-2+
EOF
)
完成上述配置后,重启 Apache 服务使更改生效。通过这些步骤,您可以轻松地在本地环境中设置和管理多个虚拟主机。