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

一、准备工作

1.1 安装Apache服务器

yaml 复制代码
#下载安装apache
yum install httpd -y

#启动apache服务
systemctl start httpd
systemctl enable httpd

#查看服务状态
netstat -lntp  |grep http

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

复制代码
443为HTTPS服务的默认端口

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

复制代码
启用SSL功能,安装mod_ssl.so模块
yaml 复制代码
yum install -y mod_ssl

1.4 获取SSL证书

二、配置apache

2.1 配置apache文件

复制代码
vhost的域名配置文件.conf,在目录:/etc/httpd/conf.d
  • HTTP配置:
xml 复制代码
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配置:
xml 复制代码
<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 配置
xml 复制代码
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>
  • Apache 配置Https多个端口号映射
xml 复制代码
// 通过下方简单配置
<VirtualHost *:443>

	<Location /b8082>  //这个可以自定义名称
    ProxyPass http://127.0.0.1:8081  
    ProxyPassReverse http://127.0.0.1:8081
    </Location>
    
    <Location /b8081>    
    ProxyPass http://127.0.0.1:8082  
    ProxyPassReverse http://127.0.0.1:8082
    </Location>

    <Location /b8081/a>    
    ProxyPass http://127.0.0.1:8083  
    ProxyPassReverse http://127.0.0.1:8083
    </Location>
    
</VirtualHost>

2.2 生效配置文件

  • 查看配置文件是否正常
xml 复制代码
# apachectl -t
Syntax OK
  • 重启apache配置
xml 复制代码
systemctl restart httpd
相关推荐
阿巴~阿巴~13 分钟前
Linux 第一个系统程序 - 进度条
linux·服务器·bash
小白爱电脑16 分钟前
什么是2.5G交换机?
运维·网络·5g·千兆宽带
找不到、了20 分钟前
分布式理论:CAP、Base理论
java·分布式
天天摸鱼的java工程师23 分钟前
2025已过半,Java就业大环境究竟咋样了?
java·后端
人生在勤,不索何获-白大侠27 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
游戏开发爱好者832 分钟前
iOS 出海 App 安全加固指南:无源码环境下的 IPA 加固与防破解方法
websocket·网络协议·tcp/ip·http·网络安全·https·udp
Zedthm34 分钟前
LeetCode1004. 最大连续1的个数 III
java·算法·leetcode
艺杯羹1 小时前
MyBatis之核心对象与工作流程及SqlSession操作
java·mybatis
神的孩子都在歌唱1 小时前
3423. 循环数组中相邻元素的最大差值 — day97
java·数据结构·算法
我科绝伦(Huanhuan Zhou)1 小时前
华为泰山服务器重启后出现 XFS 文件系统磁盘“不识别”(无法挂载或访问),但挂载点目录仍在且无数据
运维·服务器·华为