apache虚拟主机的使用
虚拟主机技术是使用一台服务器实现发布多个网站的技术。
实现一台服务器多个网站的方法有:基于网卡,基于端口,基于域名。
基于网卡:一台主机添加多块网卡,就相当于有多个IP
基于端口:一台主机通过同一个IP不同的端口
基于域名:一台主机通过同一个IP同一个端口不通的域名
Rpm版apache文件路径:
配置文件路径:/etc/httpd/conf/httpd.conf
网站根文件路径:/var/www/html/index.html
执行命令文件路径:/usr/sbin
日志文件路径:/var/log/httpd
源码版apache文件路径:
配置文件路径:/usr/local/apache/conf
网站根文件路径:/usr/local/apache/htdocs
执行命令文件路径:/usr/local/apache/bin
日志文件路径:/usr/local/apache/logs
Linux基于域名搭建虚拟主机实现不同网站发布步骤:
v 配置静态IP,关闭防火墙,注意apache的安装方式。
[[email protected] Desktop]# ifconfig eth0 192.168.1.1
[[email protected] Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[[email protected] Desktop]# iptables -F
[[email protected] Desktop]# service iptables save
[[email protected] Desktop]# setenforce 0
[[email protected] Desktop]# vim /etc/sysconfig/selinux
[[email protected] Desktop]# service httpd status
v 配置DNS,使用linux搭建bind服务器。
[[email protected] /]# rpm -ivh /media/RHEL_6.4\ x86_64\ Disc\ 1/Packages/bind-9.8.2-0.17.rc1.el6.x86_64.rpm
[[email protected] etc]# vim /etc/named.conf
修改参数:
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
zone "." IN {
type hint;
file "named.ca";
};
zone "a.com" IN {
type master;
file "a.zone";
};
zone "b.com" IN {
type master;
file "b.zone";
};
[[email protected] named]# cp /var/named/named.empty a.zone
[[email protected] named]# vim a.zone
添加参数:www A 192.168.1.1
[[email protected] named]# chown named.named a.zone
[[email protected] named]# cp a.zone b.zone
[[email protected] named]# service named restart
v 配置apache配置文件,打开vhost开关。
源码版:
[[email protected] conf]# vim /usr/local/apache/conf/httpd.conf
注释内容:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
[[email protected] conf]# vim /usr/local/apache/conf/extra/httpd-vhosts.conf
修改参数:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache/htdocs/a"
ServerName www.a.com
ErrorLog "/usr/local/apache/logs/error.log"
CustomLog "/usr/local/apache/accesslog/access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache/htdocs/b"
ServerName www.b.com
ErrorLog "/usr/local/apache/logs/error.log"
CustomLog "/usr/local/apache/accesslog/access.log" common
</VirtualHost>
v 创建相关文件夹和文件
[[email protected] apache]# mkdir accesslog
[[email protected] apache]# mkdir accesslog
[[email protected] apache]# mkdir /usr/local/apache/htdocs/a
[[email protected] apache]# mkdir /usr/local/apache/htdocs/b
[[email protected] a]# echo jintiantianqibucuo > index.html
[[email protected] b]# echo mingtianhuigenghao >index.html
使用用户密码安全性登陆访问网页:
v 创建用户认证数据文件并创建一个用户:
[[email protected] Desktop]# htpasswd -c /usr/local/apache/conf/.passwd ren
注释:-c:表示创建一个数据文件,没有改选项则是添加一个用户
v 添加用户授权配置:
[[email protected] Desktop]# vim /usr/local/apache/conf/httpd.conf
添加参数:
<directory "/usr/local/apache/htdocs/a">
authname "ren"
authtype basic
authuserfile /usr/local/apache/conf/.passwd
require valid-user
</directory>