虚拟主机类型
基于域名的虚拟主机
为每个虚拟主机使用不同的域名,但是其对应的IP地址是相同的。例如www.kgc.com和www.accp.com两个站点的IP都是192.168.17.130.这是使用最为普遍的虚拟web主机类型
基于IP地址的虚拟主机
为每个虚拟主机使用不同的域名,且各自对应的IP地址也不相同。这种方式需要为服务器配备多个网络接口,因此应用并不是非常广泛
基于端口的虚拟主机
这种方式并不使用域名、IP地址来区分不同的站点内容,而是使用不同的TCP端口号,因此用户在浏览不同的虚拟站点时需要同时指定端口号才能访问。
首先我们先把Nginx装起来
#先安装语言环境包
[root@localhost ~]# yum install -y gcc gcc-c++ pcre* zlib-devel
#创建程序用户
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
#挂载并解压
[root@localhost ~]# mount.cifs //192.168.17.1/LNMP /abc
Password for root@//192.168.17.1/LNMP:
[root@localhost ~]# cd /abc
[root@localhost abc]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.bz2
mysql-boost-5.7.20.tar.gz php-5.6.11.tar.bz2 php-7.1.20.tar.gz
ncurses-5.6.tar.gz php-7.1.10.tar.bz2 zend-loader-php5.6-linux-x86_64_update1.tar.gz
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt
#执行编译安装
[root@localhost abc]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2 rh
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
#优化路径,便于系统识别nginx命令
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#优化,制作管理脚本,便于使用service管理
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start |stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@localhost nginx-1.12.2]# chkconfig --add nginx
[root@localhost nginx-1.12.2]# chkconfig --level 35 nginx on
#现在我们做的是基于域名进行解析,所以现在需要配置DNS域名解析
[root@localhost nginx-1.12.2]# yum install -y bind
[root@localhost nginx-1.12.2]# 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";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
…………省略内容
[root@localhost nginx-1.12.2]# vim /etc/named.rfc1912.zones
…………省略
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
…………省略
[root@localhost nginx-1.12.2]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost 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.17.146
[root@localhost named]# cp -p kgc.com.zone accp.com.zone
[root@localhost named]# systemctl start named
#进入主配置文件添加虚拟主机域名,在文件末尾最后一个大括号上面添加
注意:大括号是相对应的,不可多添,也不可少
vim /usr/local/nginx/conf/nginx.conf
#现在我们去创建站点目录及站点内容
[root@localhost named]# cd /var
[root@localhost var]# ls
account cache db games kerberos local log named opt run target yp
adm crash empty gopher lib lock mail nis preserve spool tmp
[root@localhost var]# mkdir -p www/kgc www/accp
[root@localhost var]# ls
account cache db games kerberos local log named opt run target www
adm crash empty gopher lib lock mail nis preserve spool tmp yp
[root@localhost var]# cd www
[root@localhost www]# ls
accp kgc
[root@localhost www]# echo "hello ! kgc" > kgc/index.html
[root@localhost www]# echo "hello ! accp" > accp/index.html
[root@localhost www]# cat kgc/index.html
hello ! kgc
[root@localhost www]# cat accp/index.html
hello ! accp
[root@localhost www]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost www]# service nginx start
#关闭防火墙
[root@localhost www]# systemctl stop firewalld.service
[root@localhost www]# setenforce 0
#进行访问域名验证
#进入nginx主配置文件
[root@localhost www]# vim /usr/local/nginx/conf/nginx.conf
#建立站点首页
[root@localhost named]# cd /var/www
[root@localhost www]# mkdir kgc8080
[root@localhost www]# ls
kgc kgc8080
[root@localhost www]# echo "kgc8080~" > kgc8080/index.html
[root@localhost www]# service nginx restart //重启服务
#使用不同端口进行访问
#首先先为服务器添加一块网卡
对DNS区域配置文件进行修改
[root@localhost ky02]# vim /etc/named.rfc1912.zones
[root@localhost named]# vim kgc.com.zone
[root@localhost named]# vim ky02.com.zone
#配置nginx主配置文件
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost named]# cd /var/www
[root@localhost www]# mkdir ky02
[root@localhost www]# ls
kgc ky02
[root@localhost www]# echo "ky02~" > ky02/index.html
[root@localhost www]# service nginx restart //重启服务
#使用不同IP进行访问