详解在Ubuntu上的Apache配置SSL(https证书)的正确姿势

首先看一下阿里云官方的教程:

文件说明:

1. 证书文件xxxxxx.pem,包含两段内容,请不要删除任何一段内容。

2. 如果是证书系统创建的CSR,还包含:证书私钥文件xxxxxxxx.key、证书公钥文件public.pem、证书链文件chain.pem。

( 1 ) 在Apache的安装目录下创建cert目录,并且将下载的全部文件拷贝到cert目录中。如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下并且命名为xxxxxxxx.key;

( 2 ) 打开 apache 安装目录下 conf 目录中的 httpd.conf 文件,找到以下内容并去掉“#”:

#LoadModule ssl_module modules/mod_ssl.so (如果找不到请确认是否编译过 openssl 插件)
#Include conf/extra/httpd-ssl.conf

( 3 ) 打开 apache 安装目录下 conf/extra/httpd-ssl.conf 文件 (也可能是conf.d/ssl.conf,与操作系统及安装方式有关), 在配置文件中查找以下配置语句:

# 添加 SSL 协议支持协议,去掉不安全的协议
SSLProtocol all -SSLv2 -SSLv3
# 修改加密套件如下
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
# 证书公钥配置
SSLCertificateFile cert/public.pem
# 证书私钥配置
SSLCertificateKeyFile cert/xxxxxxx.key
# 证书链配置,如果该属性开头有 '#'字符,请删除掉
SSLCertificateChainFile cert/chain.pem

( 4 ) 重启 Apache。

( 5 ) 通过 https 方式访问您的站点,测试站点证书的安装配置,如遇到证书不信任问题,请查看帮助视频。

然而这只能参考。在Ubuntu下面,我是用apt安装的Apache,但是它没有httpd.conf,只有一个apache2.conf,好吧,其实这个文件和httpd.conf差不多,它里面是这样注释的:

# It is split into several files forming the configuration hierarchy outlined
# below, all located in the /etc/apache2/ directory:
#
# /etc/apache2/
# |-- apache2.conf
# | `-- ports.conf
# |-- mods-enabled
# | |-- *.load
# | `-- *.conf
# |-- conf-enabled
# | `-- *.conf
# `-- sites-enabled
# `-- *.conf
#

这个版本的Apache把配置文件分散到了其他小文件中,结构就是上面那样子的。你要是愿意的话,也可以自己写一个httpd.conf然后include进去。

重点讲一下https的配置,第一步,你要保证你外部环境的443端口是打开的。

第二步确保你安装了ssl_module。没有就apt-get install openssl ,可能还需要一些依赖,但是都是小问题。

然后打开ports.conf,以下几句是不可少的:

<IfModule ssl_module>
 Listen 443
</IfModule>

<IfModule mod_gnutls.c>
 Listen 443
</IfModule>

接着打开mods-available,找到ssl.conf和ssl.load

ssl.load长这样:

