Web服务器

简介

www是world wide web的缩写,也就是全球信息广播的意思。通常说的上网就是使用www来查询用户 所需要的信息。www可以结合文字、图形、影像以及声音等多媒体,并通过可以让鼠标单击超链接的方 式将信息以Internet传递到世界各处去。

与其他服务器类似,当你连接上www网站,该网站肯定会提供一些数据,而你的客户端则必须要使用可 以解析这些数据的软件来处理,那就是浏览器。www服务器与客户端浏览器之间的连接图。

1、 www所用的协议: 浏览器怎样向web服务器请求数据以及服务器怎样把文档传送给浏览器呢?这就是 由http协议来定义的,(Hyper Text Transport Protocol,HTTP,超文本传输协议)。

2、www服务器需要提供可让客户端浏览的平台。目前最主流的Web服务器是Apache、Microsoft的 Internet信息服务器(Internet Information Services,IIS)和unix nginx。

3、服务器所提供的最主要数据是超文本标记语言(Hyper Text Markup Language,HTML)、多媒体 文件(图片、影像、声音、文字等,都属于多媒体或称为超媒体),HTML只是一些纯文本数据,通过所谓 的标记来规范所要显示的数据格式。

4、客户端收到服务器的数据之后需要软件解析服务器所提供的数据,最后将效果呈现在用户的屏幕上。那 么著名的浏览器就有内建在Windows操作系统内的IE浏览器了,还有Firefox浏览器和Google的 chrome浏览器。

2、web服务器的类型

(1)仅提供用户浏览的单向静态网页 单纯是由服务器单向提供数据给客户端,Server不需要与client端有互动,所以你可以到该网站上去浏 览,但是无法进行数据的上传。

(2)提供用户互动接口的动态网站 这种类型的网站可以让服务器与用户互动,常见的例如留言板,博客。这种类型的网站需要通过"网页程 序语言"来实现与用户互动的行为。

3、web服务器基本配置 服务器端:此处使用nginx提供web服务,RPM包获取:http://nginx.org/packages/

root@localhost \~# dnf install nginx -y

root@localhost \~# nginx -v

root@localhost \~# nginx -V

root@localhost \~# rpm -ql nginx

4、虚拟主机配置实战

实验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.59.142

hello world

实验2:建立两个基于ip地址访问的网站,要求如下

该网站ip地址的主机位为100,设置首页目录为/www/ip/100,网页内容为:this is 100。

该网站ip地址主机位为200,设置首页目录为/www/ip/200,网页内容为:this is 200。

#添加IP地址

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

root@client conf.d# cat testip.conf

