How to Setup Symfony 4 on LAMP Based Debian 9 Server

Create a DigitalOcean Server

Today, you could find a number of cloud hosting providers in the market today and the choice really comes down to your mix of performance requirements and budget.

  • Server IP address
  • Username
  • Password or SSH key

The Symfony LAMP Stack

Symfony relies on a typical LAMP stack comprising of PHP, Apache/Nginx, MySQL and a Linux distribution. You need to install all these software components first, and then configure Apache/Nginx according to the Symfony’s requirements. Luckily all components for creating the Symfony stack are open source so you just need to run the commands and install them via SSH on your server.

Update the Packages on Debian

The first action item on the list is to update the packages on the Debian machine.

$ cat /etc/debian_version
$ apt-get update
$ apt-get upgrade
$ apt-get dist-upgrade

Install Apache2 for Symfony 4

Go to the SSH terminal and install Apache2 first with following command:

apt-get install apache2 -y
cd /etc/apache2/sites-available/
a2enmod rewrite
<VirtualHost *:80>ServerName domain.tldServerAlias www.domain.tldDocumentRoot /var/www/html/symfony4/public<Directory /var/www/html/symfony4/public>Options FollowSymLinks MultiViewsAllowOverride AllOrder allow,denyAllow from All</Directory># uncomment the following lines if you install assets as symlinks# or run into problems when compiling LESS/Sass/CoffeeScript assets# <Directory /var/www/html># Options FollowSymlinks# </Directory>ErrorLog /var/log/apache2/project_error.logCustomLog /var/log/apache2/projec_access.log combined</VirtualHost>
<VirtualHost *:80>ServerName domain.tldServerAlias www.domain.tldDocumentRoot /var/www/html/symfony4/public<Directory /var/www/html/symfony4/public>AllowOverride NoneRequire all grantedAllow from All<IfModule mod_rewrite.c>Options -MultiViewsRewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php [QSA,L]</IfModule></Directory># uncomment the following lines if you install assets as symlinks</VirtualHost>
service apache2 reload

Install Nginx for Symfony 4

If you do not wish to use Apache, Nginx is a great option for a web server. The process is pretty much similar.

apt-get updateapt-get install nginx -y
server {server_name domain.tld www.domain.tld;root /var/www/html/symfony4/public;location / {# try to serve file directly, fallback to index.phptry_files $uri /index.php$is_args$args;}location ~ ^/index\.php(/|$) {fastcgi_pass unix:/var/run/php7.1-fpm.sock;fastcgi_split_path_info ^(.+\.php)(/.*)$;include fastcgi_params;# optionally set the value of the environment variables used in the application# fastcgi_param APP_ENV prod;# fastcgi_param APP_SECRET <app-secret-id>;# fastcgi_param DATABASE_URL “mysql://db_user:db_pass@host:3306/db_name”;# When you are using symlinks to link the document root to the# current version of your application, you should pass the real# application path instead of the path to the symlink to PHP# FPM.# Otherwise, PHP’s OPcache may not properly detect changes to# your PHP files (see for more information).fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;fastcgi_param DOCUMENT_ROOT $realpath_root;# Prevents URIs that include the front controller. This will 404:# http://domain.tld/index.php/some-path# Remove the internal directive to allow URIs like thisinternal;}# return 404 for all other php files not matching the front controller# this prevents access to other php files you don’t want to be accessible.location ~ \.php$ {return 404;}error_log /var/log/nginx/project_error.log;access_log /var/log/nginx/project_access.log;}
service nginx reload

Install PHP 7.2 For Symfony 4

An important Symfony 4 dependency is PHP 7.1.3 (or greater) on the server. To install the required version, I need to update/install a few packages on Debian 9 to install PHP 7.2. Go to the SSH terminal (and make sure that you are in the root) and run the following commands:

sudo apt install ca-certificates apt-transport-https
wget -q -O- | sudo apt-key add -
sudo echo “deb stretch main” | tee /etc/apt/sources.list.d/php.list
sudo apt update
sudo apt install php7.2
php -v
sudo apt install php7.2-cli php7.2-common php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-mysql php7.2-xml libapache2-mod-php7.2`

Install MySQL to Configure Databases

Let’s quickly install MySQl database.

apt-get install mysql-server

Install Composer on Debian 9

Installing Composer globally is a good idea because this way, every user can easily use it. So, I will install it in /user/local/bin directory.

php -r “copy(‘', ‘/tmp/composer-setup.php’);”
sudo php /tmp/composer-setup.php — install-dir=/usr/local/bin — filename=composer`

Finally, Install Symfony 4 on the Debian 9 Server

At this point, I’ve configured the Apache server for the webroot by giving the URL: /var/www/html/symfony4/public.

composer create-project symfony/skeleton symfony4
###> doctrine/doctrine-bundle ###APP_ENV=devAPP_DEBUG=1APP_SECRET=bd4d4fxxxx035a97fxxxed13f18646f# customize this line!DATABASE_URL=”mysql://db_user:db_password@”###< doctrine/doctrine-bundle ###

Simplified Symfony Deployment at Cloudways

Finally I’ve deployed the symfony 4 with some crucial steps following the linux commands. Going along the article you must note that linux skills are necessary to work with SSH and CLI commands. Not everyone know about this and familiar with SSH. Cloudways has simplified the process. You don’t need to build the complete stack like above. The thunderstack will be launched as soon as you setup the first server at cloudways with PHP application. After signup at Cloudways you can easily launch your first PHP server and app like this:

composer create-project symfony/skeleton symfony4

Final Words

Symfony 4 has been widely adopted and appreciated by PHP developers and is already been downloaded in millions. You can also use the standard versions of symfony 3.x (you just need to update the command to install the specific version):

composer create-project symfony/framework-standard-edition your_project_name “3.0.*”
composer create-project symfony/framework-standard-edition your_project_name “3.1.*”



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shahroz Nawaz

Shahroz Nawaz

Co-Founder at Web Hosting Size, Product Manager at Botsify, Ex- Cloudways,