【4858.com】怎么着布署apache虚拟主机的实例小结,Linux配置虚拟主机后

By admin in 4858.com on 2019年5月5日

Linux配置虚拟主机后,只可以访问到主页怎么做?

Apache服务器

1、基于ip地址的虚拟主机

前几日布置了lamp后,加多了三个虚拟主机,配置http.conf后,扩大虚拟主机,测试访问发掘只有域名下能访问,ljt.com不过域名下具有的都访问不到。

一 简介

复制代码 代码如下:

【4858.com】怎么着布署apache虚拟主机的实例小结,Linux配置虚拟主机后。Httpd-vhost..conf

1 www:world  wide  web 万维网

Listen 80
<VirtualHost 172.20.30.40>
    DocumentRoot /home/httpd/html1
    ServerName www.ok1.com
    ErrorLog /usr/local/apache/logs/error1_log
    CustomLog /usr/local/apache/logs/access1_log combined
</VirtualHost>
<VirtualHost 172.20.30.50>
    DocumentRoot /home/httpd/html2
    ServerName www.ok2.com
    ErrorLog /usr/local/apache/logs/error2_log
    CustomLog /usr/local/apache/logs/access2_log combined
</VirtualHost>

<VirtualHost *:80>

http 协议: 超文本传输协议

 2、基于IP 和多端口的虚拟主机配置

DocumentRoot “/usr/local/apache/htdocs/cyds”

HTML语言: 超文本标记语言

复制代码 代码如下:

ServerName cyds.ljt.com

② U宝马X3L:统壹财富一定 协议+域名:端口+网页文件名

Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

Errorlog logs/cyds.ljt.com.err

                 

<VirtualHost 172.20.30.40:80>
 DocumentRoot /www/example1-80
 ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>

CustomLog logs/cyds.ljt.com.access common

三 搭建www的服务器的章程

 DocumentRoot /www/example1-8080
 ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
 DocumentRoot /www/example2-80
 ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
 DocumentRoot /www/example2-8080
 ServerName www.example2.org
</VirtualHost>

<Directory “/usr/local/apache/htdocs/cyds”>

windows   IIS+asp+SQLserver

三、单个IP 地址的服务器上依照域名的虚拟主机配置

Options FollowSymLinks

Internet  Information  server

复制代码 代码如下:

AllowOverride None

Linux apache+mysql+php

# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>

Order allow,deny

二 安装

要是你感到方面包车型客车篇章还不够详细能够看下下边包车型客车稿子:

Allow from all

壹、lamp源码安装

实行目的:在apache完结基于域名的虚拟主机
尝试用的XAMPP版本为一.柒.七,内含apache版本为2.贰.二一

</Directory>

                       生产情形    安全  稳定   错误提示关闭  
 源码包,才能供给高,复杂

尝试前筹算:

</VirtualHost>

 

1.

查了半天资料:

尝试遇到(复制的生育境况 错误提醒开启)

为了测试分歧的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其间增多实验用的域名若干,如

复制代码 代码如下:

127.0.0.1   test1.net
127.0.0.1   test2.net

那般,则在浏览器中输入该俩域名时,Windows将其分析为1二七.0.0.1人置地址。即,在浏览器中走访localhost,
test壹.net, test2.net均可访问XAMPP的应接页。

贰.
在apache目录下创立目录,以放置您不一样的网址。为敬爱XAMPP原有的htdocs中的接待页内容,实验此外创立了与htdocs平级的htdocs一索引,在其下创立了test1.net,
test2.net三个子目录用以放置实验用的网址。如下 –
apache/htdocs1/test1.net – 放置test壹.net网址内容
apache/htdocs1/test二.net – 放置test二.net网址内容

 在那三个目录中各新建hello world一网页 index.html,内容 –

复制代码 代码如下:

<HTML>
<HEAD></HEAD>
<BODY>
<H1>hello~,
这是[对应的网址名,用以区别].net</H1></BODY>
</HTML> 

实行步骤:

  1. 找到apache/conf/httpd.conf, 将中间的

ServerAdmin
ServerName
DocumentRoot
注释掉。

  1. 在httpd.conf中,找到行

 Include “conf/extra/httpd-vhosts.conf”
