How to host Symfony on Digitalocean Droplet - ServerAvatar

Published : Sep 14, 2024

Share This Post

cloud-platform

Digitalocean

Digitalocean is one of the most popular cloud platform right now. It offers on-demand cloud computing for various use cases as well as some managed solutions.

Get started with Digitalocean
application

Symfony

Symfony is a powerful PHP framework used to develop web applications. It offers a robust set of features and tools to help streamline the development process.

More info

Symfony is an open-source PHP framework known for its flexibility and comprehensive features. It allows developers to efficiently create and manage sophisticated web applications, providing routing, templating, and security tools. With its extensive range of components and customization options, Symfony facilitates the creation of web solutions without requiring extensive technical knowledge. DigitalOcean is a preferred choice for hosting Symfony applications due to its scalable infrastructure and straightforward deployment features. ServerAvatar simplifies the process by providing DigitalOcean VPS hosting and offering an intuitive, step-by-step approach to deploying Symfony applications.

Create Server in Digitalocean

Digitalocean is one of the most popular cloud platforms right now. It offers on-demand cloud computing for various use cases as well as some managed solutions. We can deploy any PHP-based applications on Digitalocean Droplets. Droplets are the servers or cloud computing instances in Digitalocean. If you want to learn about droplet management in-depth in Digitalocean, We recommend you to read our Digitalocean guide.

To deploy any application on Digitalocean, We first need a Droplet. So, we will first create a droplet and perform the initial setup.

Step 1: Go to the Create Droplets page.

Log in to your Digitalocean account and click on the Create button given on the top-right corner of the page. From the drop-down menu, click on the Droplets option to see a droplet creation form.

Create a Droplet - Step 1

Step 2: Choose Region

On the Droplet creation page, you will find several sections for selecting different options. We will proceed step by step.

The first thing you have to do is select the region for your droplet. Digitalocean provides several options when it comes to selecting the location. The general rule is to select the region that is closest to your audience. It means that for the majority of your audience, Your sites will load faster due to low latency.

Create a Droplet - Step 2

Step 3: Choose an Image or an OS

In this step, you have to select the operating system for your droplet. Generally, the Symfony application requires Ubuntu 18.04 LTS or a later version for compatibility and support.

However, We recommend you to use Ubuntu 20.04 LTS x64 or Ubuntu 22.04 LTS x64. These operating systems are a requirement if you are using ServerAvatar to manage your droplets.

Create a Droplet - Step 3

Step 4: Choose the size of the Droplet

Here you have to choose the size of your Droplet. As we mentioned above, Digitalocean provides many different types of droplets. Select the one that fits best for your project.

Create a Droplet - Step 4

If your site is brand new and does not have much traffic, start with a small plan. Once the project starts getting traction, you can upgrade the plan to handle more visitors on the site.

Step 5: Choose Authentication Method

Now you have to select the authentication method. There are two types of authentication methods. They are as follows:

  1. Authentication via SSH Keys
  2. Authentication via Password

Using SSH keys for authentication is the best option. If you are not familiar with SSH keys, you can choose the password method. Make sure to set a strong password for your root user, which is the most powerful user with all the privileges on Linux OS.

Create a Droplet - Step 5

Step 6: Selecting Additional Options

There are three additional options you can choose from. They are optional but important in different use cases. The additional options are:

  1. Monitoring: It is a free feature by Digitalocean. It allows you to see additional monitoring data for your server.
  2. Backups: We recommend you to enable full server backups if you are going to deploy in a production environment. The cost of the backups depends on the size of the droplet.
  3. Managed Database: You do not need a Managed database if you are using ServerAvatar to manage sites on a droplet. A managed database is a pre-configured MySQL Droplet.

Step 7: Finalise Details

In the last step of this process, We have to enter the name of the droplet, and the number of droplets you want to create, Define any tags, and select the project. Enter all the details and click on the Create Droplet button to create a droplet.

Create a Droplet - Step 7

In a few minutes, You will have the Droplet up and running. Now, We have to perform the initial configuration on the Droplet to host PHP-based frameworks and software.

Initial Server Configuration

The Initial server configuration includes the installation and configuration of various packages required to host your website. Usually, You have to write commands and modify configuration files. Luckily, With ServerAvatar, Your full server configuration and optimization can be automated.

Here are the three ways to automatically configure your server with ServerAvatar.

Install and Setup Symfony on Cloud using ServerAvatar

Getting Started With Symfony:

Symfony is a powerful PHP framework used to develop web applications. It offers a robust set of features and tools to help streamline the development process. Whether you're building a simple blog or a complex enterprise application, Symfony provides the structure and flexibility you need. In this blog post, we will guide you through the steps to create and configure your Symfony application, ensuring you have a solid foundation to start your development journey.

Requirements:

  • PHP 8.2 or higher: Symfony requires a modern version of PHP to leverage the latest features and improvements. Ensure you have PHP 8.2 or a higher version installed on your machine.