# Depends: setenvif mime socache_shmcb
LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so
ssl.conf长这样:
<IfModule mod_ssl.c>

 # Pseudo Random Number Generator (PRNG):
 # Configure one or more sources to seed the PRNG of the SSL library.
 # The seed data should be of good random quality.
 # WARNING! On some platforms /dev/random blocks if not enough entropy
 # is available. This means you then cannot use the /dev/random device
 # because it would lead to very long connection times (as long as
 # it requires to make more entropy available). But usually those
 # platforms additionally provide a /dev/urandom device which doesn't
 # block. So, if available, use this one instead. Read the mod_ssl User
 # Manual for more details.
 #
 SSLRandomSeed startup builtin
 SSLRandomSeed startup file:/dev/urandom 512
 SSLRandomSeed connect builtin
 SSLRandomSeed connect file:/dev/urandom 512

 ##
 ## SSL Global Context
 ##
 ## All SSL configuration in this context applies both to
 ## the main server and all SSL-enabled virtual hosts.
 ##

 #
 # Some MIME-types for downloading Certificates and CRLs
 #
 AddType application/x-x509-ca-cert .crt
 AddType application/x-pkcs7-crl .crl

 # Pass Phrase Dialog:
 # Configure the pass phrase gathering process.
 # The filtering dialog program (`builtin' is a internal
 # terminal dialog) has to provide the pass phrase on stdout.
 SSLPassPhraseDialog exec:/usr/share/apache2/ask-for-passphrase

 # Inter-Process Session Cache:
 # Configure the SSL Session Cache: First the mechanism
 # to use and second the expiring timeout (in seconds).
 # (The mechanism dbm has known memory leaks and should not be used).
 #SSLSessionCache dbm:${APACHE_RUN_DIR}/ssl_scache
 SSLSessionCache shmcb:${APACHE_RUN_DIR}/ssl_scache(512000)
 SSLSessionCacheTimeout 300

 # Semaphore:
 # Configure the path to the mutual exclusion semaphore the
 # SSL engine uses internally for inter-process synchronization.
 # (Disabled by default, the global Mutex directive consolidates by default
 # this)
 #Mutex file:${APACHE_LOCK_DIR}/ssl_mutex ssl-cache

 # SSL Cipher Suite:
 # List the ciphers that the client is permitted to negotiate. See the
 # ciphers(1) man page from the openssl package for list of all available
 # options.
 # Enable only secure ciphers:
 SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM

 # SSL server cipher order preference:
 # Use server priorities for cipher algorithm choice.
 # Clients may prefer lower grade encryption. You should enable this
 # option if you want to enforce stronger encryption, and can afford
 # the CPU cost, and did not override SSLCipherSuite in a way that puts
 # insecure ciphers first.
 # Default: Off
 SSLHonorCipherOrder on

 # The protocols to enable.
 # Available values: all, SSLv3, TLSv1, TLSv1.1, TLSv1.2
 # SSL v2 is no longer supported
 SSLProtocol all -SSLv2 -SSLv3

 # Allow insecure renegotiation with clients which do not yet support the
 # secure renegotiation protocol. Default: Off
 #SSLInsecureRenegotiation on

 # Whether to forbid non-SNI clients to access name based virtual hosts.
 # Default: Off
 #SSLStrictSNIVHostCheck On

</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

之后就是站点的配置了,这里使用默认的default-ssl.conf:

