HTTP服务

HTTP:HyperText Transfer Protocol,超文件传输协议

上世纪90年代初,诞生于量子实验室

    http/09版本:仅支持纯文件(超连接),ASCII

    HTML:编写超文本的语言  ,HyperText mark Language

URI:统一资源标识符,Uniform Resource Indentifier 

    URL:是URI的一个子集,Uniform resource locater


HTTP资源获取的方法:(method)

    1、GET(0.9版本)

    2、PUT,POST DELETE(1.0版本)HEAD(只返回首部,不返回主体)trace options connection


MIME机制:Multipurpose Internet Mail extension,多用途互联网邮件扩展

    MIME:将非文件数据在传输前重新编码为文件格式再传输。接收方能够用相反的方式将其重新还原为        原来的格式,还能够调用相应的程序来打开此文件


SMTP:Simple mail transmission protocol


动态网页:在服务器端存储的文档非HTML格式,而是编程语言开发的脚本,脚本接受参数之后在服务器运行一次,运行完成后会生成HTML格式文档,把生成的文档发给客户端


HTTP客户端数据报文

    1、包含IP协议信息

        source IP

        destination IP

    2、TCP协议信息

        source port

        destination port

    3、http协议信息

        GET /file.html

        Host:www.btsbox.com(WEB虚拟主机机制,所以有添加此信息)


请求报文语法

<method> <request-URL><version>   # 获得方法 请求的资源 http版本

<headers> #http协议首部

              #空白行

<entity-body> #报文主体

              #空白行

响应报文语法

<version> <status><reason-phrase>  #http版本  状态码 成功或失败的解释

<headers>    #响应报文首部

             #空白行

<entity-body>

             #空白行


状态代码:

    1XX:纯信息

    2XX:成功类的状态信息

    3XX:重定向类的信息(301永久重定向,302临时重定向,304内容没有发生改变,使用本地缓存)

    4XX:客户端错误类的信息(404不存在的文件)

    5XX:服务器端错误类的信息


报文示例

请求报文:

GET / HTTP/1.1          #说明协议 和请求的路径 "/"一般为主页

Host:www.mageedua.com   #报文首部都是以HOST:这类格式出现,既以这类格式出现的,都为报文首部

Connection:keep-aliv    #连接方式为长连接,不断开连接

                        #空白行

响应报文:

HTTP/1.1 200 OK   #返回成功状态码

X-powered-By:PHP/5.2.17 #说明是一个PHP动态网页

Vary:Accep-Encodeing,cookie,User-Agent

Cache-Control:max-age=3,must-revalidate

Content-Encoding:gzip   #网页压缩技术

Content-Length:6931     #网页长度 

                        #空白行


上面两个报文的第一行通常称作报文“起始行(start line)",后面的标签格式的内容称作首部域(Header field),每个首部域都由名称(name)和值(value)组成,中间用逗号分隔。另外,响应报文通常还有一个称作body的信息主体,即响应给客户端的内容


web服务器的主要操作

1、建立连接-->接受或拒绝客户端连接请求

2、接收请求-->通过网络读取HTTP请求报文

3、处理请求-->解析请求报文并做出相应的动作

4、访问资源-->访问请求报文中相应的资源

5、构建响应-->使用正确的首部生成http响应报文

6、发送响应-->向客户端发送生成的响应报文

7、记录日志--当已经完成的http事务记录进日志文件


http/1.1

    增强了缓存功能、

    引入了长连接机制


长连接机制:

    空闲超时

    请求次数限制

WEB服务器模型:

单进程/单线程服务器模型

多进程/多线程服务器模型

事件驱动、状态通知服务器模型

多进程、响应多请求服务器模型

MPM:Multi path Modules (多道处理模块)

    prefork(一个请求用一个进程响应)

    work(一个请求用一个线程响应,启动多个进程,每个进程生成多个线程)

    event(一个进程响应多个请求,基于事件处理模型)

    winnt

Server soft:

    httpd

    IIS

    nginx

    lighttpd

    thttpd


