In each chapter, new functions will be added to the application, and we will use this approach to gradually introduce Symfony into the framework, and learn about the best practices in web development on Symfony.
In this part, you will not write the code. Instead, you will configure the development development environment.
Configuring the Development Environment
First of all you need to check that your computer has the appropriate software for web development. As an operating system, we will use Ubuntu 12. Of the software at least, we need an Apache web server, MySQL and PHP 5.3.3 or later. For Windows, I recommend installing XAMPP , there are all the libraries and the environment for the developer is already set up. Also, instead of the Apache server, you can use the built-in server in php 5.4 and higher, as it will be shown below.
Installing Apache as the primary web server
Go to the terminal and type:
sudo apt-get install apache2
then enable the Apache mod-rewrite module :
sudo a2enmod rewrite
Installing MySQL server
sudo apt-get install mysql-server mysql-client
sudo apt-get install php5 libapache2-mod-php5 php5-mysql
Installing an Intl extension :
sudo apt-get install php5-intl
Now you need to restart the Apache service:
sudo service apache2 restart
Install Symfony 2
The first thing to do is prepare a directory for the web server where you want to install a new project. Let’s call it the joboard:
/var/www/joboard(do not create the directory).
In order to download the latest version of Symfony2 to this directory, we need to install a package manager for PHP – Composer . The framework itself and all the libraries necessary for it will be downloaded automatically through this utility.
Composer is a dependency manager for third-party libraries for PHP, which can also be used to download Symfony 2 Standard Edition.
Now let’s install Composer by running the command:
php -r "readfile('https://getcomposer.org/installer');" | php mv composer.phar /usr/local/bin/composer
or if for some reason it does not work for you, you can install it like this:
curl -s https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
And now the installation of Symfony2 Standard Edition (you need to go to the directory
cd /var/www/ composer create-project symfony/framework-standard-edition joboard "2.5.*"
During the installation, you will need to answer a few questions, just press Enter all the time.
Web Server Configuration
The embedded PHP server
You can run the project on a symphony, without setting up a virtual host to do this, go to the web directory of your project and run the built-in local server in PHP: php -S localhost: 8000, then go to http: // localhost: 8000
The second and simpler way is to go to the project’s root directory and run a command that will launch the exact same server, only you do not need to specify in the address bar app_dev.php:
php app/console server:run
To configure Apache for your new project, create a virtual host. To do this, go to your terminal and enter the following command:
sudo nano /etc/apache2/sites-available/joboard.local.conf
Now we have a file called joboard.local . Insert the description of the virtual host (see below) inside the created file, and then press CTRL + O and Enter to save it, then CTRL + X to exit the editor.
<VirtualHost *:80> ServerName joboard.local DocumentRoot /var/www/joboard/web DirectoryIndex app.php ErrorLog /var/log/apache2/joboard-error.log CustomLog /var/log/apache2/joborad-access.log combined <Directory "/var/www/joboard/web"> AllowOverride All Allow from All </Directory> </VirtualHost>
The domain name joboard.local , is used in the Apache configuration and must be declared locally. If you work in Linux, then you can register a local domain in a file
/etc/hosts. If you are running Windows, this file is located in the directory
C:\Windows\System32\drivers\etc\. Open this file and add the following line:
If you want the domain name to work on the local computer, you need to enable the newly created virtual host and restart Apache. So open the terminal and type:
sudo a2ensite joboard.local.conf sudo service apache2 restart
Symfony comes with a visual configuration tester, it will help you make sure that your web server and PHP are properly configured to use Symfony. Use the following URL to verify the configuration:http: //joboard.local/config.php
The screenshot shows that for the normal operation of Symfony, the framework defines a number of requirements. Below is a list of actions to solve all the problems noted.
Change permissions on the directory
HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1` sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX app/cache app/logs
Set ACL if you do not already have it:
sudo apt-get install acl
Set the parameter
sudo nano /etc/php5/apache2/php.ini
If you get an error with setfacl, try the following commands:
rm -rf app/cache/* rm -rf app/logs/* HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1` sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
Find the parameter
date.timezonefor the section
[date]and set it to your time zone. After that, delete the “;” at the beginning of the line.
# etc/php5/apache2/php.ini date.timezone = Europe/Minsk
Set the parameter
short_open_tagto the value
Offin the same php.ini file
#/etc/php5/apache2/php.ini short_open_tag = Off
Install and enable PHP accelerator (APC recommended)
sudo apt-get install php-apc sudo service apache2 restart
After restarting Apache, open the browser window and type http: //joboard.local/app_dev.php . You should see the following page: