Symfony is a widely-used PHP framework that’s lauded for its robustness, flexibility, and comprehensive community support. This article will guide you through the process of installing Symfony Framework on a Rocky Linux 9 system, a popular open-source operating system based on Red Hat Enterprise Linux.
Before we begin, ensure you meet the following prerequisites:
- You’re running Rocky Linux 9 on your server.
- You have a fresh OS install to prevent potential issues.
- You have SSH access to the server (or just open Terminal if you’re on a desktop).
- You have an active internet connection to download necessary packages and dependencies.
- You have a
non-root sudo user
or access to theroot user
. Acting as anon-root sudo user
is recommended as you can potentially harm your system if you’re not careful when acting as the root. - The Extra Packages for Enterprise Linux (EPEL) repository is enabled. This provides additional packages not included in the default Rocky Linux repositories.
Step-by-step Guide
Step 1: Update System Packages
Start by updating all system packages to their latest versions to prevent potential conflicts. You can do this with the following commands:
sudo dnf update sudo dnf install dnf-plugins-core
Step 2: Install PHP 8
Use Remi’s repository to install PHP 8.1 and the required extensions:
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm sudo dnf module reset php sudo dnf module enable php:remi-8.1 sudo dnf install php php-fpm php-mbstring php-xml php-gd php-json php-mysqlnd php-zip php-intl
You can verify the installed PHP version by running: php -v
.
Step 3: Install Composer
Next, download and install Composer, the PHP package manager:
cd ~ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Step 4: Install MySQL and Create a Database
For our Symfony application, we’ll use MySQL as the database. Install MySQL server using dnf
:
sudo dnf install mysql-server
Start the MySQL service and enable it to start after system reboots:
sudo systemctl start mysqld sudo systemctl enable mysqld
Run mysql_secure_installation
to improve MySQL security. Then, log in with the root MySQL user:
mysql -u root -p
Create a database for Symfony:
CREATE DATABASE symfony_db;
Step 5: Install Nginx and Configure Symfony Stack
We’ll use Nginx as our web server. Install Nginx from the repository:
sudo dnf install nginx
Start Nginx and set it to start automatically on reboot:
sudo systemctl start nginx sudo systemctl enable nginx
Configure Nginx to work with PHP-FPM, which will process PHP files:
sudo nano /etc/nginx/nginx.conf
Uncomment the following lines by removing the # symbol:
include /etc/nginx/default.d/*.conf; server { listen 80; # note that these lines are originally from the "location /" block index index.php index.html index.htm; try_files $uri $uri/ =404; }
Save and exit from the file after making the changes, then test the Nginx configuration and restart it:
sudo nginx -t
sudo systemctl restart nginx
Step 6: Install Symfony Framework on Rocky Linux 9
Now, download the Symfony installer to get the symfony
CLI command:
wget https://get.symfony.com/cli/installer -O - | bash
Ensure the Symfony CLI is correctly installed by checking its version: symfony -v
.
Step 7: Create a New Symfony Project
Use the Symfony CLI to create a new project. This command sets up a new Symfony project in the specified directory:
symfonynewmy_project --webapp
Set permissions for the web server:
chown -R nginx:nginx my_project chmod -R 755 my_project
This will create a new Symfony project called my_project
with a default directory structure.
Step 8: Configure Nginx Web Server
Now, we’ll configure Nginx to serve our Symfony application using the my_project/public
directory.
Create an Nginx virtual host configuration file:
sudo nano /etc/nginx/conf.d/myproject.conf
Add the following configuration:
server { listen 80; listen [::]:80; root /home/youruser/symfony-project/my_project/public; index index.php index.html index.htm; server_name symfony-project.test; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \\\.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Check the Nginx configuration and reload it:
sudo nginx -t
sudo systemctl reload nginx
Step 9: Configure Environment Variables
Update the .env
file with your database credentials:
nano .env
Update the following lines with your database credentials:
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/symfony_db"
Replace db_user
, db_password
with your actual MySQL credentials and symfony_db
with your database name.
Step 10: Install Symfony Dependencies
Navigate to your project directory and use Composer to install PHP dependencies for Symfony:
cd my_project composer install
This will install all libraries that Symfony requires including Twig, Doctrine, etc. under the vendor/
folder.
Step 11: Initialize the Database Schema
The final step is to build our database schema which can be done with the following commands:
php bin/console doctrine:database:create php bin/console make:migration php bin/console doctrine:migrations:migrate
This creates the configured database, makes an initial migration, and runs it to build all defined database tables and relationships.
At this point, our Symfony setup is complete! The application can be accessed via the configured Nginx virtual host: http://symfony-project.test
.
With the Symfony Framework now installed, you’re ready to start building robust web applications. Remember to check the official Symfony website for additional help or useful information.
Cloud Vps from Shape.host are an excellent choice for hosting your Symfony applications. They offer a unique blend of power, reliability, and affordability, backed by top-notch customer service. Be sure to check them out!