Deploy Symfony 4 App To Shared Hosting FTP
Deploy Symfony 4 App To Shared Hosting FTP
Note: that first command sets the symfony env to prod (windows), see notice on the doc here:
https://symfony.com/doc/current/deployment.html#c-install-update-your-vendors
And if you are using encore run this command:
node_modules\.bin\encore production
If you are deploying to an apache server run this command to create the recommended .htaccess
file:
composer require symfony/apache-pack
Its best to keep your public files separate from your logic / application files, this means uploading
your files to two separate locations on the server.
Upload your public folder contents to your public_html or www folder on your FTP sever.
If there is already a .htaccess file on your server — its best to merge your file with the one on the
server as it might contain server config thats needed (ie PHP version to use).
Upload the following folders (and contents) from your application to a new folder in your root of
your FTP server named symfony:
bin
config
src
templates
translations
vendor
Note: Only upload the bin folder if you want to run the console, maybe via a cron job, also best to
rename console to console.php
Also create the var folder but dont upload its contents.
And upload your composer.json file to this symfony folder — this is needed to help symfony find
its application files.
So you should have this on your server:
\symfony
bin
config
src
templates
translations
vendor
composer.json\public_html
index.php
.htaccess
bundles
build
Note: the contents of the public_html will depend on your application, you might have more folders
eg img, js, css etc. The bundles folder is needed if you have used any bundles that have front end (js
/ css) files. The build folder contains your built js and css files that are created by encore.
To:
require __DIR__.’/../symfony/vendor/autoload.php’;
This says where your application files are located relative to the index.php file.
Now you need to set your environment variables (ie database url etc), locally these are usually in
your .env file.
Using your .env file on the server is not recommended so we need to set environment variables
another way.
You can do this either inside a .htaccess file, your index.php, or in your parameters.yaml config file.
.htaccess
You can set environment variables in a .htaccess file like this (if you are running apache):
SetEnv APP_ENV prod
SetEnv DATABASE_URL 'mysql://user:[email protected]:3306/dbname'
index.php
Some hosts might have disabled doing the above so you can add your environment variables to your
index.php (or another .php file that you include inside your index.php file) file like:
$_SERVER[‘APP_ENV’]=’prod’;
$_SERVER['DATABASE_URL']='mysql://user:[email protected]:3306/dbname'
Note: DATABASE_URL is the new convention for symfony 4 apps, the password needs to be url
encoded.
parameters.yaml
The third option is to store your environment variables in a parameters.yaml config file (stored
under config/packages).
parameters:
APP_ENV: "prod"
env(DATABASE_URL): "mysql://user:[email protected]:3306/dbname"
encore configuration
If you have used encore for your js and css, you also need to tell symfony where your manifest.json
is relative to your logic files.
So edit or create: config\packages\prod\framework.yaml file and add:
framework:
assets:
json_manifest_path:
'%kernel.root_dir%/../../public_html/build/manifest.json'
(Note: due to formatting the last line is split but in your config file the json_manifest_path and its
value should be on the same line)
Before running this command copy your database from your server to your local database server.
Run the above command and then execute the generated SQL on your server using phpmyadmin.
You can use the above command to generate the required SQL to create your whole database too.
Upload it and load it in your browser — this tells you what php version is running.
Otherwise check your server logs files:
Check any generated error_log file in your public_html folder, or use cPanel or what ever you can
use to see server errors.
Or check your symfony log: symfony/var/log/prod.log
Then try Google any error you find in them.
Check these instruction for anything else you may want to do:
https://symfony.com/doc/current/deployment.html
Good luck! :) Please share your experiences.