当前位置: 首页 > 编程文章 >

Linux环境下Apache多端口配置虚拟主机的方法

Linux环境下Apache多端口配置虚拟主机的方法

作者:苏舟时间:2025-08-11 11:37:34浏览:300

在Linux系统中,Apache默认使用80端口和/var/www目录。通过修改ports.conf添加监听端口并配置sites-available下的虚拟主机文件,可实现单域名或多域名的多端口访问。配置完成后需重启服务并通过浏览器验证路径是否正常显示。

本文目录展开

Apache默认配置与基础环境说明

在Linux操作系统中,Apache Web服务器默认采用的配置参数包括特定的文档根目录与监听端口。

其默认的文档存放路径通常设定为:/var/www。

默认情况下,服务器监听的网络端口号为:80。

若希望发布自定义的系统资源目录,可按照以下步骤进行配置,具体执行命令如下:

(1)添加监听端口

#cd /etc/apache2

#vim ports.conf

在该配置文件中追加以下内容:

NameVirtualHost *:8000

Listen 8000

(2)配置虚拟目录

#cd /etc/apache2/sites-available

#cp default default-me

#vim default-me

编辑后的配置文件内容应包含以下结构:


    ServerAdmin webmaster@localhost
    DocumentRoot /wwwroot
   
Options FollowSymLinks
AllowOverride None
   
   
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
   
    ErrorLog /var/log/apache2/error.log
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined

其中加粗显示的代码部分为核心配置关键点。

(3)发布站点

# ln -s /etc/apache2/sites-available/default-me /etc/apache2/sites-enabled/001-default

(4)重启服务

#/etc/init.d/apache2 restart

(5)测试

http://localhost:8000/

如果能够通过上述地址正常访问页面,则表明配置已成功生效。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Virtual Hosts

#

# If you want to maintain multiple domains/hostnames on your

# machine you can setup VirtualHost containers for them. Most configurations

# use only name-based virtual hosts so the server doesn't need to worry about

# IP addresses. This is indicated by the asterisks in the directives below.

#

# Please see the documentation at

#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:81
NameVirtualHost *:82
NameVirtualHost *:83
NameVirtualHost *:84
NameVirtualHost *:85
NameVirtualHost *:86
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any block.
#
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www/www-nongye
ServerName www.dede.com
ErrorLog D:/AppServ/www/www-nongye/errornongye.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
Options FollowSymLinks
#Deny from all
Allow from all
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www-chinaec
ServerName www.dede.com
DirectoryIndex index.php index.html
ErrorLog D:/AppServ/www-chinaec/errornongjiale.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www/www-05110
ServerName www.dede.com
ErrorLog D:/AppServ/www/www-05110/errornong05110.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www/wordpress
ServerName www.dede.com
ErrorLog D:/AppServ/www/wordpress/errornongwordpress.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www/magento
ServerName www.dede.com
ErrorLog D:/AppServ/www/magento/errormagento.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www/magento1322
ServerName www.dede.com
ErrorLog D:/AppServ/www/magento1322/errormagento1322.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
ServerAdmin jsw7001@hotmail.com
DocumentRoot D:/AppServ/www/www-nongjiale
ServerName www.dede.com
ErrorLog D:/AppServ/www/www-nongjiale/errornongjiale.log
CustomLog logs/dummy-host2.appservnetwork.com-access_log common
ServerAdmin webmaster@dummy-host2.x
DocumentRoot "C:/Apache2.2/docs/dummy-host2.x"
ServerName dummy-host2.x
ErrorLog "logs/dummy-host2.x-error.log"
CustomLog "logs/dummy-host2.x-access.log" common
1、单域名单端口设置
如:www.abc.com 默认用80访问
特别说明,apache的配置默认都在安装目录下的conf目录里面
或者是/etc/apache2
不过一定要注意,listen.conf文件中端口监听是否开启
如下图,表示已经开启80端口监听
直接默认
1、单域名单端口设置
如:www.abc.com 默认用80访问
特别说明,apache的配置默认都在安装目录下的conf目录里面
或者是/etc/apache2
不过一定要注意,listen.conf文件中端口监听是否开启
如下图,表示已经开启80端口监听
直接默认修改default-server.conf
这个配置最好简单
安装好了,之后,默认已经能够使用了,只是要把它修改成你需要的域名及访问路径罢了。
给一个标准配置吧
代码如下:
DocumentRoot "/srv/www/htdocs"
#
# Configure the DocumentRoot
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.2/mod/core.html#options
# for more information.
Options None
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
AllowOverride None
# Controls who can get stuff from this server.
Order allow,deny
Allow from all
2、单域名多端口设置
代码如下:
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
NameVirtualHost *:80
NameVirtualHost *:81
  DirectoryIndex index.html index.php
  ServerName "www.abc.com "
  DocumentRoot "/srv/www/htdocs/" 
 
  DirectoryIndex index.php
    ServerName "www.abc.com:81 "
  DocumentRoot "/srv/www/htdocs/bbs/" 
 
