Install and Create your First Symfony(3.4) Project

By: Sunil Kumar |  In: PHP  |  Last Updated: 2019/01/11

Symfony is a popular PHP framework, especially when working on a high-level web application. Installing and setting up a Symfony project is a little bit tricky. But when it is done step by step in the proper way, it is simpler than anything else.

In this guide, we will be installing and setting up our first Symfony Project.

Installing Symfony

Before Installing Symfony make sure you update your package list.

sudo apt-get update

#for Linux and mac os

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

#for windows os

php -r "file_put_contents('symfony', file_get_contents('https://symfony.com/installer'));"

In Linux and macOS, a global Symfony command is created. In Windows, move the Symfony file to a directory that’s included in the PATH environment variable and create a symfony.bat file to create the global command or move it to any other directory convenient for you:

move symfony c:\wamp\bin\php
cd c:\wamp\bin\php
(echo @ECHO OFF & echo php "%~dp0symfony" %*) > symfony.bat
symfony
move symfony c:\projects
cd projects
php symfony

Create a Symfony Project

Once the Symfony installer is installed, we can proceed to create our firstSymfony project.

symfony new my_symfony_project 3.4

This command is going to create an empty Symfony project(my_symfony_project) into your directory.

This command is also going to check for any other dependency and compatibility with your current PHP package.

LIke in the project I created default timezone was not set-up correctly. So it was throwing an error.

symfony installation error

I corrected that error by setting default timezone to Asia/Kolkata in my php.ini(/etc/php/5.6/cli/php.ini) file.

After fixing the error you can check by using the below command

php my_symfony_project/bin/symfony_requirements

Once all the errors are fixed it will output Success message- “Your system is ready to run Symfony projects” like this.

symfony installation success

It will also list any other optional recommendations for your Symfony projects.  LIke here it is asking for “Intl extension”.

If you want to install “Intl extension” you can install it using

sudo apt-get install php5.6-intl

Create VirtualHost(If using Apache web server)

Now If you need to configure Symfony 2 with a domain. Create a new Apache configuration file under directory /etc/apache2/sites-available/ and add Virtual Host for your Symfony 2 application.

sudo nano /etc/apache2/sites-available/symfony_site.com.conf
<VirtualHost *:80>

ServerName symfony_site.com

DocumentRoot /home/project/symfony_project/testing_project/web

<Directory /home/project/symfony_project/testing_project/web>

     AllowOverride All
     Order Allow,Deny
     Allow from All
     <IfModule mod_rewrite.c>
         Options -MultiViews
         RewriteEngine On
         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteRule ^(.*)$ app.php [QSA,L]
     </IfModule>

</Directory>

ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined

</VirtualHost>

Create a server block(If using Nginx)

If you are using Nginx web server and want to configure a new domain, create a block file at

sudo nano /etc/nginx/sites-enabled/symfony_site.conf

And place the following code inside

 server
{
 listen 80;
 server_name symfony_site.com;
 index app.php app_dev.php;
 root /home/project/symfony_project/testing_project/web;
   location ~ \.php$ {
     try_files $uri =404;
     fastcgi_read_timeout 120;
     fastcgi_split_path_info ^(.+\.php)(/.+)$;
     #fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
 }
 location / {
             # Check if a file or directory index file exists, else route it to index.php.
             try_files $uri $uri/ /index.php;
     }
 }

Edit etc/hosts file

Now you need to edit etc/hosts file to register your domain on your local system

sudo nano /etc/hosts

And add the following line

127.0.0.1 symfony_site.com

Now head over to your web browser and hit your domain (http://symfony_site.com here). You will see the Symfony welcome page as below-

Welcome symfony

Start Coding!

Now your Symfony site is ready and you can start working with it.

Comments


Leave a Comment

Your email address will not be published.

*


Sunil Kumar


I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Join more than 10,000 others Web Developers


%d bloggers like this: