天天看点

Day09 WEB服务(Enginner03)一、HTTP服务基础二、 搭建单web服务器三、虚拟web主机(多站点)四、数字证书基础五、安全的web服务六、部署动态网站

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

服务端提供网页

浏览器下载并显示网页

端口号:80/tcp

Hyper Text Markup Language(HTML) 超文本标记语言

Hyper Text Transfer Protocol(HTTP) 超文本传输协议

服务端:httpd、nginx、tomcat

客户端:elinks、firefox、IE、chrome

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

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

httpd的全局主配置文件

42行,Listen ip:port #监听ip地址和端口号

95行,ServerName #设置服务器的域名

119行,DocumentRoot #设置网站的根目录

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

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

那么一台服务器如何区分不同的站点呢?有三种方式

基于域名(必须掌握)

基于端口(了解)

基于IP地址(不用)

由全局主配置文件来定义,一般存放自定义配置文件。

具体在第353行

<VirtualHost *:@@Port@@>

ServerAdmin [email protected]

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>

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

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

主配置文件定义了/禁止所有人访问;同时又定义了只有/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

允许所有人访问,但禁止172.34.0.0/24访问

<Directory "/var/www/html/doc">

Require not ip 172.34.0.0/24

首先防火墙是否限制

其次服务本身的访问控制

再次看SELinux是否限制

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

SELinux限制了httpd只允许访问下面的文件,为他们打上了httpd的标签:

/etc/httpd/conf/httpd.conf

/etc/http/conf.d/*.conf

/etc/www

查看SELinux标签

ls -Zd /var/www

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

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

PKI(Public Key Infrastructure)

公钥

私钥

证书

证书颁发机构

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

端口443/tcp

HTTPS(Security Hyper Text Transfer Protocol) 安全超文本传输协议

SSL(Secure Socket Layer) 安全套接字层

TLS(Transport Layer Security) 安全传输层协议

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

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

部署私钥到/etc/pki/tls/private下

59行,把DocumentRoot改为/webroot

60行,把ServerName改为server0.example.com:443

100行,修改证书文件名为server0.cert

107行,修改密钥文件名为server0.key

122行,修改CA证书文件名为example-ca.cert

安装支持python引擎的软件包

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]

Listen 8909

<VirtualHost *:8909>

DocumentRoot /var/www/nsd

ServerName webapp0.example.com

wsgiscriptAlias / /var/www/nsd/webinfo.wsgi #让浏览器找wsgi去翻译网页

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.

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.

allow this access for now by executing:

#grep httpd /var/log/audit/audit.log | audit2allow -M mypol

#semodule -i mypol.pp

又是SELinux搞的鬼!

查看SELinux允许http的哪些端口

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

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