How to Setup Symfony 4 on LAMP Based Debian 9 Server

Symfony is a favourite choice of PHP developer for building small to large scale applications. With every new release, Symfony evolves into a better toolkit for developers. Symfony allows for rapid application development processes so that developers could build full-scale API, ecommerce platforms, fintech and accounting apps etc. on the other side of the spectrum, developers could create simple websites with the help of Twig templating engine.

Symfony 4 has a smart recipe system and simplified directory structure which doesn’t overload the code files in your project. Symfony Flex is there to install libraries with alias names and register them automatically in bundle’s configurations. The new version also introduces lightening fast routing system using hash-map lookups for static routes, and combined regular expressions for routes with placeholders.

Given the popularity of Symfony 4, I decided to configure and setup this version on a Debian 9 machine. In this article, I will show you how you can setup a complete Symfony Stack to run Symfony applications. The process includes steps regarding permissions, webroots and web 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.

For the purpose of this article, I will go with DigitalOcean, a cloud hosting provider that is much loved by the developers. You can easily sign up for a DigitalOcean account, and choose your distribution, size and the data center of your server, as shown in the following GIF:

Now, the next step is to launch the SSH terminal. If you are a Linux or a Mac user, you get it by default on your machine. If you are a Windows user, you need to download PuTTY. The good news is that Windows 10 comes with its own PowerShell that you could use for the purposes of this article.

After launching the droplet, you need the following three items to login to the SSH terminal:

  • Server IP address
  • Username
  • Password or SSH key

Since I am using Windows, I’ll use PuTTY for all SSH related activities. Note down the above items from the droplet and login to the SSH terminal. In PuTTY, when you enter the IP address and hit Enter, the server will ask you about caching the key. Simple hit Yes.

Next, enter the credentials and you will login to your DigitalOcean droplet with root access.

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.

Symfony has also defined the web server configuration for Nginx and Apache in their documentation so I’ll cover the main settings here and you could read the rest there.

Update the Packages on Debian

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

Start by checking the Debian version on the server by running the following command:

$ cat /etc/debian_version

So I have Debian 9.4 on my DigitalOcean server.

Next run the following commands to update the packages via SSH:

$ apt-get update
$ apt-get upgrade
$ apt-get dist-upgrade

Once everything has been updated, I am ready to install the web server.

Install Apache2 for Symfony 4

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

apt-get install apache2 -y

Once the command finishes, access the IP of your server and you will see the Apache welcome page:

Next. go into the sub-directory structure with the command:

cd /etc/apache2/sites-available/

Quickly enable the mod_rewrite option first with this command:

a2enmod rewrite

Now open 000-default.conf file and add the following to configure the web host in Apache 2:

<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/html/symfony4/public
<Directory /var/www/html/symfony4/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow 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.log
CustomLog /var/log/apache2/projec_access.log combined
</VirtualHost>

Now, Apache2 is configured to run the project from /var/www/html folder. But you can also extend the configuration with few more options that best suit Symfony 4. Here is an example:

<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/html/symfony4/public
<Directory /var/www/html/symfony4/public>
AllowOverride None
Require all granted
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
</Directory>
# uncomment the following lines if you install assets as symlinks
</VirtualHost>

You can also use the latest Apache options such as Require all granted and configure environment variables in the above settings. Now reload the Apache server with the following command so that the new settings could take effect:

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.

Start by installing Nginx with the following command:

apt-get update
apt-get install nginx -y

Now run cd /etc/nginx/conf.d and configure the file as:

server {
server_name domain.tld www.domain.tld;
root /var/www/html/symfony4/public;
location / {
# try to serve file directly, fallback to index.php
try_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 https://github.com/zendtech/ZendOptimizerPlus/issues/126
# 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 this
internal;
}
# 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;
}

Now reload the server by running the following command:

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
https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
sudo echo “deb
https://packages.sury.org/php/ stretch main” | tee /etc/apt/sources.list.d/php.list

Now in the next step, run these commands:

sudo apt update
sudo apt install php7.2

After successful installation, check the PHP version:

php -v

I also need to install a few more libraries to make sure that PHP 7.2 work properly on the server. Run the following command to install the required libraries:

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`

The libraries will be installed and PHP 7.2 is properly configured for Symfony 4.

Install MySQL to Configure Databases

Let’s quickly install MySQl database.

Start by running the following command:

apt-get install mysql-server

The process will pause to ask your permission. Type (y) and press Enter. In the next window, set the password for the MySQL root user.

The process finishes in a matter of minutes.

Next, I’ll configure MySQL according to the requirements of the LAMP stack. For this, enter the following command:

mysql_secure_installation

At this point, server setup is complete as per the requirements of Symfony 4. I will now focus on installing the framework itself.

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.

Let’s first copy the installer to the /tmp directory:

php -r “copy(‘https://getcomposer.org/installer', ‘/tmp/composer-setup.php’);”

Next, run the following command to execute the Composer file and install it globally:

sudo php /tmp/composer-setup.php — install-dir=/usr/local/bin — filename=composer`

Now that Composer is installed, I will proceed with the actual Symfony 4 installation.

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.

Go into the html folder and run the following Composer command to install Symfony 4:

composer create-project symfony/skeleton symfony4

The framework will be installed in a few seconds. Because Symfony 4 is very optimized with a small codebase and dependencies, it will not ask for any credentials during the installation. Once done, you need to configure the database.

Once the process finishes, access your server’s IP and you will see the welcome page:

Next up, I will add the database credentials to .env file of the Symfony.

###> doctrine/doctrine-bundle ###
APP_ENV=dev
APP_DEBUG=1
APP_SECRET=bd4d4fxxxx035a97fxxxed13f18646f
# customize this line!
DATABASE_URL=”mysql://db_user:db_password@127.0.0.1:3306/db_name”
###< 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:

Now, login to the SSH terminal by using the Master Credentials from application access details and run the following command in your application.

composer create-project symfony/skeleton symfony4

Note down PHP 7.1 is the mandatory version with Symfony 4. You have PHP 5.6, 7 and 7+ available at Cloudways. You can follow the complete tutorial on Deploy Symfony 4 on cloud

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):

For Symfony 3.0:

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

For Symfony 3.1:

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

If you need help in installing and setting up Symfony 4 on Debian 9 machine, do let me know in the comments.

P.S.: please Retweet if you liked this article, and don’t hesitate to follow me on Twitter, and/or here on Medium. ;-) Thanks for reading.

Like what you read? Give Shahroze Nawaz a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.