虚拟主机基于域名、端口、IP访问nginx
一、虚拟主机基于域名访问nginx
1.为虚拟主机提供域名解析
echo "192.168.181.130 www.nj.com www.benet.com" >> /etc/hosts
2.为虚拟主机准备网页文档
mkdir -p /var/www/html/benet
mkdir -p /var/www/html/nj
echo"<h1>www.nj.com</h1>" > /var/www/html/nj/index.html
echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html
3.修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.benet.com;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 80;
server_name www.nj.com;
location / {
root /opt/tank;
index index.html;
}
}
4.重启服务,访问测试
cd /usr/local/nginx/sbin
./nginx
ps -ef | grep nginx 查看是否启动成功命令
二、虚拟主机基于IP访问nginx
1.添加网卡
iflconfig ens33:0 192.168.80.11 netmask 255.255.255.0
2.修改Nginx的配置文件
server {
listen 80;
server_name 192.168.116.91;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 80;;
server_name 192.168.116.90;
location / {
root /opt/tank;
index index.html;
}
}
3.重启服务,访问测试
三、虚拟主机基于端口访问nginx
vim /usr/local/nginx/conf/nginx.conf
server {
listen 8080;
server_name 192.168.116.90;
location / {
root /opt/Super_Marie;
index index.html;
}
}
server {
listen 8888;
server_name 192.168.116.91;
location / {
root /opt/tank;
index index.html;
}
}
虚拟主机基于域名、端口、IP访问apache
一、虚拟主机基于域名访问apache
1,为虚拟主机提供域名解析
vim /var/named/chroot/var/named/kgc.com.zone
@ IN NS dnssvr.kgc.com.
dnssvr IN A 173.17.17.2
www IN A 173.17.17.11
vim /var/named/chroot/var/named/kcce.com.zone
@ IN NS dnssvr.kgc.com.
www IN A 173.17.17.11
2,为虚拟主机准备网页文档
mkdir -p /var/www/html/kgccom
mkdir -p /var/www/html/kccecom
echo "<h1 > www.kgc.com</h1>" >/var/www/html/kgccom/index.html
echo "<h1 > www.kcce.com </h1>" >/var/www/html/kccecom/index.html
3,添加虚拟主机配置
vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/kgccom"
ServerName www.kgc.com
ErrorLog "logs/www.kgc.com.error_log"
CustomLog "logs/www.kgc.com.access_log" common
< Directory "/var/www/html" >
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/kccecom"
ServerName www.kcce.com
<VirtualHost>
在虚拟机设置中添加网卡第二张网卡,并显示
4,修改区域配置文件
[root@localhost ~]# vim /etc/named.rfc1912.zones
//找到以下格式,按5yy复制五行,在任意一个5行的最后一行按o在下一行插入,修改其中域名
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
修改完成后按Esc,输入:wq保存退出
5,修改区域数据配置文件
[root@localhost ~]# cd /var/named/
[root@localhost named]# ls
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.56.131 //此处为添加的主机A记录,地址指向自己
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# vim kgc.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.56.131
//此处的文件内容不需要做任何修改
可直接输入:q退出
//为方便后面测试解析,接下来我们启动服务,并关闭防火墙及安全功能
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
6,做虚拟主机配置文件
[root@localhost conf]# mkdir extra
[root@localhost conf]# cd extra/
[root@localhost extra]# vim vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/accp/" //站点目录
ServerName www.accp.com //域名
ErrorLog "logs/www.accp.com.error_log" //错误日志
CustomLog "logs/www.accp.com.access_log" common //访问日志
<Directory "/var/www/html/">
Require all granted //访问权限为允许所有
</Directory>
</VirtualHost>
按Esc退出插入模式,输入:11,19 s/accp/kgc/g //把从11行到19行把其中的accp改成kgc,做全局替换
11 <VirtualHost *:80>
12 DocumentRoot "/var/www/html/kgc/"
13 ServerName www.kgc.com
14 ErrorLog "logs/www.kgc.com.error_log"
15 CustomLog "logs/www.kgc.com.access_log" common
16 <Directory "/var/www/html/">
17 Require all granted
18 </Directory>
19 </VirtualHost>
7,创建站点
[root@localhost httpd]# cd /var/www/html/
[root@localhost html]# mkdir accp kgc
[root@localhost accp]# vim index.html
<h1>this is accp web</h1>
[root@localhost accp]# cd ../kgc/
[root@localhost kgc]# vim index.html
<h1>this is kgc web</h1>
在主配置文件中的包含语句需要进行检查,因为之前的写入内容未在主配置文件中进行声明:
[root@localhost conf]# vim httpd.conf
//按大写字母G到末行,按o在下行插入
Include conf/extra/vhost.conf
输入完成后按Esc,输入:wq保存退出
[root@localhost extra]# systemctl start httpd
8,访问
二、虚拟主机基于端口访问apache
1,修改端口
[root@www ~]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
< VirtualHost 173.1 7.17.11:80>
DocumentRoot "/var/www/htm/kccecom"
ServerName www.kcce.com
</VirtualHost>
<VirtualHost 173.17.17.11:8353>
DocumentRoot "/var/www/htm/kccepad"
ServerName www.kcce.com
</virtualHost>
Listen 80
Listen 8353
#监听端口写在主配置文件中
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir kgc02
[root@localhost html]# cd kgc02/
[root@localhost kgc02]# vim index.html
<h1>this is kgc02 web</h1>
[root@localhost kgc02]# vim /etc/httpd/conf/httpd.conf
输入:/Listen查到到监听端口位置
Listen 192.168.56.131:80
Listen 192.168.56.131:8080
#Listen 80
2,查看进程
[root@localhost kgc02]# netstat -ntap
三、虚拟主机基于IP访问apache
1,修改配置文件
[root@localhost kgc02]# cd /etc/httpd/conf/extra/
[root@localhost extra]# vim vhost.conf
<VirtualHost 192.168.56.131:80>
DocumentRoot "/var/www/html/accp/"
ErrorLog "logs/www.accp.com.error_log"
CustomLog "logs/www.accp.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.56.134:80>
DocumentRoot "/var/www/html/accp02/"
ErrorLog "logs/www.accp02.com.error_log"
CustomLog "logs/www.accp02.com.access_log" common
<Directory "/var/www/html/">
Require all granted
</Directory>
</VirtualHost>
#修改完成后按Esc,输入:wq保存退出
2,创建目录站点
[root@localhost extra]# cd /var/www/html/
[root@localhost html]# mkdir accp02
[root@localhost html]# cd accp02/
[root@localhost accp02]# vim index.html
<h1>this is 134 accp02 web</h1>
[root@localhost accp02]# vim ../accp/index.html
<h1>this is 131 accp web</h1>
#修改完成后按Esc,输入:wq保存退出
3,修改监听地址
[root@localhost accp02]# vim /etc/httpd/conf/httpd.conf
Listen 192.168.56.131:80
Listen 192.168.56.134:80
#Listen 192.168.56.131:8080