Steps for going live - Symfony2 - php

I've been working on my website for some time now and I am now ready to push this brand new project to live.
I have already already read this link on Symfony2 with regards to what should be done, but I still think it is missing some details. For example, it talks nothing about some of the commands that I run to get the site to work on live.
Currently this is what I would like to do to get this to work on live:
php composer.phar install --no-dev --optimize-autoloader --no-scripts
rm -rf app/cache/*
php app/console assetic:dump --env=prod --no-debug
php app/console assets:install web --symlink --env=prod
Is there anything else that needs to be done, for example, I don't want live to be able to execute my fixtures, nor do I want any users to be able to go to www.mysite.com/app_dev.php

Check out How to deploy a Symfony2 application.
Refer to link above. The typical steps taken while deploying a Symfony2 application include:
Upload your modified code to the live server;
Update your vendor dependencies (typically done via Composer, and may be done before uploading);
Running database migrations or similar tasks to update any changed data structures;
Clearing (and perhaps more importantly, warming up) your cache.
Common Post-Deployment Tasks
Configure your app/config/parameters.yml file
Update your vendors
$ php composer.phar install --no-dev --optimize-autoloader
Clear your Symfony cache
$ php app/console cache:clear --env=prod --no-debug
Dump your Assetic assets
$ php app/console assetic:dump --env=prod --no-debug
Other things!
There may be lots of other things that you need to do, depending on your setup:
Running any database migrations
Clearing your APC cache
Running assets:install (taken care of already in composer.phar install)
Add/edit CRON jobs
Pushing assets to a CDN
...

take a look at capifony (https://github.com/everzet/capifony). Unfortunately the Website (http://capifony.org) is currently down..

The best solution to me is to have your project in a versionning system like git or svn without the vendors dir of course ...
This way, your simply have to do :
1) git clone your project into the prod dir
2) php composer.phar install to install your vendors
2b) create the mysql user with correct login and password according to your parameters.yml
3) php app/console doctrine:database:create to create your database with the credentials you set up in mysql
4) php app/console doctrine:schema:update --force to perform the database tables creation
5) testing the project :)
If you are not using a versionning system just upload your project to your server with an ftp software without the vendors directory ( it will be feeded by step 2) then perform 3rd , 4th and 5th steps !
you now should be able to reach you project through you domain url .

Related

What would a Laravel deploy script look like?

I'm using Buddy Works to deploy my project to a server.
When creating a pipeline to deploy my Laravel project, I'm asked to enter deployment actions; this is where I'm stuck.
So far, I have set it to run the following composer commands:
composer validate
composer install
Because this is usually what I do when running my project locally. But I understand that I also need to run Apache and such (I use WAMP so it sorts all that for me).
I've searched on Google and all I got was composer install. Is this all I need to do or is there more?
Prior to using deployment tools, I used to upload files directly to the server, so CI/CD or other DevOps duties are very new to me.
It's good idea to reload php as well as migrate new tables :)
composer install --no-interaction --prefer-dist --optimize-autoloader
echo "" | sudo -S service php7.3-fpm reload
php artisan migrate --force
If you're using DigitalOcean and you have $12 to spare you could use ready tools such as Laravel Forge :)

docker and php: getting dependencies (composer) into the container

A little context: I am new to docker and dont know of any best-practices yet. My task is to create a webservice based on php and i decided to use docker-compose for provisioning.
Because we are all fancy devs, we know - there is no way to build a php application without using composer these days.
My question:
Should i install dependencies in the build? So by adding this to my Dockerfile:
RUN cd /app && composer install --no-interaction --prefer-source --optimize-autoloader
Or should i install dependencies during development and build the container image with included dependencies?
I only know of one way NOT to do it: install dependencies locally on the dev machine and build the container afterwards. But how would the "best practice" look like?
And - because i am a newbee in this field - how would i run a "composer require some/package" for my app service container?
By the way
I also noticed a message "Do not run Composer as root/super user!" when building the container. I added COMPOSER_ALLOW_SUPERUSER=1 to my Env file (as seen here), but this message still appears.
Is it possible to NOT execute composer install as root in a docker container? Can i ignore that message?
Thanks in advance,
Philipp
For the moment i am working towards the following solutuion:
Development environment:
Add the whole src as volume:
volumes:
- .:/app
initial composer install command
docker-compose exec app composer install
install new composer package:
docker-compose exec app composer require some/package
The package will be installed via the container in your composer package directory (/vendor by default of course), the source code can be inspected in your ide, etc.
Production environment:
Only app state defined as volume, for example:
volumes:
- public/uploads:/app/public/uploads
add /vendor to .dockerignore
and run composer install during build
RUN cd /app && composer install --no-dev --no-interaction --optimize-autoloader
So you will have a pre-built image to deploy, but still be able to develop in a more agile way.
There are still two downsides in this solution, maybe someone has input for me:
1) build becomes kind of slow - it seems composer caching does not work properly
A solution (i dont know if this is possible in composer) could be a volume for the composer cache directory.
2) all packages created by docker are owned by root, so you can only work on those files/folders with sudo/root session.
I have no idea how to fix this properly. Maybe i could create a user with the name of the dev user for the development container and have this user run commands and php-fpm, etc... For production root would be okay i guess.
I am still very new to docker and open for better solutions, so i wont accept this answer.
UPDATED
To add new user in docker user in Dockerfile. as follows
RUN useradd -ms /bin/bash newuser
USER newuser
if you want to create home user too
WORKDIR /home/newuser
or
mkdir /home/newuser
if you want to back to root user, you can do like
USER root
two-sides-of-a-coin -
It is a good practice to do on building images period which I also do the same as you.
we will get latest update from public repositories but another side of coin is that we cannot ensure they are the same version as we develop.
you need to handle this by specifying exact version in composer.json (do not use ^, *).

Symfony Composer install without clearing cache

We are using a different cache dir to the default one and are using build agents for deployment. We run the composer install on the build agent where the cache dir doesn't exist, and then rsync it over to the web servers where we then run the command to clear and warm up the cache (all done from a Bamboo deployment).
Of course an error comes back in the logs as it can't create the cache dir when the composer install runs on the agent (and we don't want it to as we do this after).
Is there any way that when I run the composer install I can get it to skip the cache clear? I can't see it as a parameter option for composer.
All you need is to remove following line:
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
from post-install-cmd and/or post-update-cmd section in your composer.json file.
Assuming you're build agent is doing install, and not update, it may be useful, to keep this line in post-update-cmd section for development work.
As much as others say it doesn't, Sensio\Bundle\DistributionBundle\Composer\ScriptHandler‌​::installAssets will do a cache clear if required which will cause issues if you can not write to cache (I do this once rsynced from build agent onto live server).
The only way I can find at the moment to get around this is to remove the 2 lines from composer.json:
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
and then once you are on the server that has your cache dir available, run the following 2 commands (by this stage you should have already done your composer install/update):
php bin/console cache:clear --env=prod --no-debug
php bin/console assets:install --env=prod
The assets:install may or may not be needed depending on the vendor bundles installed. Anything that uses the "public" folder to store resources will need this so that the symbolic links are set up correctly for /web/bundles/bundleName. I also do an assetic dump after this.
By doing this you can now do the long parts (git clone and composer install) on a separate server or in a different directory and then rsync it across to where you want it to go. Then you just refresh and warm up the cache and do the final config of assets/assetic (or anything you are doing that requires cache) with minimal down time or any issues of your site going down if something goes wrong half way through deployment.

Symfony2.7.1 fresh installation. Failed to load resource

Symfony2 developers:
Recently, I am planning to get familiar with Symfony2, using it as api project. Current version is v2.7.1.
I already have apache installed as my local environment. So the installation root directory is:
//localhost/symfony
So I followed official guide to install it. Everything looks fine after installation. But when I view the page in development environment (app_dev.php).
//localhost/symfony/web/app_dev.php
Page content display as expected, but not styled. Then I open console and find some 404 response as follows:
Failed to load resource
//localhost/symfony/web/bundles/framework/css/structure.css
//localhost/symfony/web/bundles/framework/css/body.css
//localhost/symfony/web/bundles/framework/css/exception.css
//localhost/symfony/web/app_dev.php
//localhost/symfony/web/bundles/framework/css/structure.css
//localhost/symfony/web/bundles/framework/css/body.css
//localhost/symfony/web/bundles/framework/css/exception.css
I looked into project/web/bundles directory, found nothing inside but two empty file.
framework and sensiodistribution.
Fresh installation without any config change. I got this problem. Could you guys give some explanation why it occurs. Otherwise I didn't get the full package?
Do I miss those files inside project/web/bundles?
If the case in which i missed those file. So could you send me an archive file on current version 2.7.1?
You must run the command app/console assets:install to generate symlinks (or files) in /web/bundles
If these files exists then you probably have problem with URL Rewriting check your .htaccess or configure your host. You can also use command php app/console server:start and check there if it works
You can run composer install to install all necessary bundles and libraries and probably composer.json has defined postinstall commands.
If you use assetic then you can try to run php app/console assetic:dump more on http://symfony.com/doc/current/cookbook/assetic/asset_management.html
In Symfony 2.6 you have new asset manager read more on http://symfony.com/blog/new-in-symfony-2-6-smarter-assets-install-command
php app/console assets:install or php app/console assets:install --symlink
for Symfony 3.x the directory changed from app to bin so commands are:
php bin/console assets:install or php bin/console assets:install --symlink
Since I landed here from a Symfony 3 search this might be useful for Symfony 3 users having the same problem
Run php bin/console assets:install at the project root

2-step build and deployment of Symfony application

We are using Atlassian Bamboo to deploy our web applications to testing and production servers. This is a two-step process.
build and test the release
deploy the release to the environment
This run relatively stable, but we are running into some issues with Symfony 2 projects.
Step 1 simply checks out the most recent version from the app from Git, does some tests and other tasks, including composer:install. This last one will execute some scripts (post-install): buildBootstrap, clearCache, installAssets, installRequirementsFile and removeSymfonyStandardFiles.
This step is executed on the build server. Since the parameters.yml file is not present in Git, composer install fails. If we do a composer install --no-scripts, the build succeeds as these scripts are never called.
Step 2 is to ship the files to the production server, install the parameters.yml (which is copied from a predefined location on the target server), do a app/console cache:clear and app/console assets:install. The release appears to be working just fine on the target server, but the buildBootstrap, installRequirementsFile and removeSymfonyStandardFiles scripts or equivalent have not run. What are the consequences of that? Are there any app/console alternatives for them (running app/console doesn't appear to show any)?
Alternatively, are we just doing it wrong? We want to let as much work be done by the build server, as the target servers are often limited in capabilities (eg. shared hosting).
Build
checkout from git
copy parameters for test purposes
do a composer install (not an update)
run phpunit for testing (using PHPUnit as task type)
After test if all is good an Artifact will be created, so add some tasks to remove all libraries:
rm -rf vendor
rm -rf app/cache
The build ends here.
Deploy
Download the artifact
I'm deploying with Capistrano/Capifony, so here I run it.
For your question about composer scripts don't worry if they doesn't runs, you don't need it if vendor list doesn't change. My advise in any case is to have small artifact which doesn't include libraries, and install libs with composer. If you want to do heavy work on deploy server (and not in production) you can configure Capifony with set deploy_via, :copy.

Categories