我只是尝试在服务器上部署我的应用程序,但我遇到了资产问题。
我将我的应用上传到子文件夹rapp
,所以我有/var/www/html/rapp
首先:我必须在webpackEncore上更改我的publicPath以包含/rapp/public
以便/rapp/public/build
只有/build
在dev中工作(使用php bin/console server:run
命令)
有没有办法将publicPath保留在公共文件夹中?为什么我必须指定子文件夹?
其次,第一次更改似乎我有一个很好的URL来获取我的资产http://domain/rapp/public/build/main.css
,但它为我的所有资产文件(js和css)返回404
我按照推荐的VHost(改为某些测试)
<VirtualHost *:80>
#ServerName domain.tld
#ServerAlias www.domain.tld
DocumentRoot /var/www/html/rapp/public
<Directory /var/www/html/rapp/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
编辑:在我的根应用程序文件夹和公共应用程序文件夹中添加.htaccess root app文件夹:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule !\.(js|gif|jpg|png|css|txt)$ public/index.php [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
和公共应用文件夹
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rapp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
这是我的webpack encore config
// webpack.config.js
var Encore = require('@symfony/webpack-encore');
Encore
// the project directory where all compiled assets will be stored
.setOutputPath('public/build/')
// the public path used by the web server to access the previous directory
.setPublicPath('/rapp/public/build')
// will create public/build/main.js and public/build/main.css
.addEntry('main', './assets/js/main.js')
//Add entry if other js/css needed. first parameter is the generated filename.
.addEntry('reader', './assets/js/reader.js')
//file upload with dropzone
.addEntry('dropzone', './assets/js/dropzone.js')
//Admin chapter js
.addEntry('admin-chapter', './assets/js/chapter.js')
// allow sass/scss files to be processed
.enableSassLoader()
// allow legacy applications to use $/jQuery as a global variable
.autoProvidejQuery()
.enableSourceMaps(!Encore.isProduction())
// empty the outputPath dir before each build
.cleanupOutputBeforeBuild()
// create hashed filenames (e.g. app.abc123.css)
.enableVersioning()
.createSharedEntry('vendor', [
'jquery',
])
.configureFilenames({
images: '[path][name].[hash:8].[ext]'
})
;
// export the final configuration
module.exports = Encore.getWebpackConfig();
(我使用dev选项启动了Encore构建,而不是生产)
创建manifest.json,创建所有带版本控制的文件,但由于文件请求没有版本控制,似乎找不到它。
我刚开始使用这个项目的webpack,所以我是一个完整的菜鸟,我不太了解apache / htaccess配置,所以也许我想念一些明显的...
如果我对某些事情不清楚,请告诉我这里已经很晚了,我再也不能正常思考^^“
编辑:我更改了我的vhost配置(在conf文件:/etc/apache2/sites-enabled/rapp.conf
上)和htaccess在我的app文件夹(root和public)上
我仍然没有工作。
答案 0 :(得分:0)
如Symfony Configuring a Web Server documentation中所述。您的Apache虚拟主机文件应该如下所示
<VirtualHost *:80>
#ServerName domain.tld
#this tells Apache to load http://domain.tld from the specified directory
DocumentRoot /var/www/html/rapp/public
#this sets the access rules of the directory
<Directory /var/www/html/rapp/public>
AllowOverride All
Order Allow,Deny
Allow from All
#for Apache 2.4 replace the Allow entries above with this line
#Require all granted
#this is what loads symfony at http://domain.tld/route
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
</Directory>
#this allows the use of bin/console assets:install --symlink
#which is executed when you run composer.phar install
#which should not be needed since you use webpack for asset management
#<Directory /var/www/html/rapp>
# Options +FollowSymlinks
#</Directory>
#this prevents routing from being executed on `http://domain.ltd/bundles/file.ext`
#resulting in a simple 404
<Directory /var/www/html/rapp/public/bundles>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
#this prevents routing from being executed on `http://domain.ltd/build/file.ext`
#resulting in a simple 404
<Directory /var/www/html/rapp/public/build>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
# optionally set the value of the environment variables used in the application
#SetEnv APP_ENV prod
#SetEnv APP_SECRET <app-secret-id>
#SetEnv DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
您的webpack配置应该是
// webpack.config.js
var Encore = require('@symfony/webpack-encore');
Encore
// the project directory where all compiled assets will be stored
.setOutputPath('public/build/')
// the public path used by the web server to access the previous directory
.setPublicPath('/build')
//...
;
然后删除您不需要的.htaccess
个文件。
处理新配置
然后重新启动Apache服务器以加载新的vhost配置。
然后删除您的/var/www/html/rapp/var/cache/prod
和/或/var/www/html/rapp/var/cache/dev
目录。
在服务器上打开终端并导航至/var/www/html/rapp
。
然后运行php composer.phar install --no-dev --optimize-autoloader
这也应该执行symfony cache:clear
和assets:intall
然后运行yarn build
,它会将您的资产处理到/var/www/html/rapp/public/build
目录。
但是我强烈建议从public/build
部署生产构建文件,而不是在生产服务器上安装node.js和webpack。正如Symfony Documentation on How do I deploy my Encore Assets?
最终结果:
http://domain.ltd
将从/var/www/html/rapp/public/index.php
http://domain.ltd/route
/var/www/html/rapp
输出文件http://domain.ltd/build/asset.ext
对于资产的webpack版本控制,请确保更新生产框架配置以加载manifest.json文件,如下所示:
#/var/www/html/rapp/config/packages/framework.yml
framework:
#...
assets:
json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'
#...