应用程序服务器

    IIS

    tomcat(apache,JSP,open source)

    websphere(IBM,JSP,commodity)

    weblogic(Oracle,JSP,commodity)

    JBoss(Readhat,open source,commodity,核心是tomcat)


Apache

ASF:Apache software Foundation


httpd:

    web server, Open source

    version: 2.2 2.4


httpd特性:

    事先创建进程

    按需维持适当的进程

    模块设计,核心比较小,各种功能都通过模块添加(包括PHP)

        支持运行时配置,支持单独编译模块

    支持多种方式的虚拟主机配置

         虚拟主机:

                基于IP的虚拟主机

                基于端口的虚拟主机

                基于域名的虚拟主机

    支持https协议(mod_ssl)

    支持用户认证

        简单认证

        摘要认证

        基于表单认证

    访问控制机制

        基于IP或主机名的访问控制机制 

        基于每目录的访问控制

    支持URL重写


httpd: SELinux(事先让其处于permssive,disabled)

httpd进程:

    /usr/sbin/httpd(MPM:prefork)

        httpd: root,root (master process)

        httpd: apache,apache (worker process)

    /etc/rc.d/init.d/httpd (服务脚本)

httpd 端口:

    80/tcp, 443/tcp

httpd工作目录

    /etc/httpd:工作根目录,相当于程序安装目录

    /etc/httpd/conf:配置文件目录

        主配置文件:httpd.conf

        /etc/httpd/conf.d/*.conf :是对httpd.conf的分割

    /etc/httpd/moduless:模块目录

    /etc/httpd/logs --> /var/log/httpd:日志目录

        日志文件有两类:访问日志access_log,错误日志:err_log

    /var/www:网站存储位置

        html:静态网站存储位置

        cgi-bin:动态网站存储位置

        cgi:可以让httpd调用其他程序的一种机制,Common Gateway Interface

            cgi:Client-->httpd(index.cgi)-->Spwan process(Index.cgi)-->httpd-->Client

                http以index.cgi文件中指定的程序调用程序转换,如在bash脚本的 #!/bin/bash

            fastcgi:当用户请求动态网页,http将请求直接传递给cgi的子进程运行,这样可以实                        现静态网页服务与动态服务物理分离。

    /etc/httpd/conf/magic:定义如何识别MIME格式的文档

LAMP:apache+mysql+php


httpd的测试工具:

LoadRunner:专业级的HTTP性能测试工具


httpd的安装

[root@Centos6 dnstop-20140915]# yum install httpd


http.conf配置文件说明:

    格式:directive value,指令不区分大小写, value则可能要区分大小写

ServerTokens OS    #显示web的信息,可以接受多个参数
KeepAlive Off   #是否打开长连接
MaxKeepAliveRequests 100 #请求的资源数
KeepAliveTimeout 15  #长连接的超时时间
# prefork MPM  #定义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>   #perfork模型参数
StartServers       8    #启动进程数
MinSpareServers    5    #最小空闲进程
MaxSpareServers   20    #最大空闲进程
ServerLimit      256    #MaxClients的上限值,调整需先杀死所有进程,再设置启动,方生效
MaxClients       256    #最大客户端数
MaxRequestsPerChild  4000  #每个子进程最多响应多少用户请求
</IfModule>
# worker MPM        
# 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    #启动的进程数
MaxClients         300    #最大的客户端
MinSpareThreads     25    #最小线程数
MaxSpareThreads     75    #最大线程
ThreadsPerChild     25    #每个进程启动的线程数
MaxRequestsPerChild  0    #每个进程的最大请求
</IfModule>
#Listen 12.34.56.78:80
Listen 80    #监听所有IP的80端口
# Dynamic Shared Object (DSO) Support   #加载的模块
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_alias_module modules/mod_authn_alias.so
LoadModule authn_anon_module modules/mod_authn_anon.so
LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule ldap_module modules/mod_ldap.so

# Load config files from the config directory "/etc/httpd/conf.d".
#
Include conf.d/*.conf    #加载的配置文件
 If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
#  . On SCO (ODT 3) use "User nouser" and "Group nogroup".
#  . On HPUX you may not be able to use shared memory as nobody, and the
#    suggested workaround is to create a user www and use that user.
#  NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
#  when the value of (unsigned)Group is above 60000; 
#  don't use Group #-1 on these systems!
#
User apache        #httpd启动的用户
Group apache        #httpd启动的组

#主服务模式 Section 2
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. [email protected]
#
ServerAdmin root@localhost  #网站管理员联系方式
#ServerName  #主机地址, 如果没有在DNS中指定,会反解IP所对应的FQDN 
DocumentRoot "/var/www/html"  #网页存放路径
<Directory />
    Options FollowSymLinks   #Options定义的属性可以为多个, 彼此使用空格隔开
    AllowOverride None        #允许覆盖,覆盖下面Order的功能
        Order allow,deny    #Order用于定义基于主机访问功能的,IP,网络地址或主机定义访问                              控制
    Allow from all
</Directory>        #定义Document的访问属性
#Options属性:
    Index:定义索引,索引是指列出目录下所有的文件
    None:不支持任何选项
    FollowSymLinks:跟随符号连接,意味访问符号连接所指向的文件,会降低服务器的性能
    Includes:允许执行服务器端包含(SSI),此方式不安全
    SymLinksifOwnerMatch:允许执行符号连接,但属主必须与执行httpd进程的属主相匹配,不                               开放
    ExecCGI:允许执行CGI脚本
    MultiViews:判断客户端使用的语言,并显示相应语言的网页,一般比较消耗资源
    ALL:启用所有选项
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received. 
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid.  This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
# See also: http://httpd.apache.org/docs/misc/FAQ.html#forbidden
#
<IfModule mod_userdir.c>  #此模块允许用户是否可以在自己的目录建立各种的网页文件
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    UserDir disabled  #默认关闭

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disabled" line above, and uncomment
    # the following line instead:
    # 
    #UserDir public_html  #如果需要启用,注释disabled项,开启此启,会在用户的家目录                                    中建立一个public_html文件,让用户存放各种网页文件
#启用此项后,需在用户的家目录(系统用户)中建立public_html文件夹,再创建各种的html文档,但需要让用户的家目录具有其他用户可执行的权限,方可以网址http://10.189.9.202/~hadoop的方式访问

</IfModule>

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
#<Directory /home/*/public_html>   #定义用户的家目录权限认证
#    AllowOverride FileInfo AuthConfig Limit
#    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
#    <Limit GET POST OPTIONS>    #限定访问方法,使用GET POST OPTIONS
#        Order allow,deny
#        Allow from all        #以上三种方法,所有用户都可访问
#    </Limit>
#    <LimitExcept GET POST OPTIONS>    #除这三种方法
#        Order deny,allow
#        Deny from all                #全部拒绝
#    </LimitExcept>
#</Directory>

