search
Homephp教程php手册Apache 配置虚拟主机三种方式,apache虚拟主机

Apache 配置虚拟主机三种方式,apache虚拟主机

一、基于IP

 1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP:

[root@localhost root]# ifconfig eth0:1 192.168.1.11
[root@localhost root]# ifconfig eth0:2 192.168.1.12
[root@localhost root]# ifconfig eth0:3 192.168.1.13

 2. 修改hosts文件,添加三个域名与之一一对应:

192.168.1.11   www.test1.com
192.168.1.12   www.test2.com
192.168.1.13   www.test3.com

 3. 建立虚拟主机存放网页的根目录,如在/www目录下建立test1、test2、test3文件夹,其中分别存放1.html、2.html、3.html

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

 

 4. 在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置:

 


  ServerName www.test1.com
  DocumentRoot /www/test1/
  
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   




  ServerName www.test1.com
  DocumentRoot /www/test2/
  
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   




  ServerName www.test1.com
  DocumentRoot /www/test3/
  
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   

 5. 大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com

 

二、基于主机名

 1. 设置域名映射同一个IP,修改hosts:

192.168.1.10  www.test1.com
192.168.1.10  www.test2.com
192.168.1.10  www.test3.com

 2. 跟上面一样,建立虚拟主机存放网页的根目录

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

 

 3. 在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着在httpd-vhosts.conf中写入如下配置:

 

  为了使用基于域名的虚拟主机,必须指定服务器IP地址(和可能的端口)来使主机接受请求。可以用NameVirtualHost指令来进行配置。 如果服务器上所有的IP地址都会用到, 你可以用*作为NameVirtualHost的参数。在NameVirtualHost指令中指明IP地址并不会使服务器自动侦听那个IP地址。 这里设定的IP地址必须对应服务器上的一个网络接口。

  下一步就是为你建立的每个虚拟主机设定配置块,的参数与NameVirtualHost指令的参数是一样的。每个定义块中,至少都会有一个ServerName指令来指定伺服哪个主机和一个DocumentRoot指令来说明这个主机的内容存在于文件系统的什么地方。

  如果在现有的web服务器上增加虚拟主机,必须也为现存的主机建造一个定义块。其中ServerName和DocumentRoot所包含的内容应该与全局的保持一致,且要放在配置文件的最前面,扮演默认主机的角色。

NameVirtualHost *:80
 

  ServerName *

  DocumentRoot /www/ 

  ServerName www.test1.com

  DocumentRoot /www/test1/

  

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  

 

 

  ServerName www.test2.com

  DocumentRoot /www/test2/

  

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  

  ServerName www.test3.com

  DocumentRoot /www/test3/

  

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  

 4. 大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com

 

三、基于端口

1.  修改配置文件

  将原来的

    Listen 80
      改为
      Listen 80
      Listen 8080


2. 更改虚拟主机设置:


    DocumentRoot /var/www/test1/
    ServerName www.test1.com



    DocumentRoot /var/www/test2
    ServerName www.test2.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.