<IfModule mod_ssl.c>
 <VirtualHost _default_:443>
 ServerName 

 ################加入你自己的站点配置##########

 # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 # error, crit, alert, emerg.
 # It is also possible to configure the loglevel for particular
 # modules, e.g.
 #LogLevel info ssl:warn

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

 # For most configuration files from conf-available/, which are
 # enabled or disabled at a global level, it is possible to
 # include a line for only one particular virtual host. For example the
 # following line enables the CGI configuration for this host only
 # after it has been globally disabled with "a2disconf".
 #Include conf-available/serve-cgi-bin.conf

 # SSL Engine Switch:
 # Enable/Disable SSL for this virtual host.
 SSLEngine on

 # A self-signed (snakeoil) certificate can be created by installing
 # the ssl-cert package. See
 # /usr/share/doc/apache2/README.Debian.gz for more info.
 # If both key and certificate are stored in the same file, only the
 # SSLCertificateFile directive is needed.
 SSLCertificateFile /etc/apache2/cert/public.pem
 SSLCertificateKeyFile /etc/apache2/cert/xxxxxxx.key

 # Server Certificate Chain:
 # Point SSLCertificateChainFile at a file containing the
 # concatenation of PEM encoded CA certificates which form the
 # certificate chain for the server certificate. Alternatively
 # the referenced file can be the same as SSLCertificateFile
 # when the CA certificates are directly appended to the server
 # certificate for convinience.
 SSLCertificateChainFile /etc/apache2/cert/chain.pem

 # Certificate Authority (CA):
 # Set the CA certificate verification path where to find CA
 # certificates for client authentication or alternatively one
 # huge file containing all of them (file must be PEM encoded)
 # Note: Inside SSLCACertificatePath you need hash symlinks
 # to point to the certificate files. Use the provided
 # Makefile to update the hash symlinks after changes.
 #SSLCACertificatePath /etc/ssl/certs/
 #SSLCACertificateFile /etc/apache2/ssl.crt/ca-bundle.crt

 # Certificate Revocation Lists (CRL):
 # Set the CA revocation path where to find CA CRLs for client
 # authentication or alternatively one huge file containing all
 # of them (file must be PEM encoded)
 # Note: Inside SSLCARevocationPath you need hash symlinks
 # to point to the certificate files. Use the provided
 # Makefile to update the hash symlinks after changes.
 #SSLCARevocationPath /etc/apache2/ssl.crl/
 #SSLCARevocationFile /etc/apache2/ssl.crl/ca-bundle.crl

 # Client Authentication (Type):
 # Client certificate verification type and depth. Types are
 # none, optional, require and optional_no_ca. Depth is a
 # number which specifies how deeply to verify the certificate
 # issuer chain before deciding the certificate is not valid.
 #SSLVerifyClient require
 #SSLVerifyDepth 10

 # SSL Engine Options:
 # Set various options for the SSL engine.
 # o FakeBasicAuth:
 # Translate the client X.509 into a Basic Authorisation. This means that
 # the standard Auth/DBMAuth methods can be used for access control. The
 # user name is the `one line' version of the client's X.509 certificate.
 # Note that no password is obtained from the user. Every entry in the user
 # file needs this password: `xxj31ZMTZzkVA'.
 # o ExportCertData:
 # This exports two additional environment variables: SSL_CLIENT_CERT and
 # SSL_SERVER_CERT. These contain the PEM-encoded certificates of the
 # server (always existing) and the client (only existing when client
 # authentication is used). This can be used to import the certificates
 # into CGI scripts.
 # o StdEnvVars:
 # This exports the standard SSL/TLS related `SSL_*' environment variables.
 # Per default this exportation is switched off for performance reasons,
 # because the extraction step is an expensive operation and is usually
 # useless for serving static content. So one usually enables the
 # exportation for CGI and SSI requests only.
 # o OptRenegotiate:
 # This enables optimized SSL connection renegotiation handling when SSL
 # directives are used in per-directory context.
 #SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
 <FilesMatch "\.(cgi|shtml|phtml|php)$">
 SSLOptions +StdEnvVars
 </FilesMatch>
 <Directory /usr/lib/cgi-bin>
 SSLOptions +StdEnvVars
 </Directory>

 # SSL Protocol Adjustments:
 # The safe and default but still SSL/TLS standard compliant shutdown
 # approach is that mod_ssl sends the close notify alert but doesn't wait for
 # the close notify alert from client. When you need a different shutdown
 # approach you can use one of the following variables:
 # o ssl-unclean-shutdown:
 # This forces an unclean shutdown when the connection is closed, i.e. no
 # SSL close notify alert is send or allowed to received. This violates
 # the SSL/TLS standard but is needed for some brain-dead browsers. Use
 # this when you receive I/O errors because of the standard approach where
 # mod_ssl sends the close notify alert.
 # o ssl-accurate-shutdown:
 # This forces an accurate shutdown when the connection is closed, i.e. a
 # SSL close notify alert is send and mod_ssl waits for the close notify
 # alert of the client. This is 100% SSL/TLS standard compliant, but in
 # practice often causes hanging connections with brain-dead browsers. Use
 # this only for browsers where you know that their SSL implementation
 # works correctly.
 # Notice: Most problems of broken clients are also related to the HTTP
 # keep-alive facility, so you usually additionally want to disable
 # keep-alive for those clients, too. Use variable "nokeepalive" for this.
 # Similarly, one has to force some clients to use HTTP/1.0 to workaround
 # their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and
 # "force-response-1.0" for this.
 # BrowserMatch "MSIE [2-6]" \
 # nokeepalive ssl-unclean-shutdown \
 # downgrade-1.0 force-response-1.0

 </VirtualHost>
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

发现了吗,这是把阿里云教程里的配置项分散到了两个配置文件里面。

