Configuring Symfony 2.7 App with Apache 2.4 HTTP Server on Ubuntu 20.04
Tags: symfony, apache, linux, php
Published: 2021-11-08 04:48:53

Normally, when we are configuring legacy applications is difficult to find accurate documentation about the used tools *(Dependencies, Libraries, Environment)*.

Currently, [Symfony's version](https://symfony.com/releases) is **5.3.10** thus actual documentation points out towards these newer versions from the framework even though previous documentation is still accessible *since version 2.7*

![Symfony Logo](https://symfony.com/images/opengraph/symfony.png)

Avoid looking for hundred sites and freaking out whether any solutions work or work partially, in this post we are going a step to step to configure a Symfony 2.7 app in Apache 2.4 HTTP Server running in Ubuntu 20.04.


## 1) Configure 000-default.conf

I don't recommend editing `etc/apache2/apache2.conf` because it is the main configuration file of Apache so if you broke that file probably your Apache will not work.

Add the next code in `etc/apache2/sites-available/000-default.conf` inside the block `<VirtualHost *:80>` or create your own configuration file in `etc/apache2/sites-available` if you do the last one remember run the script `a2ensite YOUR_CONFIGURATION_FILE.conf` for Apache recognize it.

```xml
DocumentRoot /var/www/html/YOUR_PROJECT_NAME/web
<Directory /var/www/html/YOUR_PROJECT_NAME/web>
      AllowOverride All
      Require all granted
      Allow from All
</Directory>
```
>**[DocumentRoot] (https://httpd.apache.org/docs/2.4/mod/core.html#documentroot):** Apache will serve files located here
>**[Directory] (https://httpd.apache.org/docs/2.4/mod/core.html#directory):** Group of files where we are applying Apache rules
>**[AllowOverride All] (https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride):** Allow configuration from .htaccess
>**[Require all granted] (https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require):** Access is allowed unconditionally.
>**[Allow from All] (https://httpd.apache.org/docs/2.4/mod/mod_access_compat.html#allow):** All hosts can access the server.

Finally, report Apache about that change and reload it.

```
service apache2 reload
```

The previous configuration aimed to configure with mod_php/PHP-CGI if you want to configure with PHP-FPM, I recommend [visiting the official documentation or if you want to validate the Apache configuration for yourself](https://symfony.com/doc/2.7/setup/web_server_configuration.html)

*Check `/var/log/apache2/error.log` if something wrong happens*

## 2) File permission in `app/cache` and `app/logs`

`app/cache` and `app/logs` folders must be accessed by web-server user `www-data` *(This is the default username, keep in mind if your web server has a different username or group name you should replace it with the corresponding name)* to Symfony Applications work as expected. I highly recommended changing the default group of the project's folder to `www-data` *even `/var/www/` to avoid relating this folder with the root user*

### a) Firstly, we change the group of `/var/www/` and its children recursively
```
sudo chown -R :www-data /var/www
```

### b) Add your user Linux account to `www-data` group. 
```
sudo usermod -a -G www-data YOUR_USER
exit
```
Remember to exit your current session or reload your terminal to save changes

### c) Change folder's permission `app`
Sometimes `www-data` must create logs and cache folders itself.
```
chmod 775 -R app
```
Or you can create manually the folders and specifically add the permissions.
```
chmod 775 -R app/cache
chmod 775 -R app/logs
```
[Official Symfony Documentation approach this topic as well](https://symfony.com/doc/2.7/setup/file_permissions.html)

I created a table to let you know the possible combination when you are defining file permission in a Unix environment

> r = Read
w = Write
x = Execute

| Permissions | Owner | Group | Others |
| ------------|-------|-------|--------|
| 0           |   -   |    -  | -      |
| 1           |   -   |    -  | x      |
| 2           |   -   |    w  | -      |
| 3           |   -   |    w  | x      |
| 4           |   r   |    -  | -      |
| 5           |   r   |    -  | x      |
| 6           |   r   |    w  | -      |
| 7           |   r   |    w  | x      |


## Conclusion

This configuration is oriented to **development environment** and you could start coding as fast as possible, if you need a production environment keep in mind Symfony recommendations and links that I had previously shown you and be careful exposing *ONLY* your `web` folder.

I hope this could be very helpful to you, This is my first article written in English and oriented to developers. I expect your feedback or it is something wrong let me know to make the corresponding corrections.