第四章 web服务器
web服务器简介
www可以结合文字、图形、影像以及声音等多媒体,并通过可以让鼠标单击超链接的方式将信息以Internet传递到世界各处去。
www所用的协议:Hyper Text Transport Protocol,HTTP,超文本传输协议。 www服务器需要提供可让客户端浏览的平台。目前最主流的Web服务器是Apache、Microsoft的Internet信息服务器(Internet 服务器所提供的最主要数据是超文本标记语言(Hyper Text Markup Language,HTML)、多媒体文件(图片、影像、声音、文字等,都属于多媒体或称为超媒体),HTML只是一些纯文本数据,通过所谓的标记来规范所要显示的数据格式。 Services,IIS)和unix nginx。
URL:Uniform Resource Locator,统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。
网址格式:<协议>://<主机或主机名>[:port]/<目录资源,路径>:
浏览器常支持的协议有:http、https、ftp等。 主机地址或者主机名:主机地址就是服务器在因特网所在的IP地址。如果是主机名的话,那么就需要域名解析了。 端口号(port):http为80,https为443 (IANA:互联网数字分配机构)
web服务器的类型
1.仅提供用户浏览的单向静态网页
单纯是由服务器单向提供数据给客户端,Server不需要与client端有互动,所以你可以到该网站上去浏览,但是无法进行数据的上传。
2.提供用户互动接口的动态网站
这种类型的网站可以让服务器与用户互动,常见的例如留言板,博客。这种类型的网站需要通过“网页程序语言”来实现与用户互动的行为。常见的例如:PHP网页程序语言,配合数据库系统来进行数据的读、写。当你在向服务器请求数据时,其实是通过服务器端同一个网页程序在负责将数据读出或写入数据库,变动的是数据库的内容,网页程序并没有任何改变。
web服务器基本配置

