实验环境:centos7.6,2G内存,50G硬盘大小,虚拟机ip:172.16.1.7,172.16.1.8,172.16.1.9
利用nginx服务搭建一个网站
第一步:编写虚拟主机配置文件
cd /etc/nginx/conf.d/
vim www.conf
server{
listen 80; --端口
server_name www.zhangsb.com; --域名
location /{
root /usr/share/nginx/html; --站点目录
index index.html; --首页文件
}
}
第二步:写一个网页代码
cd /usr/share/nginx/html
vim zsb.html
<html>
<meta charset="utf-8">
<head>
<title>陋室小屋</title>
</head>
<style>
a{
text-decoration:none;
}
</style>
<body>
<a href="http://www.binamiao.xyz">
欢迎来到陋室小屋!!!
</a>
</body>
</html>
第三步:重启服务
每次修改配置文件,都要重启服务
重启服务的方法:
1.systemctl reload nginx(配置文件中涉及到地址的修改就用这个)
2.nginx -t reload(一般用这个)
nginx命令参数
-t : test configuration and exit
检查测试配置文件语法
-s : send signal to a master process: stop, quit, reopen, reload
控制服务停止或者重新启动
第四步:在Windows的hosts文件添加
10.0.0.7 www.zhangsb.com
第五步:测试
在浏览器输入:www.zhangsb.com/zsb.html
部署搭建网站常见错误:
01. 网站服务配置文件编写不正确
404 错误
解决方法一: 修改nginx配置文件---location
解决方法二: 在站点目录中创建相应目录或文件数据信息
403 错误
解决方法一: 不要禁止访问
解决方法二: 因为没有首页文件
02. DNS信息配置不正确
03. nginx配置文件修改一定要重启服务;
站点目录中代码文件信息调整,不需要重启服务
利用nginx服务搭建一个多网站
第一步:创建多个虚拟配置文件
创建多个虚拟配置文件
cd /etc/nginx/conf.d/
vim bbs.conf
server{
listen 80;
server_name bbs.zhangsb.com;
location /{
root /html/blog;
index index.html;
}
}
vim blog.conf
server{
listen 80;
server_name blog.zhangsb.com;
location /{
root /html/blog;
index index.html;
}
}
vim www.conf
server{
listen 80;
server_name www.zhangsb.com;
location /{
root /html/www;
index index.html;
}
}
nginx -t --可以检查配置文件语法是否正确
第二步:创建站点目录和首页文件
mkdir -p /html/{www,bbs,blog}
for name in {www,bbs,blog};do echo "10.0.0.7 $name.zhangsb.com" >/html/$name/index.html ;done
for name in {www,bbs,blog};do cat /html/$name/index.html ;done
第三步:在Windows的hosts文件添加
10.0.0.7 www.zhangsb.com bbs.zhangsb.com blog.zhangsb.com
第四步:测试
Windows:
linux:
虚拟主机访问方式:
a.基于地名的方式进行访问
b.基于地址的方式进行访问(只能用地址访问)
server {
listen 10.0.0.7:80;
server_name www.zhangsb.com;
location / {
root /html/www;
index index.html;
}
}
c.基于端口的访问,访问时要写端口,默认是80
server {
listen 8080;
server_name www.zhangsb.com;
location / {
root /html/www;
index index.html;
}
}
ps:网站页面的访问原理:(想找出ip地址对应的主机,在找到符合端口的主机,显示该主机的网站页面)
01. 将域名进行解析 www.zhangsb.com — 10.0.0.7
02. 建立TCP的连接(四层协议) 10.0.0.7 目标端口 80
03. 根据应用层HTTP协议发出请求
请求报文: hosts: bbs.zhangsb.com
04. 没有相同域名的server主机,会找满足端口要求的第一个主机
显示主机的网站页面