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:


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 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>
  <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule .* - [F]
  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

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:

You can add it before or after the default localhost line like:       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 instead of using that old ugly URL with /public at the end.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *