How to set up Symfony in Ubuntu

Symfony is a set of PHP Components, a Web Application framework, a Philosophy, and a Community — all working together in harmony.

Summary

The following steps will show you how to install and run Symfony on an Ubuntu 18.04 LTS based Nginx server.

After showing you how to set up this PHP framework in Ubuntu, we’ll be showing you what it takes to set up your Symfony Application using the Bunnyshell Platform.

How to set up Symfony in Ubuntu

Pre-Requisites

We will assume that you have a basic server based on Ubuntu running.

Before proceeding with the installation, it’s always a good idea to make sure your sources and existing software are updated.

# sudo apt-get update 

# sudo apt-get upgrade

Before Symfony, we need to install other components that are essential.

Installing Nginx

In order to display web pages to our site visitors, we are going to employ Nginx, as our web server.

# sudo apt update

# sudo apt install nginx

It is recommended that you enable the most restrictive profile that will still allow the traffic you want.

# sudo ufw allow ‘Nginx HTTP’

Verify the change by running:

# sudo ufw status

Output should show that HTTP traffic is allowed:

# Output

# Status: active

# To                         Action      From

# —                         ——      —-

# OpenSSH                    ALLOW       Anywhere

# Nginx HTTP                 ALLOW       Anywhere

# OpenSSH (v6)               ALLOW       Anywhere (v6)

# Nginx HTTP (v6)            ALLOW       Anywhere (v6)

If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by running the following command:

# ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//’

Type the following address in your browser to test that nginx is working.

# http://server_IP

nginx_default

Installing MySQL

Now that you have a web server, you need to install MySQL (a database management system) to store and manage the data for your site.

# sudo apt install mysql-server

To secure the installation you need to run mysql’s built in script:

# sudo mysql_secure_installation

Follow the steps to complete the securitization process.

Note that in Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password.

# sudo mysql

# mysql > SELECT user,authentication_string,plugin,host FROM mysql.user;

Output

+——————+——————————————-+———————–+———–+

| user             | authentication_string                     | plugin                | host      |

+——————+——————————————-+———————–+———–+

| root             |                                           | auth_socket           | localhost |

| mysql.session    | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |

| mysql.sys        | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |

| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |

+——————+——————————————-+———————–+———–+

4 rows in set (0.00 sec)

In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command:

# mysql > ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

# mysql > FLUSH PRIVILEGES;

Then you can check the auth privileges like mentioned earlier. You can now exit the mysql interface:

# mysql > exit;

Installing PHP and Configuring Nginx to Use it

Since Nginx does not contain native PHP processing like some other web servers, you will need to install php-fpm.

# sudo add-apt-repository universe

# sudo apt install php-fpm php-mysql

Next you need to edit your nginx site config:

# sudo nano /etc/nginx/sites-available/example.com

Add the following content to it:

server {

        listen 80;

        root /var/www/html;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name example.com;

        location / {

                try_files $uri $uri/ =404;

        }

        location ~ \.php$ {

                include snippets/fastcgi-php.conf;

                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

        }

        location ~ /\.ht {

                deny all;

        }

}

 

Here’s what each of these directives and location blocks do:

  • listen — Defines what port Nginx will listen on. In this case, it will listen on port 80, the default port for HTTP.
  • root — Defines the document root where the files served by the website are stored.
  • index — Configures Nginx to prioritize serving files named index.php when an index file is requested, if they’re available.
  • server_name — Defines which server block should be used for a given request to your server. Point this directive to your server’s domain name or public IP address.
  • location / — The first location block includes a try_files directive, which checks for the existence of files matching a URI request. If Nginx cannot find the appropriate file, it will return a 404 error.
  • location ~ \.php$ — This location block handles the actual PHP processing by pointing Nginx to the fastcgi-php.conf configuration file and the php7.2-fpm.sock file, which declares what socket is associated with php-fpm.
  • location ~ /\.ht — The last location block deals with .htaccess files, which Nginx does not process. By adding the deny all directive, if any .htaccess files happen to find their way into the document root they will not be served to visitors.

After adding this content, save and close the file. Enable your new server block by creating a symbolic link from your new server block configuration file (in the /etc/nginx/sites-available/ directory) to the /etc/nginx/sites-enabled/ directory:

# sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Then, unlink the default configuration file from the /sites-enabled/ directory:

# sudo unlink /etc/nginx/sites-enabled/default

If you need to restore the default configuration:

# sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

Test your new configuration file:

# sudo nginx -t

If any errors show up, recheck your file before continuing.

Reload Nginx to make the necessary changes:

# sudo systemctl reload nginx

Next you can create a php info file to test that your configuration works.

# sudo nano /var/www/html/info.php

Put the following content in it:

<?php

phpinfo();

Save and close the file.

Go to the following address in the browser:

# http://server_IP/info.php

php info

If you see a page that looks like this, you’ve set up PHP processing with Nginx successfully.

Now you can remove the php info file because it is a security risk:

