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"
    </VirtualHost>
  • 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"
    </VirtualHost>
  • 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>

    <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"
    </VirtualHost>

2.2 生效配置文件

  • 查看配置文件是否正常

    apachectl -t

    Syntax OK

  • 重启apache配置

    systemctl restart httpd

相关推荐
yw00yw31 分钟前
小程序插件使用
java·小程序·apache
00后程序员张40 分钟前
iOS 应用上架常见问题与解决方案,多工具组合的实战经验
android·ios·小程序·https·uni-app·iphone·webview
2501_9160074716 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
.Shu.16 小时前
计算机网络 HTTPS 全流程
网络协议·计算机网络·https
唐叔在学习2 天前
万字长文深度解析HTTPS协议
后端·https
Mr_Xuhhh2 天前
NAT、代理服务、内网穿透
网络·网络协议·http·https·udp·智能路由器
一个天蝎座 白勺 程序猿2 天前
Apache IoTDB(4):深度解析时序数据库 IoTDB 在Kubernetes 集群中的部署与实践指南
数据库·深度学习·kubernetes·apache·时序数据库·iotdb
喂完待续2 天前
【Tech Arch】Spark为何成为大数据引擎之王
大数据·hadoop·python·数据分析·spark·apache·mapreduce
2501_915918412 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview