www是world wide web的缩写,即全球信息广播系统。它通过结合文字、图形、影像和声音等多媒体元素,使用超链接技术,使用户可以通过Internet访问和获取所需的信息。当用户连接到一个www网站时,该网站会提供数据,用户的客户端需要使用浏览器来解析这些数据。
(1)静态网页:这些网页是单向的,仅提供信息浏览,不涉及用户与服务器之间的互动。用户可以查看内容,但不能上传数据。
(2)动态网站:这类网站允许服务器与用户进行互动,例如留言板和博客。这种互动通常通过网页程序语言(如PHP)结合数据库系统来实现数据的读写。此外,还有一种交互式动态网页主要在客户端实现,通过JavaScript或Flash动画格式在客户端执行程序。
[root@localhost ~]# dnf install nginx -y #使用 dnf 包管理器来安装 Nginx Web 服务器。-y 选项表示自动回答“是”以确认所有提示。
[root@localhost ~]# nginx -v #显示已安装的 Nginx 的版本信息。-v 选项表示版(version)。
[root@localhost ~]# nginx -V #显示已安装的 Nginx 的详细版本信息和编译时的配置参数。-V 选项表示详细版本(verbose version)。
[root@localhost ~]# rpm -ql nginx #列出与 Nginx 包相关的所有文件和目录。-q 选项表示查询(query),-l 选项表示列出文件(list files)。
[root@localhost httpd]# tree /etc/nginx #显示 /etc/nginx 目录下的文件和子目录结构。tree 是一个用于以树状图形式显示目录内容的命令。
[root@localhost ~]# systemctl disable firewalld --now #禁用并立即停止 firewalld 服务。systemctl 是用于控制 systemd 系统和服务管理器的命令,disable 选项表示禁用服务,--now 表示立即执行该操作。
[root@localhost ~]# setenforce 0 #将 SELinux(Security-Enhanced Linux)模式设置为“Permissive”(宽容模式)。在这种模式下,SELinux 不会强制执行安全策略,但会记录违反策略的行为。
[root@localhost ~]# getenforce #显示当前的 SELinux 模式。在这个例子中,输出为 Permissive,表示 SELinux 处于宽容模式。
[root@localhost ~]# systemctl restart nginx #重新启动 Nginx 服务。systemctl 是用于控制 systemd 系统和服务管理器的命令,restart 选项表示重启服务。
[root@localhost ~]# curl -I localhost #使用 curl 工具发送一个 HTTP 请求到本地服务器(即 localhost),并只获取响应头信息(-I 选项表示只获取头部信息)。这个命令通常用于测试 Web 服务器是否正常运行。
实验1:搭建一个web服务器,访问该服务器时显示“hello world”欢迎界面
[root@localhost ~]# echo "hello world" > /usr/share/nginx/html/index.html
[root@localhost ~]# curl localhost
hello world
[root@localhost ~]# curl 192.168.58.153
hello world
实验2:建立两个基于ip地址访问的网站,要求如下
该网站ip地址的主机位为100,设置首页目录为/www/ip/100,网页内容为:this is 100。
该网站ip地址主机位为200,设置首页目录为/www/ip/200,网页内容为:this is 200。
[root@localhost ~]# nmtui
#进入后添加192.168.58.100/24 192。168.58.200/24
[root@localhost ~]# nmcli connection up ens160
#创建两个网页文件根目录,并定义网页内容
[root@localhost ~]# mkdir -pv /www/ip/{100,200}
[root@localhost ~]# echo this is 100 > /www/ip/100/index.html
[root@localhost ~]# echo this is 200 > /www/ip/200/index.html
#设置selinux,必须设置,否则无法看到网页页面内容
[root@server html]# setenforce 0
[root@server html]# getenforce
Permissive
#定义基于不同ip地址来访问网站的配置文件
#新建文件,写入如下配置
[root@localhost ~]# vim /etc/nginx/conf.d/test_ip.conf
server {
listen 192.168.58.100:80;
root /www/ip/100;
location / {
}
}
server {
listen 192.168.58.200:80;
root /www/ip/200;
location / {
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl 192.168.58.100
this is 100
[root@localhost ~]# curl 192.168.58.200
this is 200
实验3:建立两个基于不同端口访问的网站,要求如下:
建立一个使用web服务器默认端口的网站,设置网站首页目录为/www/port/80,网页内容为:the
port is 80。
建立一个使用10000端口的网站,设置网站首页目录为/www/port/10000,网页内容为:the port
is 10000。
[root@localhost ~]# mkdir -pv /www/port/{80,10000}
[root@localhost ~]# echo the port is 80 > /www/port/80/index.html
[root@localhost ~]# echo the port is 10000 > /www/port/10000/index.html
[root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses
192.168.58.153/24
[root@localhost ~]# nmcli connection up ens160
[root@localhost ~]# cat /etc/nginx/conf.d/test_port.conf
server {
listen 192.168.58.153:80;
root /www/port/80;
location / {
}
}
server {
listen 192.168.58.153:10000;
root /www/port/10000;
location / {
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl 192.168.58.153:10000
the port is 10000
[root@localhost ~]# curl 192.168.58.153
the port is 80
实验4:建立两个基于域名访问的网站,要求如下:
新建一个网站,域名为www.ceshi.com,设置网站首页目录为/www/name,网页内容为this is
test。
新建一个网站,域名为rhce.first.day,同时可通过ce.first.day访问,设置网站首页目录
为/www/ce,网页内容为:today is first day of class。
[root@localhost conf.d]# nmcli connection modify ens160 +ipv4.addresses
192.168.58.154/24
[root@localhost conf.d]# nmcli connection up ens160
[root@localhost ~]# mkdir /www/{name,ce}
[root@localhost ~]# echo this is test > /www/name/index.html
[root@localhost ~]# echo today is first day of class > /www/ce/index.html
[root@localhost ~]# vim /etc/nginx/conf.d/test_servername.conf
server {
listen 192.168.58.154:80;
server_name www.ceshi.com;
root /www/name;
location / {
}
}
server {
listen 192.168.58.154:80;
server_name rhce.first.day ce.first.day;
root /www/ce;
location / {
}
}
[root@localhost ~]# vim /etc/hosts
192.168.58.154 www.ceshi.com rhce.first.day ce.first.day
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl www.ceshi.com
this is test
[root@localhost ~]# curl rhce.first.day
today is first day of class
[root@localhost ~]# curl ce.first.day
today is first day of class
HTTP的缺陷:
HTTPS的介绍:
工作原理:
优点:
[rootlocalhost ~]# nmcli connection modify ens160 +ipv4.addresses
192.168.58.156/24
[root@localhost ~]# nmcli connection up ens160
[root@localhost ~]# mkdir -pv /www/https/
[root@localhost ~]# echo https > /www/https/index.html
[root@localhost conf.d]# cd /etc/pki/tls/certs/
[root@localhost certs]# openssl genrsa -out https.key
[root@localhost certs]# openssl req -utf8 -new -key https.key -x509 -days
100 -out https.crt
[[root@localhost ~]# cat /etc/nginx/conf.d/test_https.conf
server {
listen 192.168.58.156:443 ssl;
root /www/https;
ssl_certificate /etc/pki/tls/certs/https.crt;
ssl_certificate_key /etc/pki/tls/certs/https.key;
location / {
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl --insecure https://192.168.58.156
https
[root@localhost ~]# curl -k https://192.168.58.156
https
动态网站并不是指具有动画功能的网站,而是指网站内容能够根据不同情况动态变更的网站。动态网站一般通过数据库进行架构。除了设计网页外,还需要通过数据库和编程来实现更多自动和高级的功能。使用网页脚本语言(如PHP、JSP等),通过脚本将网站内容动态存储到数据库中用户访问网站时,通过读取数据库来动态生成网页。
[root@localhost nginx]# nmcli connection modify ens160 +ipv4.addresses
192.168.58.157/24
[root@localhost nginx]# nmcli connection up ens160
[root@localhost ~]# dnf install php php-fpm -y
[root@localhost ~]# systemctl restart nginx php-fpm
[root@ntp-server ~]# echo "" >
/usr/share/nginx/html/index.php