在默认配置中,October CMS 目录位于 Web 访问的根目录。为了在生产环境中实现极致安全性,您应该配置 Web 服务器使用公共文件夹以确保只有特定目录中的文件可以被访问。
首先你需要使用 october:mirror 命令生成一个公共文件夹:
php artisan october:mirror它将在项目的根目录中创建一个名为 public 的新目录。在该目录中,该命令会创建符号链接,指向项目中存在的所有插件、模块和主题的资产和资源目录。
在 Apache 中,服务器和虚拟主机的文档位置由
DocumentRoot指令管理。
Web 服务器配置必须更新,以指向 public 目录,而不是项目的根目录。
In Windows operating systems, the
october:mirrorcommand can only be executed in a console running as administrator.
应在每次系统更新或安装新插件后执行 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 设置可用于强制在所有地方使用 secure HTTPS 链接。
LINK_POLICY=secure你还可以 强制 应用程序 URL 严格用于每个链接,它在 app.url 配置中定义。
LINK_POLICY=force安全模式是一种额外的保护级别,通过禁用编辑器中的PHP代码部分来防止运行任意PHP代码。安全模式还将启用安全的Twig环境,该环境会限制不安全的方法调用。
cms.safe_mode 参数可以在 config/cms.php 文件中找到。默认情况下,该值从 CMS_SAFE_MODE 环境变量加载。安全模式禁用 PHP 代码段 在 CMS 模板中。
参数可以取以下值之一:
true - 安全模式已启用false - 安全模式已禁用null - 安全模式处于活动状态 如果调试模式被禁用。如果你计划在生产环境中使用安全模式,你也应该在开发环境启用它,以检查你的主题是否能与安全的 Twig 环境协同工作。你可能需要修改插件,以允许使用 October\Contracts\Twig\CallsAnyMethod 和 October\Contracts\Twig\CallsMethods 接口调用方法。
或者,您可以更改为更宽松的 Twig 策略,使用 cms.security_policy_v1 配置值,该策略会转而阻止不安全的方法。
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>:::