当前位置: 首页 > news >正文

(Linux)搭建静态网站——基于http/https协议的静态网站

简单了解nginx配置文件

在这里插入图片描述

1.下载并开启nginx服务

  1. 下载
    [root@localhost ~]# dnf install nginx -y
  2. 开启
    [root@localhost ~]# systemctl restart nginx

1.(1)搭建静态网站——基于http协议的静态网站

实验1:搭建一个web服务器,访问该服务器时显示“hello world”欢迎界面
  1. 进入指定目录文件下,/usr/share/nginx/html 是 Nginx Web 服务器的默认根目录,用于存放静态文件。当你访问配置了 Nginx 的域名或 IP 地址时,Nginx 会从这个目录中查找并返回相应的文件。
    [root@localhost ~]# cd /usr/share/nginx/html

  2. 写入指定内容"hello world"

    [root@localhost html]# echo "hello world" > index.html
    #或者
    [root@localhost html]# vim index.html
    hello world
    
  3. 使用 curl 工具来发送 HTTP 请求,并获取响应(验证是否配置成功)

    #向本地服务器发送一个 HTTP 请求,并返回服务器的响应
    [root@localhost html]# curl localhost
    hello world
    #或者
    #向该 IP 地址发送一个 HTTP 请求,并返回服务器的响应。
    [root@localhost html]# curl 192.168.190.131
    hello world
    