DirectoryIndex index.html index.html.var   #主页的文件名称

AccessFileName .htaccess    #定义网站的每个目录的访问权限,需把规则放到目录下的以.htacces                            s文件中, 如使用用户名和密码规则,但非常影响apache的执行效率
                            子目录对父目录的权限是继承的。所以性能会受影响
<Files ~ "^\.ht">    #匹配以.ht所有的文件的权限,~表示匹配
    Order allow,deny
    Deny from all
    Satisfy All
</Files>  

TypesConfig /etc/mime.types   #支持哪此类型的二进制文件,这里指定mime类型的文件       
DefaultType text/plain    #默认文件类型为纯文本类型

<IfModule mod_mime_magic.c>   #判断mod_mime_magic.c模块存在,文件位置在以下路径中
#   MIMEMagicFile /usr/share/magic.mime
    MIMEMagicFile conf/magic
</IfModule>

HostnameLookups Off  #记录访问日志中,关闭记录主机名,只记录IP,以提升效率

#EnableMMAP off
#EnableMMAP off
ErrorLog logs/error_log  #定义的错误日志
LogLevel warn   #日志级别为warnning

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common  #common表示通用模式
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

#定义访问日志的格式,每个%表示定义的了个宏,%h:客户端地址,%l:以哪个用户的身份,%u:登陆网站所使用的用户,%t:什么时间,\"%r\":访问的方法,%>s:最后一次请求的状态码,%b:响应报文的大小,\"%{Referer}i:显示访问的时候从哪个连接来的,比如从一个页面中的连接跳到目录页面,\"%{User-Agent}i\":客户端浏览器类型
#CustomLog logs/access_log common   #访问日志文件位置,并以混合模式记录

