在默认配置中,October CMS 目录位于 Web 访问的根目录。为了在生产环境中实现极致安全,您应该配置 Web 服务器以使用公共文件夹,以确保只能访问特定目录中的文件。
首先你需要生成一个公共文件夹,使用 october:mirror 命令:
php artisan october:mirror它将在项目的根目录中创建一个名为 public 的新目录。在该目录内,该命令会为项目中存在的所有插件、模块和主题创建指向资产和资源目录的符号链接。
在 Apache 中,服务器和虚拟主机的文档位置由
DocumentRoot指令管理。
Web 服务器配置必须更新以指向公共目录而不是项目的根目录.
在 Windows 操作系统中,
october:mirror命令只能在以管理员身份运行的控制台中执行。
october:mirror 命令应在每次系统更新后或安装新插件时执行。您可以指示 October CMS 在每次使用 Composer 更新项目后运行该命令。自动镜像功能通过 system.auto_mirror_public 配置参数进行管理。
本节描述了旨在提高应用程序性能的步骤,并建议所有生产环境都采用,因为它将显著改善页面加载时间。
在配置中,禁用调试模式并启用缓存层。例如,如果你正在使用.env 文件:
APP_DEBUG=false
CMS_ROUTE_CACHE=true
CMS_ASSET_CACHE=true
CMS_TWIG_CACHE=true在控制台中,使用以下命令缓存系统结构:
php artisan october:optimize
composer dump-autoload --optimize在共享主机环境中,必须采取额外步骤来保护您的项目文件免受与您共享服务器的其他用户的影响。
请咨询您的托管服务提供商,以获取合适的权限掩码。一般规则是,应用程序文件不得被其他用户访问。所有文件必须能够被所有者用户和 Web 服务器访问和管理。配置文件必须能够被所有者用户和 Web 服务器访问,但 Web 服务器不得更改它们。
October CMS 可以自动为新文件和目录设置权限。默认权限由 system.default_mask.file 和 system.default_mask.folder 配置参数管理。例如,如果您正在使用 .env 文件声明环境变量:
DEFAULT_FILE_MASK=644
DEFAULT_FOLDER_MASK=755当使用反向代理(例如 CloudFlare)时,主机服务器可能在内部使用不安全的协议,且 October CMS 在生成链接时会反映这一点。这可能导致响应中生成 http:// 和 https:// 的混合链接。 system.link_policy 设置可用于在所有地方强制使用 安全 的 HTTPS 链接。
LINK_POLICY=secure你也可以 强制 应用程序 URL 严格用于每个链接,这在 app.url 配置中定义。
LINK_POLICY=force安全模式是一种额外的保护层,通过禁用编辑器中的 PHP 代码部分来阻止运行任意 PHP 代码。安全模式还会启用一个安全的 Twig 环境,该环境会限制不安全的方法调用。
cms.safe_mode 参数可以在 config/cms.php 文件中找到。默认情况下,该值从 CMS_SAFE_MODE 环境变量加载。安全模式禁用 PHP 代码段 在 CMS 模板中。
该参数可以取以下值之一:
真 - 安全模式已启用false - 安全模式已禁用null - 安全模式处于活跃状态,如果调试模式被禁用。如果你计划在生产环境中使用安全模式,你也应该在开发环境启用它以检查你的主题是否适用于安全的 Twig 环境。你可能需要修改插件以允许使用 October\Contracts\Twig\CallsAnyMethod 和 October\Contracts\Twig\CallsMethods 接口调用方法。
或者,你可以更改为使用 cms.security_policy_v1 配置值的更宽松的 Twig 策略,该策略转而阻止不安全方法。
CMS_SECURITY_POLICY_V1=true本节描述了各种 Web 服务器的配置。
::: details Apache
为了运行 October CMS 应用程序,Apache 服务器必须具有以下配置:
All 值。在某些情况下,您可能需要取消注释项目 .htaccess 文件中的 RewriteBase 指令:
# RewriteBase /如果您已将 October CMS 安装到子目录中,请将其名称添加到指令值中。这样,您就可以拥有类似于 example.tld/subdirectory/page 的 URL。
# RewriteBase /subdirectory/:::
::: details Nginx
在 Nginx 站点配置的 server 部分中使用以下代码。如果您已将 October CMS 安装到子目录中,请将 location 指令中的第一个 / 替换为子目录名称。
location / {
# Let October CMS handle everything by default.
# The path not resolved by October CMS router will return October CMS's 404 page.
# Everything that does not match with the allowlist below will fall into this.
rewrite ^/.*$ /index.php last;
}
# Pass the PHP scripts to FastCGI server
location ~ ^/index.php {
# Write your FPM configuration here
}
# Allowlist
location ~ ^/(favicon\.ico|sitemap\.xml|robots\.txt|humans\.txt) { try_files $uri /index.php; }
# Block all .dotfiles except well-known
location ~ /\.(?!well-known).* { deny all; }
## Static Files
location ~ ^/storage/app/(uploads/public|media|resources) { try_files $uri 404; }
location ~ ^/storage/temp/public { try_files $uri 404; }
location ~ ^/modules/.*/(assets|resources) { try_files $uri 404; }
location ~ ^/modules/.*/(behaviors|widgets|formwidgets|reportwidgets)/.*/(assets|resources) { try_files $uri 404; }
location ~ ^/plugins/.*/.*/(assets|resources) { try_files $uri 404; }
location ~ ^/plugins/.*/.*/(behaviors|reportwidgets|formwidgets|widgets)/.*/(assets|resources) { try_files $uri 404; }
location ~ ^/themes/.*/(?:assets|resources) { try_files $uri 404; }:::
::: details Lighttpd
将以下代码粘贴到 Lighttpd 站点配置文件中,并更改 host address 和 server.document-root 以匹配您的项目位置。
$HTTP["host"] =~ "domain.example.tld" {
server.document-root = "/var/www/example/"
url.rewrite-once = (
"^/(plugins|modules/(system|backend|cms))/(([\w-]+/)+|/|)assets/([\w-]+/)+[-\w^&'@{}[\],$=!#().%+~/ ]+\.(jpg|jpeg|gif|png|svg|swf|avi|mpg|mpeg|mp3|flv|ico|css|js|woff|ttf)(\?.*|)$" => "$0",
"^/(system|themes/[\w-]+)/assets/([\w-]+/)+[-\w^&'@{}[\],$=!#().%+~/ ]+\.(jpg|jpeg|gif|png|svg|swf|avi|mpg|mpeg|mp3|flv|ico|css|js|woff|ttf)(\?.*|)$" => "$0",
"^/storage/app/uploads/public/[\w-]+/.*$" => "$0",
"^/storage/app/media/.*$" => "$0",
"^/storage/app/resources/.*$" => "$0",
"^/storage/temp/public/[\w-]+/.*$" => "$0",
"^/(favicon\.ico)$" => "$0",
"(.*)" => "/index.php$1"
)
}:::
:::details 微软 IIS
在 web.config 文件中使用以下配置,以在 IIS 上运行 October CMS:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="October CMS to handle all non-allowlisted URLs" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/.well-known/*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/storage/app/uploads/public/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/storage/app/media/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/storage/app/resources/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/storage/temp/public/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/themes/.*/(assets|resources)/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/plugins/.*/(assets|resources)/.*" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" pattern="^/modules/.*/(assets|resources)/.*" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>:::