AWS Google Cloud

Install Symfony 5 with PHP 7.4 on Ubuntu with Nginx Let’s Encrypt – AWS

Install Symfony 5 with PHP 7.4 on Ubuntu with Nginx and Let’s Encrypt. In this guide you are going to learn how to install Symfony an opensource PHP framework used to build web applications, APIs or microservices and many more.

You will also install Nginx and secure your isntallation with Let’s Encrypt free SSL certificate.

This setup is tested on AWS, so it will work on other cloud hosting providers like Google Cloud, Azure, etc or any VPS or any dedicated servers.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites for AWS

  1. A running EC2 Instance. Learn how to create an AWS EC2 instance.
  2. Assigned a Elastic IP to your EC2 Instance.
  3. Setup Amazon RDS and connect it with EC2 Instance.
  4. Setup and configure Route 53 and point your domain to AWS.
  5. Successful SSH connection to your EC2 Instance.

SSH to your EC2 Instance and perform the steps listed below.

Initial Server Setup

Let’s start by updating the local package index with the following command to the latest available version.

sudo apt update
sudo apt upgrade

Once the update is done you can start installing the required packages.

Install Nginx

To install Nginx execute the following commands.

sudo apt install nginx

Now Nginx is installed and you need to configure firewall.

Configure Firewall with UFW

Now you can set up Uncomplicated Firewall (UFW) with Nginx to allow public access on default web ports for HTTP and HTTPS

sudo ufw app list

You will see all listed applications.

Output
Available applications:
   Nginx Full
   Nginx HTTP
   Nginx HTTPS
   OpenSSH

Now we will allow firewall for SSH and for port 80 and 443.

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'

With this command you can view the status of UFW.

sudo ufw status

You will see the output as follows.

Output
Status: active
 To                         Action      From
 --                         ------      ----
 Nginx Full                 ALLOW       Anywhere                  
 OpenSSH                    ALLOW       Anywhere                  
 Nginx Full (v6)            ALLOW       Anywhere (v6)             
 OpenSSH (v6)               ALLOW       Anywhere (v6)

Install PHP 7.4 FPM

Execute the following command to install PHP 7.4 FPM and some required extensions using the following commands.

Add the ondrej/php which has PHP 7.4 FPM package and other required PHP extensions.

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Install PHP 7.4 and extensions.

sudo apt install php7.4 php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl php7.4-bcmath unzip -y

Install Composer

To install Symfony latest version you need to use Composer to download Symfony. Execute the following command to install Composer.

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer

Download Symfony 5

Once you have Nginx, PHP and Composer installed you can go ahead and download Symfony using Composer.

Navigate to your web root directory and proceed to download Symfony.

cd /var/www/html
sudo composer create-project symfony/skeleton symfony5

If you are using micro machine types your installation gets killed because of low memory, in this case you can follow this guide to setup swap space. This method allows you to install Symfony on a micro machine.

Once the installation is completed you need to setup correct permissions.

sudo chown -R www-data:www-data /var/www/html/symfony5/
sudo chmod -R 755 /var/www/html/symfony5/

Configure Nginx for Symfony

Start by disabling default Nginx configurations.

sudo rm /etc/nginx/sites-available/default
sudo rm /etc/nginx/sites-enabled/default

Create a new Nginx configuration for your website in the sites-available directory.

sudo nano /etc/nginx/sites-available/domainname.com

Copy and paste the following configuration, ensure that you change the domain name with your domain name.

 server {
     listen 80;
     listen [::]:80;
     server_name domainname.com www.domainname.com;

     error_log /var/log/nginx/error.log;

     root  /var/www/html/symfony5/public/;
     index index.html index.php;

     client_max_body_size 100M;

     location / {
         try_files $uri $uri/ /index.php?$args;
     }

     location = /favicon.ico {
         log_not_found off;
         access_log off;
     }

     location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+.php)(/.+)$;
         fastcgi_pass unix:/run/php/php7.4-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
         fastcgi_param DOCUMENT_ROOT $realpath_root;
         fastcgi_read_timeout 3600;
         fastcgi_index index.php;
         fastcgi_buffers 16 16k; 
         fastcgi_buffer_size 32k;
         include fastcgi_params;
         internal;
     }
} 

