当前位置:   article > 正文

WEB服务器搭建与配置

web服务器搭建

WEB服务简介

# 目前最主流的三个Web服务器是Apache、Nginx、 IIS

- Web服务器一般指网站服务器,可以向浏览器等Web客户端提供文档,也可以放置网站文件,让全世界浏览;可以放置数据文件,让全世界下载。

- WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。

- 服务器是一种被动程序只有当Internet上运行其他计算机中的浏览器发出的请求时,服务器才会响应

WEB 服务协议

# WEB 服务应用层使用HTTP协议

# HTML(标准通用标记语言下的一个应用)文档格式。--index.html

# 浏览器统一资源定位器(URL)。

# 为了解决HTTP协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议HTTPS。为了数据传输的安全,HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密。

# WWW 采用的是浏览器/服务器结构

#web服务器只能解析静态页面。 动态页面:只要和数据库进行连接的都属于动态页面,比如java写的代码,PHP的代码,python的代码。

#web服务器:apache (参考服务器配置、优化。静态并发量最高2000) nginx(tengine) IIS #端口全部为80!https为443端口

Apache服务的搭建与配置

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。

Apache的主程序名叫httpd。

  1. Apache安装配置

  1. [root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld # 永久关闭防火墙
  2. [root@localhost ~]# setenforce 0
  3. [root@localhost ~]# yum -y install httpd # 下载安装httpd
  4. [root@localhost ~]# systemctl start httpd # 开启httpd服务
  5. [root@localhost ~]# netstat -lntp | grep 80
  6. tcp6 0 0 :::80 :::* LISTEN 3135/httpd

# 端口80.可以改

index.html:默认主页名称

默认发布网站的目录:/var/www/html

系统产生apache账户,家目录是:/var/www

apache目录介绍

apache的工作目录(基准目录)

conf 存储配置文件

conf.d 存储配置子文件

logs 存储日志

modules 存储模块

run 存储Pid文件,存放的pid号码。是主进程号

认识主配置文件

  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  2. ServerRoot "/etc/httpd" #工作目录
  3. Listen 80 #监听端口
  4. Listen 192.168.2.8:80 # 指定监听的本地网卡 可以修改
  5. User apache # 子进程的用户,有可能被人改称www账户
  6. Group apache # 子进程的组
  7. ServerAdmin root@localhost # 设置管理员邮件地址
  8. DocumentRoot "/var/www/html" # 发布网站的默认目录,想改改这里。
  9. IncludeOptional conf.d/*.conf # 包含conf.d目录下的*.conf文件
  10. # 设置DocumentRoot指定目录的属性
  11. <Directory "/var/www/html"> # 网站容器开始标识
  12. Options Indexes FollowSymLinks # 找不到主页时,以目录的方式呈现,并允许链接到网 站根目录以外
  13. AllowOverride None # 对目录设置特殊属性:none不使用.htaccess控制,all 允许
  14. Require all granted # granted表示运行所有访问,denied表示拒绝所有访问
  15. </Directory> # 容器结束
  16. DirectoryIndex index.html # 定义主页文件,当访问到网站目录时如果有定义的 主页文件,网站会自动访问

  1. 访问控制

  1. 准备测试页面

[root@localhost ~]# echo 'test......' > /var/www/html/index.html    #编写测试文件
  1. 访问控制测试

  1. Apache默认允许所有主机可访问
  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf # 编辑组配置文件
  2. [root@localhost ~]# systemctl restart httpd # 修改配置文件后重启服务

Require all granted # 默认所有人可访问

  1. [root@localhost ~]# curl -I 192.168.56.120
  2. HTTP/1.1 200 OK
  3. Date: Tue, 28 Feb 2023 12:49:16 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Tue, 28 Feb 2023 12:04:18 GMT
  6. ETag: "c-5f5c1653028bb"
  7. Accept-Ranges: bytes
  8. Content-Length: 12
  9. Content-Type: text/html; charset=UTF-8
  10. [root@localhost ~]# curl 192.168.56.120
  11. test......
  1. 只拒绝一部分客户端访问:
  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf # 编辑组配置文件
  2. [root@localhost ~]# systemctl restart httpd # 修改配置文件后重启服务

<RequireALL> # 需要添加此标签,否则不生效

Require not ip 192.168.56.110 # 拒绝链接的IP,多个空格隔开

</RequireALL>

  1. [root@localhost ~]# curl -I 192.168.56.110
  2. HTTP/1.1 403 Forbidden
  3. Date: Thu, 02 Mar 2023 11:43:05 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
  6. ETag: "1321-5058a1e728280"
  7. Accept-Ranges: bytes
  8. Content-Length: 4897
  9. Content-Type: text/html; charset=UTF-8
  1. 拒绝所有人连接
  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  2. 131<Directory "/var/www/html">
  3. 132 Options Indexes FollowSymLinks
  4. 133 ALLowOverride None
  5. 134 <RequireALL>
  6. 135 Require all denied136 </RequireALL>
  7. 137 </Directory>
  8. [root@localhost ~]# systemctl resatrt httpd
  1. [root@localhost ~]# curl -I 192.168.56.110
  2. HTTP/1.1 403 Forbidden
  3. Date: Thu, 02 Mar 2023 11:46:25 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
  6. ETag: "1321-5058a1e728280"
  7. Accept-Ranges: bytes
  8. Content-Length: 4897
  9. Content-Type: text/html; charset=UTF-8

修改默认网站发布目录
  1. [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  2. 119 DocumentRoot "/www" # 修改网站根目录为/www
  3. 131 <Directory "/www"> # 把这个也对应的修改为/www
  4. [root@localhost ~]# mkdir /www
  5. [root@localhost ~]# echo 'abc..def...ghj...' >> /www/index.html
  6. [root@localhost ~]# cat /www/index.html
  7. abc..def...ghj...
  8. [root@localhost ~]# curl -I 192.168.56.110
  9. HTTP/1.1 200 OK
  10. Date: Thu, 02 Mar 2023 11:51:15 GMT
  11. Server: Apache/2.4.6 (CentOS)
  12. Last-Modified: Thu, 02 Mar 2023 11:48:57 GMT
  13. ETag: "12-5f5e969f14d15"
  14. Accept-Ranges: bytes
  15. Content-Length: 18
  16. Content-Type: text/html; charset=UTF-8
  17. [root@localhost ~]# curl 192.168.56.110
  18. abc..def...ghj...
  • 虚拟主机

虚拟主机:多个网站在一台服务器上。web服务器都可以实现。

三种:基于域名 基于端口 基于Ip

  1. 基于域名
  1. [root@localhost ~]# cd /etc/httpd/conf.d/
  2. [root@localhost conf.d]# vim test.conf
  3. <VirtualHost *:80> #指定虚拟主机端口,*代表监听本机所有ip,也可以指定ip
  4. DocumentRoot /soso #指定发布网站目录,自己定义
  5. ServerName www.soso666.com #指定域名,可以自己定义
  6. <Directory "/soso/">
  7. AllowOverride None #设置目录的特性,如地址重写
  8. Require all granted #允许所有人访问
  9. </Directory>
  10. </VirtualHost>
  11. <VirtualHost *:80>
  12. DocumentRoot /soho
  13. ServerName test.soso666.com
  14. <Directory "/soho/">
  15. AllowOverride None
  16. Require all granted
  17. </Directory>
  18. </VirtualHost>
  19. [root@localhost ~]# mkdir /soso #创建发布目录
  20. [root@localhost ~]# mkdir /soho
  21. [root@localhost ~]# echo qianfen > /soso/index.html #创建测试页面
  22. [root@localhost ~]# echo qfedu > /soho/index.html
  23. [root@localhost ~]# systemctl restart httpd

[root@localhost ~]# vim /etc/hosts

测试访问

  1. [root@localhost ~]# curl -I 192.168.56.110
  2. HTTP/1.1 200 OK
  3. Date: Thu, 02 Mar 2023 12:48:51 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Thu, 02 Mar 2023 12:45:08 GMT
  6. ETag: "8-5f5ea32eb06a2"
  7. Accept-Ranges: bytes
  8. Content-Length: 8
  9. Content-Type: text/html; charset=UTF-8
  10. [root@localhost ~]# curl -I www.soso666.com
  11. HTTP/1.1 200 OK
  12. Date: Thu, 02 Mar 2023 12:49:07 GMT
  13. Server: Apache/2.4.6 (CentOS)
  14. Last-Modified: Thu, 02 Mar 2023 12:45:08 GMT
  15. ETag: "8-5f5ea32eb06a2"
  16. Accept-Ranges: bytes
  17. Content-Length: 8
  18. Content-Type: text/html; charset=UTF-8
  19. [root@localhost ~]# curl -I test.soso666.com
  20. HTTP/1.1 200 OK
  21. Date: Thu, 02 Mar 2023 12:49:24 GMT
  22. Server: Apache/2.4.6 (CentOS)
  23. Last-Modified: Thu, 02 Mar 2023 12:45:17 GMT
  24. ETag: "6-5f5ea33706f38"
  25. Accept-Ranges: bytes
  26. Content-Length: 6
  27. Content-Type: text/html; charset=UTF-8
  1. 基于端口
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
  1. [root@qfedu.com ~]# vim /etc/httpd/conf.d/test.conf
  2. <VirtualHost *:80>
  3. DocumentRoot /soso
  4. ServerName www.soso666.com
  5. <Directory "/soso/">
  6. AllowOverride None
  7. Require all granted
  8. </Directory>
  9. </VirtualHost>
  10. <VirtualHost *:81> #修改端口
  11. DocumentRoot /soho
  12. ServerName test.soso666.com
  13. <Directory "/soho/">
  14. AllowOverride None
  15. Require all granted
  16. </Directory>
  17. </VirtualHost>
  18. [root@qfedu.com ~]# systemctl restart httpd

测试访问:

  1. [root@localhost ~]# curl -I www.soso666.com
  2. HTTP/1.1 200 OK
  3. Date: Thu, 02 Mar 2023 12:54:06 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Thu, 02 Mar 2023 12:45:08 GMT
  6. ETag: "8-5f5ea32eb06a2"
  7. Accept-Ranges: bytes
  8. Content-Length: 8
  9. Content-Type: text/html; charset=UTF-8
  10. [root@localhost ~]# curl -I www.soso666.com:81
  11. HTTP/1.1 200 OK
  12. Date: Thu, 02 Mar 2023 12:54:23 GMT
  13. Server: Apache/2.4.6 (CentOS)
  14. Last-Modified: Thu, 02 Mar 2023 12:45:17 GMT
  15. ETag: "6-5f5ea33706f38"
  16. Accept-Ranges: bytes
  17. Content-Length: 6
  18. Content-Type: text/html; charset=UTF-8
  1. 基于IP
  1. [root@localhost ~]# ifconfig ens33:0 192.168.56.111 # 添加一个临时IP
  2. [root@localhost ~]# vim /etc/httpd/conf.d/test.conf
  3. [root@localhost ~]# systemctl restart httpd

测试访问:

  1. [root@localhost ~]# curl -I 192.168.56.111
  2. HTTP/1.1 200 OK
  3. Date: Thu, 02 Mar 2023 13:03:41 GMT
  4. Server: Apache/2.4.6 (CentOS)
  5. Last-Modified: Thu, 02 Mar 2023 11:48:57 GMT
  6. ETag: "12-5f5e969f14d15"
  7. Accept-Ranges: bytes
  8. Content-Length: 18
  9. Content-Type: text/html; charset=UTF-8
  10. [root@localhost ~]# curl -I 192.168.56.110
  11. HTTP/1.1 200 OK
  12. Date: Thu, 02 Mar 2023 13:03:45 GMT
  13. Server: Apache/2.4.6 (CentOS)
  14. Last-Modified: Thu, 02 Mar 2023 12:45:08 GMT
  15. ETag: "8-5f5ea32eb06a2"
  16. Accept-Ranges: bytes
  17. Content-Length: 8
  18. Content-Type: text/html; charset=UTF-8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT领航者/article/detail/61641
推荐阅读
相关标签
  

闽ICP备14008679号