鸟哥的服务器《十三》Web服务器
来源:互联网 发布:linux卸载jdk 编辑:程序博客网 时间:2024/05/01 17:30
1. 客户端访问Web服务器的几个阶段
客户端向服务器发出Web请求
Web 服务器与客户端机那里连接
服务器找到文档传回客户端
资源通过浏览器解释并显示在屏幕上
2. HTTP报文详解
HTTP报文:它是HTTP应用程序之间发送的数据块。这些数据块以一些文本形式的元信息开头,这些信息描述了报文的内容及含义,后面跟着可选的数据部分。这些报文都是在客户端、服务器和代理之间流动。
HTTP报文的流动方向:一次HTTP请求,HTTP报文会从“客户端”流到“代理”再流到“服务器”,在服务器工作完成之后,报文又会从“服务器”流到“代理”再流到“客户端”
报文的语法:所有的HTTP报文都可以分为两类,请求报文和响应报文。请求和响应报文的基本报文结构大致是相同的,只有起始行的语法有所不同。
请求报文:它会向Web服务器请求一个动作
请求报文的格式:
起始行: < method> < request-URL> < version>
头部: < headers>
主体: < entity-body>响应报文:它会将请求的结果返回给客户端。
响应报文的格式:
起始行: < version> < status> < reason-phrase>
头部: < headers>
主体: < entity-body>下面是对各部分的简要描述:
1、方式(method):客户端希望服务器对资源执行的动作,是一个单独的词,比如,GET、POST或HEAD
2、请求URL(request-URL):要直接与服务器进行对话,只要请求URL是资源的绝对路径就可以了,服务器可以假定自己是URL的主机/端口
3、版本(version):报文所使用的HTTP版本。其格式:HTTP/<主要版本号>.<次要版本号>
4、状态码(status-code):状态码是三位数字,描述了请求过程中所发生的情况。每个状态码的第一位数字都用于描述状态的一般类别(比如,“成功”、“出错”等等)
5、原因短语(reason-phrase):数字状态码的可读版本,包含行终止序列之前的所有文本。原因短语只对人类有意义,因此,尽管响应行HTTP/1.0 200 NOT OK和HTTP/1.0 200 OK中原因短语的含义不同,但同样都会被当作成功指示处理
6、头部(header):可以有零个或多个头部,每个首部都包含一个名字,后面跟着一个冒号(:),然后是一个可选的空格,接着是一个值,最后是一个CRLF首部是由一个空行(CRLF)结束的,表示了头部列表的结束和实体主体部分的开始
7、实体的主体部分(entity-body):实体的主体部分包含一个由任意数据组成的数据块,并不是所有的报文都包含实体的主体部分,有时,报文只是以一个CRLF结束。HTTP报文的组成部分:对报文进行描述的起始行、包含属性的头部块、可选的,包含数据的主体部分
1、起始行:所有的HTTP报文都以一个起始行作为开始。请求报文的起始行说明了要做些什么。响应报文的起始行说明发生了什么。
请求报文的起始行:该行包含了一个方法和一个请求的URL,还包含HTTP 的版本。
响应报文的起始行:该行包含了响应报文使用的HTTP版本、数字状态码、原因短语。
2、头部:HTTP首部字段向请求和响应报文中添加了一些附加信息。本质上来说,它们只是一些名/值对的列表。头部和协议配合工作,共同决定了客户端和服务器能做什么事情。
头部的分类:通用头部:既可以出现在请求报文中,也可以出现在响应报文中,它提供了与报文相关的最基本的信息 Connection:允许客户端和服务器指定与请求/响应连接有关的选项 Date:提供日期和时间标志,说明报文是什么时间创建的 MIME-Version:给出了发送端使用的MIME版本 Trailer:如果报文采用了分块传输编码方式,就可以用这个首部列出位于报文拖挂部分的首部集合 Transfer-Encoding:告知接收端为了保证报文的可靠传输,对报文采用了什么编码方式 Update:给出了发送端可能想要“升级”使用的新版本或协议 Via:显示了报文经过的中间节点(代理、网关) Cache-Control:用于随报文传送缓存指示 - 请求头部:请求头部是只在请求报文中有意义的头部。用于说明是谁或什么在发送请求、请求源自何处,或者客户端的喜好及能力 Client-IP:提供了运行客户端的机器的IP地址 From:提供了客户端用户的E-mail地址 Host:给出了接收请求的服务器的主机名和端口号 Referer:提供了包含当前请求URI的文档的URL UA-Color:提供了与客户端显示器的显示颜色有关的信息 UA-CPU:给出了客户端CPU的类型或制造商 UA-OS:给出了运行在客户端机器上的操作系统名称及版本 UA-Pixels:提供了客户端显示器的像素信息 User-Agent:将发起请求的应用程序名称告知服务器 Accept:告诉服务器能够发送哪些媒体类型 Accept-Charset:告诉服务器能够发送哪些字符集 Accept-Encoding:告诉服务器能够发送哪些编码方式 Accept-Language:告诉服务器能够发送哪些语言 TE:告诉服务器可以使用那些扩展传输编码 Expect:允许客户端列出某请求所要求的服务器行为 Range:如果服务器支持范围请求,就请求资源的指定范围 If-Match:如果实体标记与文档当前的实体标记相匹配,就获取这份文档 If-Modified-Sinec:除非在某个指定的日期之后资源被修改过,否则就限制这个请求 If-None-Match:如果提供的实体标记与当前文档的实体标记不相符,就获取文档 If-Range:允许对文档的某个范围进行条件请求 If-Unmodified-Since:除非在某个指定日期之后资源没有被修改过,否则就限制这个请求 Authorization:包含了客户端提供给服务器,以便对其自身进行认证的数据 Cookie:客户端用它向服务器传送数据 Cookie2:用来说明请求端支持的cookie版本 Max-Forward:在通往源端服务器的路径上,将请求转发给其他代理或网关的最大次数 Proxy-Authorization:这个首部在与代理进行认证时使用的 Proxy-Connection:这个首部是在与代理建立连接时使用的 - 响应头部:响应头部为客户端提供了一些额外信息,比如谁在发送响应、响应者的功能,甚至与响应相关的一些特殊指令 Age:(从最初创建开始)响应持续时间 Public:服务器为其资源支持的请求方法列表 Retry-After:如果资源不可用的话,在此日期或时间重试 Server:服务器应用程序软件的名称和版本 Title:对HTML文档来说,就是HTML文档的源端给出的标题 Warning:比原因短语更详细一些的警告报文 Accept-Ranges:对此资源来说,服务器可接受的范围类型 Vary:服务器会根据这些首部的内容挑选出最适合的资源版本发送给客户端 Proxy-Authenticate:来自代理的对客户端的质询列表 Set-Cookie:在客户端设置数据,以便服务器对客户端进行标识 Set-Cookie2:与Set-Cookie类似 WWW-Authenticate:来自服务器的对客户端的质询列表 - 实体首部:描述主体的长度和内容,或者资源自身 Allow:列出了可以对此实体执行的请求方法 Location:告知客户端实体实际上位于何处,用于将接收端定向到资源的位置(URL)上去 Content-Base:解析主体中的相对URL时使用的基础URL Content-Encoding:对主体执行的任意编码方式 Content-Language:理解主体时最适宜使用的自然语言 Content-Length:主体的长度 Content-Location:资源实际所处的位置 Content-MD5:主体的MD5校验和 Content-Range:在整个资源中此实体表示的字节范围 Content-Type:这个主体的对象类型 ETag:与此实体相关的实体标记 Expires:实体不再有效,要从原始的源端再次获取实体的日期和时间 Last-Modified:这个实体最后一次被修改的日期和时间 - 扩展首部:规范中没有定义的新首部,开发者可以自定义一个首部的值/对
3、实体的主体部分:该部分其实就是HTTP要传输的内容,是可选的。HTTP报文可以承载很多类型的数字数据,比如,图片、视频、HTML文档电子邮件、软件应用程序等等。
HTTP方法:并不是每个服务器都实现了所有的方法。即使服务器实现了所有这些方法,这些方法的使用很可能也是受限的。例如,支持DELETE方法或PUT方法的服务器可能并不希望任何人都能够删除或存储资源,这些限制通常都是在服务器的配置中进行设置的。
常用的HTTP方法:
GET方法:通常用于请求服务器发送某个资源。不包含主体
HEAD方法:与GET方法类似,但服务器在响应中只返回首部,使用HEAD方法可以,在不获取资源的情况下了解资源的情况(比如,判断其类型);通过查看响应中的状态码,看看某个对象是否存在;通过查看首部,测试资源是否被修改了;不包含主体
POST方法:该方法是用来向服务器发送数据的,常用于HTML表单,包含主体
PUT方法:该方法的语义就是让服务器用请求的主体部分来创建一个由所请求的URL命名的新文档,如果那个URL已经存在的话,就用这个主体来替代它。包含主体
TRACE方法:主要用于验证请求是否如愿穿过了请求/响应链,不包含主体
OPTIONS方法:决定可以在服务器上执行那些方法,不包含主体
DELETE方法:该方法就是请服务器删除请求URL所指定的资源,但是客户端应用程序无法保证删除操作一定会被执行,因为HTTP规范允许服务器在不通知客户端的情况下撤销请求,不包含主体扩展方法:指的是没有在HTTP/1.1规范中定义的方法,这些方法为开发者提供了一种扩展这些HTTP服务能力的手段。
状态码:HTTP状态码被分成了五大类。状态码为客户端提供了一种理解事务处理结果的便捷方式。
1、100~199(信息性状态码):HTTP/1.1向协议中引入了信息性状态码
2、200~299(成功状态码):客户端发起请求时,这些请求通常都是成功的。服务器有一组用来表示成功的状态码,分别对应于不同类型的请求
3、300~399(重定向状态码):重定向状态码要么告知客户端使用替代位置来访问他们所感兴趣的资源,要么就提供一个替代的响应而不是资源的内容
4、400~499(客户端错误状态码):有时客户端会发送一些服务器无法处理的东西。浏览网页时,我们都看到过臭名昭著的404 Not Found错误码,这只是服务器在告诉我们,它对我们请求的资源一无所知
5、500~599(服务器错误状态码):有时客户端发送了一条有效请求,服务器自身却出错了,这些会返回5xx状态码
3. WWW 所需软件与其结构
软件
httpd:提供Apache主程序
mysql:客户端
mysql-server:服务器程序
php:PHP 主程序含给 Apache 使用的模块
php-devel:PHP的发展工具
php-server:提供给PHP程序读取M有SQL数据库的模块配置文件
Apache 相关:
/etc/httpd/conf/httpd.conf 主配置文件
/etc/httpd/conf/*.conf 很多的额外参数文件,是主配置文件的组成部分
/usr/lib64/httpd/modules/、/etc/httpd/modules/ 外挂放置目录
/var/www/html/ 首页放置目录
/var/www/error / 错误信息放置目录
/var/www/icons/ 小图标放置目录
/var/www/cgi-bin/ 网页程序放置目录
/var/log/httpd/ 日志文件放置目录
/usr/sbin/apachectl 主要执行文件
/usr/sbin/httpd 二进制执行文件
/usr/sbin/htpasswd 密码保护MySQL相关:
/etc/my.cnf 主配置文件
/var/lib/mysql/ 文件存储PHP 相关:
/etc/httpd/conf/php.conf PHP设置参数
/etc/php.ini 主配置文件
/usr/lib64/httpd/modules/libphp5.so 提供给Apache 的模块
/etc/php.d/mysql.ini、/usr/lib64/php/modules/mysql.so MySQL接口
/usr/bin/phpsize、/usr/include/php/ 加速器功能
4. Apache 服务的配置文件
/etc/httpd/conf/httpd.conf 是主配置文件
[root@CentOS ~]# cd /etc/httpd[root@CentOS httpd]# pwd/etc/httpd[root@CentOS httpd]# ls -ltotal 8drwxr-xr-x. 2 root root 4096 Apr 7 03:28 confdrwxr-xr-x. 2 root root 4096 Apr 7 03:34 conf.dlrwxrwxrwx. 1 root root 19 Apr 7 03:28 logs -> ../../var/log/httpdlrwxrwxrwx. 1 root root 29 Apr 7 03:28 modules -> ../../usr/lib64/httpd/moduleslrwxrwxrwx. 1 root root 19 Apr 7 03:28 run -> ../../var/run/httpd[root@CentOS ~]# vim /etc/hosts #没有解析名时要有默认本地主机,否则报错127.0.0.1 localhost.localdomain localhost
/etc/httpd/conf/httpd.conf 共分为全局配置、主服务器配置、虚拟主机配置 3 部分。
- Apache 全局配置
- 主服务器配置
5. Apache 服务器控制存取方式
在配置文件 /etc/httpd/conf/httpd.conf 中,有4种控制存取方式:
针对目录的 形式
针对文件的 形式
针对位置的 形式
形式
<Dirtory /> Option FollowSymLinks AllowOverride None</Dirtory>
- Option 选项
- 浏览权限的设置
1 . 分布式特定目录存取控制(.htaccess 文件控制存取)
.htaccess 文件是一个访问控制文件,该文件主要用来配置相应目录的访问位置。
下面看看 AllowOverride 中的配置
AllowOverride none
.htaccess 文件文件完全被忽略,要打开该文件,可以使用如下方法
<Dirtory /> Option FollowSymLinks AllowOverride None</Dirtory>
然后将其中的 AllowOverride None 修改为 AllowOverride AuthConfig ,修改后的配置如下
<Dirtory /> Option FollowSymLinks AllowOverride None</Dirtory>
通过上面的设置,即可在需要进行访问控制的目录下创建一个 .htaccess 文件。文件名也可以在 http.conf 中通过 AccessFileName ToBeProteccted 进行重新设置
位于 httpd.conf 的相应文件的AllowOverride,主要是控制.htaccess文件中允许进行的设置,其 Override 详细参数
2 . 整体存取控制
限制访问所有以P开头的目录
\
6. Apache 服务的高级配置
- 创建虚拟目录
创建虚拟目录可以使用 Alias 指令或 AliasMatch 指令
使用 Alias 指令创建
Alias 虚拟目录 真实目录
使用 AliasMatch 指令创建虚拟目录
AliasMatch 正则表达式 真实的本机目录
- 设置目录权限
创建虚拟目录并经服务器重启后,客户端暂时还是不能访问,需要使用容器来配置虚拟目录的目录权限
<Dirtory 目录路径> ...... </Dirtory>
例如为 /download 设置目录权限
<Dirtory "export/public/software">Option Indexs MutiViewsAllowOverride NoneOrder allow,denyAllow from all</Dirtory>
- 配置与管理虚拟主机
方法一:在一台计算机上运行多台 Web 服务器,每台 Web 服务器上部署一个网站,然后为每台Web服务器指定其监听的 IP 地址与端口
方法二:在一台 Web 服务器上部署一个网站,为每个网站指定不同的目录用于存放每个网站的文件,同时需要为不同的网站指定不同的IP地址和端口,或者指定不同的主机名称,此种方法最为常用。此时,可以把每个网站看成是一台虚拟主机
1 . 基于IP地址的虚拟主机(IP Based)
除了设置监听地址外,还需要使用 容器来定义虚拟主机
<VirtualHost IP地址:[端口号] [IP地址:[端口号]] ...> ······</VirtualHost>
在一台服务器上分别设置两个 Web 站点,一个是 IP地址是192.168.1.2,另一个IP地址是192.168.1.4
[root@CentOS ~]# /sbin/ifconfig eth0:0 192.168.1.2 netmask 255.255.255.0 # 找到 http.conf 添加内容 <VirtualHost 192.168.1.2> # 绑定第一台虚拟主机IP ServerAdmin webmaster@testdomain.tst # 设置管理员邮箱 DocumentRoot /var/www/testdomain # 设置虚拟主机文档路径 ServerName www.testdomain.tst # 设置主机名 ErrorLog /var/log/httpd/www.testdomain.tst/error.log # 设置错误记录文档 </VirtualHost> <VirtualHost 192.168.1.4> # 绑定第二台虚拟主机IP ServerAdmin webmaster@othertestdomain.tst # 设置管理员邮箱 DocumentRoot /var/www/othertestdomain.tst # 设置虚拟主机文档路径 ServerName www.othertestdomain.tst # 设置主机名 ErrorLog /var/log/httpd/www.othertestdomain.tst/error.log # 设置错误记录文档 </VirtualHost>
2 . 基于主机的虚拟主机(Name Based)
NameVirtualHost IP地址: 端口
假设IP地址是192.168.1.4的服务器,要为www.test1.tst 和www.test2.tst同时服务,则可以通过 httpd.conf 中添加内容实现
NameVirtualHost 192.168.1.4 ; 指定绑定IP地址。如果想在服务器上所有IP地址进行绑定,可以使用*
# 第一台虚拟主机配置:<VirtualHost *> ServerName www.test1.tst # 设置第一个虚拟主机名ServerAlias test1.tst *.test1.tst # 为了在浏览器输入test.tst也能访问DocumentRoot /var/www/test1 # 指定虚拟文档位置</VirtualHost># 第二台虚拟主机配置<VirtualHost *> ServerName www.test2.tst # 设置第一个虚拟主机名DocumentRoot /var/www/test2 # 指定虚拟文档位置</VirtualHost>
- 用户认证和授权
1 . 简介
[root@CentOS ~]# htpasswd [-bcD] [-mdps] 密码文件名字 用户名
#创建一个用于 .htaccess 密码认证的用户 Neo[root@CentOS ~]# htpasswd -mbc .htpasswd Neo 000000
2 . 指定相应的认证方式
<Dirtory /home/clinuxer/public>AuthType BasicAuthName "Restricted Files"AuthUserFile /usr/local/apache/passwd/.htpasswdRequire user clinuxer</Directory>
摘要认证
htdigest -c /etc/httpd/digesturefile “Infomation” ly
匿名认证
Anonymous userName1 userName2 …
基于文件的认证
AuthUserFile userFilePath
AuthGroupFile groupFilePath基于数据库的认证
3 . 具体实例
# 创建用户[root@CentOS ~]# cd /hhwork/public/software[root@CentOS software]# /usr/bin/htpasswd -c /usr/local/apache/passwd/.htpasswd Rose[root@CentOS ~]# /usr/bin/htpasswd 。htpasswd apple# 设置该目录允许采用 .htaccess 进行身份验证[root@CentOS ~]# vim /etc/httpd/conf/httpd.conf Alias /dec/ "hhwork/public/software" <Directory /hhwork/public/software> Option Indexes MutiViews # 允许列目录 AllowOverride AuthConfig # 启用用户身份验证 Order deny,allow allow from all # 允许所有用户访问 </Directory>[root@CentOS ~]# /etc/init.d/httpd restart# 在 /hhwork/public/software 目录下新建一个具有以下内容的 .htaccess 文件 [root@CentOS ~]# cd /hhwork/public/software[root@CentOS ~]# touch /htaccess[root@CentOS ~]# vim .htaccess AuthName "Test example" AuthType Basic AuthUserFile /usr/local/apache/passwd/.htpasswd require user Rose# 使用浏览器访问受限目录
4 . 具体实例
# 在 /usr/local/apache/passwd/ 目录下创建一个具有如下内容的 .htaccess 文件 allowusr: Rose stella# 打开 .htaccess 文件 AuthName "Test Group Zero" AuthType Basic AuthUserFile /usr/local/apache/passwd/.htpasswd AuthGroupFile /usr/local/apache/passwd/.htgroup Require group allowusr# 使用同组的 Rose 和staella 这两个用户都可以访问Apache 服务器上的访问受限的目录
- 配置 SSI(服务器端包含指令)
在配置文件 http.conf 或 .htaccess 中给 Option 指令添加 +Includes 值
Option +includes可以通过两种方法实现服务器指定解析 HTML 文件
1 . AddType text/html .shtml
AddOutputFilter INCLUDE .shtml
2 . chmod +x pagename.html
- MIME 类型简介(多用途互联网邮件扩展)
AddType Type/SubType extension
例如为.tgz包指定MIME类型
AddType application/x-tat.tgz
7. PHP 的默认参数修改
[root@CentOS ~]# cd /etc/httpd/conf.d[root@CentOS conf.d]# ll *.conf-rw-r--r--. 1 root root 295 May 21 2009 manual.conf-rw-r--r--. 1 root root 118 May 20 2009 mod_dnssd.conf-rw-r--r--. 1 root root 1796 Apr 22 2005 perl.conf-rw-r--r--. 1 root root 674 Nov 12 2010 php.conf-rw-r--r--. 1 root root 9473 Dec 8 2009 ssl.conf-rw-r--r--. 1 root root 352 Sep 9 2004 webalizer.conf-rw-r--r--. 1 root root 299 May 21 2009 welcome.conf-rw-r--r--. 1 root root 43 Jul 14 2009 wsgi.conf[root@CentOS conf.d]# vim /etc/httpd/conf.d/php.conf<IfModule prefork.c> #根据不同的 PID 模式给予不同的 PHP 运行模块 LoadModule php5_module modules/libphp5.so</IfModule><IfModule worker.c> LoadModule php5_module modules/libphp5-zts.so</IfModule>AddHandler php5-script .php # 所以扩展名一定是 .php 结尾 AddType text/html .php #.php 结尾的文件是纯文本文档DirectoryIndex index.php #首页文件名增加 index.php #AddType application/x-httpd-php-source .phps[root@CentOS ~]# vim /etc/php.ini register_globals = Offlog_errors = On #是否将PHP程序的错误记录起来ignore_repeated_error = On ignore_repeated_source = On display_errors = Off #是否在浏览器上面显示相关错误信息display_startup_error = Offpost_max_size = 20M #729行#上传容量限制file_upload = On upload_max_filesize = 16M #878行memory_limit = 128M
8. 启动WWW服务于测试PHP模块
# 启动[root@CentOS ~]# /etc/init.d/httpd startStarting httpd: [ OK ][root@CentOS ~]# /etc/init.d/httpd configtestSyntax OK[root@CentOS ~]# chkconfig httpd on或者[root@CentOS ~]# /usr/sbin/apachectl start #开启[root@CentOS ~]# /usr/sbin/apachectl stop #关闭# 先看看 port 有没有启动[root@CentOS ~]# netstat -tulnp | grep 'httpd'tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3555/httpd # 再看看登录文件的信息记录了什么[root@CentOS ~]# tail /var/log/httpd/error_log [Mon Apr 17 21:28:23 2017] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0[Mon Apr 17 21:28:23 2017] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)[Mon Apr 17 21:28:24 2017] [notice] Digest: generating secret for digest authentication ...[Mon Apr 17 21:28:24 2017] [notice] Digest: done[Mon Apr 17 21:28:24 2017] [warn] mod_wsgi: Compiled for Python/2.6.2.[Mon Apr 17 21:28:24 2017] [warn] mod_wsgi: Runtime using Python/2.6.5.[Mon Apr 17 21:28:24 2017] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.2 mod_ssl/2.2.15 OpenSSL/1.0.0-fips mod_wsgi/3.2 Python/2.6.5 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations
9. MySQL 的基本设定
#启动[root@CentOS ~]# /etc/init.d/mysqld start[root@CentOS ~]# netstat -tulnp | grep 'mysql'tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3871/mysqld # 测试是否能连上mysql[root@CentOS ~]# mysql -u root# 设置mysql管理员密码[root@CentOS ~]# mysqladmin -u root password 'your.password' #以后就需要密码了[root@CentOS ~]# mysql -u root -p #设置密码your.passwordEnter password: your.passwordmysql> grant all priviliges on kevin.* to kevin@localhost identified by 'kevinpw' ;mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+3 rows in set (0.01 sec)mysql> select * from user where user = 'kevinuser' ;Empty set (0.00 sec)mysql> exitBye# MySQL root密码遗忘紧急处理# 将MySQL关闭,将/var/lib/mysql/*那个目录的数据删除,然后重启MySQL,数据库会重建,密码会消失
MySQL效率调优/etc/my.cnf
10. 安装设置及详细操作
[root@CentOS ~]# yum list all http*httpd.x86_64 2.2.15-5.el6.centos @anaconda-CentOS-201106060106.x86_64/6.0httpd-manual.noarch 2.2.15-5.el6.centos @anaconda-CentOS-201106060106.x86_64/6.0httpd-tools.x86_64 2.2.15-5.el6.centos @anaconda-CentOS-201106060106.x86_64/6.0Available Packageshttpd.x86_64 2.2.15-59.el6.centos base httpd-devel.i686 2.2.15-59.el6.centos base httpd-devel.x86_64 2.2.15-59.el6.centos base httpd-manual.noarch 2.2.15-59.el6.centos base httpd-tools.x86_64 2.2.15-59.el6.centos base [root@CentOS ~]# getenforce # 确保SELinux是禁用状态Enforcing[root@CentOS ~]# setenforce 0 #当前生效[root@CentOS ~]# vim /etc/sysconfig/selinux #永久有效或者 [root@CentOS ~]# vim /etc/selinux/config #与上一个是同一个文件SELINUX=permissive[root@CentOS ~]# yum install httpd[root@CentOS ~]# rpm -ql httpd | less #查看安装生成了哪些文件[root@CentOS ~]# netstat -tulnp | grep :80[root@CentOS ~]# ps aux | grep httpd[root@CentOS ~]# tree /etc/httpd/conf.d//etc/httpd/conf.d/├── manual.conf├── mod_dnssd.conf├── perl.conf├── php.conf├── README├── ssl.conf├── webalizer.conf├── welcome.conf #默认欢迎页面└── wsgi.conf0 directories, 9 files[root@CentOS ~]# mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak #删除首页[root@CentOS ~]# service httpd restart[root@CentOS ~]# vim /var/www/html/a.html #自定义首页,首页需要在配置文件定义默认页面[root@CentOS ~]# grep "Section" httpd.conf #主配置文件分三个部分### Section 1: Global Environment### Section 2: 'Main' server configuration ### Section 3: Virtual Hosts[root@CentOS ~]# yum install httpd-manual #本地查看主配置文件命令帮助[root@CentOS ~]# vim /etc/httpd/conf.d/manual.conf #安装之后只需要在访问路径之后加上manual即可,如http://192.168.1.110/manual/
#第一部分:全局变量,Global Environment[root@CentOS ~]# vim /etc/httpd/conf/httpd.conf ServerTokens OS #系统信息ServerRoot "/etc/httpd" #根目录PidFile run/httpd.pid #进程文件路径Timeout 60 #接收发送连接无响应的超时时间KeepAlive Off #是否使用常连接,对单个用户来说可以避免重复的三次握手过程,提高系统性能MaxKeepAliveRequests 100 #常连接请求数量KeepAliveTimeout 15 #常连接超时时间#知识扩展:专门的压力测试工具LoadRummerprefork (一个请求用一个进程响应)worker (一个请求用一个线程响应, (启动多个进程,每个进程生成多个线程))event (一个进程处理多个请求)[root@CentOS ~]# httpd -l #列出进程编译所支持的模块Compiled in modules: core.c prefork.c http_core.c mod_so.c[root@CentOS ~]# rpm -ql httpd | grep bin/usr/sbin/apachectl/usr/sbin/htcacheclean/usr/sbin/httpd #默认MPM的prefork模型/usr/sbin/httpd.event #MPM的event模型/usr/sbin/httpd.worker #MPM的worker模型/usr/sbin/httxt2dbm/usr/sbin/rotatelogs/usr/sbin/suexec/var/www/cgi-bin/var/www/icons/binary.gif/var/www/icons/binary.png/var/www/icons/binhex.gif/var/www/icons/binhex.png/var/www/icons/small/binary.gif/var/www/icons/small/binary.png/var/www/icons/small/binhex.gif/var/www/icons/small/binhex.png[root@CentOS ~]# httpd.worker -l #列出worker模型的web服务器支持的模块Compiled in modules: core.c worker.c http_core.c mod_so.c[root@CentOS ~]# httpd.event -l #列出event模型的web服务器支持的模块Compiled in modules: core.c event.c http_core.c mod_so.c<IfModule prefork.c> #prefork模型(容器)StartServers 8 #启动几个空闲进程MinSpareServers 5 #最少空闲进程MaxSpareServers 20 #最大空闲进程ServerLimit 256 #最大客户端数的上限数,MaxClients 256 #最大客户端数,必须低于最大客户端数的上限数MaxRequestsPerChild 4000 #每个客户端最大请求数</IfModule><IfModule worker.c> #worker模型StartServers 4 #启动几个空闲进程MaxClients 300 #最大客户端数MinSpareThreads 25 #最小空闲线程MaxSpareThreads 75 #最大空闲线程ThreadsPerChild 25 #一个进程生成多少个线程MaxRequestsPerChild 0 #每个客户端最大请求数</IfModule>Listen 80 #指定监听的地址和端口,Listen 8080 #地址可省略,可同时监听两个端口LoadModule auth_basic_module modules/mod_auth_basic.so33 #模块化Include conf.d/*.conf #包含的配置文件User apache #指定用户和组Group apache
# 第二部分:主服务器'Main' server configuration [root@CentOS ~]# grep Section httpd.conf### Section 1: Global Environment### Section 2: 'Main' server configuration #2和3不能同时配置出现### Section 3: Virtual Hosts# 只提供一个主机ServerAdmin root@localhost #服务器管理员ServerName www.example.com:80 #服务器名,不启用则用当前IP反向解析服务器主机名UseCanonicalName Off #正式名称DirectoryIndex index.html index.html.var #指定首页文件DocumentRoot "/var/www/html" #文档根目录,首页所在目录,URL指向此路径<Directory "/var/www/html"> #定义路径如何被访问(容器) Options Indexes FollowSymLinks #Indexes:索引根目录下所有文件(危险操作),FollowSynLinks: 允许访问符号链接指向的原文件,Includes: 允许执行服务端包含(SSI)(危险操作),ExecCGI: 允许运行CGI脚本,All: 支持所有选项 AllowOverride None #允许覆盖下面两项,也可以用认证配置来覆盖用户的访问请求(AuthConfig) Order allow,deny #定义服务器的访问控制列表(有次序) Allow from all #允许所有人访问</Directory>[root@CentOS ~]# httpd -M #列出额外装载的模块# 用认证配置来覆盖用户的访问请求(AuthConfig)AllowOverride AuthConfigAuthType BasicAuthName "Restricted Files"AuthUserFile "/erc/httpd/conf/htpasswd" #文件必须存在Require valid-user #允许所有用户访问(使用Require user kevin指定用户访问)[root@CentOS ~]# htpasswd -c -m /etc/httpd/conf/htpasswd kevin #第一次需要-c创建文件New password: [root@CentOS ~]# htpasswd -m /etc/httpd/conf/htpasswd tomNew password: [root@CentOS ~]# cat /etc/httpd/conf/htpasswdkevin:$apr1$Plhk95dy$FuUJfP/NXvPaQ7gi/MY5D1tom:$apr1$s1X7dP41$1ZbNhnjZDPVY9sAbjQ34U/# 再次登录发现需要输入账号密码才能登录# 用认证配置来覆盖用户的访问请求(AuthConfig)AllowOverride AuthConfigAuthType BasicAuthName "Restricted Files"AuthUserFile "/erc/httpd/conf/htpasswd" #文件必须存在AuthGroupFile "/etc/http/conf/htgroup"Require group myusers #允许组内访问[root@CentOS ~]# cat /etc/httpd/conf/htgroup #添加组内成员myusers: kevin tom [root@CentOS ~]# htpasswd -c -m /etc/httpd/conf/htpasswd kevin #第一次需要-c创建文件New password: [root@CentOS ~]# htpasswd -m /etc/httpd/conf/htpasswd tomNew password: [root@CentOS ~]# cat /etc/httpd/conf/htpasswdkevin:$apr1$Plhk95dy$FuUJfP/NXvPaQ7gi/MY5D1tom:$apr1$s1X7dP41$1ZbNhnjZDPVY9sAbjQ34U/# 再次登录发现需要输入账号密码才能登录#支持个人用户家目录的网页文件(注意目录权限)<IfModule mod_userdir.c> UserDir public_html </IfModule>[kevin@CentOS ~]$ mkdir public_html[kevin@CentOS ~]$ vim public_html/index.html[root@CentOS ~]# chmod o+x /home/kevin/ #此时浏览http://192.168.1.110/~kevin/ 会显示kevin的首页#定义.htaccess and .htpasswd 的访问控制权限<Files ~ "^\.ht"> Order allow,deny Deny from all Satisfy All</Files>AccessFileName .htaccess #定义每个目录的访问控制权限,只需要将这个文件名新建到当前子目录下(会继承),文件中写访问控制机制(Auth等)(效率低,建议注释掉)TypesConfig /etc/mime.types #让httpd支持MIME(多功能互联网邮件扩展)类型的存放文件DefaultType text/plain #接上,默认文本类型下的明文文本类型<IfModule mod_mime_magic.c> #接上# MIMEMagicFile /usr/share/magic.mime MIMEMagicFile conf/magic</IfModule>HostnameLookups Off #日志中是否记录访问的主机名ErrorLog logs/error_log #错误日志LogLevel warn #日志级别LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined #访问日志格式,定义宏,官方手册有说明。http://192.168.1.110/manual/LogFormat "%h %l %u %t \"%r\" %>s %b" common #定义宏LogFormat "%{Referer}i -> %U" refererLogFormat "%{User-agent}i" agent%h:远程端地址%l:远端主机上登录的的主机用户名%u:登录网站使用的用户,账号密码登录的用户名%t:日志产生的时间%r:请求报文第一行%>s:显示原始状态码%b:响应报文的大小%{Referer}i:首部中显示的时候内容。Referer是网页跳转的时候,从哪个网页来的,%{User-Agent}i:首部中的浏览器的类型CustomLog logs/access_log combined #定义访问日志文件和类型,注意combined是上面一段定义的Alias /icons/ "/var/www/icons/" #定义路径别名,路径中出现/icons/其实代表/var/www/icons/# 测试配置文件语法的正确性[root@CentOS ~]# httpd -tSyntax OK# 安装elinks[root@CentOS ~]# yum install elinks# 查看错误日志[root@CentOS ~]# tail /var/log/httpd/error_log
# 第三部分:虚拟主机Virtual Hosts[root@CentOS ~]# grep Section httpd.conf### Section 1: Global Environment### Section 2: 'Main' server configuration### Section 3: Virtual Hosts #2和3不能同时配置出现# 提供多个不同的站点虚拟主机 基于IP IP1:80 IP2:80 基于端口 IP:80 IP:8080 基于域名 IP:80 主机名不同 www.magedu.com www.a.org www.b.net得先取消中心主机,注释中心主机的DocumentRoot即可。虚拟主机的定义:<VirtualHost HOST>…………</VirtualHost>基于IP,HOST的写法:HOST IP1:80 IP2:80基于端口:HOST IP:80 IP:8080基于域名: *:80ServerName 不同#基于IP的虚拟主机:[root@CentOS ~]# vim /etc/httpd/conf/httpd.conf #DocumentRoot "/var/www/html"然后添加虚拟主机配置信息即可,或者新建单独的配置文件/etc/httpd/conf.d/virtual.conf[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.conf<VirtualHost 192.168.1.110:80> ServerName www.centos.tst DocumentRoot "/www/centos.tst"</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos2.tst DocumentRoot "/www/centos2.tst"</VirtualHost>[root@CentOS ~]# httpd -tWarning: DocumentRoot [/www/centos.tst] does not existWarning: DocumentRoot [/www/centos2.tst] does not existSyntax OK[root@CentOS ~]# mkdir -pv /www/{centos,centod2}.tstmkdir: created directory `/www/centos.tst'mkdir: created directory `/www/centod2.tst[root@CentOS ~]# vim /www/centos.tst/index.html<title>centos.tst</title><h1>centos.tst</h1>[root@CentOS ~]# vim /www/centos2.tst/index.html<title>centos2.tst</title><h1>centos2.tst</h1>[root@CentOS ~]# httpd -t #检查语法Syntax OK[root@CentOS ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:CF:88:43 inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:576 Metric:1 RX packets:32114 errors:0 dropped:0 overruns:0 frame:0 TX packets:16936 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:8406148 (8.0 MiB) TX bytes:1748974 (1.6 MiB)[root@CentOS ~]# ip addr add 192.168.1.105/24 dev eth0 #添加网卡IP地址[root@CentOS ~]# ip addr show1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 576 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:cf:88:43 brd ff:ff:ff:ff:ff:ff inet 192.168.1.110/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.105/24 scope global secondary eth0# 访问192.168.1.110和192.168.1.105都可以打开#基于端口的虚拟主机:[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.conf<VirtualHost 192.168.1.110:80> #基于IP ServerName www.centos.tst DocumentRoot "/www/centos.tst"</VirtualHost><VirtualHost 192.168.1.105:80> #基于IP ServerName www.centos2.tst DocumentRoot "/www/centos2.tst"</VirtualHost><VirtualHost 192.168.1.110:8080> #基于port ServerName www.centos3.tst DocumentRoot "/www/centos3.tst"</VirtualHost>[root@CentOS ~]# mkdir /www/centos3.tst/[root@CentOS ~]# vim /www/centos3.tst/index.html<title>centos3.tst</title><h1>centos3.tst</h1>Listen 80 #监听端口Listen 8080# 访问192.168.1.110和192.168.1.105和192.168.1.110:8080都可以打开#基于主机名的虚拟主机:(混合使用的虚拟主机)[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.conf NameVirtualHost 192.168.1.105:80<VirtualHost 192.168.1.110:80> ServerName www.centos.tst DocumentRoot "/www/centos.tst"</VirtualHost><VirtualHost 192.168.1.105:80> #注意区别 ServerName www.centos2.tst DocumentRoot "/www/centos2.tst"</VirtualHost><VirtualHost 192.168.1.110:8080> ServerName www.centos3.tst DocumentRoot "/www/centos3.tst"</VirtualHost><VirtualHost 192.168.1.105:80> #注意区别 ServerName www.centos4.tst DocumentRoot "/www/centos4.tst"</VirtualHost>#修改本机(Win7)hosts文件,否则不能解析主机名C:\Windows\System32\drivers\etc\hosts192.168.1.105 centos4.tst192.168.1.110 centos2.tst#Win7下ping 这两个主机名发现可以ping通C:\Users\John>ping www.centos2.tst正在 Ping www.centos2.tst [192.168.1.110] 具有 32 字节的数据:来自 192.168.1.105 的回复: 字节=32 时间=1ms TTL=64C:\Users\John>ping www.centos4.tst正在 Ping www.centos4.tst [192.168.1.105] 具有 32 字节的数据:来自 192.168.1.105 的回复: 字节=32 时间=1ms TTL=64# 是用IP(192.168.1.105)访问时,那个在第一个哪个先解析(www.centos2.tst)# 现在日志信息在同一个位置[root@CentOS ~]# tail /var/log/httpd/error_log #错误日志[root@CentOS ~]# tail /var/log/httpd/access_log #访问日志# 让每个站点使用不同的日志[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.conf NameVirtualHost 192.168.1.105:80<VirtualHost 192.168.1.110:80> ServerName www.centos.tst DocumentRoot "/www/centos.tst" CustomLog /var/log/httpd/centos.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> #注意区别 ServerName www.centos2.tst DocumentRoot "/www/centos2.tst" CustomLog /var/log/httpd/centos2.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.110:8080> ServerName www.centos3.tst DocumentRoot "/www/centos3.tst" CustomLog /var/log/httpd/centos3.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> #注意区别 ServerName www.centos4.tst DocumentRoot "/www/centos4.tst" CustomLog /var/log/httpd/centos4.tst/access_log combined</VirtualHost>[root@CentOS ~]# httpd -t[root@CentOS ~]# tail /var/log/messages[root@CentOS ~]# tail /var/log/httpd/error_log [root@CentOS ~]# ll -d /var/log/httpd/drwx------. 2 root root 4096 Mar 22 14:53 /var/log/httpd/[root@CentOS ~]# mkdir -pv /var/log/httpd/centos.tst/ #创建目录[root@CentOS ~]# mkdir -pv /var/log/httpd/centos2.tst/[root@CentOS ~]# mkdir -pv /var/log/httpd/centos3.tst/[root@CentOS ~]# mkdir -pv /var/log/httpd/centos4.tst/[root@CentOS ~]# service httpd restart[root@CentOS ~]# tree /var/log/httpd//var/log/httpd/├── access_log├── centos2.tst│?? └── access_log├── centos3.tst│?? └── access_log├── centos4.tst│?? └── access_log├── centos.tst│?? └── access_log├── error_log├── ssl_access_log├── ssl_error_log└── ssl_request_log# 让www.centos4.tst 拒绝192.168.1.101 访问先测试用192.168.1.101打开 网页www.centos4.tst,可以访问[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.confNameVirtualHost 192.168.1.105:80<VirtualHost 192.168.1.110:80> ServerName www.centos.tst DocumentRoot "/www/centos.tst" CustomLog /var/log/httpd/centos.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos2.tst DocumentRoot "/www/centos2.tst" CustomLog /var/log/httpd/centos2.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.110:8080> ServerName www.centos3.tst DocumentRoot "/www/centos3.tst" CustomLog /var/log/httpd/centos3.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos4.tst DocumentRoot "/www/centos4.tst" CustomLog /var/log/httpd/centos4.tst/access_log combined <Directory "/www/centos4.tst"> #访问控制 Options none AllowOverride none Order deny,allow Deny from 192.168.1.101 </Directory></VirtualHost>[root@CentOS ~]# httpd -tSyntax OK# 用192.168.1.101网页登录http://www.centos4.tst/,发现被禁止# 让www.centos2.tst 必须通过账号密码才能访问[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.conf<VirtualHost 192.168.1.110:80> ServerName www.centos.tst DocumentRoot "/www/centos.tst" CustomLog /var/log/httpd/centos.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos2.tst DocumentRoot "/www/centos2.tst" CustomLog /var/log/httpd/centos2.tst/access_log combined <Directory "/www/centos2.tst"> #身份认证 AllowOverride authconfig AuthType basic AuthName "Restrict area." AuthUserFile "/etc/httpd/conf/.htpasswd" Require vaild-user </Directory></VirtualHost><VirtualHost 192.168.1.110:8080> ServerName www.centos3.tst DocumentRoot "/www/centos3.tst" CustomLog /var/log/httpd/centos3.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos4.tst DocumentRoot "/www/centos4.tst" CustomLog /var/log/httpd/centos4.tst/access_log combined <Directory "/www/centos4.tst"> Options none AllowOverride none Order deny,allow AllowOverride none Order deny,allow Deny from 192.168.1.101 </Directory></VirtualHost>[root@CentOS ~]# httpd -tservice httpd restart httpd restart[root@CentOS ~]# htpasswd -c -m /etc/httpd/.htpasswd tomNew password: Re-type new password: Adding password for user tom[root@CentOS ~]# htpasswd -m /etc/httpd/.htpasswd kevinNew password: Re-type new password: Adding password for user kevin[root@CentOS ~]# httpd -t# 网页登录192.168.1.105发现需要账号密码# 定义默认虚拟主机设定默认虚拟主机<VirtualHost _default_:80>DocumentRoot /www/default80# ...</VirtualHost><VirtualHost _default_:*>DocumentRoot /www/default# ...</VirtualHost>[root@CentOS ~]# vim /etc/httpd/conf.d/virtual.confNameVirtualHost 192.168.1.105:80 <VirtualHost _default_:80> #注意此处 ServerName default DocumentRoot "/www/default"</VirtualHost><VirtualHost 192.168.1.110:80> ServerName www.centos.tst DocumentRoot "/www/centos.tst" CustomLog /var/log/httpd/centos.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos2.tst DocumentRoot "/www/centos2.tst" CustomLog /var/log/httpd/centos2.tst/access_log combined <Directory "/www/centos2.tst"> AllowOverride authconfig AuthType basic AuthName "Restrict area." AuthUserFile "/etc/httpd/conf/.htpasswd" Require vaild-user </Directory></VirtualHost><VirtualHost 192.168.1.110:8080> ServerName www.centos3.tst DocumentRoot "/www/centos3.tst" CustomLog /var/log/httpd/centos3.tst/access_log combined</VirtualHost><VirtualHost 192.168.1.105:80> ServerName www.centos4.tst DocumentRoot "/www/centos4.tst" CustomLog /var/log/httpd/centos4.tst/access_log combined <Directory "/www/centos4.tst"> Options none AllowOverride none Order deny,allow AllowOverride none Order deny,allow Deny from 192.168.1.101 </Directory></VirtualHost>[root@CentOS ~]# mkdir /www/default[root@CentOS ~]# vim /www/default/index.html<title>default</title><h1>default</h1>[root@CentOS ~]# httpd -t# 使用本机(Win7)访问http://192.168.1.105/时显示是默认虚拟主机
# 定义本地主机(URL)<Location /status> #样例 SetHandle server-status #调用某个文件时执行某个动作 Order Deny,Allow Deny from all Allow from .foo .com</Location>[root@CentOS ~]# vim /etc/httpd/conf/httpd.conf <Location /server-status> SetHandler server-status Order allow,deny Allow from all</Location># 本机(Win7)访问http://192.168.1.110/server-status会显示服务器状态信息
11. 基于openssl的https服务配置
[root@CentOS ~]# yum install mod_ssl #先安装相关模块[root@CentOS ~]# rpm -ql mod_ssl #查看安装了哪些配置文件/etc/httpd/conf.d/ssl.conf/usr/lib64/httpd/modules/mod_ssl.so/var/cache/mod_ssl/var/cache/mod_ssl/scache.dir/var/cache/mod_ssl/scache.pag/var/cache/mod_ssl/scache.sem
# 这里需要一台linxu主机当做 CA,启动另一个linux虚拟机,ip地址192.168.1.120# 要想做CA,需要生成自签证书[root@CentOS ~]# cd /etc/pki/CA #生成私钥[root@CentOS CA]# pwd/etc/pki/CA[root@CentOS CA]# (umask 077; openssl genrsa -out private/cakey.pem 2048)Generating RSA private key, 2048 bit long modulus...............................+++.........+++e is 65537 (0x10001)[root@CentOS CA]# ll private/total 4-rw-------. 1 root root 1679 Apr 20 04:44 okey.pem[root@CentOS ~]# vim /etc/pki/tls/openssl.cnf #编辑自签证书的默认信息[ req_distinguished_name ] #修改默认信息countryName = Country Name (2 letter code)countryName_default = CNstateOrProvinceName = State or Province Name (full name)stateOrProvinceName_default = JiLinlocalityName = Locality Name (eg, city)localityName_default = ChangChun0.organizationName = Organization Name (eg, company)0.organizationName_default = Changchun Jianzhu XueyuanorganizationalUnitName = Organizational Unit Name (eg, section)organizationalUnitName_default = Dianqi Xinxi Xueyuan# 给自己生成自签证书[root@CentOS CA]# pwd/etc/pki/CA[root@CentOS CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [CN]:State or Province Name (full name) [JiLin]:Locality Name (eg, city) [ChangChun]:Organization Name (eg, company) [Changchun Jianzhu Xueyuan]:Organizational Unit Name (eg, section) [Dianqi Xinxi Xueyuan]:Common Name (eg, your name or your server's hostname) []:ca.contos.tstEmail Address []:admin@centos.tst[root@CentOS CA]# vim /etc/pki/tls/openssl.cnfdir = /etc/pki/CA #修改路径certs = $dir/certs # Where the issued certs are keptcrl_dir = $dir/crl # Where the issued crl are keptdatabase = $dir/index.txt # database index file.#unique_subject = no # Set to 'no' to allow creation of # several ctificates with same subject.new_certs_dir = $dir/newcerts # default place for new certs.certificate = $dir/cacert.pem # The CA certificateserial = $dir/serial # The current serial numbercrlnumber = $dir/crlnumber # the current crl number # must be commented out to leave a V1 CRLcrl = $dir/crl.pem # The current CRLprivate_key = $dir/private/cakey.pem# The private keyRANDFILE = $dir/private/.rand # private random number file[root@CentOS CA]# touch index.txt[root@CentOS CA]# echo 01 > serial[root@CentOS CA]# lscacert.pem certs crl index.txt newcerts private serial[root@CentOS CA]# # CA做好了,等待签证
# 返回服务器,制作签名请求文件[root@CentOS ~]# (umask 077; openssl genrsa 1024 > /etc/httpd/ssl/httpd.key) #写法不同,跟上面的那个作用一样Generating RSA private key, 1024 bit long modulus............++++++...................++++++e is 65537 (0x10001)[root@CentOS ~]# ll /etc/httpd/ssl/httpd.key -rw-------. 1 root root 887 Apr 20 17:15 /etc/httpd/ssl/httpd.key[root@CentOS ssl]# pwd/etc/httpd/ssl[root@CentOS ssl]# openssl req -new -key httpd.key -out httpd.csrYou are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [XX]:CNState or Province Name (full name) []:JiLinLocality Name (eg, city) [Default City]:ChangChunOrganization Name (eg, company) [Default Company Ltd]:Changchun Jianzhu XueyuanOrganizational Unit Name (eg, section) []:Dianqi Xinxi XueyuanCommon Name (eg, your name or your server's hostname) []:www.centos4.tst #服务器的虚拟主机的主机名Email Address []:Please enter the following 'extra' attributesto be sent with your certificate requestA challenge password []:An optional company name []:# 这时候需要传输这个生成的签名请求文件到CA处,否则无法验证签名[root@CentOS ssl]# scp httpd.csr 192.168.1.120:/tmproot@192.168.1.120's password: httpd.csr 100% 704 0.7KB/s 00:00
# CA签署证书[root@CentOS ~]# openssl ca -in /tmp/httpd.csr -out /tmp/httpd.crt -days 3650Using configuration from /etc/pki/tls/openssl.cnfCheck that the request matches the signatureSignature okCertificate Details: Serial Number: 1 (0x1) Validity Not Before: Apr 20 09:29:57 2017 GMT Not After : Apr 18 09:29:57 2027 GMT Subject: countryName = CN stateOrProvinceName = JiLin organizationName = Changchun Jianzhu Xueyuan organizationalUnitName = Dianqi Xinxi Xueyuan commonName = www.centos4.tst X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: F7:D8:DE:53:BE:BE:4F:BF:D3:CE:24:7C:D9:55:9A:8B:40:CB:92:1C X509v3 Authority Key Identifier: keyid:A1:23:25:12:32:84:B7:F5:B3:ED:48:46:30:44:D6:86:3C:BB:C6:ECCertificate is to be certified until Apr 18 09:29:57 2027 GMT (3650 days)Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]yWrite out database with 1 new entriesData Base Updated[root@CentOS ~]# cat /etc/pki/CA/index.txtV 270418092957Z 01 unknown /C=CN/ST=JiLin/O=Changchun Jianzhu Xueyuan/OU=Dianqi Xinxi Xueyuan/CN=www.centos4.tst[root@CentOS ~]# cat /etc/pki/CA/serial02
# 服务器取得签名确认文件[root@CentOS ssl]# scp 192.168.1.120:/tmp/httpd.crt ./root@192.168.1.120's password: httpd.crt 100% 3946 3.9KB/s 00:00 [root@CentOS ssl]# lshttpd.crt httpd.csr httpd.key
# CA 完成最后删除操作[root@CentOS ~]# ls /etc/pki/CA/certs/[root@CentOS ~]# ls /etc/pki/CA/newcerts/ #查看签署的文件记录01.pem[root@CentOS ~]# rm -f /tmp/http* #产出产生的临时文件
# 配置让服务器可以使用此证书[root@CentOS ssl]# vim /etc/httpd/conf.d/ssl.conf <VirtualHost 192.168.1.110:443>ServerName www.centos.tstDocumentRoot "/www/centos.tstErrorLog logs/ssl_error_logTransferLog logs/ssl_access_logLogLevel warnSSLEngine on #是否启用SSLSSLProtocol all -SSLv2 #支持的协议SSLCipherSuite DEFAULT:!EXP:!SSLv2:!DES:!IDEA:!SEED:+3DES #加密机制SSLCertificateFile /etc/httpd/ssl/httpd.crt #证书文件SSLCertificateKeyFile /etc/httpd/ssl/httpd.key #私钥文件[root@CentOS ssl]# httpd -tSyntax OK[root@CentOS ssl]# service httpd restartStopping httpd: [ OK ]Starting httpd: [ OK ][root@CentOS ssl]# netstat -tulnp | grep httpdtcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 41390/httpd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 41390/httpd tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 41390/httpd # 有监听443端口# 在Win7(C:\Windows\System32\drivers\etc)的Hosts文件中修改192.168.1.110 www.centos.tst# 使用Win7的浏览器:https://www.centos.tst/, 出现证书不受信任# 解决方法:将CA的证书发送给客户端一份[root@CentOS ~]# cd /etc/pki/CA/ #将cacert.pem传送到Win7[root@CentOS CA]# pwd/etc/pki/CA# 在Win7中将文件改名cacert.pem --> cacert.crt# 将证书导入浏览器(双击安装)# 将所有的证书放入下列存储 --> 受信任的根证书颁发机构 --> # 在浏览器的设置中中可以查看这个证书# 用浏览器登录https://www.centos.tst/,就不会出现警告
- 鸟哥的服务器《十三》Web服务器
- Image Web服务器控件(十三)
- 第五十三篇: JAVA简易WEB服务器(三)
- 十三章 SAMBA服务器
- 豆瓣的 Web 服务器
- 豆瓣的 Web 服务器
- 豆瓣的 Web 服务器
- 豆瓣的 Web 服务器
- 豆瓣的 Web 服务器
- J2EE的web服务器
- WEB服务器的配置
- web服务器的实现
- web服务器的实现
- 常用的web服务器
- Web服务器的功能
- web服务器的作用!
- Web服务器的类型
- web服务器的搭建
- 提高团队协作效率就靠它们了!
- 递归的函数
- 强化学习基础 第三讲 蒙特卡罗方法
- npm常用指令
- code[vs] 3143 二叉树的序遍历
- 鸟哥的服务器《十三》Web服务器
- 互补PWM中关于死区对占空比的影响
- 121. Best Time to Buy and Sell Stock
- 找相同字符串(非AC代码,luogu上第一个点过不了TAT)
- MVC-MVP-MVVM
- 最长上升子序列
- win10+Anconda4.2.0(python3.5.2)+tensorflow1.0.1+opencv3.2.0环境配置
- Android List,Set,Map集合安全 集合区别 并发集合类性能分析
- uva 10801 Lift Hopping Dijkstra最短路