ServerSignature On

Alias /icons/ "/var/www/icons/"  #定义访问别名,icons为别名,/var/www/icons为系统真实路径

<Directory "/var/www/icons">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>


<Location /server-status>    #服务器的状态信息,可以使用IE在URL后添加server-status查看
    SetHandler server-status
    Order deny,allow
    Deny from all
   Allow from 10.189.8.26
</Location>

criptAlias /cgi-bin/ "/var/www/cgi-bin/"  #在哪个目录下可以执行CGI脚本
<Directory "/var/www/cgi-bin">  
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>



修改HTTP运行模型

[root@Centos6 conf.d]# httpd -l   #查看httpd支持的模型
Compiled in modules:
  core.c
  prefork.c
  http_core.c
  mod_so.c
  [root@Centos6 conf.d]# httpd.event -l  
Compiled in modules:
  core.c
  event.c
  http_core.c
  mod_so.c
[root@Centos6 conf.d]# httpd.worker -l
Compiled in modules:
  core.c
  worker.c
  http_core.c
  mod_so.c
[root@Centos6 conf.d]# vim /etc/sysconfig/httpd   #启用worker模型
  HTTPD=/usr/sbin/httpd.worker

httpd配置文件语法检查:

[root@Centos6 ~]# httpd -t
Syntax OK

elinks:纯文件网页浏览工具

    -dump:打开网页,立即退出

    -source:显示网页的源码


httpd网页的用户认证

  1、基于主机的用户认证

<Directory "/var/www/html">
    AllowOverride None   #None,表示基于主机IP的认证机制
    Order allow,deny
    Allow from all
</Directory>
#Order 示例
    Order allow,deny
    allow form 192.168.0.0/24   
    #只允许这个网段的主机访问,其他的都deny
    Order deny,allow
    deny form 129.168.0.0/24
    #拒绝这个网段的主机访问,其他都允许
    Order deny,allow
    deny form 192.168.0.11 192.168.0.12
    #只拒绝这两个主机访问网站,其他都允许 
#地址的表现方式:
    IP
    network/netmask
    HOSTNAME:
    Domainname:a.com 
    Partial IP : 172.16,172.16.0.0/16

 2、基于用户和组的用户认证

<Directory "/var/www/html">
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
   AllowOverride AuthConfig    #AuthConfig指定使用配置文件验证用户方式
        AuthType Basic        #认证方式为Basic模式,还有digest模式认证
        AuthName "请输入用户名和密码"    #输入用户密码框的提示
        AuthUserFile "/usr/local/apache/htpasswd"  #存放用户的文件路径
        AuthGroupFile "/usr/local/apache/htgroup"  #存放组的文件路径
        Require valid-user    #指定在存放用户文件中的哪此文件可以访问,valid-user表示所有
        Require user hadoop   #表示只有在文件中的hadoop用户可以访问
        
            Order allow,deny
    Allow from all
</Directory>
#使用htpasswd命令建立站点访问的用户,htpasswd表示使用Basic认证模式,也可以使用htdigest命令建立用户,表示认证模式为digest模式
[root@Centos6 ~]# htpasswd  -c -m /usr/local/apache/htpasswd hadoop  
New password: 
Re-type new password: 
Adding password for user hadoop
#htpasswd -c -m :c表示创建认证文件,-m表示加密使用md5
#htpasswd -D :表示删除一个用户
[root@Centos6 www]# cat /usr/local/apache/htpasswd 
hadoop:$apr1$.Znxpwih$wCoylwMZfbrg0irYJ4Br70  #apr1表示加密模式
#认证组文件的建立
[root@Centos6 www]# cat  /usr/local/apache/htgroup 
httpd: hadoop readhat   #httpd是组名, hadoop readhat为用户