Install and Setup Symfony on Cloud using ServerAvatar:

Step 1: Create a Custom Application

  • The first step is to create a new custom application. This can be done easily from your server dashboard. Navigate to the application section and click on the Create button to set up your new application.

Install and Setup Symfony on Cloud using ServerAvatar

  • To proceed with the installation and deployment of Symfony, ensure you have met the prerequisites.

  • Enter an Application Name of your choice.

  • Specify a Domain (choose between a Test Domain or a Primary Domain).

    • Note: If you're hosting Symfony on your domain, make sure your domain is correctly pointed to your server's current IP address.
  • Select a Method as a Custom from the given options. Click on the Create Application option.

Install and Setup Symfony on Cloud using ServerAvatar

Step 2: Enable SSH Credentials

  • To securely access your server, you need to enable SSH credentials. SSH (Secure Shell) provides a secure way to log in to your server remotely, ensuring that your data and commands are encrypted. This step is crucial for managing your server and deploying your Symfony application.

Install and Setup Symfony on Cloud using ServerAvatar

Step 3: Login Using SSH Credentials

  • Once you have enabled your SSH credentials, you can log in to your server. Open a terminal and use the following command:
    • sudo ssh {username}@{ip}
  • Replace {username} with your actual username and {ip} with your server's IP address. You will be prompted to enter your password. After successful authentication, you will have remote access to your server.

Install and Setup Symfony on Cloud using ServerAvatar

Step 4: Navigate to the Application Path

  • With SSH access established, navigate to your application's directory on the server. This is where you will set up your Symfony project. Use the cd command to change directories to your application's root path.

Install and Setup Symfony on Cloud using ServerAvatar

Step 5: Remove the Default index.html

  • Your server might have a default index.html file in the application directory. To avoid conflicts and ensure a clean setup, remove this file by running:
    • rm -rf index.html

Install and Setup Symfony on Cloud using ServerAvatar

Step 6: Run Composer to Create a New Symfony Project

  • Composer is a dependency manager for PHP that makes it easy to install and manage libraries and frameworks. Use Composer to create a new Symfony project in your current directory by executing:
    • composer create-project symfony/skeleton .

Install and Setup Symfony on Cloud using ServerAvatar

  • This command downloads and sets up the basic structure of a Symfony application.

Step 7: Install Webapp Pack

  • To add essential tools and features to your Symfony project, you need to install the Symfony web app pack. Run the following command:
    • composer require symfony/webapp-pack
  • This package includes commonly used bundles and components that will help you develop your web application efficiently.

Step 8: Configuration Without Docker

  • During the setup process, you may be prompted to configure the application with Docker. For simplicity, choose not to use Docker by entering n when prompted. This will configure Symfony to run in a traditional server environment.

Install and Setup Symfony on Cloud using ServerAvatar

Step 9: Set Custom Webroot

  • Next, set the custom webroot as public for your application. This can be done from the application dashboard in the application settings. The webroot is the directory where your web server looks for the application files to serve.

Install and Setup Symfony on Cloud using ServerAvatar

Step 10: Fix Permissions

  • Fix file permissions are essential for your application to run smoothly. Adjust the permissions from the application dashboard in the application settings to ensure your web server can access and modify the necessary files.

Install and Setup Symfony on Cloud using ServerAvatar

Step 11: Verify Installation

  • To confirm that your Symfony installation is successful, visit your application domain in a web browser. You should see the Symfony welcome page, indicating that your setup is complete and your application is ready for development.

Install and Setup Symfony on Cloud using ServerAvatar

Step 12: Configure Environment File

  • Edit the .env File: The .env file in your Symfony project contains environment-specific settings, such as database credentials and configuration options. Open this file in a text editor and update the settings according to your environment.

Step 13: Set Up the Database

  • Create a Database: From the server dashboard, navigate to the databases section and create a new database. This database will store your application's data.

Install and Setup Symfony on Cloud using ServerAvatar

  • Configure Database Credentials: Update the .env file with your database credentials. Replace the placeholders with your actual database information:
    • DATABASE_URL=”mysql://db_user:db_password@127.0.0.1:3306/db_name

Install and Setup Symfony on Cloud using ServerAvatar

  • Run Migrations: If your Symfony application includes database migrations, run them to set up your database schema. Migrations are a way to define changes to your database structure in a version-controlled manner. Execute the following command to apply the migrations:
    • php bin/console doctrine:migrations:migrate

Install and Setup Symfony on Cloud using ServerAvatar

Conclusion:

Congratulations! You have successfully created and configured your Symfony application. With your environment set up and your database connected, you're now ready to start building your web application using Symfony's powerful tools and features. Whether you're developing a small personal project or a large-scale enterprise application, Symfony provides the flexibility and capabilities to help you achieve your goals. Enjoy your journey with Symfony!