Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

This post shows you how to set up a symfony project and the virtual host to go with it. We're going to create a project called "sport".


Create database


mysql> CREATE DATABASE IF NOT EXISTS symfony CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| symfony |
+--------------------+
4 rows in set (0.01 sec)

Install symfony


Default webroot in OS is /Library/WebServer/Documents/ so CD into it. Assuming that you've composer installed globally.


Inanzzz-MBP:~ inanzzz$ cd /Library/WebServer/Documents/
Inanzzz-MBP:Documents inanzzz$ composer create-project symfony/framework-standard-edition sport

Assign permissions


Inanzzz-MBP:~ inanzzz$ cd sport
Inanzzz-MBP:sport inanzzz$ rm -rf app/cache/*
Inanzzz-MBP:sport inanzzz$ rm -rf app/logs/*
Inanzzz-MBP:sport inanzzz$ HTTPDUSER=`ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
Inanzzz-MBP:sport inanzzz$ sudo chmod +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs
Inanzzz-MBP:sport inanzzz$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs

Create a bundle


Inanzzz-MBP:sport inanzzz$ php app/console generate:bundle --namespace=Your/NewBundle

Create a virtual host


Inanzzz-MBP:sport inanzzz$ sudo nano /private/etc/apache2/extra/httpd-vhosts.conf

# Add these lines
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents/sport/web"
ServerName sport.local

<Directory "/Library/WebServer/Documents/sport/web">
Options Indexes FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

ErrorLog "/private/var/log/apache2/sport.local-error_log"
CustomLog "/private/var/log/apache2/sport.local-access_log" common
</VirtualHost>

Enable virtual host in apache config


Inanzzz-MBP:sport inanzzz$ sudo nano /etc/apache2/httpd.conf

# Enable
LoadModule dir_module libexec/apache2/mod_dir.so
LoadModule alias_module libexec/apache2/mod_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

# <Directory "/Library/WebServer/Documents"> block should have:
Options FollowSymLinks Multiviews Indexes
AllowOverride All

# Enable
Include /private/etc/apache2/extra/httpd-autoindex.conf
Include /private/etc/apache2/extra/httpd-languages.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf

Set host


Inanzzz-MBP:sport inanzzz$ sudo nano /etc/hosts

# Add this line
127.0.0.1 sport.local

Restart apache


Inanzzz-MBP:sport inanzzz$ sudo apachectl restart

Test


If you call http://sport.local/app_dev.php/hello/inanzzz in your browser, you should see "Hello inanzzz!" message.