Sling Academy
Home/PHP/How to deploy a Symfony app to an Ubuntu server

How to deploy a Symfony app to an Ubuntu server

Last updated: January 13, 2024

Introduction

Deploying a Symfony application to an Ubuntu server involves several key steps. This tutorial will guide you through the process from pre-requisites to successfully running your Symfony app on an Ubuntu server. We assume that you have a Symfony application already built and ready to deploy.

Prerequisites

  • A VPS or a physical server running Ubuntu.
  • Terminal access to your Ubuntu server.
  • Basic command-line knowledge.
  • SSH credentials for your server.
  • Your Symfony application.

The Main Steps

Step 1: Setting up the Server

Start by updating the package manager data and upgrading any existing packages:

sudo apt-get update
sudo apt-get upgrade

After your server packages are up to date, install the necessary software:

sudo apt-get install apache2 php libapache2-mod-php php-cli
sudo apt-get install php-intl php-xml php-mysql php-mbstring php-curl php-zip

Enable the Apache rewrite module to allow Symfony’s .htaccess redirection to function:

sudo a2enmod rewrite

Step 2: Database Setup (Optional)

If your application uses a database, install MySQL or MariaDB and create your project’s database:

sudo apt-get install mysql-server
mysql -u root -p
CREATE DATABASE symfony;
GRANT ALL PRIVILEGES ON symfony.* TO 'symfony_user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;

Step 3: Configuring Apache

Create a virtual host configuration for your Symfony application:

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/symfony/public
    <Directory /var/www/symfony/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>
</VirtualHost>

Enable the site and restart Apache to load the new configuration:

sudo a2ensite yourdomain.conf
sudo systemctl restart apache2

Step 4: Deploying Symfony Application

Transfer your Symfony application to the server via rsync or FTP:

rsync -avz /path/to/symfony user@yourserver:/var/www/symfony

Set the correct permissions for the Symfony var directory:

sudo chown -R www-data: /var/www/symfony/var

Install Composer dependencies (if Composer is not installed, first ensure you install it):

cd /var/www/symfony
composer install --no-interaction --prefer-dist --optimize-autoloader

Step 5: Environment Configuration

Create an environment variable file to store sensitive information:

cp /var/www/symfony/.env /var/www/symfony/.env.local

Edit the .env.local file, setting the parameters (e.g., database URL) required for production:

nano /var/www/symfony/.env.local

Advanced Configuration (CI/CD)

In advanced scenarios, you might want to set up a continuous integration / continuous deployment (CI/CD) pipeline using tools like Jenkins, GitLab CI/CD, or GitHub Actions. Ensure your application code is stored in a Git repository and automate the testing and deployment process.

Here’s a basic example using GitHub Actions scripts:

name: Symfony CI

on:
  push:
    branches: [ master ]

tasks:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ['7.4']

    steps:
    - uses: actions/checkout@v2
    - name: Setup PHP with extensions
      uses: shivammathur/setup-php@v2
      with:
        php-version: 
    - name: Validate composer.json and composer.lock
      run: composer validate
    - name: Install dependencies
      run: composer install --prefer-dist --no-progress --no-suggest
    - name: Execute tests
      run: ./vendor/bin/simple-phpunit

Conclusion

With these steps, you’ve successfully deployed a Symfony app on an Ubuntu server. Remember to periodically maintain your application by updating dependencies, applying security patches, and optimizing the server’s performance.

Next Article: Working with Symfony Configuration Files

Previous Article: How to Dockerize a Symfony App for Production

Series: Symfony & Doctrine Tutotirlas

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array