1. WEB服务简介
WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。 Web服务器是在运行在物理服务器上的一个程序,它永久地等待客户端(主要是浏览器,比如Chrome,Firefox等)发送请求。
当收到请求之后,它会生成相应的响应并将其返回至客户端。Web服务器通过HTTP协议与客户端通信,因此也被称为HTTP服务器.
2. WEB服务原理
Web服务器的工作原理并不复杂,一般可分成如下4个步骤:
①建立连接:客户机通过TCP/IP协议建立到服务器的TCP连接
②请求过程:客户端向服务器发送HTTP协议请求包,请求服务器里的资源文档
③应答过程:服务器向客户机发送HTTP协议应答包,如果请求的资源包含有动态语言的内容,那么服务器会调用动态语言的解释引擎负责处理“动态内容”,并将处理得到的数据返回给客户端。由客户端解释HTML文档,在客户端屏幕上渲染图形结果
④关闭连接:客户机与服务器断开
3.常见的WEB应用服务:
Apache(Apache HTTP Server)Apache软件基金会,本身只支持html即静态网页。
IIS(Internet Information Services)微软Tomcat Apache软件基金会,Tomcat是应用(java)服务器,它只是一个servlet容器,是Apache的扩展。
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。
练习:
1.apache的安装
[root@localhost ~]# yum install httpd -y
......
Running transaction
Installing : httpd-2.4.6-67.el7.centos.x86_64 1/1
Verifying : httpd-2.4.6-67.el7.centos.x86_64 1/1
Installed:
httpd.x86_64 0:2.4.6-67.el7.centos
Complete!
2.apache的配置、启动
[root@localhost ~]#vi /etc/httpd/conf/httpd.conf
[root@localhost ~]#echo 'hello world' >> /var/www/html/index.html
[root@localhost ~]#systemctl start httpd
[root@localhost ~]#systemctl enable httpd
在浏览器输入http://192.168.163.149
3.apache虚拟主机的配置
Apache 配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置(实际的生产环境都是以源码包进行编译后安装的,我们也采取同样的方式)
3.0 apache的下载安装
①.下载源码包
[root@localhost ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.37.tar.gz
--2018-12-03 18:34:40-- http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.37.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9177278 (8.8M) [application/x-gzip]
Saving to: ‘httpd-2.4.37.tar.gz’
100%[==================================>] 9,177,278 4.30MB/s in 2.0s
2018-12-03 18:34:42 (4.30 MB/s) - ‘httpd-2.4.37.tar.gz’ saved [9177278/9177278]
②.安装依赖(补丁包安装完全)
③.解压装置
[root@localhost ~]# tar zxvf httpd-2.4.37.tar.gz
......(省略)
httpd-2.4.37/docs/manual/urlmapping.html
httpd-2.4.37/buildconf
httpd-2.4.37/Makefile.in
httpd-2.4.37/srclib/
httpd-2.4.37/srclib/Makefile.in
[root@localhost ~]#
查看有哪些和apr相关的包找出来后一个一个安装
安装所需要的包
[root@localhost ~]# yum install apr-devel -y
......()省略
Running transaction
Installing : apr-devel-1.4.8-3.el7.x86_64 1/1
Verifying : apr-devel-1.4.8-3.el7.x86_64 1/1
Installed:
apr-devel.x86_64 0:1.4.8-3.el7
Complete!
[root@localhost ~]# yum install apr-util -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package apr-util-1.5.2-6.el7.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# yum install apr-util-devel -y
......(省略)
Installed:
apr-util-devel.x86_64 0:1.5.2-6.el7
Dependency Installed:
cyrus-sasl.x86_64 0:2.1.26-21.el7 cyrus-sasl-devel.x86_64 0:2.1.26-21.el7
expat-devel.x86_64 0:2.1.0-10.el7_3 libdb-devel.x86_64 0:5.3.21-20.el7
openldap-devel.x86_64 0:2.4.44-5.el7
Complete!
[root@localhost ~]#
再次执行语句 查看
安装所需要的gcc编译器
[root@localhost ~]# yum install gcc -y
......(省略)
Installed:
gcc.x86_64 0:4.8.5-16.el7
Dependency Installed:
cpp.x86_64 0:4.8.5-16.el7 glibc-devel.x86_64 0:2.17-196.el7
glibc-headers.x86_64 0:2.17-196.el7 kernel-headers.x86_64 0:3.10.0-693.el7
libmpc.x86_64 0:1.0.1-3.el7 mpfr.x86_64 0:3.1.1-4.el7
Complete!
[root@localhost ~]#
再次执行 httpd-2.4.37/configure --prefix=/opt/apache 出现如下错误:
检查和pcre相关的包并安装
[root@localhost ~]# ls /media/Packages/ | grep "^pcre"
pcre2-10.23-2.el7.x86_64.rpm
pcre-8.32-17.el7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
[root@localhost ~]# yum istall pcre-devel -y
......(省略)
Running transaction
Installing : pcre-devel-8.32-17.el7.x86_64 1/1
Verifying : pcre-devel-8.32-17.el7.x86_64 1/1
Installed:
pcre-devel.x86_64 0:8.32-17.el7
Complete!
[root@localhost ~]#
接着检测,如出现如下所示则成功
[root@localhost ~]# httpd-2.4.37/configure --prefix=/opt/apache
......(省略)
configure: summary of build options:
Server Version: 2.4.37
Install prefix: /opt/apache
C compiler: gcc -std=gnu99
CFLAGS: -pthread
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
LDFLAGS:
LIBS:
C preprocessor: gcc -E
[root@localhost ~]#
④.编译
[root@localhost ~]# make
......(省略)
te.la -rpath /opt/apache/modules -module -avoid-version mod_rewrite.lo
make[4]: Leaving directory `/root/modules/mappers'
make[3]: Leaving directory `/root/modules/mappers'
make[2]: Leaving directory `/root/modules'
make[2]: Entering directory `/root/support'
make[2]: Leaving directory `/root/support'
make[1]: Leaving directory `/root'
[root@localhost ~]#
⑤.安装
[root@localhost ~]# make install
......(省略)
Installing man pages and online manual
mkdir /opt/apache/man
mkdir /opt/apache/man/man1
mkdir /opt/apache/man/man8
mkdir /opt/apache/manual
make[1]: Leaving directory `/root'
3.1 基于IP的虚拟主机配置
#使用同一个网络接口上绑定ip
[root@localhost ~]# ifconfig ens33:1 192.168.163.200 up
[root@localhost ~]# ifconfig ens33:2 192.168.163.201 up
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.163.149 netmask 255.255.255.0 broadcast 192.168.163.255
inet6 fe80::fca9:bfc4:508c:b766 prefixlen 64 scopeid 0x20<link>
......(省略)
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.163.200 netmask 255.255.255.0 broadcast 192.168.163.255
ether 00:0c:29:7c:ee:ab txqueuelen 1000 (Ethernet)
ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.163.201 netmask 255.255.255.0 broadcast 192.168.163.255
ether 00:0c:29:7c:ee:ab txqueuelen 1000 (Ethernet)
......(省略)
[root@localhost ~]#
#在/etc/hosts文件中设置ip及名称
[root@localhost ~]# echo '192.168.163.200 c75' >> /etc/hosts
[root@localhost ~]# echo '192.168.163.201 c76' >> /etc/hosts
[root@localhost ~]# ping c75
PING c75 (192.168.163.200) 56(84) bytes of data.
64 bytes from c75 (192.168.163.200): icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from c75 (192.168.163.200): icmp_seq=2 ttl=64 time=0.030 ms
^C
--- c75 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.030/0.044/0.058/0.014 ms
[root@localhost ~]# ping c76
PING c76 (192.168.163.201) 56(84) bytes of data.
64 bytes from c76 (192.168.163.201): icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from c76 (192.168.163.201): icmp_seq=2 ttl=64 time=0.030 ms
64 bytes from c76 (192.168.163.201): icmp_seq=3 ttl=64 time=0.031 ms
^C
--- c76 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.030/0.031/0.032/0.000 ms
[root@localhost ~]#
#建立虚拟主机存放网页的 跟目录,并创建首页文件index.html
[root@localhost ~]# mkdir /opt/apache/www/c75 -p
[root@localhost ~]# mkdir /opt/apache/www/c76 -p
[root@localhost ~]# echo 'c75,hello_world' > /opt/apache/www/c75/index.html
[root@localhost ~]# echo 'c76,hello_world' > /opt/apache/www/c76/index.html
[root@localhost ~]#
#修改配置文件,在最后添加如下代码
[root@localhost ~]# vi /opt/apache/conf/httpd.conf
......(省略)
Listen 192.168.163.200:80
Listen 192.168.163.200:80
Include /opt/apache/vhost/*.conf
[root@localhost ~]# mkdir -p /opt/apache/vhost
#添加下面的代码则是虚拟主机的
[root@localhost ~]# vi /opt/apache/vhost/c75.conf
<VirtualHost 192.168.163.200:80>
ServerName c75
DocumentRoot /opt/apache/www/c75
<Directory "/opt/apache/www/c75/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# vi /opt/apache/vhost/c76.conf
<VirtualHost 192.168.163.201:80>
ServerName c76
DocumentRoot /opt/apache/www/c76
<Directory "/opt/apache/www/c76/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#测试apache的配置是否有问题,如有问题请查看日志
[root@localhost ~]# cd /opt/apache/bin/
[root@localhost bin]# ./apachectl -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost bin]# ./apachectl start
服务不能启动,出现如下错误:
方法一:yum remove httpd
80端口被占用:
方法二:
netstat -nlp | grep 80
kill pid
方法三:
查看/opt/apache/conf/httpd.conf 文件
测试:
3.2 基于端口的虚拟主机配置
#如果服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置
[root@localhost ~]# ifconfig ens32:3 192.168.163.203 up
[root@localhost ~]# ifconfig
......(省略)
ens33:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.163.203 netmask 255.255.255.0 broadcast 192.168.163.255
ether 00:0c:29:7c:ee:ab txqueuelen 1000 (Ethernet)
......(省略)
[root@localhost ~]# vi /etc/hosts
192.168.163.203 c133
[root@localhost ~]# mkdir /opt/apache/www/c133 -p
[root@localhost ~]# mkdir /opt/apache/www/c133/8080 -p
[root@localhost ~]# mkdir /opt/apache/www/c133/8081 -p
[root@localhost ~]# echo '8080,hello_world' > /opt/apache/www/c133/8080/index.html
[root@localhost ~]# echo '8081,hello_world' > /opt/apache/www/c133/8081/index.html
#修改配置文件,在最后添加如下代码
[root@localhost ~]# vi /opt/apache/conf/httpd.conf
Listen 192.168.163.203:8080
Listen 192.168.163.203:8081
Include /opt/apache/vhost/*.conf
[root@localhost ~]# vi /opt/apache/vhost/c133_8080.conf
<VirtualHost 192.168.163.203:8080>
ServerName c133
DocumentRoot /opt/apache/www/c133/8080
<Directory "/opt/apache/www/c133/8080/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# vi /opt/apache/vhost/c133_8081.conf
<VirtualHost 192.168.163.203:8081>
ServerName c133
DocumentRoot /opt/apache/www/c133/8081
<Directory "/opt/apache/www/c133/8081/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#测试apache的配置是否有问题,如有问题请查看日志
[root@localhost ~]# cd /opt/apache/bin/
[root@localhost bin]# ./apachectl -t
Syntax OK
[root@localhost bin]# ./apachectl start
httpd (pid 1505) already running
[root@localhost bin]# kill 1505
[root@localhost bin]# ./apachectl start
[root@localhost bin]#
测试:
3.3 基于域名的虚拟主机配置
[root@localhost ~]# ifconfig ens33:4 192.168.163.204 up
[root@localhost ~]# ifconfig
......(省略)
ens33:4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.163.204 netmask 255.255.255.0 broadcast 192.168.163.255
ether 00:0c:29:7c:ee:ab txqueuelen 1000 (Ethernet)
......(省略)
[root@localhost ~]# vi /etc/hosts
192.168.163.204 www.2041.com
192.168.163.204 www.2042.com
[root@localhost ~]# ping www.2041.com
PING www.2041.com (192.168.163.204) 56(84) bytes of data.
64 bytes from www.2041.com (192.168.163.204): icmp_seq=1 ttl=64 time=0.025 ms
64 bytes from www.2041.com (192.168.163.204): icmp_seq=2 ttl=64 time=0.026 ms
^C
--- www.2041.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.025/0.025/0.026/0.005 ms
[root@localhost ~]#
[root@localhost ~]# ping www.2042.com
PING www.2042.com (192.168.163.204) 56(84) bytes of data.
64 bytes from www.2041.com (192.168.163.204): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from www.2041.com (192.168.163.204): icmp_seq=2 ttl=64 time=0.025 ms
64 bytes from www.2041.com (192.168.163.204): icmp_seq=3 ttl=64 time=0.031 ms
^C
--- www.2042.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.023/0.026/0.031/0.005 ms
[root@localhost ~]#
[root@localhost ~]# mkdir www/2041.com -p
[root@localhost ~]# mkdir www/2042.com -p
[root@localhost ~]# echo '2041_hello' > ../apache/www/2041.com/index.html
[root@localhost ~]# echo '2042_hello' > ../apache/www/2042.com/index.html
#修改配置文件,在最后添加如下代码
[root@localhost ~]# vi /opt/apache/conf/httpd.conf
Listen 192.168.163.204:80
Include /opt/apache/vhost/*.conf
#添加下面的代码则是虚拟主机的
[root@localhost ~]# vi www/2041.com.conf
<virtualHost 192.168.163.204:80>
ServerName www.2041.com
DocumentRoot /opt/apache/www/2041.com
<Directory "/opt/apache/www/2041.com/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# vi www/2042.com.conf
<virtualHost 192.168.163.204:80>
ServerName www.2042.com
DocumentRoot /opt/apache/www/2042.com
<Directory "/opt/apache/www/2042.com/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
#测试apache的配置是否有问题,如有问题请查看日志
[root@localhost ~]# cd /opt/apache/bin/
[root@localhost bin]# ./apachectl -t
Syntax OK
[root@localhost bin]# ./apachectl start
[root@localhost bin]#
C:\Windows\System32\drivers\etc里面有一个hosts文件,找到并修改hosts文件:
在里面加上IP地址和域名 否则window解析不了
此时再在windows的浏览器输入域名即可 。