虚拟主机配置
搭建一个web服务器,访问该服务器时显示“hello world”欢迎界面 。
1 [root@localhost ~]# echo "hello world" > /usr/share/nginx/html/index.html 2 [root@localhost ~]# curl localhost 3 hello world 4 [root@localhost ~]# curl 192.168.168.1535 hello world
建立两个基于ip地址访问的网站,
该网站ip地址的主机位为100,设置首页目录为/www/ip/100,网页内容为:this is 100。该网站ip地址主机位为200,设置首页目录为/www/ip/200,网页内容为:this is 200。
1 #添加ip地址
2 [root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.168.100/24 +ipv4.gateway 192.168.168.2 ipv4.dns 114.114.114.114 ipv4.method manual autoconnect yes
3 [root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.168.200/24
4 [root@localhost ~]# nmcli connection up ens33
6 #创建两个网页文件根目录,并定义网页内容
7 [root@localhost ~]# mkdir -pv /www/ip/{100,200}
8 [root@localhost ~]# echo this is 100 > /www/ip/100/index.html
9 [root@localhost ~]# echo this is 200 > /www/ip/200/index.html
10 #设置selinux,必须设置,否则无法看到网页页面内容
11 [root@server html]# setenforce 0
12 [root@server html]# getenforce
13 Permissive
14
15 #定义基于不同ip地址来访问网站的配置文件
16 #新建文件,写入如下配置
17 [root@localhost ~]# vim /etc/nginx/conf.d/test_ip.conf
18 server {
19 listen 192.168.168.100:80;
20 root /www/ip/100;
21 location / {
22 }
23 }
24 server {
25 listen 192.168.168.200:80;
26 root /www/ip/200;
27 location / {
28 }
29 }
30 [root@localhost ~]# systemctl restart nginx
31 [root@localhost ~]# curl 192.168.168.100
32 this is 100
33 [root@localhost ~]# curl 192.168.168.200
34 this is 20035
建立两个基于不同端口访问的网站,建立一个使用web服务器默认端口的网站,设置网站首页目录为/www/port/80,网页内容为:the port is 80。
建立一个使用10000端口的网站,设置网站首页目录为/www/port/10000,网页内容为:the port is 10000。
1 [root@localhost ~]# mkdir -pv /www/port/{80,10000}
2 [root@localhost ~]# echo the port is 80 > /www/port/80/index.html
3 [root@localhost ~]# echo the port is 10000 > /www/port/10000/index.html
4 [root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.168.153/24
5 [root@localhost ~]# nmcli connection up ens33
6 [root@localhost ~]# cat /etc/nginx/conf.d/test_port.conf
7 server {
8 listen 192.168.168.153:80;
9 root /www/port/80;
10 location / {
11 }
12 }
13 server {
14 listen 192.168.168.153:10000;
15 root /www/port/10000;
16 location / {
17 }
18 }
19 [root@localhost ~]# systemctl restart nginx
20 [root@localhost ~]# curl 192.168.168.153:10000
21 the port is 10000
22 [root@localhost ~]# curl 192.168.168.153
23 the port is 8024
建立两个基于域名访问的网站,新建一个网站,域名为www.ceshi.com,设置网站首页目录为/www/name,网页内容为this is test。
新建一个网站,域名为rhce.first.day,同时可通过ce.first.day访问,设置网站首页目录
为/www/ce,网页内容为:today is first day of class。
1 [root@localhost conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.168.154/24
2 [root@localhost conf.d]# nmcli connection up ens33
3 [root@localhost ~]# mkdir /www/{name,ce}
4 [root@localhost ~]# echo this is test > /www/name/index.html
5 [root@localhost ~]# echo today is first day of class > /www/ce/index.html
6
7 [root@localhost ~]# vim /etc/nginx/conf.d/test_servername.conf
8 server {
9 listen 192.168.168.154:80;
10 server_name www.ceshi.com;
11 root /www/name;
12 location / {
13 }
14 }
15 server {
16 listen 192.168.168.154:80;
17 server_name rhce.first.day ce.first.day;
18 root /www/ce;
19 location / {
20 }
21 }22
23[root@localhost ~]# vim /etc/hosts
24 192.168.168.154 www.ceshi.com rhce.first.day ce.first.day
25 [root@localhost ~]# systemctl restart nginx
26 [root@localhost ~]# curl www.ceshi.com
27 this is test
28 [root@localhost ~]# curl rhce.first.day
29 today is first day of class
30 [root@localhost ~]# curl ce.first.day
31 today is first day of class
搭建静态网站——基于https协议的静态网站
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道。HTTPS并不是一个新协议,而是HTTP+SSL(TLS)。原本HTTP先和TCP(假定传输层是TCP协议)直接通信,而加了SSL后,就变成HTTP先和SSL通信,再由SSL和TCP通信,相当于SSL被嵌在了HTTP和TCP之间。

SSL协议分为两层:
SSL记录协议 (SSL Record Protocol):它建立在可靠的传输协议(如TCP)之上,为高层协议提供数据封装、压缩、加密等基本功能。SSL握手协议(SSL Handshake Protocol):它建立在SSL记录协议之上,用于在实际的数据传输开始前,通讯双方进行身份认证、协商加密算法、交换加密密钥等。
https网站配置
1 #https功能由ngx_http_ssl_module模块提供
2
3 [rootlocalhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.168.156/24
4 [root@localhost ~]# nmcli connection up ens33
5 [root@localhost ~]# mkdir -pv /www/https/
6 [root@localhost ~]# echo https > /www/https/index.html
7
8 [root@localhost conf.d]# cd /etc/pki/tls/certs/
9 #key是私钥文件
10 [root@localhost certs]# openssl genrsa -out https.key
11 #crt是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息
12 [root@localhost certs]# openssl req -utf8 -new -key https.key -x509 -days 100 -out https.crt
13 [[root@localhost ~]# cat /etc/nginx/conf.d/test_https.conf
14 server {
15 # listen 80;
16 listen 192.168.168.156:443 ssl;
17 root /www/https;
18 ssl_certificate /etc/pki/tls/certs/https.crt;
19 ssl_certificate_key /etc/pki/tls/certs/https.key;
20 location / {
21 }
22 }
23 [root@localhost ~]# systemctl restart nginx
24 [root@localhost ~]# curl --insecure https://192.168.168.156
25 https
26 [root@localhost ~]# curl -k https://192.168.168.156
27 https
搭建动态网站
动态网站并不是指具有动画功能的网站,而是指网站内容可根据不同情况动态变更的网站,一般情况下动态网站通过数据库进行架构。 动态网站除了要设计网页外,还要通过数据库和编程序来使网站具有更多自动的和高级的功能。
动态网页:使用网页脚本语言,比如php、JSP等,通过脚本将网站内容动态存储到数据库,用户访问网站是通过读取数据库来动态生成网页的方法。
1 [root@localhost nginx]# nmcli connection modify ens33 +ipv4.addresses 192.168.168.157/24 2 [root@localhost nginx]# nmcli connection up ens33 3 [root@localhost ~]# dnf install php php-fpm -y 4 [root@localhost ~]# systemctl restart nginx php-fpm 5 [root@ntp-server ~]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php 6 #使用浏览器访问



