#基于数据库存取用户名的认证方式
<Directory /www/docs/private>
AuthName "Private"
AuthType Basic
AuthBasicProvider dbm
AuthDBMUserFile /www/passwords/passwd.dbm
Require valid-user
</Directory>
#以上配置完成,需使用htdbm命令创建DBM认证文件

查看http加载的额外模块

[root@Centos6 www]# httpd -M   
httpd: apr_sockaddr_info_get() failed for Centos6.6A
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 auth_basic_module (shared)


DocumentRoot /www/baidu.com/
<directory "www/baidu.com">
    option
    AllowOverride
</directory>   #此directory容器是定义/www/biadu.com,他是一个路径,是定义访问文件的权限

<location "/p_w_picpaths">

</location>  #location容器是定义URL的访问路径目录

apache虚拟主机的定义:


<VirtualHost HOST>  

    Option

    ...

</VirtualHost>

    注:需使用虚拟主机,必须先注销中心主机,方法:注释DocumentRoot


host的格式:

    ip1:80

    ip2:80

基于端口:

    IP:80

    IP:8080

基于域名:

    *.80

示例如下

#修改/etc/httpd/conf/httpd.conf文件中的DocumentRoot,将其注释
#DocumentRoot "/var/www/html"
#将需要使用的端口在/etc/httpd/conf/httpd.conf文件中开启
Listen 80
Listen 8080
#将虚拟主机功能开启,修改httpd.conf文件配置
NameVirtualHost 10.189.9.203:80 
#表示在此IP上开启基于域名的虚拟主机,如果是所有IP,使用*:80 

#建立虚拟主机:
[root@Centos6 ~]# cat  /etc/httpd/conf.d/virtual.conf  #也可以直接在httpd.conf中添加
<VirtualHost _default_:80>   #定义默认访问主页,匹配一般从上往下匹配虚拟主机
        DocumentRoot "/www/default80"
        # ...
</VirtualHost>
<VirtualHost _default_:*>
        DocumentRoot "/www/default"
        # ...
</VirtualHost>
<VirtualHost 10.189.9.202:80>
   ServerName www.a.com
   DocumentRoot "/www/a.com"
</VirtualHost>

<VirtualHost 10.189.9.202:8080>
  ServerName www.b.com
  DocumentRoot "/www/b.com"
</VirtualHost>

<VirtualHost 10.189.9.203:80>
 ServerName wwww.c.com
  DocumentRoot "/www/c.com"
  <Directory "/www/c.com">    #定义c.com基于主机的访问规则,其拒绝10.189.8.26
        options none
        AllowOverride none
        Order deny,allow
        Deny from 10.189.8.26
  </Directory>
</VirtualHost>

<VirtualHost 10.189.9.203:80>
 ServerName   www.d.com
 DocumentRoot "/www/d.com"
 CustomLog /var/log/httpd/a.com/access_log combined
 ErrorLog /var/log/httpd/a.com/error_log
 LogLevel warn
   <Directory /www/d.com>
        Options none
        AllowOverride authconfig
        AuthType basic
        AuthName "Restrict area"
        AuthUserFile "/www/d.com/.htpasswd"
        Require valid-user
   </Directory>
</VirtualHost>

[root@Centos6 ~]# cat /www/a.com/index.html  #建立虚拟主机的网页目录及index.html文件
<title>A.com</title>
<h1>This is a.com site.</h1>
[root@Centos6 ~]# cat /www/b.com/index.html 
<title>B</title>
<h1>This is b.com site.</h1>
[root@Centos6 ~]# cat /www/c.com/index.html 
<title>C</title>
<h1>This is c.com site.</h1>
[root@Centos6 ~]# cat /www/d.com/index.html 
<title>D</title>
<h1>This is d.com site.</h1>


搭建https站点

