How to Install Symfony2 in a VPS (2.0 and 2.1)

ServerGrove likes the phpperu.org community. We were able to make it to symfonyLive in San Francisco and met with them too. Here is a how-to on how to deploy symfony2 applications into a VPS and can be of course adapted to fit any other VPS.

I am thankful to the Lord Jesus Christ for all these opportunities to meet with a lot of people on the past weeks and also pondered my time in San Francisco. San Francisco had a stark contrast, very big in IT and focused in business, but at the same time there were people starving of meaningful and lasting relationships and too busy to realize life is more than just business. The needy could be the lonely billionaire going to a wild party just because there is nothing else to do, or the poor on the streets whose life in a too high tech city is too lonely to stay sane. I am very glad to see in this midst the church community open and reaching out to everybody in love and with the Gospel of grace to them. I hope anyone would ponder life in all its aspects and also would give the time to ponder the great Gospel of grace.

Without further ado, let’s install our symfony 2.0 and 2.1 default app into a VPS100.

Log into your cpanel `https://control.servergrove.com/login` with your email and password provided:

After we click on the link for the shown server we will be redirected to our server panel or be prompted to build our server if we have not done it already. There is also a tab to rebuild the server with different OS options as shown:

I was advised to go for the `Ubuntu 11.04 x86_64 – PHP 5.3.x – 64Bit (recommended)` option which had already php installed on it and had my favorite stable system built for 64 bit architecture. I could then upgrade to php 5.4 with this base since that is the configuration I was after. Your shiny dashboard would now look like the following:

Up to here we have setup the server, it has default services ready for you like `apache, cron, mysql, php, ssh, and a syslog`. All of these services come ready running and preconfigured with defaults. Should you need to enable them to start by default on-boot just tick the checkbox next to its description.

For the ssh service we can see that we have to connect through port `22123` and with our IP address (for me 69.195.222.35) from the dashboard previous screen we can start configuring how we are going to connect to our box to setup our first symfony2 app. So to that now we turn. If you forgot the root password like me then just head to the dashboard website, click on *System Users* tab on the left, then click on *edit* root user and provide new root password. It is blazing fast to change the root password of the box. Now do:

ssh root@69.195.222.35 -p 22123

First of all we need a domain and we can easily create this from the Server Groove’s Control Panel by going under “Domains >> Add Domain”. We can type an example domain like ‘phpperu.org’ or similar to test our first deploy. There will always be a preview link so you don’t have to worry at this time about registering this domain if you have not done that yet.

After creation we notice the control panel has created for us a virtual host and is already enabled:

// /etc/apache2/sites-enabled/phpperu.org
### File generated automatically by ServerGrove Control Panel. Do not edit manually as changes will be overwritten.
### If you need to enter custom directives do it below between the lines defined specially for this.
### If you want to avoid ServerGrove Control Panel writing to this file, change the following setting to 0 or remove the line completely.
### sgcontrol.enabled=1

 
<VirtualHost *:80>
        CustomLog /var/log/apache2/phpperu.org-access.log combined
        DocumentRoot /var/www/vhosts/phpperu.org/httpdocs
        ServerName phpperu.org
        ServerAlias www.phpperu.org phpperu.org.35.69-195-222.groveurl.com
        DirectoryIndex index.php
 
 
### sgcontrol.custom.config.start

### sgcontrol.custom.config.end
</VirtualHost>

Notice we can customize our vhost further but we need to switch the value `sgcontrol.enabled` to 0 to prevent the control panel from overwriting this file.

Here we will start installing our symfony2 app:

// go to folder the control panel generated and remove it
cd /var/www/vhosts/
rm -rf phpperu.org/
 
// create project from Symfony2 Standard Edition
composer create-project symfony/framework-standard-edition phpperu.org/
 
// here modify app/config/parameters.yml | parameters.ini
 
// run symfony commands to create database
php app/console doctrine:database:create
 
// switch ownership to apache2 web user
sudo chown -R www-data:www-data * .*
 
// grant access to cache and logs
sudo chmod -R 777 app/cache app/logs

Then go again back to the Control Panel and under `services / server.phpperu.org / domains / phpperu.org / web hosting` page look for the tab `General` and row `Domain`. There should be a `settings` button towards the right side. Clicking on it will allow you to set a new document root directory. Just change it from `/var/www/vhosts/phpperu.org/httpdocs` to `/var/www/vhosts/phpperu.org/web` as we are dealing with a symfony2 project which serves pages through the front controller app.php or index.php under `web` folder. Notice: Because you have removed the old folder `httpdocs` in the previous steps the information on the form to change the directory could take a little longer to load but the information will load if you want a bit longer.
If we now go to the url provided on the control panel to test (for me is `http://phpperu.org.35.69-195-222.groveurl.com/` + `app_dev.php`) then we should have on our browser:

Now in order to demo a functioning site we comment out the app_dev.php lines that prevent from running the kernel:

// edit it with: root@server:/var/www/vhosts/phpperu.org# nano web/app_dev.php
/*if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array(
        '127.0.0.1',
        '::1',
    ))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}*/

Once commented out we get the default `dev` route:

Warning: You should remove the commenting above and disallow thereby completely the access to app_dev.php as having this accessible online renders your file very vulnerable. Please understand that this is only to demo that the site has been properly installed. If you instead of allowing access to `app_dev.php` want to work with what you have on `app.php` for the symfony-standard version that we have installed then what you need to do to get a quick default working site is the following:

// 1. move `AcmeDemoBundle` from test, dev section to prod section on your app/AppKernel.php
            new Acme\DemoBundle\AcmeDemoBundle(),
// 2. add routing entries to app/config/routing.yml
_welcome:
    pattern:  /
    defaults: { _controller: AcmeDemoBundle:Welcome:index }
 
_demo_secured:
    resource: "@AcmeDemoBundle/Controller/SecuredController.php"
    type:     annotation
 
_demo:
    resource: "@AcmeDemoBundle/Controller/DemoController.php"
    type:     annotation
    prefix:   /demo
// 3. hard flush your cache
rm -rf app/cache/*
// 4. retry your browser on just / or `app.php` and it should work. The only difference is that it will not show the second tile for configuration.

For the symfony2.0 case it is as easy as the 2.1 version depicted above. The only difference is that instead of `symfony-standard` we will install the `knplabs/symfony-with-composer` through cloning as follows:

cd /var/www/vhosts/
git clone git://github.com/cordoval/symfony-with-composer.git phpperu.org/
cd phpperu.org/
git checkout bugfix/minimumStability
php bin/vendors install

2 thoughts on “How to Install Symfony2 in a VPS (2.0 and 2.1)

Leave a Reply to IamPersistent Cancel reply

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