Deploying SF2 Projects on Plesk VPS.
Having recently setup the new codeconsortium.com website i thought i would share some of the steps i took to get things working as it was not all smooth sailing. First off i just to clarify the platform i am using, it is a linux setup utilising Plesk and a typical LAMP setup with that.
My project written in Symfony2 and all custom code (minus FOSUserBundle), did not get off to a great start on deployment, so for other people deploying their web apps on a VPS running plesk i will share the steps i took to get things working.
First you need to obviously create your account and you can do that under Plesk control panel, this is a relatively simple step and as we are more concerned about the more complex arena of what we need to do on the command line i shall leave the creating of the account under Plesk to the documentation that it ships with.
Once you have your account setup and you have your SSH credentials handy we can begin. Firstly, download the Symfony2 framework from their site preferably WIHOUT vendors, this will greatly reduce the transfer time of things if you are going to just FTP them onto your server, but you could use git to do this also.
If you don’t have git i suggest using yum to install git. You can do this with:
$ yum install git
Once git is installed you can download SF2 that way, otherwise just FTP SF2 onto the server, preferably inside your httpdocs directory.
The next steps we need to take is to resolve some issues with the default setup of vhost.conf, which should be located in your domains conf/vhost.conf. Once you are in your domain dir (something like /var/www/vhosts/YOUR-DOMAIN.COM/) you should find the file which you can open with the vi editor like so:
$ vi vhost.conf
Then we need to make some additions, which i shall explain after this snippet, you need to edit your vhost.conf file to look like this (codeconsortium.com is my own domain, substitute this with your own accordingly.):
php_admin_flag engine on php_admin_flag safe_mode off php_admin_value open_basedir /var/www/vhosts/codeconsortium.com/:/tmp: Options +FollowSymLinks AllowOverride AuthConfig FileInfo Order deny,allow Allow from allDocumentRoot /var/www/vhosts/codeconsortium.com/httpdocs/symfony/web
A few notes on the above snippet now, firstly, the most common issue people have is with open_basedir, which has a tendency to create permission errors with scripts, usually resulting in a blank screen with no error output. The other issue we resolve is to allow scripts and resources to be accessible through symlinks (particularly useful for assets where it is more efficient to symlink your bundles assets than copy them, particularly when you update your bundles later on). Then the section after regarding AllowOverride etc, opens up some of the remaining permissions issues. The last part of this vhost.conf allows us to use thw web folder as our actual web root directory, thusly protecting the rest of your source code (particularly your configs containing database passwords etc) from outside snooping. Once this is done, just restart apache like so:
$ service httpd restart
Now that your vhost.conf configuration is out of the way, you will need to upload your project to your symfony/src/ directory on your server. You can do this either through FTP or the SCP utility on the command line, its up to you. I personally prefer using Cyberduck FTP client because you can add all manner of file types to exclude, namely large revision control files such as .git files etc. Excluding such files can cut your upload time in half in some projects with very large repositories.
Once your project is uploaded successfully, we should run the vendors install script, which you can do via:
$ php bin/vendors install
This will install all the vendors that you did not download from the SF2 website. It makes much more sense to use the power of your VPS’s large bandwidth to download vendors than to download them to your local work machine and then use what is usually a very poor upload bandwidth of your ISP to get it all onto the server.
Before we run the check.php utility we will want to set the proper permissions on both the cache and log directories, so from your symfony/app directory do the following:
$ chmod 777 cache
$ chmod 777 logs
This is necessary so that symfony can write to the cache and logs directories. Once that is done, we must now run the check utility from the command line also, this helps identify any issues with your setup which will likely be a few on there. To run this type:
$ php app/check.php
Following the issues highlighted modify your php.ini accordingly, usually you will do this by opening it in vi, and your default php.ini is usually in /etc/php.ini but we don’t want to edit this one, we want to edit your local php.ini which will overwrite the default one. If you choose to edit the default one found in /etc/php.ini then any changes made will effect all users on that server, depending on your root level of access on your VPS you may not actually be able to edit this anyway.
To edit your local php.ini file using vi again, edit:
$ vi /var/www/vhosts/YOUR-DOMAIN.COM/etc/php.ini
OR if you do not have root access it will just be
$ vi etc/php.ini
And make all the according changes as per the issues the check.php script mentioned. Then we restart apache again:
$ service httpd restart
Just to be sure that we got everything right, we can test that our settings are the right ones being used by creating a little test script in our httpdocs/symfony/web directory, create a file called phpinfo.php and edit it to look like the following:
$ vi phpinfo.php
Once all of those issues are resolved, we now want to make sure that there are no funky ‘.htaccess’ files in your root dir that might interfere with your project, so cd into your domains httpdocs directory and locate the default .htaccess directory, and delete it, if you don’t find one in there then that is good also. DO NOT remove the .htaccess file found in symfony/web you need this.
Now go to your browser and under your domain run your script, e.g; www.your-domain.com/phpinfo.php. This should output a really nice looking page with all the configs of php. Check for all the appropriate settings and check all the php.ini includes you see on the page are correct. Hopefully the chosen php.ini should have its configs overwriting the default ones, if not see what php.ini file your setup is favouring and edit that accordingly.
Now we should go back to our symfony directory and setup our entities and database stuff. Go to your symfony/app/config directory and edit your parameters.ini file so that you have input all your database credentials. If you don’t have any database credentials then you need to go into your Plesk control panel and create a new database and get your login credentials from there.
$ vi parameters.ini
To implement this you now need to run these 2 commands (the first one you will need to run for each bundles group namespace):
$ php app/console generate:doctrine:entities <your-bundles-namespace>
$ php app/console doctrine:schema:update
That should be your database setup now, now all you have to do is install your assets. As we enabled the FollowSymLinks setting under our vhost.conf this should be no problem, just run the command below:
$ php app/console assets:install --symlink web/
Now that this is done, we should be ready to test our site out, check your domain to see if you can access your site.
Hopefully all is well and you don’t have any issues, but if you do, check your error log, which you can find in your domain’s statistics directory, statistics/logs/error_log if you do not have a statistics directory then check /var/log/httpd/error_log and see what issues are coming up there. The last occuring issues will be at the very bottom of the error log, so look their first.
Assuming all is well, we can now delete the phpinfo.php script (its not good to leave this around, it can be a security hazard to leak all of your setup info)
$ rm phpinfo.php
Also remove app_dev.php and config.php from your web directory as these are only needed for dev environments.
$ rm config.php app_dev.php
I would also recommend once everything is working and you have tested out everything on your site that you further edit your php.ini file in your local domain area so that you set the error reporting to:
error_reporting = E_ALL & ~E_DEPRECATED display_errors = Off display_startup_errors = Off
but keep ‘log_errors = On‘ as you will want a log of anything that is breaking down if someone reports it then you can trace the log to duplicate the conditions hopefully under which the bug was generated, or at least point you in the right direction.
The last step is to setup some additional tools, namely php’s APC, which is really useful for optimising your sites php performance overall. Though installing APC is not required, it can dramatically improve performance and is recommended. You can read about that in my other blog article http://www.reecefowell.com/2012/01/17/installing-apc-on-plesk/