Saturday, May 12, 2018

Symfony 4 Framework: install and setup with virtualbox/vagrant

At first, prepare a development environment for PHP:
Basic preparation of PHP for development

Then log-in to the virtual machine:

Then move to the vagrant directory:
$ cd /vagrant

Install composer


Install the composer:
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Check if you could install it successfully:
$ composer

If this is displayed after running "composer" command, you have installed it successfully.

Then run the following:
$ composer self-update

Install Symfony 4.0


Run the following to install symfony:
$ cd /vagrant
$ composer create-project symfony/website-skeleton my-symfony-project

You will have your symfony project:

Checking for Security Vulnerabilities


Symfony has a utility called "Security Checker" (or sec-checker) to check if your project's dependencies contain any known security vulnerability. This is NOT mandatory, but if you want it, you can install it by:
$ cd my-symfony-project/
$ composer require sec-checker --dev

Also, you can install a tool to check if all requirements are met:
$ cd my-symfony-project/
$ composer require symfony/requirements-checker

Configuration of Symfony


We will check if symfony is working. Go to the URL like:
http://192.168.33.10/my-symfony-project/public/

If you see the following, your symfony is working correctly.

--- optional below ---
If you see errors because it can't write in the log file, run the following command to change the folder's owner.
For Apache users:
$ sudo chown -R apache:apache /vagrant/my-symfony-project/{path to the log folder}
For Nginx (with php-fpm) users:
$ sudo chown -R php-fpm:php-fpm /vagrant/my-symfony-project/{path to the log folder}

If this doesn't work, change/add the following line in Vagrantfile.
For Apache users:
config.vm.synced_folder "./", "/vagrant", type: "virtualbox", owner: 'vagrant', group: 'apache', mount_options: ['dmode=777', 'fmode=777']
For Nginx (with php-fpm) users:
config.vm.synced_folder ".", "/vagrant", type: "virtualbox", owner: 'vagrant', group: 'php-fpm', mount_options: ['dmode=777', 'fmode=777']
And "vagrant reload".

If even that doesn't work, it might be because of SE linux. Turn off SE linux this way:
$ sudo getenforce
Enforcing
$ sudo setenforce 0
$ sudo getenforce
Permissive
--- optional above ---

Database


You can configure for your database from here:



My mysql's user name is root, password is also root, so the database URL is:
mysql://root:root@127.0.0.1:3306/testdb

You don't need to have a database named "testdb" because we will create the db on console. Run the following command:
$ cd /vagrant/my-symfony-project/
$ php bin/console doctrine:database:create

Database was created:


An error occurred while loading the web debug toolbar.


You might see this error at the bottom:

This is a bug according to this page:
https://github.com/symfony/symfony-docs/issues/9178#issuecomment-363006113

References


Symfony doc: https://symfony.com/doc/current/doctrine.html