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

news/2024/7/12 3:08:05 标签: apache, ssl, https, apache https, apache Certbot

文章目录

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

一、准备工作

1.1 安装Apache服务器

yum install httpd -y

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

443为HTTPS服务的默认端口

sslso_11">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_21">二、配置apache

apache_23">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
systemctl restart httpd

http://www.niftyadmin.cn/n/5175052.html

相关文章

Spring-ProxyFactory

ProxyFactory选择cglib或jdk动态代理原理 ProxyFactory在生成代理对象之前需要决定是使用JDK动态代理还是CGLIB技术&#xff1a; public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {Overridepublic AopProxy createAopProxy(AdvisedSupport co…

戴德金分割构造实数

戴德金分割 具有最小上界性的有序域 R \mathbb{R} R存在 此外&#xff0c; R \mathbb{R} R包容着 Q \mathbb{Q} Q作为其子域。 第二句话表示 Q ⊂ R \mathbb{Q}\subset \mathbb{R} Q⊂R而且把 R \mathbb{R} R中的假发与乘法运算用于 Q \mathbb{Q} Q的元时&#xff0c;与有理数…

IP多播需要使用两种协议(IGMP和多播路由选择协议)

目录 IGMP 多播路由选择协议 组播协议包括组成员管理协议和组播路由协议: 组成员管理协议用于管理组播组成员的加入和离开(IGMP) 组播路由协议负责在路由器之间交互信息来建立组播树(多播路由选择协议) IGMP 图中标有 IP 地址的四台主机都参加了一个多播组&#xff0c;其…

排序 算法(第4版)

本博客参考算法&#xff08;第4版&#xff09;&#xff1a;算法&#xff08;第4版&#xff09; - LeetBook - 力扣&#xff08;LeetCode&#xff09;全球极客挚爱的技术成长平台 本文用Java实现相关算法。 我们关注的主要对象是重新排列数组元素的算法&#xff0c;其中每个元素…

逻辑回归-癌症病预测与不均衡样本评估

1.注册相关库(在命令行输入&#xff09; pip install scikit-learn pip install pandas pip install numpy 2.导入相关库 import pandas as pd import numpy as np from sklearn.metrics import classification_report from sklearn.model_selection import train_test_split…

[Android]修改应用包名、名称、版本号、Icon以及环境判断和打包

1.修改包名 在Android Studio中更改项目的包名涉及几个步骤&#xff1a; 打开项目结构: 在Android Studio中&#xff0c;确保您处于Android视图模式&#xff08;在左侧面板顶部有一个下拉菜单可以选择&#xff09;。 重命名包名: 在项目视图中&#xff0c;找到您的包名&…

RT-DTER 引入用于低分辨率图像和小物体的新 CNN 模块 SPD-Conv

论文地址:https://arxiv.org/pdf/2208.03641v1.pdf 代码地址:https://github.com/labsaint/spd-conv 卷积神经网络(CNN)在图像分类、目标检测等计算机视觉任务中取得了巨大的成功。然而,在图像分辨率较低或对象较小的更困难的任务中,它们的性能会迅速下降。 这源于现有CNN…

acwing算法基础之数学知识--求一个数x的所有约数

目录 1 基础知识2 模板3 工程化 1 基础知识 使用试除法来求取一个数x的所有约数&#xff0c; void f(int x) {vector<int> res;for (int i 1; i < x / i; i) {if (x % i 0) {res.emplace_back(i);if (i ! n / i) res.emplace_back(n / i);}}//res即为数x的所有约数…