You are currently viewing Configure Symfony on apache server

Configure Symfony on apache server

Developing with Symfony framework involves using a web server to render your webApp pages. There are various options that allow you do this. But the most common way remains using the command php bin/console server:start which gives us a quik access to our website over localhost:8000

This command allows to start the PHP built-in web server, which prevents from getting bothered with configuring a full-featured web server such as Apache or Nginx.

Generally this built-in server is sufficient for testing purpose, and allows compiling and serving web pages with no problem. But sometimes, you may be led to implement some features that require binaries out of PHP. This kind of situations can cause an issue with this built-in server.

In my case I was asked to find a tool that allows converting a web page to a PDF. And I used wkhtmltopdf in order to accomplish this. It was the first time I use such library, that calls a bin to ensure the conversion work. Although, it’s no longer patched and updated as before, it’s still one of the most complete and stable open source libraries for web to pdf /coversion

In the beginning, I used the built-in server to test the tool. pdf pages were generated, so there was nothing suspicious. But, as soon as the logic behind the generation gets a bit complicated (use of CSS libraries, adding cover, table of content…), the results were just horrible. I didn’t think that the problem could be related to the server, so I wasted more than one day looking for the solution in the very wrong side, before I realize what was the real issue.

After defining the source of the problem, I was taken to configure my local Apache server. The new configuration of Apache is based on virtualHost. This concept allows the server to manage multiple hostnames that resolve to a single address.

The first thing we should do, is to search for the httpd.conf file. It’s the file where the main configuration of the server is set (in some distributions it may be called apache2.conf). Then do type ctrl+f to look for the keyword include this keyword is useful to define all the folders that are loaded within the main file. In general, the loaded tree is like this:

config tree

In my case, I want to setup a domain name, so I am interested by sites-enabled. This folder may contain .conf files but it is recommended to use “symbolic links” to files in the “sites-available” directory. So, the steps are as bellow:

1- Open sites-available directory and copy “000-default.conf” to “domain-name.conf” with “domain-name” as the domain name of the Symfony app.

2- Inside the “domain-name.conf” the minimum setup to put is:

<VirtualHost *:80> # means server listens on 80 port
DocumentRoot /var/www/foo/public #the folder of the landing page
ServerName domain-name # the domain name chosen by the developper

3- Create a symbolic link to this file in the “sites-enabled” directory using this command sudo a2ensite domain-name

4- Edit your /etc/hosts file to link the domain-name to localhost by adding this line: domain-name

5- Restart your apache server via sudo systemctl restart apache2

Now, if you type in your browser the address http://domain-name/index.php you will access your Symfony app. But in order to avoid having the index.php in your address, a final step is required. It’s the Url rewrite.

Go back to your domain-name.conf file and add the following inside the VirtualHost tag:

<IfModule mod_rewrite.c> # checks if the rewrite module is up
Options -MultiViews #
RewriteEngine On # activate the rewriting
RewriteCond %{REQUEST_FILENAME} !-f # checks that the url doesn’t match a file
RewriteRule ^(.*)$ index.php [QSA,L]

After this, restart your apache server, and here we go! the apache server is configured to access your symfony app through domain-name url.

This article can help in setting up your symfony server either for testing purpose or when publishing your app in the prod server, because you won’t be using the test server in such environement.

Leave a Reply