如被讲授则解注。该公文记载了虚拟主机的参数。[先前虚拟主机参数是直接填写在httpd.conf中的,为了越来越好地公司文件,将其分手出去,类似于少数编制程序语言一样。因此httpd.conf中include它,即一对1于把它的剧情填在了httpd.conf中。]

  1. 以此httpd-vhosts.conf文件格式基本如下 –

复制代码 代码如下:

#blah-blah
NameVirtualHost *:80
#blah-blah
#blah-blah
<VirtualHost *:80>
    ServerAdmin XXXXXXXX
    DocumentRoot “XXXXXXXX”
    ServerName XXXXXXX
    ServerAlias XXXXXX
    ErrorLog “logs/XXXXXX-error.log”
    CustomLog “logs/XXXXXXX-error.log” combined  
</VirtualHost>

亟待修改的,就是<VirtualHost>中的参数了。这么些可以参见apache官方文书档案。根据实验域名,能够扩大五个<VirtualHost>:

复制代码 代码如下:

<VirtualHost *:80>
    ServerAdmin adm@test1.net
    DocumentRoot “C:/xampp/htdocs1/test1.net”
    ServerName test1.net
    ServerAlias www.test1.net
    ErrorLog “logs/test1-error.log”
    CustomLog “logs/test1-access.log” combined

    <Directory “C:/xampp/htdocs1/test1.net”>
    order allow,deny
    allow from all
    </Directory>   
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin adm@test2.net
    DocumentRoot “C:/xampp/htdocs1/test2.net”
    ServerName test2.net
    ServerAlias www.test2.net
    ErrorLog “logs/test1-error.log”
    CustomLog “logs/test1-access.log” combined

    <Directory “C:/xampp/htdocs1/test2.net”>
    order allow,deny
    allow from all
    </Directory>   
</VirtualHost>

只顾,假设不在各VirtualHost中定义Directory的可访问性,你将碰着的是Access
Forbidden!就连本来的localhost也是。

  1. 由于事先注释掉了httpd.conf中的ServerName,

    DocumentRoot等,为了仍旧能以localhost访问原XAMPP招待页,就在扩张一个VirtualHost,如下

复制代码 代码如下:

<VirtualHost *:80>
    ServerAdmin adm@localhost
    DocumentRoot “C:/xampp/htdocs”
    ServerName localhost

    ErrorLog “logs/localhost-error.log”
    CustomLog “logs/localhost-access.log” combined

    <Directory “C:/xampp/htdocs”>
    order allow,deny
    allow from all
    </Directory>   
</VirtualHost>

为了防止失误,把它放置在第二个Virtualhost位置。

从那之后,apache基于域名的虚拟主机配置完结。能够经过

#
# 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
# <URL:;
# 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 *:80

#
# 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 <VirtualHost> block.
#

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot “E:/skydao/apache2/htdocs”
    ServerName localhost
    ServerAlias www.skydao.com
    ErrorLog “logs/localhost-error.log”
    CustomLog “logs/localhost-access.log” combined

    <Directory “E:/skydao/apache2/htdocs”>
    order allow,deny
    allow from all
    </Directory>   
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot “E:/skydao/apache2/htdocs/project1”
    ServerName project1.com
    ServerAlias www.project1.com
    ErrorLog “logs/project1-error.log”
    CustomLog “logs/project1-access.log” combined

    <Directory “E:/skydao/apache2/htdocs/project1”>
    order allow,deny
    allow from all
    </Directory>   
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot “E:/skydao/apache2/htdocs/zendTest/public”
    ServerName zendTest.com
    ServerAlias www.zendTest.com
    DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>   
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot “E:/skydao/apache2/htdocs/testRewrite”
    ServerName testRewrite.com
    ServerAlias www.testRewrite.com
    # DirectoryIndex index.php
    <Directory />
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
    </Directory>   
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin kongdaoxian@gmail.com
    DocumentRoot “E:/skydao/apache2/htdocs/test”
    ServerName test.com
    ServerAlias www.test.com
    ErrorLog “logs/zendTest-error.log”
    CustomLog “logs/zendTest-access.log” combined

    <Directory “E:/skydao/apache2/htdocs/test”>
    order allow,deny
    allow from all
    </Directory>   
</VirtualHost>

