Symfony is a powerful PHP framework that can run on shared hosting with the right configuration. In this guide, we will walk you through the process of installing and configuring Symfony on your shared hosting account, ensuring that you can deploy your applications smoothly and efficiently.

Prerequisites

Requirements

  • PHP 8.1 or higher
  • MySQL 5.7+ or MariaDB 10.3+
  • SSH access (strongly recommended)
  • Composer
  • Minimum 512MB disk space

PHP Extensions

Before you start, ensure that your hosting environment has the necessary PHP extensions installed:

  • Ctype
  • iconv
  • JSON
  • PCRE
  • Session
  • SimpleXML
  • Tokenizer

Optional Extensions

While the above extensions are required, having the following optional extensions can enhance your Symfony application:

  • APCu or Redis (for caching)
  • Intl
  • PDO with driver
  • Mbstring

Installation Methods

Connecting via SSH is the most efficient way to install Symfony. Follow these steps:

ssh username@yourserver.com

Once connected, navigate to your domain's directory:

cd ~/domains/yourdomain.com

Now, create a new Symfony project:

symfony new my_project --version=stable

Alternatively, you can use Composer:

composer create-project symfony/skeleton my_project

Method 2: Upload

  1. Create your Symfony project locally.
  2. Run composer install to install dependencies.
  3. Upload the project files to your server using FTP or any file manager.
  4. Configure your web server to point to the public directory.

Project Structure

Understanding the Symfony project structure is crucial for effective development. Here are the important directories:

  • /bin - Contains console commands.
  • /config - Holds configuration files.
  • /public - The web root (document root).
  • /src - Your PHP code resides here.
  • /templates - Contains Twig templates for rendering views.
  • /var - Used for cache and logs.
  • /vendor - Contains third-party dependencies.

Web Server Configuration

Document Root

It is essential to point your web server's document root to the /public directory. You can achieve this in two ways:

cd ~/domains/yourdomain.com
rm -rf public_html
ln -s my_project/public public_html

Option 2: Subdomain

Create a subdomain that points directly to the /public directory.

.htaccess

In the public directory, create or edit the .htaccess file to include the following:

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Environment Configuration

.env File

In the root of your project, create a .env file with the following content:

APP_ENV=prod
APP_SECRET=your_secret_key
DATABASE_URL=mysql://user:password@127.0.0.1:3306/database

Generate Secret

To generate the secret key, run:

bin/console secrets:generate-keys

Alternatively, you can generate a random string for APP_SECRET.

Database Setup

Create Database

  1. Log in to DirectAdmin and navigate to MySQL Management.
  2. Create a new database and note the credentials.

Configure Connection

In your .env file, configure the database connection:

DATABASE_URL=mysql://username:password@localhost:3306/database_name?serverVersion=8.0

Run Migrations

To set up your database schema, run:

bin/console doctrine:migrations:migrate

File Permissions

Set Permissions

Ensure that the necessary directories have the correct permissions:

chmod -R 755 var
chmod -R 755 public

Clear Cache

To clear the cache, run:

bin/console cache:clear
bin/console cache:warmup

Production Configuration

Environment

Set the following in your .env file:

APP_ENV=prod
APP_DEBUG=false

Cache

Clear and warm up the cache for production:

bin/console cache:clear --env=prod
bin/console cache:warmup --env=prod

Composer Production

Install dependencies for production:

composer install --no-dev --optimize-autoloader

Common Issues

500 Error

If you encounter a 500 error, check the following:

  1. Verify your PHP version.
  2. Check file permissions.
  3. Review var/log for detailed error messages.

Blank Page

For a blank page, try the following:

  1. Enable debug mode temporarily.
  2. Check your error logs.
  3. Verify your .htaccess file.

Cache Issues

If you suspect cache issues, clear the cache with:

rm -rf var/cache/*
bin/console cache:clear

Security

Protect Files

Secure sensitive directories by setting appropriate permissions:

  • .env
  • var/
  • config/

HTTPS

To enhance security, force HTTPS in your configuration settings.

Asset Management

Webpack Encore

For production, build your assets using:

npm run build

Your assets will be located in public/build/.

Asset Configuration

Configure your assets in webpack.config.js to ensure they are processed correctly.

Maintenance

Update Dependencies

Keep your dependencies up to date by running:

composer update

Update Symfony

To update Symfony specifically, use:

composer update symfony/*

Clear Cache

Regularly clear the cache to prevent issues:

bin/console cache:clear
bin/console doctrine:cache:clear-metadata

Performance

OPcache

Enable OPcache in your PHP configuration to improve performance.

Production Mode

Ensure your application is running in production mode:

APP_ENV=prod
APP_DEBUG=false

Preloading

For PHP 7.4 and above, configure preloading to enhance performance.

Logging

Production Logs

Monitor your application logs at var/log/prod.log for any issues.

Log Configuration

Configure logging settings in monolog.yaml to customize your logging behavior.

Need Help?

If you run into any issues or have questions, our support team is here to help you! Feel free to reach out through our ticket system - we typically respond within a few hours and are eager to assist you.