Apache配置ssl证书-实现https访问 📅 发布时间:2026/8/26 13:49:26 👁 浏览次数: 一、准备工作1.1 安装Apache服务器#下载安装apacheyum install httpd-y#启动apache服务systemctl start httpd systemctl enable httpd#查看服务状态netstat-lntp|grep http1.2 Apache服务器上已经开启了443端口443为HTTPS服务的默认端口1.3 Apache服务器上已安装了mod_ssl.so模块启用SSL功能,安装mod_ssl.so模块yum install-y mod_ssl1.4 获取SSL证书二、配置apache2.1 配置apache文件vhost的域名配置文件.conf在目录/etc/httpd/conf.dHTTP配置Listen 80 # 指定域名 ServerName www.example.com # 指定文档根目录 DocumentRoot /var/www/html # 是否启用访问日志 CustomLog /var/log/httpd/access.log combined # 指定错误日志路径 ErrorLog /var/log/httpd/error.log # 配置虚拟主机VirtualHost*:80ServerAdmin adminexample.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; modeblock/VirtualHostHTTPS配置VirtualHost*:443DocumentRoot /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; modeblock/VirtualHostHTTP 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*:80ServerAdmin adminexample.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; modeblock/VirtualHostVirtualHost*:443DocumentRoot /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; modeblock/VirtualHostApache 配置Https多个端口号映射// 通过下方简单配置VirtualHost*:443Location /b8082 //这个可以自定义名称 ProxyPass http://127.0.0.1:8081 ProxyPassReverse http://127.0.0.1:8081/LocationLocation /b8081 ProxyPass http://127.0.0.1:8082 ProxyPassReverse http://127.0.0.1:8082/LocationLocation /b8081/a ProxyPass http://127.0.0.1:8083 ProxyPassReverse http://127.0.0.1:8083/Location/VirtualHost2.2 生效配置文件查看配置文件是否正常# apachectl -t Syntax OK重启apache配置systemctl restart httpd