Symfony 2 production environment server deploy

Symfony 2 logo

Symfony 2 logo

Symfony 2 is one of the most popular PHP frameworks and particularly my favorite. My fist time to deploy a project in production environment, I have struggled a lot. One of the most common errors you might get when you execute the prod environment is “401 Page Not found”. But, actually it is really simple to make a Sysfony 2 project work. you just have play a little bit around with it knowing what you want.

Note: This post is about how to move a bundle to production environment, if what you want to know is how to install Symfony (by meeting it’s requirements), you can go here. Also to instal Symfony in windows 8 PC with wamp server you can go here.

Note: Last update on 2014-04-09.

1. On a linux alike system Log to the project folder using the cd command

2. execute this 3 commands:

  • php app/console cache:clear --env=prod --no-debug 
  • php app/console assets:install web_directory 
  • php app/console assetic:dump web_directory

3. Promote the web server's user the owner of the cache folder, as root you should execute in Linux systems

chown www-data:www-data -R app/cache

4. Promote the web server’s user the owner of the logs folder, as root you should execute in Linux systems.

 chown www-data:www-data -R app/logs

In my case it was like this:

root@khs01wxl001:/var/www/Org# php app/console assets:install web
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
root@khs01wxl001:/var/www/Org# php app/console assets:install web
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
root@khs01wxl001:/var/www/Org# php app/console assetic:dump web
Dumping all dev assets.
Debug mode is on.

root@khs01wxl001:/var/www/Org# chown www-data:www-data -R app/cache
root@khs01wxl001:/var/www/Org# chown www-data:www-data -R app/logs
root@khs01wxl001:/var/www/Org# php app/console cache:clear --env=prod --no-debug
Clearing the cache for the prod environment with debug false
PHP Parse error:  syntax error, unexpected '$content' (T_VARIABLE) in /var/www/Org/src/Osd/RetireBundle/Controller/GeneralController.php on line 15
root@khs01wxl001:/var/www/Org# php app/console cache:clear --env=prod --no-debug
Clearing the cache for the prod environment with debug false
root@khs01wxl001:/var/www/Org# php app/console assets:install web
Installing assets using the hard copy option
Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
Installing assets for Acme\DemoBundle into web/bundles/acmedemo
Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
root@khs01wxl001:/var/www/Org# php app/console assetic:dump web
Dumping all dev assets.
Debug mode is on.

root@khs01wxl001:/var/www/Org# chown www-data:www-data -R app/logs
root@khs01wxl001:/var/www/Org# chown www-data:www-data -R app/cache
root@khs01wxl001:/var/www/Org#

 

Note: This update was done because of the question from César Chalén.

If you need also apache to take you directly to the default project page you just need to create a Virtual host pointing to Symfony 2’s internal web directory (if you need more detailed info about the apache virtual hosts you can see it here). Any way check out one of my virtual hosts:

<virtualhost *:80>
        ServerAdmin webmaster@localhost
        ServerName retirement.gabs.hospitals.moh.gov.bw
        DocumentRoot /var/www/Symfony/web

        <directory /var/www/Symfomy/www/web></directory>

                Options Indexes FollowSymLinks MultiViews
                AllowOverride all
                Order allow,deny
                allow from all

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</virtualhost>

And that is it, enjoy.

5 thoughts on “Symfony 2 production environment server deploy

  1. Pingback: Symfony2 small project deploy | Ask & Answers

  2. Hello:
    Check the logs from Symfony, and post what they are saying. Probably the logs will give you some clue. Once I had the same problem and even the logs were not saying any thing. then I checked the logs from Apache and there was an issue there.
    Regards

Leave a Reply

Your email address will not be published. Required fields are marked *