#首先配置apache的虚拟主机配置
[root@localhost ssl]# vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html"  #关闭主网站功能
NameVirtualHost 10.189.9.202:80   #开启虚拟主机功能
[root@localhost ssl]# cat /etc/httpd/conf.d/virtual.conf 
<VirtualHost 10.189.9.202:80>
	ServerName www.a.com
	DocumentRoot "/www/a.com"
	CustomLog /var/log/httpd/a.com/access_log combined
	ErrorLog /var/log/httpd/a.com/Error_log
	LogLevel warn
</VirtualHost>
#新建虚拟主机的日志文件夹,文件会自动建立
#建立CA
[root@localhost CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)  #生成CA私钥
Generating RSA private key, 2048 bit long modulus
................................................+++
..+++
e is 65537 (0x10001)
#生成自签证书
[root@localhost CA]# (umask 077;openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3657)
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) [CN]:
State or Province Name (full name) [ShangHai]:
Locality Name (eg, city) [ShangHai]:
Organization Name (eg, company) [ogilvy]:
Organizational Unit Name (eg, section) [Tech]:
Common Name (eg, your name or your server's hostname) []:ogilvy.com  
Email Address []:[email protected]
root@localhost CA]# touch index.txt
[root@localhost CA]# touch serial
[root@localhost CA]# echo 01 >> serial

#在请求签证书的服务器端生成签署请求,首先生成私钥
[root@localhost ssl]# mkdir ssl #生成私钥及证书的存放路径
[root@localhost ssl]# openssl genrsa -out cakey.pem 1024  #生成私钥
[root@localhost ssl]# openssl req -new -key cakey.pem -out cacert.csr -days 3655 #导出签                                                                        署请求,即导出公钥
[root@localhost ssl]# scp cacert.csr [email protected]:/tmp/ #传送签署请求文件到CA服务器

#在CA服务器上签署证书
[root@localhost tmp]# openssl ca -in cacert.csr -out cacert.pem
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: Jun 15 20:18:22 2015 GMT
            Not After : Jun 14 20:18:22 2016 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = ShangHai
            organizationName          = ogilvy
            organizationalUnitName    = Tech
            commonName                = www.a.com
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                4D:B9:D2:76:8F:53:F5:8A:05:1F:8C:71:E9:24:46:3C:34:DA:FB:DE
            X509v3 Authority Key Identifier: 
                keyid:75:B0:94:8A:2B:20:15:D4:08:43:20:89:EE:8B:2C:45:80:BF:5A:F8

Certificate is to be certified until Jun 14 20:18:22 2016 GMT (365 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

#将签署的证书发送给服务器
[root@localhost tmp]# scp cacert.pem [email protected]:/etc/httpd/ssl/

#安装httpd的ssl支持模块
[root@localhost tmp] yum install mod_ssl
[root@localhost conf]# rpm -ql mod_ssl   #查看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

#配置ssl服务
[root@localhost conf]# grep -v "^#" ../conf.d/ssl.conf | sed '/^$/d'
LoadModule ssl_module modules/mod_ssl.so
Listen 443    #监听443端口
SSLPassPhraseDialog  builtin
SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout  300
SSLMutex default
SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
<VirtualHost 10.189.9.202:443>
ServerName     #配置针对哪个域名的虚拟主机设置,https只能对一个IP的一个域名开启 
DocumentRoot "/www/a.com"
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn
SSLEngine on   #开启https功能,必须on
SSLProtocol all -SSLv2 #开启所有ssl协议,但不支持sslv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW #支持所有加密算法,但不                                                                    支持ADH EXPORT SSLV2
SSLCertificateFile /etc/httpd/ssl/cacert.pem  #证书文件路径
SSLCertificateKeyFile /etc/httpd/ssl/cakey.pem #私钥存放目录
<Files ~ "\.(cgi|shtml|phtml|php3?)$">
    SSLOptions +StdEnvVars
</Files>
<Directory "/var/www/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0
CustomLog logs/ssl_request_log \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>  

#导出CA的公钥到客户端,即可使用https://www.a.com访问网站


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章