您的位置:首页 > 其它

web-基本应用

2016-04-22 17:18 369 查看
httpd的基本配置和应用

httpd官网:httpd.apache.org 有2.2和2.4两个版本分支

rpm安装的httpd默认工作目录(根目录):/etc/httpd/
配置文件:
主配置文件:/etc/httpd/conf/httpd.conf
分段配置文件:/etc/httpd/conf.d/*.conf

服务脚本:
/etc/rc.d/init.d/httpd (即使用service XXX start|stop... 来控制开启关闭等的服务脚本)
脚本的配置文件/etc/sysconfig/httpd (里面可以修改MPM,或者一些其他参数)

模块文件目录:
/etc/httpd/modules --> (软连接) /usr/lib64/httpd/modules

主程序文件:
/usr/sbin/httpd (prefork)
/usr/sbin/httpd.worker (worker)
/usr/sbin/httpd.event (event)

日志文件目录:
/var/log/httpd
access_log访问日志
error_log错误日志

站点文档目录:
/var/www/html

配置文件部分常用的功能:
directive value组成
指令 + 值 组成
[root@localhost ~]# grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Hosts
可以看出来,有三个章节部分
注意:section中的main server 和 virtual hosts不能同事启用。默认启用的是main server(中心主机)

1、指定监听端口
Listen [IP:]port
A:当IP省略时,则表示监听本机所有可用IP地址
B:Listen指令可以出现多次,用于指明多个不同监听端口或套接字

2、持久连接相关
持久连接:连接建立后,每个资源获取结束,不会断开连接,而是继续等待其他资源请求并完成传输
如何断开?
数量限制,如100个
时间限制,如10秒

劣势:对于并发访问量比较大的服务器,开启持久连接会让有些请求得不到响应
改进方法:缩短时间限制。httpd2.2支持秒级别,httpd2.4支持毫秒级别

非持久连接:每个请求都是单独通过专用的连接进行获取

指令:
KeepAlive On|Off
MaxKeepAliveRequests 100 数量限制
KeepAliveTimeout 10 时间限制
二者那个条件满足都会断开

3、MPM:多道处理模块 并发请求响应的不同实现
prefork worker event
httpd2.2不支持同时编译多个不同的MPM,rmp安装的httpd2.2提供了三个分别用于不同的MPM支持程序
查看当前使用了那种MPM:
[root@localhost ~]# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c

httpd -l 查看静态编译的模块
httpd -M 查看所有模块,包括静态编译和DSO模块

更换不同的MPM主程序
编辑/etc/sysconfig/httpd
启用#HTTPD=/usr/sbin/httpd.worker 去掉#,并修改对应的MPM程序路径

/etc/httpd/conf/httpd.conf 中对不同MPM进行的设定:
# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# ServerLimit: maximum value for MaxClients for the lifetime of the server
# MaxClients: maximum number of server processes allowed to start
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule prefork.c>
StartServers 8 服务开启开启8个工作进程
MinSpareServers 5 最小空闲进程数
MaxSpareServers 20 最大空闲进程数
ServerLimit 256 最大服务器上限(不超过1024,值一般等同maxclients)
MaxClients 256 最大客户端并发请求连接数量
MaxRequestsPerChild 4000 每个子进程最多响应多少个请求
</IfModule>

# worker MPM (worker模型是三级结构,主进程生成子进程,每个子进程在生成多个线程,每个线程响应一个请求)
# StartServers: initial number of server processes to start
# MaxClients: maximum number of simultaneous client connections
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestsPerChild: maximum number of requests a server process serves
<IfModule worker.c>
StartServers 4 服务开启启动4个子进程
MaxClients 300 最大并发连接
MinSpareThreads 25 最少空闲线程数
MaxSpareThreads 75 最大空闲线程数
ThreadsPerChild 25 每个进程生成25个线程
MaxRequestsPerChild 0 每个子进程最多响应多少个请求,0不限制
</IfModule>

4、DSO机制
格式:LoadModule <module_name> <module_path>
模块路径:可以使用相对路径,或者绝对路径
相对路径是相对于ServerRoot而言

httpd -M查看所有的模块

注意:修改配置后,使用httpd -t 检查语法没有问题后,使用service httpd reload来使其生效

5、定义main server的文档页面路径
DocumentRoot 指向的路径是URL的默认起始位置 (DocumentRoot /var/www/htm)
/var/www/html/images/1.jpg 用URL访问则是: http://www.magedu.com/images/1.jpg (假设是www.magedu.com下的文件)

6、站点路径访问控制
访问控制机制:
基于来源地址
基于账号

定义的方式有两种:
A、文件系统路径
<Directory "/PATH/TO/SOMEDIR">
...各种控制指令
</Directory>

B、URL路径
<Location "/URL">
...各种指令
</Location>

7、Directory中的访问控制定义
(1)options Indexes|FollowSymlinks|None|All
indexes:当访问的路径下无默认主页面存在时,且没有指定具体的访问资源,则把此路径下所有资源以列表方式呈现给用户。危险,建议-Indexes(减掉Indexes的功能)
Followsymlinks:如果某页面文件指向“DocumentRoot”以外路径的其它文件,将直接显示目标文件内容(存在风险,建议关闭)
None:什么都不启用
All:启用所有
AllowOverride None 表示不启用.htaccess里的指令来控制访问权限(一般都是停用的,否则对服务器带来较大的压力)

(2)基于来源地址的访问控制
order 检查次序
order Allow Deny 只有明确指定允许的来源地址,才能被访问,其他均拒绝(白名单)
order Deny Allow 只有明确指定拒绝的来源地址,其他的均允许访问。(黑名单)
Allow from 允许的地址
Deny from 拒绝的地址

例如:
order allow deny
allow from 172.20.0.0/16
deny from 172.20.100.2
172.20.0.0/16可访问,单172.20.100.2拒绝

from后可跟上的地址格式:
IP地址:
网络地址:
172.16
172.16.0.0
172.16.0.0/16
172.16.0.0/255.255.0.0

8、定义默认的主页面
DirectoryIndex index.html index.html.var
自左而右进行搜索,找到首次匹配的文件,就作为默认主页返回

9、配置日志
错误日志:
ErrorLog logs/error_log
LogLevel warn
错误日志路径、级别

访问日志,定义日志内容格式:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
访问日志路径,调用之前的logformat
CustomLog logs/access_log combined
访问日志 路径 格式

%h remote host,即客户端主机
%l remote logname,客户端用户通过identd登录时使用名称;一般为空(-)
%u Remote user (from auth; may be bogus if return status (%s) is 401) 用户认证登录的名字,无登录机制则为空
%t Time the request was received (standard english format) 收到客户端请求的时间,标准英文语言格式
\" 脱意符号,显示出双引号 “ 显示引号本身,而不作为引用符号
%r First line of request 请求报文的首行
<method> <url> <version> method方法(get) 网站URL,如果是/则就是默认网页 version(http协议的版本号)
%>s 响应状态码
%b Size of response in bytes, excluding HTTP headers,响应报文大小,单位是字节,不包含首部信息(可理解为响应内容的大小)
%{Referer} 记录http首部referer对应的值,即访问入口(从哪里跳转到此页面)
%{Referer}i 引用Referer首部的值
%{User-Agent}i 记录http首部User-Agent对应的值,即浏览器类型

10、路径别名
alias /URL "/path/to/somedir"
假如默认的DirectoryRoot 是/var/www/html,要访问的URL是http://www.magedu.com/bbs/index.html
alias /bbs/ "/web/bbs/htdocs/"
则实际访问的路径是/web/bbs/htdocs/index.html

注意:alias /bbs/ /web/bbs/htdocs/ 或者 alias /bbs /web/bbs/htdocs 要么都加/,要么都不加

11、设定默认字符集
AddDefaultCharset UTF-8
常用字符集编码:GBK GB2312 GB18030

12、基于用户的访问控制
质询:
WWW-Authenticate:服务器用401状态码拒绝客户端请求,并弹出对话框要求提供用户名和密码
认证;
authoriztion:客户端用户填入用户名和密码再次发送到服务器,认证通过则请求授权

安全域:需要用户认证后访问的路径
弹出的对话框应该有其名称,用于向用户通知此认证的原因等内容
httpd协议支持的认证方式:
basic 基本的,明文的,使用base64编码格式
digest 摘要(某些浏览器不支持,所以大多使用basic认证方式)

basic认证实例:
假如对/data/web/html/employee此目录做用户访问控制
(1)定义安全域
<Directory "/data/web/html/employee">
Options None
AllowOverride None
AuthType Basic (认证方式)
AuthName "just for employee" (弹出的对话框上的提示信息)
AuthUserFile /etc/httpd/users/.htpasswd (存放虚拟用户的文件,隐藏的)
Require User user1 user2 user3 (多个用户使用空格分开)
</Directory>

补充说明:
Require Valid-User 表示出现在"AuthUserFile"中所有的用户都可以登录
Require User 表示仅允许指定的用户

虚拟用户即非系统用户
(2)提供用户的账号文件
htpasswd命令用于维护此文件(AuthUserFile /etc/httpd/users/.htpasswd)

htpasswd [-c] [-m] [-d] PASSWDFILE USERNAME
-c 添加第一个用户时使用,后面再创建用户时务必不要-c,否则会覆盖掉前面创建的用户
-m 以MD5格式加密用户密码
-d 删除用户

(3)组认证
<Directory "/data/web/html/employee">
Options None
AllowOverride None
AuthType Basic
AuthName "just for employee" 提示消息
AuthUserFile /etc/httpd/users/.htpasswd 虚拟用户文件
AuthGroupFile /etc/httpd/users/.htgroup 组文件
Require Group GRP1 GRP2... 允许可登陆的组
</Directory>

组文件:
每行定义一个组,格式
GRP_NAME: user1 user2 user3 .... (user即.htpasswd文件中的虚拟账户)

13、虚拟主机
一个物理服务器服务于多个站点,每个站点可以通过一个或多个虚拟主机来实现
关闭“中心主机”(main server),注释掉DocumentRoot即可

httpd支持三种类型的虚拟主机
基于IP
基于端口
基于FQDN

定义虚拟主机的方法:
<VirtualHost "IP:PORT">
ServerName 必要
ServerAlias 不一定要
DocumentRoot 必要
</VirtualHost>

注意:大多用于main server的指令都可以用在virtualhost中

A、基于端口的虚拟主机
<VirtualHost *:80>
ServerName www.a.com
DocumentRoot /vhost/a.com/htdocs/
</VirtualHost>

<VirtualHost *:8080>
ServerName www.b.org
DocumentRoot /vhost/b.org/htdocs/
</VirtualHost>

注意:基于的端口要listen。 如:Listen 8080 Listen 80

B、基于IP的虚拟主机:
<VirtualHost 172.16.1.110:80>
ServerName www.a.com
DocumentRoot /vhost/a.com/htdocs/
</VirtualHost>

<VirtualHost 172.16.1.111:80>
ServerName www.b.org
DocumentRoot /vhost/b.org/htdocs/
</VirtualHost>

#ifconfig eth0:0 172.16.1.111/24 up 增加IP

注意:给主机配置多个IP,分别监听在不同的IP上,IP可用才行

C、基于FQDN的虚拟主机:
#NameVirtualHost *:80 必须要把注释#去掉,启用起来;根据实际情况写IP和监听的端口,*表示本机所有IP
<VirtualHost 172.16.1.110:80>
ServerName www.a.com
DocumentRoot /vhost/a.com/htdocs/
</VirtualHost>

<VirtualHost 172.16.1.110:80>
ServerName www.b.org
DocumentRoot /vhost/b.org/htdocs/
</VirtualHost>

访问的时候,就可以输入网址加以区分。这几种虚拟主机可以混用同时存在

注意:额外经常用于每个虚拟主机的配置有
ErrorLog
Customlog
<Directory>
<Location>
ServerAlias

14、内置的status页面,查看服务器的运行状态(不可以让人随意访问,可以设置白名单)
#<Location /server-status>
# SetHandler server-status
# Order deny,allow
# Deny from all
# Allow from .example.com
#</Location>

将其启用即可,如:
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 172.16.0.0/16
</Location>

15、curl命令
curl是基于URL语法在命令行方式下工作的文件传输工具,他支持FTP,FTPS,HTTP,HTTPS,GOPHER,TELNET,DICT,FILE及LDAP等协议,CURL支持https认证,并且支持http的post,put等方法,FTP上传,kerberose认证,HTTP上传,代理服务器,cookies,,用户名/密码认证,下载文件断点续传,上载文件断点续传,http代理服务器管道(proxy tunneling),甚至它还支持IPV6,socks5代理服务器,通过http代理服务器上传文件到ftp服务器等等,功能十分强大。

curl [options] [URL...]
常用选项
-A/--user-agent <string> 设置用户代理发送给服务器(模拟成某种浏览器)
curl -A "chrome 40" http://172.20.1.36/ 伪装成chrome40的客户端
-basic 使用HTTP基本认证
如果服务器端使用了basic认证,客户端则需要加上这个选项
-e/--referer <URL> 来源网址 伪装自己通过某个网址跳转到当前网站
--cacert <file> CA证书(SSL)
--compressed 要求返回是压缩的格式
-H/--header <line> 自定义头信息传递给服务器
-I/--head 显示服务器响应信息,而非get到的网页内容
--limit-rate <rete> 设置传输速度
-u/--user <user[:password]> 设置服务器的用户名和密码,结合-basic使用
-0/--http1.0 使用httpd1.0

另一个工具:elinks
-dump 不进入交互式模式,而直接将URL的内容输出值标准输出

16、 使用mod_deflate 模块压缩页面优化传输速度
(如果带宽够用,而CPU非常繁忙的话,此功能不要开启,主要作用是节约带宽,但消耗CPU)
使用场景:
(1)为了节约带宽,额外消耗CPU,同时,可能有些较老的浏览器不支持
(2)压缩适于压缩比较大的,如文本文件

SetOutputFilter DEFLATE

#mod_deflate configuration

#Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css

#Level of compression (Highest 9 - Lowest 1 )
DeflateCompressionLevel 9

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

验证:
#curl -I http://X.X.X.X #curl -I --compress http://x.x.x.x 二者可以对比一下压缩前和压缩后 实验:Content-Length: 521173 --> Content-Length: 117534

17、https
http over ssl=https TCP 443
SSL:v3
tls:v1
它的scheme是https://

SSL会话的简化过程:
(1)客户端发送可供选择的加密方式(自己支持的),并向服务器请求证书
(2)服务器端发送证书以及选定加密方式给客户端:
(3)证书验证:
如果信任给其发证书的CA
(a)验证证书的来源合法性:用CA的公钥来解密证书上的数字签名(如果能解密则说明是此CA颁发的)
(b)验证证书的内容合法性:完整性验证(用单向算法计算私钥的指纹信息,并与解密私钥的指纹信息进行比对,一致则说明内容没有被修改)
(c)检查证书的有效期
(d)检查证书是否已被吊销
(e)证书中拥有者的名字,与访问的目标主机要一致
(4)客户端生成临时会话密钥(对称),并使用服务器的公钥加密此数据发送给服务器,完成密钥交换
(5)服务器用此密钥加密用户请求的资源,响应给客户端

注意:SSL回话是基于IP地址创建,所以单IP的主机上,仅可以使用给一个https虚拟主机(意思就是一个主机上有多个虚拟主机,但只能其中一个虚拟主机能使用https)

回顾几个术语:PKI公钥基础设施 CA签证机构 CRL 证书吊销列表 X.509数字证书格式

配置httpd支持https:
(1)为服务器申请数字证书
测试:通过私建CA
(a)创建私有CA
(b)在服务器创建证书签署请求
(c)CA签证
(2)配置httpd支持使用ssl,及使用证书
#yum -y install mod_ssl 安装ssl模块
配置文件:/etc/httpd/conf.d/ssl.conf

主要修改: 配置文件:/etc/httpd/conf.d/ssl.conf
<VirtualHost 172.20.1.38:443> 根据实际情况修改
DocumentRoot "/vhost/a.com/htdocs"
ServerName www.a.com:443
SSLCertificateFile /etc/httpd/ssl/httpd.crt SSL证书
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key SSL私钥
</VirtualHost>
(3)测试基于https访问相应的主机 (可以使用s_client命令)
#openssl s_client -connect www.a.com:443 -CAfile /tmp/cacert.pem
建立连接后:
GET /rc.txt HTTP/1.1
HOST: www.a.com
回车
回车
即可看到内容

在ssl.conf文件中有这么一行
<VirtualHost _default_:443> 意思是监听本机默认虚拟主机的443端口(httpd -S可以看到默认虚拟主机是哪个)

18httpd自带的工具程序
htpasswd:basic认证基于文件实现时,用到的账户密码文件生成工具
apachectl:httpd自带的服务控制脚本,支持start,stop
apxs:由httpd-devel包提供,扩展httpd使用第三方模块的工具
rotatelogs:日志滚动工具
suexec:访问某些有特殊权限配置的资源时,临时切换至指定用户运行
ab:Apache benchmark 压力测试

19、http压力测试工具
ab
webbench
http_load

专业测试工具:
jmeter()
LoadRunner
网易研发了tcpcopy,replay到测试环境中,还原真实的用户访问场景

ab的使用方法:
ab - Apache HTTP server benchmarking tool
格式: ab [OPTIONS] URL
[ -c concurrency ] 并发数:同时有多少请求
[ -n requests ] 请求数量
[ -t timelimit ] 指定测试多长时间
-k Enable the HTTP KeepAlive feature

ab -n 20000 -c 2000 URL... 这时候会有报错,连接数超过1024个,解决方法:
#ulimit -n 2000,默认内核限制最大套接字1024个,可以使用ulimit增至2000个

======================================================================
练习:
1、建立httpd服务器(基于编译的方式进行),要求:
(a)www.aaa.com,页面文件目录为/web/vhosts/aaa.com,错误日志为/var/log/httpd/aaa.err,访问日志为/var/log/httpd/aaa.access
(b)www.bbb.com,页面文件目录为/web/vhosts/bbb.com,错误日志为/var/log/httpd/bbb.err,访问日志为/var/log/httpd/bbb.access
(c)位两个虚拟主机建立各自的主页文件index.html,内容非标为其对应的主机名
(d)通过www.aaa.com/server-status输出httpd工作状态相关信息,且只允许提供账号密码才能访问(status:status)

2、为上面的第2个虚拟主机提供https服务,使得用户可以通过https安全访问此web站点
(1)要求使用证书认证,证书中要求使用的国家(CN),州(HA),城市(zz)和组织(magedu)
(2)设置部门为ops,主机名为www.bbb.com,邮件为admin@bbb.com

第一题:

准备工作,创建2个站点的网站存储目录、主页文件;日志文件路径已有
[root@localhost ~]# mkdir -pv /web/vhosts/{aaa.com,bbb.com}

建立连个站点的index.html,内容为对应个各自的主机名
[root@localhost ~]# echo "<h1> www.aaa.com </h1>" >/web/vhosts/aaa.com/index.html

[root@localhost ~]# echo "<h1> www.bbb.com </h1>" >/web/vhosts/bbb.com/index.html

注释main server中DocumentRoot
#DocumentRoot "/var/www/html"

启用NameVirtualHost(非常重要的一步)
NameVirtualHost *:80

增加aaa和bbb站点的配置文件(可以写在一个文件中,也可以分开写)
[root@localhost ~]# cat /etc/httpd/conf.d/bbb.conf /etc/httpd/conf.d/aaa.conf
<VirtualHost *:80>
ServerAdmin webadmin@bbb.com
DocumentRoot /web/vhosts/bbb.com/
ServerName www.bbb.com
ErrorLog /var/log/httpd/bbb.err
CustomLog /var/log/httpd/bbb.access combined
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webadmin@aaa.com
DocumentRoot /web/vhosts/aaa.com/
ServerName www.aaa.com
ErrorLog /var/log/httpd/aaa.err
CustomLog /var/log/httpd/aaa.access combined
</VirtualHost>

注意:如果NameVirtualHost *:PORT,那么在<VirtualHost *:PORT>
如果NameVirtualHost IP:PORT,那么在<VirtualHost IP:PORT>;要保持一致
否则则会出现报错提示,例如这样:
[Wed Oct 28 01:12:53 2015] [warn] _default_ VirtualHost overlap on port 80, the first has precedence
[Wed Oct 28 01:12:53 2015] [warn] NameVirtualHost 172.20.1.100:80 has no VirtualHosts
两个站点,只能访问一个站点的内容了

通过www.aaa.com/server-status输出httpd工作状态相关信息,且只允许提供账号密码才能访问(status:status)
<VirtualHost 172.20.1.100:80>
ServerAdmin webadmin@aaa.com
DocumentRoot /web/vhosts/aaa.com/
ServerName www.aaa.com
ServerAlias aaa.com
ErrorLog /var/log/httpd/aaa.err
CustomLog /var/log/httpd/aaa.access combined
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 172.20.0.0/16
Options None
AuthName "input username:password"

AuthType Basic
AuthUserFile /etc/httpd/users/.htpasswd
Require User user1 user2
</Location>
</VirtualHost>

创建配置文件中AuthUserFile的目录,然后使用htpasswd命令创建用户
注意第一个用户创建-c,后面的不要加,否则会覆盖前面的
[root@localhost ~]# mkdir /etc/httpd/users
[root@localhost ~]# htpasswd -c -m /etc/httpd/users/.htpasswd user1
New password:
Re-type new password:
Adding password for user user1
[root@localhost ~]# htpasswd -m /etc/httpd/users/.htpasswd user2
New password:
Re-type new password:
Adding password for user user2
[root@localhost ~]# cat /etc/httpd/users/.htpasswd
user1:$apr1$uqtiL9zu$/QdpsakOHTjReuUrDlqQG0
user2:$apr1$pzAvpQQK$N.fubenbV0WopUKKwQSVa0
http://www.aaa.com/server-status测试OK
第二题:
准备另外一台座位私有CA服务器,然后在http服务器上生成签署请求并交于CA服务器签证
172.20.1.100 http服务器
172.20.1.101 私建CA

CA部分:
生成私钥
[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
生成自签文件,按照题目要求填写国家(CN)、州名(HA)、城市名(ZZ)、公司名(magedu)、部门(ops)等信息
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 36500
You are about to be asked to enter information that will be incorporated
into 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 blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:ca.magedu.com
Email Address []:

创建辅助性的文件;证书索引数据库和证书序列号文件(并给于起始值)
[root@localhost CA]# touch index.txt
[root@localhost CA]# echo 01 > serial

http服务上需要生成私钥、生成证书签署请求

[root@localhost ~]# mkdir /etc/httpd/ssl
[root@localhost ~]# cd /etc/httpd/ssl
[root@localhost ssl]# (umask 077;openssl genrsa -out http.key 1024)

[root@localhost ssl]# openssl req -new -key http.key -out http.csr
You are about to be asked to enter information that will be incorporated
into 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 blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:www.bbb.com (一定要跟网站的地址一致)
Email Address []: 可以为空

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 可以为空
An optional company name []: 可以为空

将证书请求(http.csr)放到CA服务器中,过程略
CA拿到签署请求进行签证
[root@localhost CA]# openssl ca -in /tmp/http.csr -out /tmp/http.crt -days 3650
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Jul 7 23:04:54 2015 GMT
Not After : Jul 4 23:04:54 2025 GMT
Subject:
countryName = CN
stateOrProvinceName = HA
organizationName = magedu
organizationalUnitName = ops
commonName = www.bbb.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
A8:61:E5:9C:45:69:15:5F:6C:BA:B5:BE:58:69:1B:93:12:54:90:EC
X509v3 Authority Key Identifier:
keyid:F7:2C:D3:D6:88:FD:05:33:4A:AB:C6:DC:49:44:EB:CC:30:57:7A:33

Certificate is to be certified until Jul 4 23:04:54 2025 GMT (3650 days)
Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

签证完毕后,将http.crt放到http服务器的/etc/httpd/ssl/
[root@localhost ssl]# ll
总用量 12
-rw-r--r-- 1 root root 3660 10月 28 20:57 http.crt
-rw-r--r-- 1 root root 635 10月 28 20:51 http.csr
-rw------- 1 root root 887 10月 28 20:49 http.key

到此证书和私钥部分已经完成,现在需要搞定https部分
httpd2.2需要安装mod_ssl
[root@localhost ssl]# yum install -y mod_ssl
安装后可以看到/etc/httpd/conf.d/下多了一个叫ssl.conf的配置文件

可以先备份一下
[root@localhost conf.d]# cp ssl.conf{,.bak}
修改/etc/httpd/conf.d/ssl.conf

<VirtualHost 172.20.1.100:443> ip根据实际情况修改
DocumentRoot "/web/vhosts/bbb.com"
ServerName www.bbb.com:443
SSLCertificateFile /etc/httpd/ssl/http.crt 证书路径
SSLCertificateKeyFile /etc/httpd/ssl/http.key 私钥的路径

先用httpd -t测试下语法,如果没有问题就重启下httpd服务

#ss -tanl 检查下端口有没有监听

如果都没有问题,就可以打开浏览器进行验证了
由于CA是私建的,所以并不受信任,可以将CA安装到测试机的“受信任的根证书办法机构”做法如下:
/etc/pki/CA/cacert.pem 复制到测试机中,将.pem后缀修改为.crt,双击安装
安装证书-->将所有的证书安装到下列存储-->浏览-->受信任的根证书颁发机构
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: