为本地apache启用cloudflare的15年证书,并启用https

为本地apache启用cloudflare的15年证书,并启用https

前置环境:ubuntu,在Ubuntu上安装apache与php环境 – 奇奇怪怪的小窝 已经按照前置教程配置好了apache

申请15年证书

cf添加域名后点击左侧的ssl/tls,点击源服务器

点击右侧的创建证书

保持这个选项,点击创建

可以参照这个

出现

这张图片的仅为示例

将源证书保存为fullchain.pem,私钥保存为privkey.pem。(此处在那个框上上点一下就能复制),保存这两个文件。

点击确定

将刚才保存到两个文件放在本地/opt/certs/下。

点击cf的ssl的概述

点击配置

点击严格,再点击保存即可。

现在配置本地apache

新建/etc/apache2/sites-available/org-ssl.conf文件

sudo nano /etc/apache2/sites-available/org-ssl.conf

注意:下述出现的.org替换为自己的域名

<VirtualHost *:443>
    ServerName .org
    ServerAlias www..org
    DocumentRoot /var/www/.org

    SSLEngine on
    SSLCertificateFile /opt/certs/fullchain.pem
    SSLCertificateKeyFile /opt/certs/privkey.pem

    ErrorLog ${APACHE_LOG_DIR}/org_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/org_ssl_access.log combined
</VirtualHost>

<Directory /var/www/.org>
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

ctrl+x,再次按y保存

依次执行下述命令

启用ssl模块
sudo a2enmod ssl
启用https虚拟主机
sudo a2ensite org-ssl.conf
禁用default-ssl.conf
sudo a2dissite default-ssl.conf
重启apache
sudo systemctl restart apache2

重启apache

sudo systemctl restart apache2

完成。这个时候浏览器打开你的打开网址就可以访问了。

注意:此处也可以使用添加了针对cf优化的ssl配置

<VirtualHost *:443>
    ServerName .org
    ServerAlias www..org
    DocumentRoot /var/www/.org

    SSLEngine on
    SSLCertificateFile /opt/certs/fullchain.pem
    SSLCertificateKeyFile /opt/certs/privkey.pem

    # -------------------------
    # 1. 启用 HTTP/2(性能提升明显)
    # -------------------------
    Protocols h2 http/1.1

    # -------------------------
    # 2. 安全头(强烈推荐)
    # -------------------------
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always set X-XSS-Protection "1; mode=block"

    # -------------------------
    # 3. 性能优化(压缩 + 缓存)
    # -------------------------

    # 启用 gzip 压缩(Apache 默认支持)
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json application/xml

    # 如果系统支持 Brotli(可选)
    <IfModule mod_brotli.c>
        AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/javascript application/javascript application/json
    </IfModule>

    # 静态资源缓存(可选但推荐)
    <FilesMatch "\.(jpg|jpeg|png|gif|svg|js|css|ico|webp)$">
        Header set Cache-Control "public, max-age=2592000"
    </FilesMatch>

    # -------------------------
    # 4. Cloudflare 真实 IP 支持
    # -------------------------
    RemoteIPHeader CF-Connecting-IP
    RemoteIPTrustedProxy 103.21.244.0/22
    RemoteIPTrustedProxy 103.22.200.0/22
    RemoteIPTrustedProxy 103.31.4.0/22
    RemoteIPTrustedProxy 104.16.0.0/13
    RemoteIPTrustedProxy 104.24.0.0/14
    RemoteIPTrustedProxy 108.162.192.0/18
    RemoteIPTrustedProxy 131.0.72.0/22
    RemoteIPTrustedProxy 141.101.64.0/18
    RemoteIPTrustedProxy 162.158.0.0/15
    RemoteIPTrustedProxy 172.64.0.0/13
    RemoteIPTrustedProxy 173.245.48.0/20
    RemoteIPTrustedProxy 188.114.96.0/20
    RemoteIPTrustedProxy 190.93.240.0/20
    RemoteIPTrustedProxy 197.234.240.0/22
    RemoteIPTrustedProxy 198.41.128.0/17

    ErrorLog ${APACHE_LOG_DIR}/org_ssl_error.log
    CustomLog ${APACHE_LOG_DIR}/org_ssl_access.log combined
</VirtualHost>

<Directory /var/www/.org>
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

保存后依次执行

启用模块
sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod deflate
sudo a2enmod brotli   # 如果系统支持
sudo a2enmod http2
sudo a2enmod remoteip

重启apache

sudo systemctl restart apache2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注