Apache配置ssl证书-实现https访问

文章目录

  • 一、准备工作
    • [1.1 安装Apache服务器](#1.1 安装Apache服务器)
    • [1.2 Apache服务器上已经开启了443端口](#1.2 Apache服务器上已经开启了443端口)
    • [1.3 Apache服务器上已安装了mod_ssl.so模块](#1.3 Apache服务器上已安装了mod_ssl.so模块)
    • [1.4 获取SSL证书](#1.4 获取SSL证书)
  • 二、配置apache
    • [2.1 配置apache文件](#2.1 配置apache文件)
    • [2.2 生效配置文件](#2.2 生效配置文件)

一、准备工作

1.1 安装Apache服务器

复制代码
yum install httpd -y

1.2 Apache服务器上已经开启了443端口

443为HTTPS服务的默认端口

1.3 Apache服务器上已安装了mod_ssl.so模块

启用SSL功能,安装mod_ssl.so模块

复制代码
yum install -y mod_ssl

1.4 获取SSL证书

使用Certbot签发和续费泛域名SSL证书:https://blog.csdn.net/cljdsc/article/details/133461361

二、配置apache

2.1 配置apache文件

vhost的域名配置文件.conf,在目录:/etc/httpd/conf.d

  • HTTP配置:

    Listen 80

    指定域名

    ServerName www.example.com

    指定文档根目录

    DocumentRoot /var/www/html

    是否启用访问日志

    CustomLog /var/log/httpd/access.log combined

    指定错误日志路径

    ErrorLog /var/log/httpd/error.log

    配置虚拟主机

    <VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html/project

    复制代码
      # 访问权限
      <Directory /var/www/html/project>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          allow from all
          Require all granted
      </Directory>
    
      # 使用PHP解析器处理.php文件
      <FilesMatch \.php$>
          SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
      </FilesMatch>
    
      # 定义PHP脚本的目录索引
      DirectoryIndex index.php index.html
      
      # 自定义错误页面
      ErrorDocument 404 /error_404.html
      
      # 设置HTTP头信息
      Header set X-Content-Type-Options "nosniff"
      Header set X-XSS-Protection "1; mode=block"
  • HTTPS配置:

    <VirtualHost *:443>
    DocumentRoot /var/www/html/project
    ServerName www.cpayfinance.com
    ServerAlias www.cpayfinance.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/cpayfinance.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/cpayfinance.com//privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/cpayfinance.com//chain.pem

    访问权限

    复制代码
      <Directory /var/www/html/project>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          allow from all
          Require all granted
      </Directory>
    
      # 使用PHP解析器处理.php文件
      <FilesMatch \.php$>
          SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
      </FilesMatch>
    
      # 定义PHP脚本的目录索引
      DirectoryIndex index.php index.html
    
      # 自定义错误页面
      ErrorDocument 404 /error_404.html
    
      # 设置HTTP头信息
      Header set X-Content-Type-Options "nosniff"
      Header set X-XSS-Protection "1; mode=block"
  • HTTP & HTTPS 配置

    Listen 80

    指定域名

    ServerName www.cpayfinance.com

    指定文档根目录

    DocumentRoot /var/www/html

    是否启用访问日志

    CustomLog /var/log/httpd/access.log combined

    指定错误日志路径

    ErrorLog /var/log/httpd/error.log

    配置虚拟主机

    <VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot /var/www/html/project

    复制代码
      # 访问权限
      <Directory /var/www/html/project>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          allow from all
          Require all granted
      </Directory>
    
      # 使用PHP解析器处理.php文件
      <FilesMatch \.php$>
          SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
      </FilesMatch>
    
      # 定义PHP脚本的目录索引
      DirectoryIndex index.php index.html
      
      # 自定义错误页面
      ErrorDocument 404 /error_404.html
      
      # 设置HTTP头信息
      Header set X-Content-Type-Options "nosniff"
      Header set X-XSS-Protection "1; mode=block"

    <VirtualHost *:443>
    DocumentRoot /var/www/html/project
    ServerName www.cpayfinance.com
    ServerAlias www.cpayfinance.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/cpayfinance.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/cpayfinance.com//privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/cpayfinance.com//chain.pem

    访问权限

    复制代码
      <Directory /var/www/html/project>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Order allow,deny
          allow from all
          Require all granted
      </Directory>
    
      # 使用PHP解析器处理.php文件
      <FilesMatch \.php$>
          SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
      </FilesMatch>
    
      # 定义PHP脚本的目录索引
      DirectoryIndex index.php index.html
    
      # 自定义错误页面
      ErrorDocument 404 /error_404.html
    
      # 设置HTTP头信息
      Header set X-Content-Type-Options "nosniff"
      Header set X-XSS-Protection "1; mode=block"

2.2 生效配置文件

  • 查看配置文件是否正常

    apachectl -t

    Syntax OK

  • 重启apache配置

    systemctl restart httpd

相关推荐
神一样的老师2 分钟前
WS63 访问 HTTPS 握手失败(-0x7780)根治
数据库·网络协议·https
砚凝霜1 天前
【软考信息安全】第九章 虚拟专用网与IPSec/SSL安全协议技术原理
网络·网络协议·ssl
zxanz11 天前
代码审计中如何高效发现漏洞?
ssl
Lsetea2 天前
curl用IP测试HTTPS报证书不匹配:--resolve与Host头别混用
linux·https·ssl证书·openssl·curl
掉进电商坑三年没爬出来的东叔2 天前
云服务器搭建个人网站实战:从零到公网访问,含域名HTTPS
运维·服务器·https
Lsetea3 天前
OpenSSL verify报unable to get issuer certificate:error 2与partial_chain排查
linux·https·ssl证书·openssl·证书链
青春不朽5123 天前
requests 超时、重试、SSL 报错?9 个高频问题与一整张排错对照表
网络·网络协议·ssl
天天进步20153 天前
Caddy 实战:5 分钟给域名开启 HTTPS,自动申请 SSL 证书并反向代理
网络协议·https·ssl
xixingzhe23 天前
正确认知HTTPS:并非万能加密,通信安全需要多层防护
网络协议·安全·https
Lsetea4 天前
OpenSSL verify报unhandled critical extension:error 34与关键扩展排查
运维·https·ssl证书·openssl·证书链