linux apache虚拟主机配置(基于ip,端口,域名)
配置环境:
linux版本:Centos6.4
httpd版本:
[root@centos64Study init.d]# pwd
/etc/init.d
[root@centos64Study init.d]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Oct 19 2017 16:43:38
1,安装httpd服务
yum install httpd -y
2,关闭selinux和防火墙
临时设置selinux为permissive( disabled )状态
[root@centos64Study ~]# getenforce
Enforcing
[root@centos64Study ~]# setenforce 0
[root@centos64Study ~]# getenforce
Permissive
永久修改方法:
[root@centos64Study ~]# vim /etc/sysconfig/selinux
SELINUX=permissive
3,关闭防火墙
service iptables stop
第一种,基于多ip访问的配置:
1,先把httpd.conf备份一下,以防出错,可以恢复
httpd.conf的文件路径( /etc/httpd/conf )
[root@centos64Study conf]# ls
httpd.conf  httpd.conf.bak  magic
2,如果需要启用虚拟主机配置,在文件httpd.conf中,先把中心主机的配置注释
#DocumentRoot "/var/www/html"
3,在httpd.conf中,会默认包conf.d目录中的所有 以.conf结尾的配置文件
Include conf.d/*.conf
所以,把虚拟主机的配置文件独立出来放在conf.d目录下
[root@centos64Study httpd]# ls
conf conf.d logs modules run
[root@centos64Study httpd]# pwd
/etc/httpd[root@centos64Study httpd]# cd conf.d
[root@centos64Study conf.d]# pwd
/etc/httpd/conf.d
[root@centos64Study conf.d]# ls
README VirtualHost.conf welcome.conf
VirtualHost.conf 配置文件内容:
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost><VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>
我的主机ip是192.168.1.8,添加另一个ip:
ip addr add 192.168.1.2/24 dev eth0
在对应的目录建立文件:
[root@centos64Study www]# tree
.
├── 7mxt.com
│   └── index.html
└── 7mxt.net
      └── index.html
重启服务: service httpd restart,在浏览器中分别用这两个ip访问,就能看到对应的页面
第二种,基于多端口访问的配置:
1,在VirtualHost.conf中增加一项配置:
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost>
<VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>
<VirtualHost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>
2,在httpd.conf监听8080端口
Listen 80
Listen 8080
3,/www建立对应的目录和文件
重启服务: service httpd restart,在浏览器中用http://192.168.1.2:8080/ 就能访问到abc.net目录下面的文件index.html内容
[root@centos64Study www]# tree
.
├── 7mxt.com
│   └── index.html
├── 7mxt.net
│   └── index.html
└── abc.net
      └── index.html
第三种:基于域名的主机配置
1,NameVirtualHost:指定192.168.1.8:80这个ip地址使用域名解释
NameVirtualHost 192.168.1.8:80
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost><VirtualHost 192.168.1.8:80>
ServerName www.7mxt.org
DocumentRoot "/www/7mxt.org"
</VirtualHost><VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost><VirtualHost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>
2,在对应的目录下建立文件
[root@centos64Study www]# tree
.
├── 7mxt.com
│   └── index.html
├── 7mxt.net
│   └── index.html
├── 7mxt.org
│   └── index.html
└── abc.net
      └── index.html
3,在windows host文件中增加主机映射配置
【C:\Windows\System32\drivers\etc】
192.168.1.8 www.7mxt.com
192.168.1.8     www.7mxt.org
4,重启服务: service httpd restart,分别用域名www.7mxt.com, www.7mxt.org就能访问到对应的文件内容
使用ip: 192.168.1.8返回的是第一个虚拟主机的配置,即:www.7mxt.com对应目录下面的内容
----------------------------------------------------------------------------------------------------------------------------------------------------
更详细的配置信息:
NameVirtualHost 192.168.1.8:80
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
CustomLog /var/log/httpd/7mxt.com/access_log combined
<Directory "/www/7mxt.com">
AllowOverride AuthConfig
AuthType Basic
AuthName "Restricted Site..."
AuthUserFile "/etc/httpd/conf/htpasswd"
AuthGroupFile "/etc/httpd/conf/htgroup"
#Require user tom
Require group myusers
#Require valid-user
</Directory>
</VirtualHost><VirtualHost 192.168.1.8:80>
ServerName www.7mxt.org
DocumentRoot "/www/7mxt.org"
CustomLog /var/log/httpd/7mxt.org/access_log combined
</VirtualHost><VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"<VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
CustomLog /var/log/httpd/7mxt.net/access_log combined
<Directory "/www/7mxt.net">
Options none
AllowOverride none
Order deny,allow
Deny from 192.168.1.100
</Directory>
</VirtualHost><VirtualHost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>
1,www.7mxt.com这个域名下面的Directory配置为 需要用户验证,支持3种情况的配置:
。只允许某个用户访问
。只允许文件中的用户
。只允许组文件中定义的用户
AllowOverride AuthConfig:  需要授权
AuthType Basic:基本授权
AuthName "Restricted Site..." :授权说明
AuthUserFile "/etc/httpd/conf/htpasswd"   :  需要授权的用户 保存在这个文件中,这个文件中的内容用
htpasswd这个命令创建,第一次带参数c表示创建,第二次不需要带c
htpasswd -c -m /etc/httpd/conf/htpasswd ghost
htpasswd -m /etc/httpd/conf/htpasswd ghostwu
AuthGroupFile "/etc/httpd/conf/htgroup": 需要授权的用户保存在这个文件中
#Require user ghost:只允许ghost这个用户登录
Require group myusers: 只允许myusers这个组中的用户登录
#Require valid-user:只允许htpasswd这个文件中定义的用户登录
[root@centos64Study conf]# ls
htgroup  htpasswd  httpd.conf  httpd.conf.bak  magic
[root@centos64Study conf]# cat htgroup 
myusers: ghost ghostwu
[root@centos64Study conf]# cat htpasswd 
ghost:$apr1$IeKI2YLO$RX8EjYBoHbMzCkHocFK0l/
ghostwu:$apr1$LoA/O0i0$0/EJu3xTXo2yCMimMUAcQ.
2,CustomLog /var/log/httpd/7mxt.org/access_log combined
自定义访问日志的文件路径,【注意:需要在/var/log/httpd下面存在7mxt.org这个目录】
3,
<Directory "/www/7mxt.net">
Options none
AllowOverride none
Order deny,allow
Deny from 192.168.1.100
</Directory>
这个配置针对的是7mxt.net,禁止192.168.1.100访问7mxt.net,其他的ip都允许
linux apache虚拟主机配置(基于ip,端口,域名)的更多相关文章
- Apache虚拟主机配置(多个域名访问多个目录)
		
Apache虚拟主机配置(多个域名访问多个目录) 为了方便管理虚拟主机,我决定使用一种方法,那就是修改httpd-vhosts.conf文件. 第一步首先要使扩展文件httpd-vhosts.conf ...
 - Apache虚拟主机配置(多个域名访问多个目录)(转)
		
Apache虚拟主机配置(多个域名访问多个目录) 为了方便管理虚拟主机,我决定使用一种方法,那就是修改httpd-vhosts.conf文件. 第一步首先要使扩展文件httpd-vhosts.conf ...
 - Linux Apache虚拟主机配置方法
		
apache 虚拟主机配置 注意: 虚拟主机可以开很多个 虚拟主机配置之后,原来的默认/etc/httpd/httpd.conf中的默认网站就不会生效了 练习: 主机server0 ip:172.25 ...
 - apache 虚拟主机配置(根据不同的域名映射到不同网站)
		
最近弄了台香港服务器做测试,Web服务器软件用的是Apache2.2,机器只有一台,ip只有一个,但是想测试几个站点,于是尝试了下Apache的虚拟主机配置.之前已经写过一篇博文了——<Apac ...
 - Apache虚拟主机配置,实现多域名访问本地项目PHP空间,以及配置403Forbidden等错误的解决办法
		
第一步: apache主配置文件修改: 用文本编辑器打开apache的conf目录下 httpd.conf 将下面以下代码取消注释 LoadModule rewrite_module modules ...
 - Apache 虚拟主机配置
		
开放虚拟主机文件 修改主配置文件 解开注释,使用虚拟主机配置文件. vim /usr/local/apache2/conf/httpd.conf Include conf/extra/httpd-vh ...
 - Linux下Apache虚拟主机配置
		
Linux下Apache虚拟主机的三种配置.这样可以实现一台主机架构多个独立域名网站.其中基于域名的最为常见.性价比也最高.下面PHP程序员雷雪松详细的讲解下Linux下Apache虚拟主机配置的具体 ...
 - Apache虚拟主机配置
		
在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录.Apache虚拟主机配置有3中方法:基于IP配置.基于域名配置和基于端口配置,这里介 ...
 - apache虚拟主机配置及解析
		
Apache虚拟主机配置及解析 1.修改httpd-vhosts.conf 打开apache(Apache24)/conf/extra/httpd-vhosts.conf文件,添加虚拟主机信息,可以这 ...
 
随机推荐
- TP3.2 图片上传及缩略图
			
基于TP自带的上传文件的类, Think/Upload.class.php 设置表单的enctype属性 下面是上传的具体方法 /** * 图片上传处理 * @param [String] $path ...
 - 使用背景图修改radio、checkbox样式
			
如果觉得设置样式太麻烦,或者页面上选中的样式太复杂,也可以用背景图去修改样式<div class=""> <label><input type=&qu ...
 - 【SICP练习】151 练习4.7
			
练习4-7 原文 Exercise 4.7. Let* is similar to let, except that the bindings of the let variables are per ...
 - 【打CF,学算法——三星级】Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
			
[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/C 题面: C. Gerald's Hexagon time limit per tes ...
 - 基于python的web应用开发-添加关注者
			
社交web允许用户之间相互联系. 例如: 关注者.好友.联系人.联络人或伙伴. 记录两个用户之间的定向联系,在数据库查询中也要使用这种联系. 一.论数据库关系 一对多关系 数据库使用关系建立记录之间的 ...
 - iOS10 相册权限
			
当我升级到Xcode8后,启动我的相机项目,直接crash,输出的日志如下: '2016-07-08 16:41:11.268943 project-name[362:56625] [MC] Syst ...
 - 《TCP-IP详解卷2:实现》【PDF】下载
			
<TCP-IP详解卷2:实现>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062539 内容简介 <TCP/IP详解·卷2 ...
 - 2.python数据类型
			
1 Number(数字) 2 字符串类型(string) 字符串内置方法 # string.capitalize() 把字符串的第一个字符大写 # string.center(width) 返 ...
 - 【java】打印流的基本实现及java.io.PrintStream、java.io.PrintWriter示例
			
package 打印流; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impor ...
 - 解读JavaScript原型链
			
var F = function(){}; F.prototype.a = function(){}; Object.prototype.b = function(){}; Function.prot ...