Magento 2 安装:css/js/图像未加载
-
12-12-2019 - |
题
我正在尝试在我的本地主机中安装 Magento2(0.1.0-alpha107 ),由 OSX 10.10 + brew 安装 php-fpm + mysql + nginx。
我安装时遵循的步骤:
mkdir /path/to/magento2 && cd /path/to/magento2
git clone git@github.com:magento/magento2.git .
composer install
cd setup
composer install
php -f index.php install --base_url=http://magento2alpha.dev/ --backend_frontname=admin --db_host=localhost --db_name=magento2alpha --db_user=root --db_pass=root --admin_firstname=Raj --admin_lastname=KB --admin_email=magepsycho@gmail.com --admin_username=admin --admin_password=pass123 --language=en_US --currency=USD --timezone=America/Chicago
到目前为止一切都很顺利。但是当你加载前端时: http://magento2alpha.dev/它仅显示纯文本(即css/images/js 丢失)。
查看源代码为您提供了类似的路径 http://magento2alpha.dev/pub/static/frontend/Magento/blank/en_US/[css/images]/[css/images file]导致404页面
我的 nginx conf 文件如下所示:
server {
listen 80;
server_name magento2alpha.dev;
root /Users/Raj/Sites/magento/magento2alpha;
location /setup {
try_files $uri $uri/ @setuphandler;
}
# Rewrite Setup's Internal Requests
location @setuphandler {
rewrite /setup /magento/magento2alpha/setup/index.php;
}
location / {
index index.php index.html;
try_files $uri $uri/ @handler;
}
# Rewrite Internal Requests
location @handler {
rewrite / /magento/magento2alpha/index.php;
}
# Rewrite magento2 static files
#location /pub/static {
# rewrite ^/pub/static/(.*)$ /magento/magento2alpha/pub/static.php?resource=$1? last;
#}
location /pub/static {
try_files $uri $uri/ @static;
}
location @static {
rewrite ^/pub/static/(.*)$ /magento/magento2alpha/pub/static.php?resource=$1? last;
}
#location ~ .php/ {
# rewrite ^(.*.php)/ $1 last;
#}
location ~ \.php$ { ## Execute PHP scripts
try_files $uri =404;
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 900s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
## Magento 2 Developer mode
fastcgi_param MAGE_MODE "developer";
}
}
我猜问题出在静态文件重写上。但这是我从 github 中得到的,但不起作用。有什么解决方法吗?
解决方案 2
最后,我必须修改我的nginx conf文件,使其工作为:
(参考: https://github.com/magento/magento2/issues/802 )
server {
listen 80;
server_name magento2alpha.dev;
root /Users/Raj/Sites/magento/magento2alpha;
location /setup {
try_files $uri $uri/ @setuphandler;
}
# Rewrite Setup's Internal Requests
location @setuphandler {
rewrite /setup /setup/index.php;
}
location / {
index index.php index.html;
try_files $uri $uri/ @handler;
}
# Rewrite Internal Requests
location @handler {
rewrite / /index.php;
}
location /pub/static {
try_files $uri $uri/ @static;
}
location @static {
rewrite ^/pub/static/(.*)$ /pub/static.php?resource=$1? last;
}
location ~ \.php$ { ## Execute PHP scripts
try_files $uri =404;
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 900s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
## Magento 2 Developer mode
fastcgi_param MAGE_MODE "developer";
fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=256M \n max_execution_time=18000";
}
}
.
我注意到的一件事是:如果在nginx的conf.d
中创建虚拟主机特定配置,则比保持在sites-enabled
dir中更多的资源昂贵和慢。
[编辑]
对不起我的坏,xdebug导致缓慢而不是nginx conf。
其他提示
内请求的任何文件 /pub/static
当前不存在的内容需要通过 Magento 进行路由。目前这是通过 /pub/static.php
.
你可以在中看到这个重写 /pub/static/.htaccess
RewriteEngine On
# Remove signature of the static files that is used to overcome the browser cache
RewriteRule ^version.+?/(.+)$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* ../static.php?resource=$0 [L]
查看您的 nginx 配置,您已将其注释掉:
# Rewrite magento2 static files
#location /pub/static {
# rewrite ^/pub/static/(.*)$ /magento/magento2alpha/pub/static.php?resource=$1? last;
#}
大概使这些行可执行可以解决问题,我对 nginx 配置不太熟悉,但类似这样 可能 还可以工作:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /../static.php?resource=$0 last;
}
}
取自: https://magento.stackexchange.com/a/64808/3326
当不在生产模式下时,Magento 2 将尝试为某些静态资源创建符号链接。您可以通过执行以下操作来更改该行为。
打开 app/etc/di.xml
并找到 virtualType name="developerMaterialization"
部分。在该部分中,您会找到一个项目 name="view_preprocessed"
需要修改或删除。您可以通过更改内容来修改它 Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink
到 Magento\Framework\App\View\Asset\MaterializationStrategy\Copy
删除下面的文件 pub/static
摆脱任何现有的符号链接。您可能要小心不要删除 .htaccess
文件。
这应该可以解决符号链接的错误。
这是迄今为止最好的答案
您可以通过更改内容来修改它
Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink
到Magento\Framework\App\View\Asset\MaterializationStrategy\Copy
在运行magento 2安装后,我有类似的问题,发现安装无法实际部署静态文件。我收到了404个用于许多管理文件,包括引导程序等。为了修复我从站点root运行命令以强制静态文件部署:
php bin/magento setup:static-content:deploy -f
.
这将重新静态资产为前端主题和adminhtml后端。