当前位置: 首页 > 编程文章 >

Ubuntu Apache配置SSL证书指南

Ubuntu Apache配置SSL证书指南

作者:叶舟时间:2026-06-01 12:21:01浏览:395

本文介绍在Ubuntu系统下使用apt安装的Apache配置SSL证书的方法。针对阿里云官方教程中httpd.conf文件不存在的情况,详细说明了如何修改ports.conf启用443端口,加载ssl_module模块,并在default-ssl.conf中完成证书路径及协议配置,实现站点HTTPS访问。

本文目录展开

阿里云官方SSL配置教程及文件说明

首先参考阿里云官方提供的配置教程:

关于证书文件的说明如下:

1. 证书文件xxxxxx.pem包含两段内容,切勿删除其中任何一段。

2. 若通过证书系统生成CSR,还包含证书私钥文件xxxxxxxx.key、公钥文件public.pem以及证书链文件chain.pem。

( 1 ) 在Apache安装目录下新建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 Apache SSL配置实战步骤

上述方法仅具参考价值。在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文件,确保包含以下关键配置:



 Listen 443

 

 Listen 443

接着进入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长这样:

 
 # 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
 

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

接下来进行站点配置,此处采用默认的default-ssl.conf文件:



 
 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
 
 SSLOptions +StdEnvVars
 
 
 SSLOptions +StdEnvVars
 
 
 # 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
 
 

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

可以看出,阿里云教程中的配置项被拆分到了两个不同的配置文件中。

最后,在浏览器中使用https访问站点即可成功。(Linux系统也可使用wget或curl进行测试)

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

上一篇
PHP正则表达式基础:转义字符、修饰符与核心函数详解

本文梳理了PHP中正则表达式的核心要素,涵盖需转义的特殊字符列表、Perl风格的修饰符用法以及关键元字符的定义。同时详细解析了preg_grep、preg_match、preg_match_all和preg_replace等常用函数的参数结构与返回值逻辑,帮助开发者快速掌握PHP正则处理的基础规范。

下一篇
Ubuntu下Apache安装、配置及CGI环境搭建指南

本文介绍在Ubuntu 11.04系统中通过apt-get安装Apache2的方法,涵盖服务启动停止、核心配置文件(apache2.conf、httpd.conf)结构解析,以及sites-enabled与mods-enabled目录机制。重点阐述如何配置/var/www/cgi-bin目录以支持Python等CGI脚本运行,包括ScriptAlias修改、AddHandler指令添加及文件权限755/666设置,解决重启报错并实现浏览器访问。

相关文章

更多
Ubuntu 12.04 x64 编译安装 LAMP (Apache, MySQL, PHP) 完整指南

本文详细记录了在 Ubuntu 12.04 x64 系统下,通过源码编译方式手动搭建 LAMP 环境的完整步骤。内容涵盖 Apache 2.4.2 及其依赖 APR、APR-Util、PCRE 和 zlib 的安装与常见错误排查,MySQL 5.5.25a 二进制包的解压配置及开机自启设置,以及 PHP 5.4.4 与 Apache 和 MySQL 的集成配置。针对编译过程中出现的 APR 未找到、mod_deflate 符号未定义、PCRE 缺失等典型报错提供了具体的解决方案。

2026.01.11 954
Ubuntu 16.04源码编译安装Apache 2.4.25教程

本文介绍在Ubuntu 16.04系统中通过源码编译安装Apache 2.4.25的方法。主要涵盖APR、APR-Util及PCRE等依赖库的安装配置,以及httpd服务的启动脚本添加、环境变量设置和常见端口占用问题的解决。

2026.09.22 662
Ubuntu Server 10.04修改Apache默认目录及恢复丢失配置的方法

在Ubuntu Server 10.04中,通过apt安装的Apache默认网页主目录为/var/www。当因磁盘空间不足需将其更改为/home/www时,若/etc/apache2/sites-enabled/000-default文件丢失会导致服务报错。该文件实为指向../sites-available/default的软链接,可通过ln命令重建并重启服务以恢复正常。

2025.03.23 618
Ubuntu 16.04源码编译安装PHP 5.6.29完整指南

本文详细记录了在Ubuntu 16.04系统中通过源码方式编译安装PHP 5.6.29的完整流程。内容涵盖从下载源码、解压、配置编译参数(特别是与Apache的apxs集成)、执行make编译与安装,到修改php.ini及httpd.conf配置文件,最后重启服务并验证安装结果的全过程。同时提供了将PHP命令加入全局变量的方法,确保CLI环境下也能正常使用。

2025.02.05 541
Ubuntu 12.04 配置 Apache+PHP+MySQL+phpMyAdmin 教程

本文介绍在 Ubuntu 12.04 裸系统环境下安装和配置 LAMP 组件的具体步骤。首先通过 apt-get 更新源,随后依次安装 MySQL、Apache2 和 PHP5,并配置 phpMyAdmin 以管理数据库。文中还包含启用 mod_rewrite 模块、设置文件读写权限以及测试各组件连通性的详细操作指令。

2026.04.07 101
Apache开启.htaccess及使用方法详解

本文介绍如何在本地Apache环境中启用.htaccess文件,主要涉及修改httpd.conf中的AllowOverride参数。同时详细解析.htaccess的多种实用功能,包括自定义错误页面、目录密码保护、IP访问限制、URL自动转向、默认首页设置以及防止图片盗链等配置方法,并分析了使用.htaccess对服务器性能和安全性的潜在影响。

2026.05.09 1,005

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。