Day09 WEB服务(Enginner03)

一、HTTP服务基础

1.1 Web通信基本概念

1.1.1 架构

基于B/S架构,其实是C/S架构的衍生版本。

1.1.2 原理

服务端提供网页
浏览器下载并显示网页
端口号:80/tcp

1.1.3 协议

Hyper Text Markup Language(HTML) 超文本标记语言
Hyper Text Transfer Protocol(HTTP) 超文本传输协议

1.1.4 服务端和客户端

服务端:httpd、nginx、tomcat
客户端:elinks、firefox、IE、chrome

1.1.5 prefork和worker模式的比较

prefork模式使用多个子进程,每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接。在大多数平台上,Prefork MPM在效率上要比Worker MPM要高,但是内存使用大得多。prefork的无线程设计在某些情况下将比worker更有优势:它可以使用那些没有处理好线程安全的第三方模块,并且对于那些线程调试困难的平台而言,它也更容易调试一些。

worker模式使用多个子进程,每个子进程有多个线程。每个线程在某个确定的时间只能维持一个连接。通常来说,在一个高流量的HTTP服务器上,Worker MPM是个比较好的选择,因为Worker MPM的内存使用比Prefork MPM要低得多。但worker MPM也有不完善的地方,如果一个线程崩溃,整个进程就会连同其所有线程一起"死掉".由于线程共享内存空间,所以一个程序在运行时必须被系统识别为"每个线程都是安全的"。

二、 搭建单web服务器

2.1 /etc/httpd/conf/httpd.conf

httpd的全局主配置文件

2.2 修改全局主配置文件

42行,Listen ip:port #监听ip地址和端口号
95行,ServerName #设置服务器的域名
119行,DocumentRoot #设置网站的根目录

2.3 httpd -t

检查主配置文件的语法是否有错误

三、虚拟web主机(多站点)

3.1 定义

由同一台服务器提供多个不同的web站点

3.2 区分方式

那么一台服务器如何区分不同的站点呢?有三种方式
基于域名(必须掌握)
基于端口(了解)
基于IP地址(不用)

3.3 /etc/httpd/conf.d/*.conf

由全局主配置文件来定义,一般存放自定义配置文件。
具体在第353行

3.4配置虚拟主机格式

<VirtualHost *:@@Port@@>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "@@ServerRoot@@/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>

3.5 /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf

httpd的虚拟主机格式帮助文件

3.6 虚拟web主机对默认web站点的影响

一旦启用了虚拟web主机以后,外部的DocumentRoot、ServerName会被忽略。虚拟web主机配置文件中第一个虚拟站点被视为默认站点,如果客户端访问的URL不属于任何虚拟主机,则用默认站点回应。因此,之前在主配置文件里面定义的ServerName必须在虚拟web主机配置文件里面增加一个虚拟主机,否则将无法访问

3.7 httpd服务访问控制

主配置文件定义了/禁止所有人访问;同时又定义了只有/var/www全部允许访问。这样的结果就是只有/var/www目录才能给所有人访问,/下的其他目录都不能访问

仅允许本机访问,禁止其他系统访问
<Directory "/var/www/abc/private">
Require ip 172.25.0.11 127.0.0.1
</Directory>

允许访问/webroot目录
<Directory "/webroot">
Require all granted
</Directory>

允许所有人访问,但禁止172.34.0.0/24访问
<Directory "/var/www/html/doc">
Require all granted
Require not ip 172.34.0.0/24
</Directory>

3.8 客户端访问服务端资源排错思路

首先防火墙是否限制
其次服务本身的访问控制
再次看SELinux是否限制

3.9 SELinux对httpd的策略保护

SELinux使用安全上下文(security context)的方式对系统的所有文件进行管理

SELinux限制了httpd只允许访问下面的文件,为他们打上了httpd的标签:
/etc/httpd/conf/httpd.conf
/etc/http/conf.d/*.conf
/etc/www

3.10 ls -Zd /var/www

查看SELinux标签

ls -Zd /var/www
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www

3.11 chcon -R --reference=/var/www /webroot

以/var/www为模板,把/webroot的SELinux标签修改为和/var/www一样,而且是递归修改

四、数字证书基础

4.1 PKI体系

PKI(Public Key Infrastructure)
公钥
私钥
证书
证书颁发机构

五、安全的web服务

5.1 定义

通过PKI体系,使用公私钥提供加密,可以确保消息的的私有性和完整性
端口443/tcp

5.2 协议

HTTPS(Security Hyper Text Transfer Protocol) 安全超文本传输协议
SSL(Secure Socket Layer) 安全套接字层
TLS(Transport Layer Security) 安全传输层协议

5.3 配置https

5.3.1 yum -y install mod_ssl

安装TLS协议支持,安装之后会增加一个文件/etc/httpd/conf.d/ssl.conf

5.3.2 部署证书

部署根证书、网站的证书到/etc/pki/tls/cert下
部署私钥到/etc/pki/tls/private下

5.3.3 /etc/httpd/conf.d/ssl.conf

59行,把DocumentRoot改为/webroot
60行,把ServerName改为server0.example.com:443
100行,修改证书文件名为server0.cert
107行,修改密钥文件名为server0.key
122行,修改CA证书文件名为example-ca.cert

六、部署动态网站

6.1 yum -y install mod_wsgi

安装支持python引擎的软件包

6.2 /usr/share/doc/mod_wsgi-3.4/README

WSGI功能的介绍文档

6.3 wget http://172.25.0.254/pub/materials/webinfo.wsgi

下载动态网站,内容如下:
#!/usr/bin/env python
import time

def application (environ, start_response):
response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', '1'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]

6.4 修改从配置文件

Listen 8909
<VirtualHost *:8909>
DocumentRoot /var/www/nsd
ServerName webapp0.example.com
wsgiscriptAlias / /var/www/nsd/webinfo.wsgi #让浏览器找wsgi去翻译网页
</VirtualHost>

6.5 journalctl -xn

httpd服务重启失败,查看详细日志。
Nov 03 17:23:40 localhost.localdomain python[4200]: SELinux is preventing /usr/sbin/httpd from name_bind access on the tcp_socket .

* Plugin bind_ports (92.2 confidence) suggests ****

If you want to allow /usr/sbin/httpd to bind to network port 8909
Then you need to modify the port type.
Do
#semanage port -a -t PORT_TYPE -p tcp 8909
where PORT_TYPE is one of the following: http_cache_port_t, http_port_t, jboss_management_port_t, jboss_messaging_port_t, ntop_po

* Plugin catchall_boolean (7.83 confidence) suggests **

If you want to allow nis to enabled
Then you must tell SELinux about this by enabling the 'nis_enabled' boolean.

Do
setsebool -P nis_enabled 1

* Plugin catchall (1.41 confidence) suggests **

If you believe that httpd should be allowed name_bind access on the tcp_socket by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
#grep httpd /var/log/audit/audit.log | audit2allow -M mypol
#semodule -i mypol.pp
又是SELinux搞的鬼!

6.6 semanage port -l |grep http

查看SELinux允许http的哪些端口

6.7 semanage port -a -t http_port_t -p tcp 8909

设置SELinux添加允许http使用8909端口,耗费资源比较大



本文转自 goldwinner 51CTO博客,原文链接:http://blog.51cto.com/355665/2068711,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值