Symfony is a free and open-source PHP web application framework and set of reusable PHP components for developing web applications. It is specially designed for those who need a simple toolkit to create a high-performance web applications. It was developed by Fabien Potencier and sponsored by SensioLabs.
In this tutorial, we’ll show you how to install Symfony framework with Nginx on Ubuntu 20.04 server.
Requirements
- A server running Ubuntu 20.04 server.
- A root password is configured on your server.
Install LEMP Server
First, you will need to install Nginx, MariaDB Server, PHP and other required packages on your server. You can install all of them with the following command:
# apt-get install nginx mariadb-server php php-fpm php-mbstring php-opcache php-xml php-zip php-curl php-mysql php-cli curl git -y
Once all the packages are installed, open php.ini file and tweak some settings:
# nano /etc/php/7.4/fpm/php.ini
Change the following lines:
memory_limit = 256M
cgi.fix_pathinfo = 0
safe_mode = Off
max_execution_time = 120
max_input_time = 300
date.timezone = "America/Chicago"
Save and close the file when you are finished.
Next, you will need to install Composer in your system. Composer is a dependency manager for PHP. You can install Composer with the following command:
# curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Once the Composer has been installed, you should see the following output:
All settings correct for using Composer
Downloading...
Composer (version 2.7.0) successfully installed to: /usr/local/bin/composer
Use it: php /usr/local/bin/composer
Once you are done, you can proceed to the next step.
Create a New Symfony Project
Next, you will need to create a sample project for Symfony. You can create it by running the following command:
# cd /var/www/html
# composer create-project symfony/skeleton symfony-blog
Next, set proper permission on Symfony project:
# chown -R www-data: /var/www/html/symfony-blog
Once you have done, you can proceed to the next step.
Configure Nginx for Symfony
Next, you will need to create a new virtual host configuration file for Symfony. You can create it with the following command:
# nano /etc/nginx/sites-available/symfony
Add the following lines:
server {
listen 80;
listen [::]:80;
server_name your-server-ip;
root /var/www/html/symfony-blog/public;
index index.php;
client_max_body_size 100m;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(?:ht|git|svn) {
deny all;
}
}
Save and close the file when you are finished. Then, check Nginx for any syntax error with the following command:
# nginx -t
Next, enable the Nginx virtual host and restart the Nginx and PHP-FPM service with the following command:
# ln -s /etc/nginx/sites-available/symfony /etc/nginx/sites-enabled/
# apt purge apache2 -y && systemctl restart nginx
# systemctl restart php7.4-fpm
Access Symfony
Symfony is now installed and configured, it’s time to access the Symfony web interface.
Open your web browser and type the URL http://your-server-ip. You will be redirected to the Symfony default page as shown below:
Congratulations! you have successfully installed and configured Symfony with Nginx and PHP-FPM on Ubuntu 20.04 server. You can also install the database server and integrate it with Symfony. Symfony is a good choice for enterprise applications due to its flexibility and simplicity.