Symfony 4 LAMP (Linux, Apache, PHP, MySQL) Setup


This tutorial covers installing the LAMP stack for a Symfony 4 website.

The LAMP stack is:

  • Linux (Ubuntu)
  • Apache
  • MySQL
  • PHP

We're going with PHP 7.2, as it is the most current PHP version at the time of recording.

PHP 7.1.3 is the bare minimum PHP version required to run a Symfony 4 application.

This tutorial is almost identical to the LAMP tutorial for Symfony 3.

However, as mentioned, our PHP version will be different.

The Apache Vhost configuration is slightly different, to reflect the Symfony 4 directory structure.

We will also need to do a little extra setup regarding environment variables. This will be done in a few videos time.

I'm going to provide abbreviated steps here, but if you'd like the full steps, please watch this video, and adapt the PHP and Apache sections accordingly.

Spinning Up A Server

To begin with we are going to create a new Digital Ocean Droplet using the $10 plan, installing a fresh copy of Ubuntu 16.04.3 x64 as our virtual server / droplet Operating System.

You can follow this process on a different VPS provider such as Linode, or Amazon AWS.

If you do not yet have a Digital Ocean account, please create one now. By using this link you will get $10 initial credit, which is going to be more than enough to complete all the examples in this course. Full disclosure: this is an affiliate link.

Be sure to upload your SSH key(s) as available. If you do not have an SSH key, then please follow this guide.

Creating your new Droplet should take about 30 seconds.

Once done, you should be able to SSH into the box by running:

ssh root@165.227.110.26

Change the IP address to match your own Droplet accordingly.

I'm going to assume you have created a local entry in /etc/hosts or similar, and have a added an entry such as:

# from your local machine
# in the /etc/hosts file
# (or windows equivalent)

165.227.110.26   crvfakeexample.com

This step is important to ensure you can browse to your made up domain name without having to actually register a real new domain. This will only be usable on your own local PC.

Apache Server Setup

Switch back to your server's terminal session now.

apt-get update
apt-get install apache2 -y
cd /etc/apache2/sites-available/
a2dissite 000-default
service apache2 reload
a2enmod rewrite
service apache2 restart
touch crvfakeexample.com.conf
vim crvfakeexample.com.conf

Ok, so we have achieved quite a lot there. There's nothing new, again, if unsure please watch the previous video where all of this and more was covered in greater depth.

The Vhost config we need is as follows:

<VirtualHost *:80>
    ServerName crvfakeexample.com
    ServerAlias www.crvfakeexample.com

    DocumentRoot /var/www/crvfakeexample.com/public
    <Directory /var/www/crvfakeexample.com/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
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/crvfakeexample.com>
    #     Options FollowSymlinks
    # </Directory>

    # optionally disable the RewriteEngine for the asset directories
    # which will allow apache to simply reply with a 404 when files are
    # not found instead of passing the request into the full symfony stack
    <Directory /var/www/crvfakeexample.com/public/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>
    ErrorLog /var/log/apache2/crvfakeexample.com_error.log
    CustomLog /var/log/apache2/crvfakeexample.com_access.log combined

    # optionally set the value of the environment variables used in the application
    #SetEnv APP_ENV prod
    #SetEnv APP_SECRET <app-secret-id>
    #SetEnv DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
</VirtualHost>

This is largely copied from the Symfony Apache example, with the Apache 2.4 change made (Require all granted).

We will need to tweak this file again during the setup of our environment variables.

After saving and exiting, be sure to enable the site and reload Apache:

a2ensite crvfakeexample.com.conf
service apache2 reload

Installing PHP 7.2 For Symfony 4

Symfony 4 expects at least Symfony 7.1.3.

In order to install PHP 7.1 or greater, a standard installation of Ubuntu needs a little help.

PHP 7.0 is the release of PHP available in the currently supplied Ubuntu package list. At the time of recording, in order to install PHP 7.1, or PHP 7.2, we can use a third party package list.

apt-get install python-software-properties -y
add-apt-repository ppa:ondrej/php
apt-get update

apt-get install php7.2 \
  libapache2-mod-php7.2 \
  php7.2-cli \
  php7.2-common \
  php7.2-mysql \
  php7.2-gd \
  php7.2-intl \
  php7.2-xml \
  php7.2-mbstring \
  php7.2-zip

This installs PHP 7.2, the latest and greatest at the time of recording:

php -v
PHP 7.2.0-2+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Dec  7 2017 20:14:31) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.2.0-2+ubuntu16.04.1+deb.sury.org+2, Copyright (c) 1999-2017, by Zend Technologies

We shall also need to install MySQL.

apt-get install mysql-server

Follow the prompts.

You will be asked to provide a root password.

To make your life as easy as possible, use the password of pass.

It shouldn't need saying, but of course don't use this password in production. This is a demo / example.

This is our basic LAMP server setup for Symfony 4.

Code For This Video

Get the code for this video.

Episodes