Skip to content

Nginx and SSL Configurations

Darkk-kami edited this page Jul 21, 2024 · 1 revision

Overview

This section covers the setup of Nginx and SSL for the Laravel application hosted on an Ubuntu server. It includes the installation of Nginx, configuration of server blocks for multiple domains, and the automation of SSL certificate installation using Certbot.`

Configuration

Below is a sample script that automates the installation and setup of nginx to serve the application

#!/bin/bash

# Variables
DOMAIN1="<domain-name>" # Replace with your domain name
DOMAIN2="<staging.domain-name>"
DOMAIN3="<deployment.domain-name>"
ROOT_PATH="/var/www/langlearnai-be"
EMAIL="[email protected]"  # Replace with your email address


# Install Nginx
sudo apt update
sudo apt install -y nginx

# Check Nginx version
nginx -v

# Create Nginx configuration file
sudo bash -c "cat > /etc/nginx/sites-available/langlearnai-be <<EOF
server {
    server_name $DOMAIN1;

    root $ROOT_PATH/api/public;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
server {
    server_name $DOMAIN2;

    root $ROOT_PATH/staging/public;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
server {
    server_name $DOMAIN3;

    root $ROOT_PATH/deployment/public;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
EOF"

# Link settings to Nginx
sudo ln -s /etc/nginx/sites-available/langlearnai-be /etc/nginx/sites-enabled/

# Test Nginx configuration
sudo nginx -t

# Restart Nginx
sudo systemctl restart nginx

# Enable Nginx to start on boot
sudo systemctl enable nginx

# Install Certbot
sudo apt install -y certbot python3-certbot-nginx

# Add SSL to domains
sudo certbot --nginx --non-interactive --agree-tos --email $EMAIL -d $DOMAIN1
sudo certbot --nginx --non-interactive --agree-tos --email $EMAIL -d $DOMAIN2
sudo certbot --nginx --non-interactive --agree-tos --email $EMAIL -d $DOMAIN3

# Restart Nginx to apply SSL certificates
sudo systemctl restart nginx
echo "Nginx and SSL setup complete."

Script Breakdown

Variables

  • DOMAIN1: The primary domain.
  • DOMAIN2: The staging domain.
  • DOMAIN3: The deployment domain.
  • ROOT_PATH: The root path for the Laravel application. e.g /var/www/langlearnai-be.
  • EMAIL: Your email address used for SSL certificate registration

Set Up

  1. Update and Install Nginx

    sudo apt update
    sudo apt install -y nginx
    nginx -v
    
  2. Create an Nginx conf file

    sudo bash -c "cat > /etc/nginx/sites-available/langlearnai-be <<EOF
    # Nginx server block configuration
    EOF"
    

    Creates a configuration file for Nginx that defines server blocks for three domains. Each server block includes:

    • Server Name: Specifies the domain.
    • Root: Defines the root directory for the Laravel application.
    • Index: Specifies the default files to serve.
    • Location Block: Handles the routing of requests.
    • PHP Handling: Configures PHP-FPM to process PHP files.
    • Security: Denies access to .ht files.
  3. Link the Configuration and Test Nginx

    sudo ln -s /etc/nginx/sites-available/langlearnai-be /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx
    sudo systemctl enable nginx
    

    This Creates a symbolic link to enable the site, Tests the Nginx configuration for syntax errors and ensures Nginx starts on system boot.

  4. Installing Certbot and SSL

    sudo apt install -y certbot python3-certbot-nginx
    

    Installs Certbot and its Nginx plugin to automate the SSL certificate installation.

    sudo certbot --nginx --non-interactive --agree-tos --email $EMAIL -d $DOMAIN1
    sudo certbot --nginx --non-interactive --agree-tos --email $EMAIL -d $DOMAIN2
    sudo certbot --nginx --non-interactive --agree-tos --email $EMAIL -d $DOMAIN3
    

    Uses Certbot to obtain and install SSL certificates for the specified domains. The --non-interactive flag runs Certbot without prompting for input, and --agree-tos automatically agrees to the terms of service.

  5. Restart Nginx

    sudo systemctl restart nginx
    echo "Nginx and SSL setup complete."
    

    Finally, Nginx is restarted to apply changes and a completion message is printed to the terminal if ran successful