Blog

Blog

Deploying Symfony Application on Shared Hosting with cPanel: A Beginner’s Tutorial

Symfony is a robust PHP framework that provides a wide range of functionalities for web development. Deploying Symfony applications on shared hosting with cPanel can be daunting for beginners. However, with the right guidance, it can be a simple process. In this tutorial, I’ll guide you through the steps of deploying a Symfony application on shared hosting.

Before you can deploy your Symfony application, you need to choose the right hosting provider. Shared hosting can be a good option for small to medium-sized projects that do not require high levels of customization or scalability. When selecting a hosting provider, look for one that supports the latest version of PHP and has a control panel that allows you to manage your files and databases.

Assuming that you already have your application code prepared for deployment, let’s proceed with the deployment of your application on shared hosting with Apache and cPanel.

Prepare your Symfony Application

Once you have chosen your hosting provider, you need to prepare your Symfony application for deployment. Here are the specific folders and files (including subfolders and files as they are) that the Symfony framework requires for proper functionality.

PROJECT_FOLDER
 |- /config
 |- /public
 |- /src
 |- /templates
 |- /vendor
 |- .env
 |- composer.json

Configure Apache to read .env file

Apache does not read the .env file by default. The .env file is used by Symfony to store environment-specific configuration variables, such as database credentials and API keys. The reason Apache does not read the .env file is that it is typically located outside of the web root directory and contains sensitive information that should not be accessible by anyone with access to the web server.

To make the environment variables defined in the .env file available to your Symfony application running on Apache, you need to use an environment variable loader. The environment variable loader will read the .env file and make the variables available to your application.

One popular environment variable loader for Symfony is the DotEnv component, which can be installed using Composer by running the following command:

composer require symfony/dotenv

After installing the DotEnv component, you need to load the environment variables in your application’s public/index.php file by adding the following code at the top of the file:

use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../.env');

Finally, the following code should be present in the public/index.php file.

<?php

use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__) . '/vendor/autoload_runtime.php';

$dotenv = new Dotenv();
$dotenv->load(__DIR__ . '/../.env');

return function (array $context) {
  return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

This code will load the environment variables from the .env file to the Symfony application.

Setting up the .htaccess file

The .htaccess file is a critical file in Symfony applications, as it controls how URLs are handled by the web server. Ensure that your .htaccess file is correctly configured for your shared hosting account. In order to configure the rewrite rules file correctly on the Apache server we need to install the following package.

composer require symfony/apache-pack

This pack installs a .htaccess file in the public directory that contains the rewrite rules needed to serve the Symfony application.

In addition to the .htaccess file located above, it is necessary to create another .htaccess file in the root directory and include the following code. This will enable Apache to redirect its search to the public folder.

RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

It is important to create two separate .htaccess files. The first should be located within the public directory, which is automatically generated by the symfony/apache-pack package to handle URLs. The second .htaccess file should be created in the root directory to instruct Apache to search the “public” directory.

Set up the Database – MySQL

If your application requires data management using a database, you will need to set it up. You can do this using the control panel provided by your hosting provider.

Create a new MySQL Database and User. Make sure to grant all privileges to the user on the database. Once you have created the database and user, update the database configuration in the .env file of your Symfony application.

DATABASE_URL=mysql://database_user:password@localhost/database_name

Replace database_user, password, and database_name with the username, password, and the database name you created earlier.

Uploading Your Symfony Application Files

After preparing your application, you need to upload it to your hosting server. You can do this using either FTP or a web-based file manager provided by your hosting provider.

Upload all the files and directories from your Symfony project directory to the desired directory on your hosting server. Ensure that the directory structure remains intact.

Testing your Symfony Application

After configuring your Symfony application, it is essential to test it to ensure it works correctly on your shared hosting account. Visit your application’s URL in a web browser and verify that it loads correctly. You should also test your application’s functionality to ensure everything works as expected.

Conclusion

Deploying Symfony applications on shared hosting can be straightforward for beginners if done correctly. With this article and the code examples provided, you should have the knowledge you need to get started and deploy your Symfony application on shared hosting.

If you face any difficulties while deploying your Symfony application on shared hosting, don’t hesitate to ask for help. Feel free to leave a comment below, and I’ll be happy to assist you.

Want to create a remarkable online presence or need some help?

I’m always open to discussing web development work or fellowship possibilities. Drop me an email, text, or call.

>