少了权力难点:

 

你只怕感兴趣的篇章:

  • Apache
    虚拟目录和暗中同意首页的安装
  • apache中伪静态配置和行使(Apache虚拟主机下Discuz伪静态)
  • 地面机apache配置基于域名的虚拟主机详解
  • Apache
    增多虚拟目录注意事项
  • windows
    apache多端口虚拟主机配置方式
  • apache虚拟主机中设置泛域名解析的章程
  • apache虚拟机配置步骤和改换访问端口的不二等秘书技(虚拟机端口映射)
  • Apache 配置多端口 多虚拟主机
    局域网访问示例
  • Apache
    多站点虚拟主机配置格局
  • win10apache配置虚拟主机后localhost无法使用的消除办法

最后大家须要在apache配置文件/opt/lampp/etc/httpd.conf中增添一下网址目录的访问权限。

开拓意况 源码包 贰进制包

<Directory "/opt/lampp/www/site1">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

 

 

2、rpm包安装     yum安装

 

httpd

配备lamp情状虚拟主机参考:

mysql

mysql-server

php

php-devel

php-mysql

叁 相关文件

apache配置文件

源码包安装:/usr/lcoal/apache2/etc/httpd.conf

    /usr/local/apache2/etc/extra/*.conf 

rpm包安装:/etc/httpd/conf/httpd.conf

暗中同意网页保存地点: 

网址根目录:/usr/local/apache2/htdocs/

rpm包安装:/var/www/html/

日记保存地方

网址日志:/usr/local/apache2/logs/

rpm包: /var/log/httpd/

            rpm包暗中认可使用日志管理程序   /var下都会轮替
  源码包才要求设置

日志管理:

#tail -f  access_log  
动态查看文件 

二25日志切割  apache自带日志里面自带日志切割

二十八日志轮替  linux自带日志管理logrotate.conf参与

#vim
/etc/logrotate.conf

/usr/local/apache2/logs/access_log{

                                 daily

                                 rotate
 30

}

       

#logrotate -f /etc/logrotate.conf
手动运维
#cd /usr/local/apache2/logs/
#ls

             全数日志都要进行日志轮替

四 配置文件

vi /root/.bashrc

   alias
sta=’/usr/local/apache2/bin/apachectl start’

   alias
sto=’/usr/local/apache2/bin/apachectl stop’

source /root/.bashrc

#sto
#sta

小心:apache配置文件严峻区分轻重缓急写

壹 针对主机意况的着力配置

ServerRoot apache主目录          

Listen 监听端口              

LoadModule 加载的相关模块

User

Group 用户和组

ServerAdmin 管理员邮箱

ServerName 服务器名(未有域名解析时,使用一时解析。不展开)

ErrorLog “logs/error_log 错误日志

CustomLog “logs/access_log” common 正确访问日志

DirectoryIndex index.html index.php 私下认可网页文件名,优先级依次

Include  etc/extra/httpd-vhosts.conf 子配置文件中内容也会加载生效

二 主页目录及权限  

DocumentRoot “/usr/local/apache2//htdocs”          

主页目录

<Directory “/usr/local/apache2//htdocs”>                 

#Directory关键字定义目录权限

Options Indexes FollowSymLinks                    

#options

None:未有任何额外权限

All: 全部权力

Indexes:浏览权限(当此目录下未有默许网页文件时,展现目录内容)

FollowSymLinks:准许软连接到其余目录

AllowOverride None

#概念是不是同意目录下.htaccess文件中的权限生效

None:.htaccess中权限不见效

All:文件中全体权力都见效

AuthConfig:文件中,唯有网页认证的权柄生效。

Require all granted 访问调整列表   40叁荒谬   40四荒谬

#概念此目录的允许访问权限

例一:仅允许IP为1玖二.16八.1.壹的主机访问

      Require all  denied

      Require ip 192.168.1.1

事例贰.仅同意1玖二.168.1.0/二四网络的主机访问

      Require  all  denied

      Require ip 192.168.1.0/24

事例三.禁止1九二.16八.一.2的主机访问,其余的都同意访问,

<RequireAll>

      Require all  granted

      Require not ip 192.168.1.2                        

</RequireAll>

事例四.同意持有访问,

Require all  granted

事例伍.回绝全体访问,

Require all  denied

三 目录别称  用途 扩张网站目录,增添服务器,使用二级域名,使用目录别称   

子配置文件名 etc/extra/httpd-autoindex.conf

Alias /icons/ “/usr/local/apache2//icons/”

    apache以为在此地 实际目录地点

概念外号  /icons/—-               

<Directory “/usr/local/apache2//icons”>

    Options Indexes MultiViews

    AllowOverride None

    Require all granted                   

</Directory>

修改子配置文件
#vim
/usr/local/apache2/extra/httpd-autoindex.conf
29 Alias /www/
“/usr/local/apache2/www/”
30 <Directory
“/usr/local/apache2/www/”>
31 Options Indexes
32 AllowOverride None
33 Require all granted
34 </Directory>

3)建立/usr/local/apache2/www目录
#mkdir /usr/local/apache2/www

4)重启服务 测试
#sto
#sta

测试 192.168.172.251/www/

四 用户认证   

界定特定目录,唯有钦赐用户可以访问。

壹) 建设构造须要保险的目录

运用小名,在系统职分树立目录,然后保卫安全

mkdir  -p  /share/soft

4858.com ,贰)修改配置文件,允许权限文件生效

vi
 /usr/local/apache2/etc/httpd.conf

453 Include
etc//extra/httpd-autoindex.conf

#vim
/usr/local/apache2/etc/extra/httpd-autoindex.conf
37 Alias /soft/ “/share/soft/”
38 <Directory
“/share/soft/”>
39 Options Indexes
40 AllowOverride All
41 Require all granted
42 </Directory>

Alias /soft/ “/share/soft/”

<Directory “/share/soft”>

    Options Indexes

    AllowOverride All #拉开权限认证文件.htaccess

    Require all granted 

</Directory>

重启apache

3)在钦点目录建设构造权力文件

树立/share/soft/.htaccess权限文件
#vim /share/soft/.htaccess
1 AuthName “172PHP”
2 AuthType basic
3 AuthUserFile
/share/apache.passwd
4 require valid-user

cd  /share/soft

vi  .htaccess #不区分轻重缓急写

AuthName “50 docs”

#提示音信

AuthType basic

#加密等级次序

AuthUserFile /share/apache.passwd

#密码文件,文件名自定义。

require valid-user

#允许密码文件中全数用户访问

4)营造密码文件,参与允许访问的用户。用户和系统用户毫不相关

/usr/local/apache2/bin/htpasswd  -c
 /share/apache.passwd  test1

-c  创建密码文件,只有增添第3个用户时,技能-c

/usr/local/apache2/bin/htpasswd  -m
 /share/apache.passwd  test2

-m  再增多更加多用户时

重启服务 测试
#sto
#sta

测试  192.168.172.251/soft/

5 虚拟主机 

1)分类

      基于IP的虚拟主机: 1台服务器,八个IP,搭建多少个网址

   基于端口的虚拟主机:
一台服务器,三个ip,搭建多少个网址,各类网络使用不一样端口访问

    基于名字的虚拟主机:
一台服务器,四个ip,搭建五个网址,各个网址采纳不一致域名访问

 

 

2)步骤:

壹 解析试验域名

192.168.172.251 www.sina.com
192.168.172.251 www.sohu.com

C:\WINDOWS\system32\drivers\etc\hosts
  windows

/etc/hosts   Linux 

贰 规划网址主目录

#mkdir /share/sina/
#mkdir /share/sohu/
#vim /share/sina/index.html
#vim /share/sohu/index.html

/share/sina————–www.sina.com

/share/sohu ———— www.sohu.com

 

3  修改配置文件

vi
 /usr/local/apache2/etc/httpd.conf

465 Include
etc//extra/httpd-vhosts.conf

#开发虚拟主机配置文件

vi /usr/local/apache2/etc/extra/httpd-vhosts.conf

<Directory “/usr/local/apache2/htdocs/sina”>

    Options Indexes

    AllowOverride None

Require all granted 

</Directory>

<Directory “/usr/local/apache2/htdocs/sohu”>

    Options Indexes

    AllowOverride None

    Require all granted 

</Directory>

 

23 <Directory
“/share/sina”>
24 Options Indexes
25 AllowOverride None
26 Require all granted
27 </Directory>
28
29 <Directory
“/share/sohu”>
30 Options Indexes
31 AllowOverride None
32 Require all granted
33 </Directory>

 

 

<VirtualHost 192.168.150.253>

#注意,只能写ip

    ServerAdmin webmaster@sina.com

#管理员邮箱

    DocumentRoot “/usr/local/apache2/htdocs/sina”

#网址主目录

    ServerName www.sina.com

#总体域名

    ErrorLog “logs/sina-error_log”

#谬误日志

    CustomLog “logs/sina-access_log” common

#做客日志

</VirtualHost>

 

<VirtualHost 192.168.150.253>

    ServerAdmin webmaster@sohu.com

    DocumentRoot “/usr/local/apache2/htdocs/sohu”

    ServerName www.sohu.com

    ErrorLog “logs/sohu.com-error_log”

    CustomLog “logs/sohu.com-access_log” common

</VirtualHost>

 

35 <VirtualHost
192.168.172.251>
36 ServerAdmin webmaster@sina.com
37 DocumentRoot “/share/sina/”
38 ServerName www.sina.com
39 ErrorLog
“logs/sina-error_log”
40 CustomLog “logs/sina-access_log”
common
41 </VirtualHost>
42
43 <VirtualHost
192.168.172.251>
44 ServerAdmin webmaster@sohu.com
45 DocumentRoot “/share/sohu/”
46 ServerName www.sohu.com
47 ErrorLog
“logs/sohu-error_log”
48 CustomLog “logs/sohu-access_log”
common
49 </VirtualHost>

 

重启服务 测试
#sto
#sta

测试 www.sina.com www.sohu.com

陆 rewrite 重写功用   U猎豹CS6L

在U昂科雷L中输入3个地点,会自行跳转为另1个

1)域名跳转 www.sina.com  ——>
 www.sohu.com

拉开虚拟主机,并不奇怪访问 

# vi
/usr/local/apache2/etc/httpd.conf

147LoadModule rewrite_module
modules/mod_rewrite.so

#开采重写模块,记得重启apache

修改配置文件,使sina目录的.htaccess文件生效

# vi extra/httpd-vhosts.conf

<Directory “/usr/local/apache2/htdocs/sina”>

    Options Indexes FollowSymLinks

    AllowOverride All

Require all granted 

</Directory>

#vim
/usr/local/apache2/etc/extra/httpd-vhosts.conf
23 <Directory
“/share/sina”>
24 Options Indexes FollowSymLinks
25 AllowOverride All
26 Require all granted
27 </Directory>

vi  /usr/local/apache2/htdocs/sina/.htaccess

建立/share/sina/.htaccess
#vim /share/sina/.htaccess

RewriteEngine on

#开启rewrite功能

RewriteCond %{HTTP_HOST} www.sina.com

把以www.sina.com 初叶的原委赋值给HTTP_HOST变量

RewriteRule  .*   

.*  输入任啥地点点,都跳转到

 

1 RewriteEngine on
2 RewriteCond %{HTTP_HOST}
www.sina.com
3 RewriteRule .*

重启服务 测试
#sto
#sta

测试   www.sina.com  ->
www.sohu.com

二)网页文件跳转

vi  /usr/local/apache2/htdocs/sina/.htaccess

RewriteEngine on

RewriteRule index(\d+).html index.php?id=$1

#
输入index(数值).html时,跳转到index.php文件,同时把数值当成变量传入index.php

#vim /share/sina/.htaccess
1 RewriteEngine on
2 RewriteRule index(\d+).html
index.php?id=$1

#cd /share/sina
#vim index.php
<?php
echo “yemian rewrite”;
?>

测试 www.sina.com/index5.html

7 常用子配置文件

httpd-autoindex.conf apache系统别称

httpd-default.conf 线程序调节制 *

httpd-info.conf    状态总结网页

httpd-languages.conf 语言编码 *

httpd-manual.conf apache扶助文书档案

httpd-mpm.conf     最洛桑接数 *

httpd-multilang-errordoc.conf 错页面 *

httpd-ssl.conf    ssl避孕套接字访问

httpd-userdir.conf 用户主目录配置

httpd-vhosts.conf 虚拟主机

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有