Setting up VirtualHost for Symfony Application on Ubuntu

If you are developing a Symfony application on your local machine by default you will get a localhost URL like:

http://localhost/symfony/symfony-test-app/public/

Which is a bit bad looking because the actual front facing application of Symfony is in app-root/public directory. You can make it look like http://symfony-test-app.com depending on your application name just by editing 2 files.

Let’s see how it works in Ubuntu:

The first file you will need to edit is virtual host configuration file or you can create one new. The root directory in this case would be: /etc/apache2/sites-available/

I am going to create a separate file for my app which is symfony-test-app.conf

sudo vim /etc/apache2/sites-available/symfony-test-app.conf

The content of symfony-test-app.conf file would be:

<VirtualHost *:80>
  ServerName symfony-test-app.com
  <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]
  </IfModule>
  DocumentRoot /var/www/html/symfony/symfony-test-app/public
  <Directory /var/www/html/symfony/symfony-test-app/public>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

Just change the ServerName, DocumentRoot and Directory line according to your application path.

The second file you will need to edit is the hosts file. The root directory in this case would be: /etc/

sudo vim /etc/hosts

The content you need to add is just a single line like:

127.0.0.1       symfony-test-app.com

You can add it before or after the default localhost line like:

127.0.0.1       localhost

That’s it, You are ready to go by enabling your newly created virtual hosts configuration file and restarting your Apache like this:

sudo a2ensite symfony-test-app.conf
sudo service apache2 restart

If you did it all good then now you can visit your in development Symfony application by http://symfony-test-app.com/ instead of using that old ugly URL with /public at the end.

Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: