How to install Symfony 3 Framework with Nginx on Ubuntu 14.04

symfony vpsSymfony 3 is an open source and high performance PHP framework used for developing web 2.0 applications. Symfony allows you to develop faster and better software than with flat PHP.
In this step by step tutorial, we will show you how to easily install Symfony 3 framework with Nginx on your Ubuntu based virtual server.

 

At the time of writing this tutorial, the latest stable version is Symfony 3.0.1 and it requires:

  • Web server (Apache, Nginx etc.) installed on your Linux virtual server.
  • PHP 5.4 or higher (the php5-fpm package is required for Nginx servers)

If you have Apache installed on your server, stop the Apache service and remove Apache packages:

/etc/init.d/apache2 stop
apt-get remove apache2*

Make sure your Ubutu VPS is fully up to date by using the following commands:

apt-get update
apt-get upgrade

In order to install Nginx, PHP-FPM, curl and MySQL on your server, run the following command:

apt-get install nginx php5-fpm php5-cli php5-mcrypt php5-gd curl mysql-client-5.5 mysql-server-5.5 mysql-server-core-5.5

Locate the PHP configuration file using the following command:

php5-fpm -i | grep php.ini
Configuration File (php.ini) Path => /etc/php5/fpm
Loaded Configuration File => /etc/php5/fpm/php.ini

Edit the /etc/php5/fpm/php.ini configuration file using the following command:

vi /etc/php5/fpm/php.ini

then, add/modify the following lines:

memory_limit = 128M
cgi.fix_pathinfo = 0
safe_mode = Off
max_execution_time = 120
max_input_time = 120
date.timezone = "America/Chicago"

Run the following command to restart the php5-fpm service for the changes to take effect:

/etc/init.d/php5-fpm restart

Install the latest stable version of Symfony on your virtual server using the following commands:

sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

Create a new Symfony project:

symfony new project_name
mv /project_name /var/www/your-domain.com

Set the proper ownership of files and directories inside the ‘/var/www/your-domain.com’ directory:

chown chown www-data:www-data -R /var/www/your-domain.com/

For performance reasons, our recommendation is to run php5-fpm in socket mode, instead of accessing via <IP_address>:PORT.
Remove the default Nginx server block, then create a new Nginx configuration file and add the following virtual block for your domain name:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
rm /etc/nginx/sites-enabled/default
vi /etc/nginx/sites-available/your-domain.com

and add the following lines:

server {
        listen 80;
        server_name your-domain.com;
        client_max_body_size 20M;
        root /var/www/your-domain.com/web;
        index index.php index.html;
        access_log /var/log/nginx/your-domain.com-access.log;
        error_log /var/log/nginx/your-domain.com-error.log;
        set $yii_bootstrap "index.php";
        charset utf-8;

location / {
        try_files $uri @rewriteapp;
        }

location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

location ~ ^/(app|app_dev|config)\.php(/|$) {

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS off;
    }

location ~ \.php {
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        if (-f $document_root$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
        }
}

In order to enable the newly created Nginx block, create a new symbolic link using the following command:

ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/your-domain.com

Test the Nginx configuration:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

and restart the Nginx web server for the changes to take effect:

/etc/init.d/nginx restart

Open http://your-domain/app.php and you should access the welcome page of Symfony 3. Learn how to create your first page in Symfony. That is it. The framework has been installed now.

Of course you don’t have to do any of this if you use one of our PHP Web Hosting services, in which case you can simply ask our expert Linux admins to install Symfony 3 PHP framework for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

 

Leave a Comment