然后在浏览器上使用https访问,成功。(linux可以使用wget或curl测试)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Apache、SSL、MySQL和PHP平滑无缝地安装

    为了这个任务所需的工具是: Apache-一个网站服务器  Mod_SSL-一个安全套接字层(SSL)的模块  OpenSSL-开放源代码工具箱(mod_ssl所需)  RSARef-仅对美国用户  MySQL-一个数据库服务器  PHP-一种脚本语言  "条条大路通罗马"--因此这只是很多能达到我们要求的配置之一.我选择这样的配置,是因为它是最简单和最快的一种.选择Mod_SSL/OpenSSL的原因是因为我有它的先前经验,是最快配置和最容易安装的一种.为了彼此方便地与Apache集

  • 给APACHE开启SSL服务

    1.首先请确认您的Apache服务器已经安装有加密模块,可以是OpenSSL,或是OpenSSL+ModSSL. 如果您的Apache web服务器安装在Unix或linux平台上,您可以通过以下网址获得OpenSSL: http://www.openssl.org/source/ 如果您的Apache web服务器运行在Windows平台上,您可以通过以下网址获得OpenSSL + ModSSL: http://www.modssl.org/contrib/ 2.通过OpenSSL给Apach

  • Apache SSL服务器配置SSL详解

    1.安装必要的软件 引用 我用的是apahce2.0.61版,可以直接官方提供的绑定openssl的apache. 文件名是:apache_2.0.61-win32-x86-openssl-0.9.7m.msi 否则单独安装windows下的openssl比较麻烦,要么找到一个第三方的编译结果,要么自己编译 2. 生成服务器证书 引用 安装好在bin目录下有一个 openssl.exe文件,用来生成证书和密钥. 1). 生成服务器用的私钥文件server.key 进入conf目录,执行命令行 o

  • 在Apache服务器上安装SSL支持的教程

    今天我会讲述如何为你的个人网站或者博客安装SSL 证书,来保护你的访问者和网站之间通信的安全. 安全套接字层或称SSL,是一种加密网站和浏览器之间连接的标准安全技术.这确保服务器和浏览器之间传输的数据保持隐私和安全.它被成千上万的人使用来保护他们与客户的通信.要启用SSL链接,Web服务器需要安装SSL证书. 你可以创建你自己的SSL证书,但是这默认不会被浏览器所信任,要解决这个问题,你需要从受信任的证书机构(CA)处购买证书,我们会向你展示如何获取证书并在apache中安装.生成一个证书签名请

  • apache+mysql+php+ssl服务器之完全安装攻略

    目的我们的目标是安装一个允许我们托管多个网站的web服务器,其中一些是针对电子商务的安全解决方案,而大部分网站是通过连接一个数据库服务器并且提取其数据的脚本驱动的.为了这个任务所需的工具是:Apache-一个网站服务器Mod_SSL-一个安全套接字层(SSL)的模块OpenSSL-开放源代码工具箱(mod_ssl所需)RSARef-仅对美国用户MySQL-一个数据库服务器PHP-一种脚本语言 "条条大路通罗马"--因此这只是很多能达到我们要求的配置之一.我选择这样的配置,是因为它是最简

  • Apache环境下配置多个ssl证书搭建多个站点的方法

    服务器上有两个项目,都要配置https,所以在阿里云申请了两个二级的免费证书. 博主用的是phpstudy,如果用的其他集成环境,其实也差不多,参考下改改就好了. 一.申请证书(这里我用的是阿里的域名) 1.登录阿里云,点击域名,找到要配置ssl的域名,点击后面的ssl证书 2.这里我申请的是免费的单域名证书,点击确定提交阿里云审核,大概10-20分钟左右就审核好了 3.点击左边的菜单,选择要配置的域名相应的证书,点击后面的下载 4.我的环境是apache,这里我下载的是apache. 二.配置

  • 配置apache默认使用ssl的方法

    然后在下面添加,具体位置似乎没有大的关系,可以自行测试一下: 复制代码 代码如下: RewriteEngine On RewriteRule /.* https://www.example.com/ [R] 替换URL为你需要的就好了.看到其它地方还添加了其他的选项,我这里就采用了最基本的官方说明了.有兴趣的可以自己在研究一下. P: Apache的官方文档有个小错误,它用了http而不是https,那不是没用了么,呵呵. 参考: http://httpd.apache.org/docs/1.3

  • 详解在Ubuntu上的Apache配置SSL(https证书)的正确姿势

    首先看一下阿里云官方的教程: 文件说明: 1. 证书文件xxxxxx.pem,包含两段内容,请不要删除任何一段内容. 2. 如果是证书系统创建的CSR,还包含:证书私钥文件xxxxxxxx.key.证书公钥文件public.pem.证书链文件chain.pem. ( 1 ) 在Apache的安装目录下创建cert目录,并且将下载的全部文件拷贝到cert目录中.如果申请证书时是自己创建的CSR文件,请将对应的私钥文件放到cert目录下并且命名为xxxxxxxx.key: ( 2 ) 打开 apac

  • 详解安装Ubuntu Linux系统时硬盘分区最合理的方法

    无论是安装Windows还是Linux操作系统,硬盘分区都是整个系统安装过程中最为棘手的环节,网上的一些Ubuntu Linux安装教程一般都是自动分区,给初学者带来很大的不便,下面我就根据多年来在装系统的经验谈谈安装Ubuntu Linux系统时硬盘分区最合理的方法. 在讲硬盘分区之前,我先来普及一下硬盘的相关分类,硬盘一般分为IDE硬盘.SCSI硬盘和SATA硬盘三种,在Linux系统中,IDE接口的硬盘被称为hd,SCSI和SATA接口的硬盘则被称为sd,其中IDE硬盘基本上已经淘汰,现在

  • 详解mybatis-plus的 mapper.xml 路径配置的坑

    mybatis-plus今天遇到一个问题,就是mybatis 没有读取到mapper.xml 文件. 特此记录一下,问题如下: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.husy.mapper.SystemUserMapper.findUserByName at com.baomidou.mybatisplus.core.override.MybatisMapperMe

  • mysql数据库详解(基于ubuntu 14.0.4 LTS 64位)

    1.mysql数据库的组成与相关概念 首先明白,mysql是关系型数据库,和非关系型数据库中最大的不同就是表的概念不一样. +整个mysql环境可以理解成一个最大的数据库:A +用mysql创建的数据库B是属于A的,是数据的仓库,相当于系统中的文件夹 +数据表C:是存放数据的具体场所,相当于系统中的文件,一个数据库B中包含若干个数据表C(注意此处的数据库B和A不一样) +记录D:数据表中的一行称为一个记录,因此,我们在创建数据表时,一定要创建一个id列,用于标识"这是第几条记录",id

  • 详解关于SpringBoot的外部化配置使用记录

    更新: 工作中突然想起来,关于Yaml的使用,并不属于Spring的范畴,是org.yaml.snakeyaml处理的.所以yaml的使用应该参考官方,不过貌似打不开... Spring利用snakeyaml将配置解析成PropertySource,然后写入到Environment,就能使用了 记录下使用SpringBoot配置时遇到的一些麻烦,虽然这种麻烦是因为知识匮乏导致的. 记录下避免一段时间后自己又给忘记了,以防万一. 如果放到博客里能帮助到遇到同样问题的同志,自是极好! SpringB

  • Java Fluent Mybatis 项目工程化与常规操作详解流程篇 上

    目录 前言 Maven依赖 配置文件调整 Knife4j配置 添加必要实体 增/改 总结 前言 接着上一篇,上篇已经测试通过,成功添加了数据.那么这篇主要是继续上一个项目,将项目进行工程化包装,增加一些必要配置,并且生成增删改查接口. GitHub代码仓库:GitHub仓库 Maven依赖 增加了druid数据库连接池,所以之前的配置文件也需要调整,下面会发出来. <dependency> <groupId>cn.hutool</groupId> <artifac

  • 详解Java如何使用注解来配置Spring容器

    目录 介绍 @Bean and @Configuration AnnotationConfigApplicationContext实例化容器 通过使用 register(Class...) 以编程方式构建容器 @ComponentScan启用组件扫描 Bean的依赖 生命周期回调 Bean指定作用域 自定义bean名称 Bean别名 Bean注入之间的依赖 @Import @ImportResource @PropertySource 支持多个properties文件 ApplicationCo

  • 详解hibernate自动创建表的配置

    详解hibernate自动创建表的配置 配置自动创建表: <prop key="hibernate.hbm2ddl.auto">update</prop>//首次创建项目时用,项目稳定后一般注释这里有4个值: update:表示自动根据model对象来更新表结构,启动hibernate时会自动检查数据库,如果缺少表,则自动建表:如果表里缺少列,则自动添加列. 还有其他的参数: create:启动hibernate时,自动删除原来的表,新建所有的表,所以每次启动后

  • 详解Pycharm安装及Django安装配置指南

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python实用宝典 ,作者Python实用宝典 Pycharm拥有强大的配置工具.Git版本管理工具.代码补全工具.Debug工具等等,这些都是进行大型项目开发的利器. 尤其是今天的主角Django,由于太过于重要了,Pycharm甚至专门给其提供了配置模板: 能直接在新建项目的时候选择Django并新建一个独立的虚拟环境: 从新建到编码测试,一套流程用起来都极

  • 详解Python3.8+PyQt5+pyqt5-tools+Pycharm配置详细教程

    个人使用环境 WIN10x64系统,Python3.8,PyCharm2020.01.03 安装过程 一.安装Python3.8 (自己参考其他教程) 二.安装PyQt5 然后在cmd下输入指令 pip install PyQt5 也可以输入这个指令 pip install PyQt5 -i https://pypi.douban.com/simple (后面是豆瓣的镜像地址,是为了加快下载速度) 提示你更新pip,就按照提示更新(这步骤是可选的,看个人需求) 在cmd下输入 python -m

随机推荐