Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packing and deploying Symfony web site

I'm new to Symfony, coming from .NET world. Using Symfony (4) documentation, I managed to create simple web site. Now I want to put it to live, but I'm struggling to find any useful information what should I do in order to "pack" everything necessary and deploy it. Indeed, there's page describing deployment (How to Deploy a Symfony Application), but I find it lacking information about:

  • what to include/exclude (obviously I don't want to pack dev dependencies, and deploying composer files also doesn't seems to make any sense)
  • what to change (there's .env file - containing APP_ENV and APP_SECRET - where do I use those values?)
  • my hosting uses folder www for public presentation, do I have to change/configure something before renaming public directory just to www?
  • do I have to configure .htaccess to not route images/css/js trough PHP?

My current project structure is:

+ bin
+ config
+ public
  + css
  - index.php
+ src
  + Controller
  - Kernel.php
+ templates
+ var
+ vendor
- .env
- .gitignore
- composer.json
- composer.lock
- symfony.lock

Edit (2018-07-17):

  • I'm using git
  • hosting is capable of deploying from git branch called production (whenever I push to this branch, it calls composer install --no-dev)
  • Configuring public directory name is done in composer.json

Example of extra configuration in composer.json:

"extra": {
    "symfony": {
        "allow-contrib": false
    },
    "public-dir": "www"
}

Regarding my original question - I'm now using hosting capability of deployment using git. In that case, I do need composer files as well. My original thought was to build and pack bare minimum of things and then deploy this package to server. (Now I still have bin, composer files or .gitignore (and probably even more odd things) deployed as well).

like image 484
Zdeněk Avatar asked Jul 05 '18 15:07

Zdeněk


1 Answers

Well, I will take a shot.

what to include/exclude (obviously I don't want to pack dev dependencies, and deploying composer files also doesn't seems to make any sense)

I notice you don't have a .gitignore. If you are not using GIT (you should) take a look at the default gitignore, it will tell you which folders you don't need.

You are right, vendors are not necessary. Composer's lock file contains the exact versions you are using. Just do composer install once you copied the files to the server. The simplest way of "deployment" is to use git(hub), and set up an ssh key on your server, and grant it read access to your git repo. (Deploy key)

what to change (there's .env file - containing APP_ENV and APP_SECRET - where do I use those values?)

The .env file only works in dev mode by default. (notice the require-dev part in your composer.json).

Symfony recommend using "real" environmental variables in production, but you could use the .env file. To do this you will have to move "symfony/dotenv" from require-dev to require in composer.json. (Do an update after)

You will also want to set APP_ENV to prod on your server, configure db acccess and mailer too.

my hosting uses folder www for public presentation, do I have to change/configure something before renaming public directory just to www?

I won't answer this fully, that would take too long. Configure apache to point to your public dir using a VirtualHost. More here: https://symfony.com/doc/current/setup/web_server_configuration.html

do I have to configure .htaccess to not route images/css/js trough PHP?

If you have installed the apache pack, you will get a .htaccess file, and that ignores files.

# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

This way requested files won't hit your PHP code. If however a file does not exist, Symfony will process the request. I recommend ignoring common file extensions eg:

# Do not allow image requests to hit symfony
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png|ico|map)$ [NC]
RewriteRule .* - [L]
like image 94
Padam87 Avatar answered Sep 28 '22 05:09

Padam87