# sudo rm /var/www/html/info.php

Installing Symfony

Before we finally delve into it, we also need Git version control to be installed. If you have it installed, you can skip the following step.

Download and install Git for Linux:

# sudo apt-get install git

Once the installation has successfully completed, the next thing to do is to set up the configuration details of the GitHub user.

# git config –global user.name “user_name”

# git config –global user.email “email_id”

To install Symfony, we need to install Composer first. It is a tool for dependency management in PHP that allows you to package all the required libraries associated with a package as one. To install Symfony and all its dependencies, Composer is required. 

# cd /tmp

# curl -sS https://getcomposer.org/installer | php

# sudo mv composer.phar /usr/local/bin/composer

The curl command downloads composer.phar package to your /tmp directory. But we would want composer to run globally hence we need to move it to /usr/local/bin/ directory under the name ‘composer’. Now we can run composer from anywhere.

Create a database and user for your application:

# mysql -u root -p

# mysql > CREATE DATABASE app;

# mysql > CREATE USER  ‘app-admin’@’localhost’ IDENTIFIED BY ‘password’;

# mysql > GRANT ALL PRIVILEGES ON app.* TO ‘app-admin’@’localhost’;

# mysql > FLUSH PRIVILEGES;

# mysql > exit;

Setup your symfony application by going to the www directory:

# cd /var/www

Next you can clone your application from github:

# git clone https://github.com/<your-user>/<your-repo>.git symfony-app

Configuring your environment variables:

# cd symfony-app

# sudo nano .env

Put the following content into your .env file:

APP_ENV=prod

APP_DEBUG=0

Next, install a PHP extension that Symfony apps use to handle XML:

# sudo apt install php7.2-xml

Next, you need to install the project dependencies, run composer install:

# cd /var/www/symfony-app

# composer install

Configure your database credentials:

Open the .env file you created earlier and add the following content to it:

DATABASE_URL=mysql://app-admin:[email protected]:3306/app

The Symfony framework uses a third-party library called Doctrine to communicate with databases. Doctrine gives you useful tools to make interactions with databases easy and flexible.

You can now use Doctrine to update your database with the tables from the cloned Github application. Run this command to do that:

# php bin/console doctrine:schema:update –force

Populating your Database Using Doctrine-Fixtures

At the moment, the newly created tables are empty. You will populate it using doctrine-fixtures. Using Doctrine-Fixtures is not a prerequisite for Symfony applications, it is only used to provide dummy data for your application.

# php bin/console doctrine:fixtures:load

Clearing and Warming Up your Cache

To ensure your application loads faster when users make requests, it is good practice to warm the cache during the deployment. Symfony has a command to clear the cache that also triggers a warm up. Run the following command for that purpose:

# php bin/console cache:clear

Configuring the Web Server and Running the Application

Create a new server block:

# sudo nano /etc/nginx/sites-available/app

Add the following content to the new server block configuration file. Ensure you replace the your_server_ip within the server block with your server IP address:

server {

    listen 80;

    listen [::]:80;

    server_name blog your_server_ip;

    root /var/www/symfony-app/public;

    index index.php;

    client_max_body_size 100m;

    location / {

        try_files $uri $uri/ /index.php$is_args$args;

    }

    location ~ \.php {

        try_files $uri /index.php =404;

        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_param SCRIPT_NAME $fastcgi_script_name;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        fastcgi_index index.php;

        include fastcgi_params;

      }

    location ~ /\.(?:ht|git|svn) {

        deny all;

    }

}

 

Enable the newly created server block:

# sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/

Check the new configuration file for errors:

# sudo nginx -t

When there are no errors  just reload nginx:

# sudo systemctl reload nginx

Finally, you can now run and test out the application. Visit http://your_server_ip in your favorite browser:

# http://your_server_ip

Setting up Symfony using Bunnyshell

  1. Go to the application section and select the Frameworks tab;
  2. Select Symfony;
  3. Select an existing virtual machine or leave it blank to create a new one;
  4. Select your desired cloud provider and your machine location and size;
  5. Give your application a name and create it.

symfony

These are the steps required to get a virtual machine that is set up to run your Symfony application without actually having to go through all the steps detailed above.

After the virtual machine is created, you’re all set to start deploying your application, and no, you won’t need to use an FTP client to upload your files manually, all you have to do is go to Applications and open the application you just created, select Deployments and click enable deployment.

deployment symfony

This will allow you to configure a repository to use as a codebase and allow you to select the branch you want to have deployed. Needless to say, this can be helpful for configuring staging and production environments based on repository branches.

set up symfony

From here you can also manage your backups and restores for the application, you can simply create a backup or restore your application with a few simple clicks, no more logging in to the server and running commands.

backup

Conclusion

As you can see, in order to set up Symfony in Ubuntu, you need to complete a lot of steps. However, if you choose to set up Symfony in the cloud with Bunnyshell, it’s a simpler process with lots of automation that makes your life easier and helps take your mind off the DevOps side of development.