So you want to install Symfony in a sub-directory? No problem. With this tutorial, you'll learn how to configure your web server to allow you to access symfony in it's own sub-directory so you can have other applications running on the same domain with no problems.
Getting Started
In this tutorial, we are going to assume a few things:
- We want to access our symfony project via http://www.mydomain.com/blog/.
- The path to our domain's document root is /var/www/mydomain.com/html/.
- The path to our pear library is /usr/share/pear/.
- You've read the symfony installation guide and are familiar with how symfony works.
If your setup is different, please make the adjustments in the tutorial below to work with your configuration.
Creating your project
You can create your project anywhere you like. The only condition is that Apache must be able to read/write to this location. You should refer to the installation guide for detailed instructions on using the symfony command-line tool.
Let's create our symfony project just below our document root:
mkdir /var/www/mydomain.com/symfony cd /var/www/mydomain.com/symfony symfony init-project symfony
Let's also initialize our blog application:
symfony init-app blog
Creating a symlink to your project
Now, we have a project setup but you can't get to it because it doesn't live in our web root. We have to create a symlink to access it.
cd /var/www/mydomain.com/html ln -s /var/www/mydomain.com/symfony/web blog
Now, apache can find the application.
Creating a symlink to the 'sf' directory
To shy away from adding un-needed directives into our httpd.conf file, we're going to create a symlink to the sf directory that lies in the symfony pear libraries.
cd /var/www/mydomain.com/symfony/web ln -s /usr/share/pear/data/symfony/web/sf/ sf
Updating your Apache httpd.conf
The next thing we need to do is modify our httpd.conf (usually located in /etc/httpd/conf/httpd.conf)
Change this:
<VirtualHost *:80>
DocumentRoot /var/www/mydomain.com/html
ServerAdmin yourname@mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DirectoryIndex index.php index.html
</VirtualHost>
To look like this:
<VirtualHost *:80>
DocumentRoot /var/www/mydomain.com/html
ServerAdmin yourname@yourdomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DirectoryIndex index.php index.html
<Directory "/var/www/mydomain.com/html/blog">
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Note: The only thing I added was the <directory> tag with the path to our symlink.
Now we need to restart Apache to make it recognize our changes in the httpd.conf.
/etc/rc.d/init.d/httpd restart
At this point, the application should be functional and Apache should let symfony use its mod_rewrite rules in the directory. CSS and JS tags will be broken, but the application should at least appear when we access our domain, http://www.mydomain.com/blog.
Update the Relative Root
Now, we have to update our application to use the correct paths for your stylesheets and javascripts.
cd /var/www/mydomain.com/symfony/blog/config nano settings.yml
I use nano, but you can use pico, vi, vim, or any other text editor that you like.
Update your relative_url_root
Change this:
all: .settings: # relative_url_root:
To this:
all:
.settings:
relative_url_root: /blog
If you are running more than one application, for example blog/admin.php, remember to change this setting for all apps.
Summary
That should do it. You should have a functional working copy of symfony running in a sub-directory on your web server. If you have any questions, please post on the community forum for assistance.