Hit Ctrl+X followed by Y and Enter to save the file and exit.

To enable this newly created website configuration, make a symbolic link of the file that you just created into the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/domainname.com /etc/nginx/sites-enabled/domainname.com

Install Let’s Encrypt SSL for Nginx

HTTPS is a protocol for secure communication between a server (instance) and a client (web browser). Due to the introduction of Let’s Encrypt, which provides free SSL certificates, HTTPS are adopted by everyone and also provides trust to your audiences.

sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-nginx

Now we have installed Cert bot by Let’s Encrypt for Ubuntu 18.04, run this command to receive your certificates.

sudo certbot --nginx certonly

Enter your email and agree to the terms and conditions, then you will receive the list of domains you need to generate SSL certificate.

To select all domains simply hit Enter

This will automatically generate the new certificate for your domain.

Open your Nginx configuration and modify it to allow HTTPS connections and install the SSL certificate.

 sudo nano /etc/nginx/sites-available/domainname.com

The following configuration will redirect all http traffic to https with www. If you need to redirect without www you can modify easily.

 server {
    listen [::]:80;
    listen 80;
    
    server_name domain.com www.domainname.com;
    
    return 301 https://www.domainname.com$request_uri;
}

 server {
     listen [::]:443 ssl http2;
    listen 443 ssl http2;
     server_name domainname.com; 

     ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem; 

     error_log  /var/log/nginx/error.log; 

     root  /var/www/html/symfony5/public/;
     index index.html index.php;

     return 301 https://www.domainname.com$request_uri; 
} 
 
server {
     listen 443 ssl http2;
     listen [::]:443 ssl http2;
     server_name www.domainname.com;

     ssl_certificate /etc/letsencrypt/live/domainname.com/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/domainname.com/privkey.pem;  

     error_log  /var/log/nginx/error.log;

     root  /var/www/html/symfony5/public/;
     index index.html index.php;

     location / {
         try_files $uri $uri/ /index.php?$args;
     }

     location = /favicon.ico {
         log_not_found off;
         access_log off;
     }

     location ~ \.php$ {
         try_files $uri =404;
         fastcgi_split_path_info ^(.+.php)(/.+)$;
         fastcgi_pass unix:/run/php/php7.4-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
         fastcgi_param DOCUMENT_ROOT $realpath_root;
         fastcgi_read_timeout 3600;
         fastcgi_index index.php;
         fastcgi_buffers 16 16k; 
         fastcgi_buffer_size 32k;
         include fastcgi_params;
         internal; 
     }
} 

The http2 value is all that is needed to enable the HTTP/2 protocol.

Hit CTRL+X followed by Y to save the changes.

Check your configuration and restart Nginx for the changes to take effect.

sudo nginx -t
sudo service nginx restart

Step 12: Renewing SSL Certificate

Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. So, let’s test the renewal feature using the following command.

Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. Now you set up a cronjob to check for the certificate which is due to expire in next 30 days and renew it automatically.

sudo crontab -e

Add this line at the end of the file

0 0,12 * * * certbot renew >/dev/null 2>&1

Hit CTRL+X followed by Y to save the changes.

This cronjob will attempt to check for renewing the certificate twice daily.

Verify Symfony Installation

Now visit your domain name on your browser you should see your Symfony welcome page.

Install Symfony

Get your Professional Google Cloud Architect certificate with this easy to learn course now.

Conclusion

Now you have learned how to install Symfony5 with PHP 7.4 and configure Nginx and also secured the installation with Let’s Encrypt certificate.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.