实验2:建立两个基于ip地址访问的网站,要求如下
  • 该网站ip地址的主机位为100,设置首页目录为/www/ip/100,网页内容为:this is 100
  • 该网站ip地址主机位为200,设置首页目录为/www/ip/200,网页内容为:this is 200
  1. 创建文件

    [root@localhost ~]# mkdir -pv /www/ip/{1,2}00
    mkdir: created directory '/www'
    mkdir: created directory '/www/ip/100'
    mkdir: created directory '/www/ip/200'#显示目录 /www/ 的状态信息
    [root@localhost ~]# stat /www/File: /www/Size: 32        	Blocks: 0          IO Block: 4096   directory
    Device: fd00h/64768d	Inode: 102252363   Links: 4
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Context: unconfined_u:object_r:default_t:s0
    Access: 2024-11-06 16:33:32.107790537 +0800
    Modify: 2024-11-06 16:33:32.109790471 +0800
    Change: 2024-11-06 16:33:32.109790471 +0800Birth: 2024-11-06 16:33:32.107790537 +0800
    
  2. 添加IP地址

    [root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.168.100/24 +ipv4.gateway 192.168.168.2 ipv4.dns 114.114.114.114 ipv4.method manual autoconnect yes[root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.190.200/24 #激活ens160的网络连接。
    [root@localhost ~]# nmcli connection up ens160
    

    或者:
    [root@localhost ~]# nmtui–利用图形化工具更便捷

    在这里插入图片描述

    image-20241106160915150

    在这里插入图片描述

  3. 配置子配置文件
    setenforce 0 命令将 selinux 的模式设置为 Permissive 模式。在 Permissive 模式下,selinux 会记录违反策略的行为,但不会阻止这些行为。换句话说,系统会继续执行操作,但会记录下所有违反 selinux 策略的行为。

    [root@localhost ~]# cd /etc/nginx/conf.d
    [root@localhost conf.d]# vim test_ip.conf
    server {listen 192.168.190.100:80;server_name _;root /www/ip/100;
    }
    server {listen 192.168.190.200:80;server_name _;root /www/ip/200;
    }
    #设置selinux,必须设置,否则无法看到网页页面内容
    [root@localhost conf.d]# setenforce 0
    [root@localhost conf.d]# curl 192.168.190.100
    this is 100
    [root@localhost conf.d]# curl 192.168.190.200
    this is 200
    
实验3:建立两个基于不同端口访问的网站,要求如下:
  • 建立一个使用web服务器默认端口的网站,设置网站首页目录为/www/port/80,网页内容为:the port is 80。
  • 建立一个使用10000端口的网站,设置网站首页目录为/www/port/10000,网页内容为:the port is 10000。
#创建文件
[root@localhost ~]# mkdir /www/port/{80.10000}
#对应分别写入index.html内容
[root@localhost ~]# echo this port is 80 > /www/port/80/index.html
[root@localhost ~]# echo this port is 10000 > /www/port/10000/index.html
#切换目录(便于操作)
[root@localhost ~]# cd /etc/nginx/conf.d/
#在conf文件中写入服务连接
[root@localhost conf.d]# vim test_port.conf 
server {listen 192.168.190.10:80;server_name _;root /www/port/80;
}server {listen 192.168.190.10:10000;root /www/port/10000;location / {}
}   
#重启服务生效
[root@localhost ~]# systemctl restart nginx
#用curl验证是否生效
[root@localhost ~]# curl 192.168.190.10:80
the port is 80
[root@localhost ~]# curl 192.168.190.10:10000
the port is 10000
实验4:建立两个基于域名访问的网站,要求如下:
  • 新建一个网站,域名为www.ceshi.com,设置网站首页目录为/www/name,网页内容为this is test。
  • 新建一个网站,域名为rhce.first.day,同时可通过ce.first.day访问,设置网站首页目录为/www/ce,网页内容为:today is first day of class
#创建目录文件
[root@localhosts ~]# mkdir /www/{name,ce}
#写内容到index.html
[root@localhosts ~]# echo today is first day of class >> /www/ce/index.html
[root@localhosts ~]# echo this is test >> /www/name/index.html
#创建并编辑网页访问文件
[root@localhosts conf.d]# vim test_servername.conf
#添加如下内容
server {listen 192.168.190.10:80;server_name www.ceshi.com;root /www/name;
}
server {listen 192.168.190.10:80;server_name rhce.first.day ce.first.day;root /www/ce;location / {}
}       
[root@localhosts conf.d]# vim /etc/hosts
#添加如下内容
192.168.190.10 localhosts www.ceshi.com rhce.first.day ce.first.day
#查看修改的内容是否有问题
[root@localhosts conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启服务
[root@localhosts conf.d]# systemctl restart nginx
#访问
[root@localhosts conf.d]# curl www.ceshi.com
this is test
[root@localhosts conf.d]# curl rhce.first.day
today is first day of class
[root@localhosts conf.d]# curl ce.first.day
today is first day of class
实验5:基于虚拟目录和用户控制的web网站
#网页认证自动生成储存用户密码和用户名的文件---需要下载httpd-tools包来提供相应的服务
[root@localhosts ~]# dnf install httpd-tools -y
#创建用户
[root@localhosts nginx]# htpasswd -cb /etc/nginx/conf.d/auth-password user1 123
#新建文件目录--作为实际访问目录,并写入实际访问的内容index.html
[root@localhosts ~]# mkdir /www/real
[root@localhosts ~]# echo real > /www/real/index.html
#编辑网页访问
[root@localhosts conf.d]# vim  test_virtual.conf
server{listen 192.168.190.131:80;root /usr/share/nginx/index;location /real {alias /www/real;auth_basic on;auth_basic_user_file /etc/nginx/conf.d/auth_password;}}
[root@localhosts conf.d]# nginx -t
[root@localhosts conf.d]# systemctl restart nginx
#访问
[root@localhosts conf.d]#  curl user1:123@192.168.190.131/real/
real
[root@localhosts conf.d]#  curl 192.168.190.131/real/ -u user1
Enter host password for user 'user1':
real

1.(2)搭建静态网站——基于https协议的静态网站

(1).添加IP
# 添加ip
[root@localhosts ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.190.20/24
# 激活更新网卡
[root@localhosts ~]# nmcil c up ens160
(2).创建访问路径
[root@localhosts ~]# mkdir -pv /www/https
# 写入index.html内容
[root@localhosts ~]# echo https > /www/https/index.html
(3).生成私钥和证书

/etc/pki/tls/certs/ 目录通常存储与 TLS(传输层安全性)相关的证书文件,这些文件在 Linux 系统中用于确保安全通信

[root@localhosts ~]# cd /etc/pki/tls/certs/
# 得到一个包含新生成的 RSA 私钥的文件 https.key
[root@localhosts certs]# openssl genrsa -out https.key
#生成一个自签名的 X.509 证书,并将其保存到名为 https.crt 的文件中
[root@localhosts certs]# openssl req -utf8 -new -key https.key -x509 -days 100 - https.crt
# 查看是否成功
[root@localhosts certs]# ls
ca-bundle.crt  ca-bundle.trust.crt  https.crt  https.key
(4).配置网页访问配置文件
[root@localhosts certs]# vim /etc/nginx/conf.d/test_https.conf
[root@localhosts certs]# cat /etc/nginx/conf.d/test_https.conf
server {listen 192.168.190.20:443 ssl;root /www/https;ssl_certificate /etc/pki/tls/certs/https.crt;ssl_certificate_key /etc/pki/tls/certs/https.key;location / {}
}
# 检验.conf文件是否能够生效
[root@localhosts certs]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重启生效
[root@localhosts certs]# systemctl restart nginx
(5).访问测试
[root@localhosts certs]# curl --insecure https://192.168.190.20
https
[root@localhosts certs]# curl -k https://192.168.190.20
https
http://www.lryc.cn/news/488379.html

相关文章:

  • 【机器学习chp3】判别式分类器:线性判别函数、线性分类器、广义线性分类器、分段线性分类器
  • 【学习】【HTML】HTML、XML、XHTML
  • ARM中ZI-data段和RW-data段
  • 关联度分析、灰色预测GM(1,1)、GM(1,1)残差模型——基于Python实现
  • linux常用命令(网络相关)
  • 【uni-app多端】修复stmopjs下plus-websocket无心跳的问题
  • VScode学习前端-01
  • Java-05 深入浅出 MyBatis - 配置深入 动态 SQL 参数、循环、片段
  • 突破自动驾驶瓶颈!KoMA:多智能体与大模型的完美融合
  • YOLO入门教程(三)——训练自己YOLO11实例分割模型并预测【含教程源码+一键分类数据集 + 故障排查】
  • 【加入默语老师的私域】C#面试题
  • 称重传感器指示器行业全面且深入的分析
  • NAT网络地址转换——Easy IP
  • 【Visual Studio系列教程】如何在 VS 上编程?
  • Mybatis-Plus 多租户插件属性自动赋值
  • AWTK-WIDGET-WEB-VIEW 实现笔记 (4) - Ubuntu
  • Python入门(7)--高级函数特性详解
  • 【数据库原理】理解数据库,基础知识
  • VConsole——(H5调试工具)前端开发使用于手机端查看控制台和请求发送
  • 论文分享 | FuzzLLM:一种用于发现大语言模型中越狱漏洞的通用模糊测试框架
  • vmWare虚拟环境centos7安装Hadoop 伪分布式实践
  • 【C++入门(一)】半小时入门C++开发(深入理解new+List+范围for+可变参数)
  • Vue 3与TypeScript集成指南:构建类型安全的前端应用
  • MATLAB和Python发射光谱
  • IEEE(常用)参考文献引用格式详解 | LaTeX参考文献规范(IEEE Trans、Conf、Arxiv)| 期刊会议名缩写查询
  • 第二十周:机器学习
  • Elasticsearch面试内容整理-Elasticsearch 基础概念
  • 机器学习算法模型系列——Adam算法
  • Qt按钮类-->day09
  • 基于xr-frame实现微信小程序的手部、手势识别3D模型叠加和石头剪刀布游戏功能