3、多域名多端口设置
代码如下:
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
NameVirtualHost *:80
NameVirtualHost *:81
  DirectoryIndex index.html index.php
  ServerName "www.abc.com "
  DocumentRoot "/srv/www/htdocs/" 
 
  DirectoryIndex index.html index.php
  ServerName "www.btc.com "
  DocumentRoot "/srv/www/htdocs/btc" 
 
  DirectoryIndex index.php
    ServerName "www.abc.com:81 "
  DocumentRoot "/srv/www/htdocs/bbs/" 
 
需要注意的问题是,多端口监听,一定要注意listen.conf文件,是否开启了相应端口。
上一篇
PHP7匿名类用法详解

PHP7引入了匿名类特性,允许在代码中直接实例化未命名的类。该功能简化了需要一次性使用的对象创建过程,提升了代码的简洁性与灵活性。本文通过具体示例展示其基本用法,帮助开发者快速掌握这一新特性。

下一篇
ThinkPHP结合AJAX长轮询实现PC与APP推送详解

本文介绍基于ThinkPHP5利用数据库标志位配合AJAX长轮询机制,实现PC端与APP端的实时消息推送。通过后端定时查询未读消息并设置已读标记,前端JavaScript每隔一秒发起请求获取新数据,从而解决离线消息无法推送的问题,适用于公告等场景。

相关文章

更多
Apache基于端口配置虚拟主机的完整步骤

本文介绍如何在Apache中通过监听不同端口来创建多个虚拟主机。主要步骤包括禁用默认DocumentRoot、在httpd.conf中添加Listen指令(如8080和81)、在conf.d目录下编写各端口的VirtualHost配置块,并创建对应的网站根目录及测试文件。最后重启httpd服务使配置生效,实现通过不同端口访问不同站点。

2026.06.24 885
Linux 配置 WWW 服务器全攻略:Apache 安装、配置与管理

本文详细介绍在 Linux 环境下配置 Apache Web 服务器的完整流程,涵盖从源码与 RPM 两种方式的安装步骤,到 httpd.conf 核心指令的参数解析。内容进一步深入讲解基于 IP 和域名的虚拟主机配置方法,以及客户端与服务端代理的设置技巧,并附带 Apache 访问与错误日志的格式详解及维护建议,帮助管理员高效部署和管理 Web 服务。

2026.11.12 845
Apache虚拟主机配置详解:IP、端口与域名模式

Apache虚拟主机允许在一台服务器上托管多个网站,主要分为基于IP地址、端口和域名三种类型。配置前需确保vhost_alias_module模块已加载,并启用Include conf/extra/httpd-vhosts.conf以导入虚拟主机配置文件。其中,基于域名的虚拟主机最为常用,必须通过NameVirtualHost指令指定监听地址,并在VirtualHost段中定义ServerName和DocumentRoot。此外,还需注意目录访问权限设置以避免403错误,以及混合配置时的优先级规则。

2026.08.09 606
利用Apache反向代理构建安全的WWW与文件服务器

本文介绍在Redhat 6.1环境下,使用Apache 1.3.9通过反向代理技术,将内部网段的WWW服务器和文件服务暴露给外部网络。该方法利用基于名字的虚拟主机区分不同域名请求,既实现了对外部用户的公共访问,又保持了内部服务器的隔离,符合防火墙安全准则。

2025.02.03 461
Apache 本地多虚拟主机配置指南

在本地 Apache 服务器中,通过修改系统 hosts 文件和 httpd.conf 配置文件,可以为不同站点分配独立域名。本文以 phpMyAdmin 和 Zendphpweb20 为例,演示如何添加自定义 DNS 解析、引入子配置文件并重启服务,实现通过 http://dbmanager 和 http://phpweb20 直接访问对应目录。

2025.08.09 346
Windows Apache多端口虚拟主机配置指南

本文介绍在Windows环境下为Apache配置多端口虚拟主机的具体步骤。通过修改httpd.conf监听不同端口,并在httpd-vhosts.conf中定义对应的ServerName和DocumentRoot,可实现同一IP不同端口访问不同目录。例如将80端口指向G:\PHP,8080端口指向G:\PHP\abc,重启服务后即可通过localhost:8080访问对应内容。

2026.01.26 196

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。