server {

listen 192.168.168.100:80;

server_name _;

root /www/ip/100;

include /etc/nginx/default.d/*.conf;

location / {

}

}

server {

listen 192.168.168.200:80;

root /www/ip/200;

location / {

}

}

#查看文件编辑语法是否有错

nginx -t

#重启nginx

root@localhost \~# systemctl restart nginx

#访问

root@localhost \~# curl 192.168.168.100

this is 100

root@localhost \~# curl 192.168.168.200

this is 200
(3)建立两个基于不同端口访问的网站

要求如下:

  • 建立一个使用web服务器默认端口的网站,设置网站首页目录为/www/port/80,网页内容为:the port is 80。

  • 建立一个使用10000端口的网站,设置网站首页目录为/www/port/10000,网页内容为:the port is 10000。

#新建IP地址

nmtui

root@localhost \~# nmcli connection up ens160

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@client conf.d# cat testport.conf

server {

listen 192.168.59.123:80;

server_name _;

root /www/port/80;

location / {

}

}

server {

listen 192.168.59.123:10000;

root /www/port/10000;

location / {

}

}

#查看文件编辑语法是否有错

nginx -t

root@localhost \~# systemctl restart nginx

root@localhost \~# curl 192.168.168.153:10000

the port is 10000

root@localhost \~# curl 192.168.168.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 \~# 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.59.142:80;

server_name www.ceshi.com;

root /www/name;

location / {

}

}

server {

listen 192.168.59.142:80;

server_name rhce.first.day ce.first.day;

root /www/ce;

location / {

}

}

root@localhost \~# vim /etc/hosts

192.168.168.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
(5)基于虚拟目录和用户控制的web网站

root@localhost \~# mkdir /www/real/

root@localhost \~# mkdir /www/name/testip

root@localhost \~# echo real-virtual > /www/real/index.html

root@localhost \~# echo test > /www/name/testip/index.html

root@client conf.d# cat nametest.conf

server {

listen 192.168.59.142:80;

server_name www.ceshi.com;

root /www/name;

location /testip {

alias /www/real;

}

}

server {

listen 192.168.59.142:80;

server_name rhce.first.day ce.first.day;

root /www/ce;

location / {

}

}

root@localhost \~# systemctl restart nginx

root@localhost \~# curl 192.168.59.142/testip/

real-virtual

#用户访问控制

root@client conf.d# cat nametest.conf

server {

listen 192.168.59.142:80;

server_name www.ceshi.com;

root /www/name;

location /testip {

alias /www/real;

auth_basic on;

auth_basic_user_file /etc/nginx/conf.d/authuser-file;

}

}

server {

listen 192.168.59.142:80;

server_name rhce.first.day ce.first.day;

root /www/ce;

location / {

}

}

root@localhost \~# dnf install httpd-tools -y

root@localhost \~# htpasswd -cb /etc/nginx/conf.d/auth-password user1

123456

root@localhost \~# systemctl restart nginx

root@localhost \~# curl www.ceshi.com/testip/ -u user1

Enter host password for user 'user1':

real-virtual

root@localhost \~# curl user1:123456@www.ceshi.com/testip/

real-virtual

5.HTTPS

(1)https安全通信工作流程

1服务器先向CA证书颁发机构认证,将自己的公钥和域名发给CA

2CA将收到的信息进行hash算法,再用私钥加密得到数字签名;后给服务器颁发一个证书,包括hash算法,服务器的公钥,服务器的域名,数字签名

3当客户端访问服务器时,服务器将自己的证书发给客户端

4客户端收到后进行验证:通过数字签名来确认服务器证书中的服务器公钥是服务器的公钥

客户端用CA的公钥解密数字签名得到信息,再用hash算法加密服务器公钥,两者进行对比

如果一样,那么确定服务器的公钥是正确的

注意:我们在安装浏览器的时候已经内置了公共合法的CA机构的证书了

5客户端就可以用服务器的公钥对两人协商的对称密钥进行加密

(2)https网站配置

#新建IP地址

nmtui

root@localhost \~# nmcli connection up ens160

root@localhost \~# mkdir -pv /www/https/

root@localhost \~# echo https > /www/https/index.html

#key是私钥文件

root@localhost conf.d# openssl genrsa -out /etc/pki/tls/certs/https.key

#crt是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持

有人的公钥,以及签署者的签名等信息

root@localhost conf.d# openssl req -utf8 -new -key /etc/pki/tls/certs/https.key -x509 -days 100 -out /etc/pki/tls/certs/https.crt

#out生成一个crt证书

root@client conf.d# cat https.conf

server {

listen 192.168.59.124:443 ssl;

root /www/https;

ssl_certificate /etc/pki/tls/certs/https.crt;

ssl_certificate_key /etc/pki/tls/certs/https.key;

location / {

}

}

#注意防火墙和selinux

root@localhost \~# systemctl restart nginx

root@localhost \~# curl -k https://192.168.59.124

https

(3)搭建动态网站

动态网页:使用网页脚本语言,比如php、JSP等,通过脚本将网站内容动态存储到数据库,用户访问网站是通过读取数据库来动态生成网页的方法。(lnp)

root@localhost \~# dnf install php php-fpm -y

root@client conf.d# yum search php-fpm

#过程:nginx对接php-fpm,让其快速启动编程语言去执行php页面,生成一个html页面,再返回给客户端

root@client \~# cat /etc/nginx/default.d/php.conf

index index.php index.html index.htm;

location ~ \.(php|phar)(/.*)?$ {

#当匹配到{.php结尾的文件 .php/.*(.php/任意文件) .php/(.php/后不跟任何的文件)}

#或者当匹配到{.phar结尾的文件 .phar/.*(.phar/任意文件) .phar/(.phar/后不跟任何的文件)}都读下面的配置

fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

fastcgi_intercept_errors on;

fastcgi_index index.php;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME document_rootfastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_pass php-fpm;

} #假设当有人匹配到php结尾的时候,要去找php-fpm server,其在/etc/nginx/conf.d/php-fpm.conf中

root@client \~# cat /etc/nginx/conf.d/php-fpm.conf

upstream php-fpm {

server unix:/run/php-fpm/www.sock; #当找到server后,就去找套接字文件(www.sock)

}

root@client conf.d# systemctl start php-fpm

#root@localhost \~# systemctl restart nginx php-fpm

root@client conf.d# ll /run/php-fpm

total 4

-rw-r--r--. 1 root root 4 Oct 26 18:33 php-fpm.pid

srw-rw----+ 1 root root 0 Oct 26 18:33 www.sock

#他已经配置好,那么我们就只需要配置index.php文件

#第一种:IP地址已经都配置完了,那么我们可以在原来的文件上修改

root@client conf.d# cd /www/ip/100/

root@client 100# ll

total 4

-rw-r--r--. 1 root root 12 Oct 23 20:53 index.html

root@client 100# cat index.php

hahahahaha

root@client conf.d# cat testip.conf

server {

listen 192.168.59.100:80;

server_name _;

root /www/ip/100;

include /etc/nginx/default.d/*.conf; #照着/etc/nginx/nginx.conf文件写

location / {

}

}

server {

listen 192.168.59.200:80;

root /www/ip/200;

location / {

}

}

root@client 100# systemctl restart nginx

root@client 100# curl 192.168.59.100

hahahahaha

#第二种:创建新的IP地址 因为没有给IP地址配置server,默认读取/etc/nginx/default.d/php.conf文件,里面的location就会自动匹配到php文件,我们只需把文件写在默认目录/usr/share/nginx/html下(/etc/nginx/nginx.conf文件中查看)

root@client 100# cat /usr/share/nginx/html/index.php

<?php phpinfo(); ?>

root@client 100# curl 192.168.59.124

相关推荐
難釋懷14 小时前
Nginx-KeepAlive
运维·nginx
2601_9564141414 小时前
迈向智慧实验室:金现代的全链路质量管控与自动化解决方案
运维·自动化
YJlio15 小时前
《Sysinternals实战指南》16.5 Ctrl2Cap 工具详解:把 Caps Lock 变成 Ctrl 的键盘改造与回退方法
linux·运维·服务器·网络·python·学习·计算机外设
王二端茶倒水15 小时前
智慧小区宽带无线运营:从网络交付到认证、计费与运维闭环
运维·物联网·架构
旅僧15 小时前
远程终端工具安装
运维
OpsEye15 小时前
日志、指标、链路追踪,谁更适合定位故障?
运维·监控·日志分析
麦麦麦当劳大王15 小时前
Linux SSH服务端配置指南
linux·运维·服务器·ssh
xiami_world16 小时前
私有化部署协同白板选型指南:从Docker容器化到信创全栈适配的架构实践
运维·人工智能·docker·ai·持续部署
weixin_3077791316 小时前
从脚本执行到智能体协作:AI辅助测试能力的范式重构
运维·开发语言·人工智能·算法·测试用例
Yiyaoshujuku16 小时前
化学谱图数据API接口,数据字段一览!
linux·服务器·前端