> ## Documentation Index
> Fetch the complete documentation index at: https://notes.chaicode.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Setup SSL for Nginx

> Learn how to set up SSL for Nginx on Ubuntu.

This guide will help you set up SSL for your Nginx server on Ubuntu using Certbot. We will use a subdomain from GoDaddy, and the steps include installing Certbot, configuring Nginx, and generating an SSL certificate.

<iframe width="560" height="315" src="https://www.youtube.com/embed/cBh6yTH-XY4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen style={{ margin: '1.5rem 0' }} />

## Prerequisites

Before going forward, make sure you have the following prerequisites:

* A server with root access
* A domain name or IP address
* A web server that can be used to serve static files
* Nginx installed and configured on the machine

## Install Certbot and the Nginx Plugin

First, install Certbot and the Nginx plugin using the following command:

```sh theme={null}
sudo apt install certbot python3-certbot-nginx
```

## Configure Nginx

Edit your Nginx configuration file to include your server name. Open the file with a text editor:

```sh theme={null}
sudo vim /etc/nginx/sites-available/default
```

Add your subdomain in the `server_name` directive:

```nginx theme={null}
server {
  ...
  server_name test.chaicode.com;
  ...
}
```

Save the file and exit the editor. Then, test your Nginx configuration:

```sh theme={null}
sudo nginx -t
```

## Create an A Record in Your Domain Registrar

Log in to your domain registrar (e.g., GoDaddy) and create an `A` record pointing to the IP address of your server.

* **Record Type:** A
* **Name:** test.chaicode.com
* **Value:** *IPv4 address of the server*

NOTE: Its always a good idea to create a elastic/static IP for your server. This will make sure that your IP address doesn't change on restart or reboot.

## Obtain an SSL Certificate

Run Certbot to obtain an SSL certificate for your subdomain:

```sh theme={null}
sudo certbot --nginx -d test.chaicode.com
```

Follow the prompts:

1. Enter the email address associated with your domain registrar account.
2. Accept the terms of service.
3. Press Enter to continue.

Certbot will automatically configure your Nginx to use the new certificate. You should see a message indicating that the certificate was successfully issued.

<img src="https://wajeshubham-portfolio.s3.ap-south-1.amazonaws.com/Screenshot+2024-07-10+001416.png" alt="Site Access" style={{ margin: '1.5rem auto', borderRadius: '8px', overflow: 'hidden', maxWidth: '80%' }} />

## Access Your Site

You can now access your site using HTTPS:

```sh theme={null}
https://test.chaicode.com
```

<img src="https://wajeshubham-portfolio.s3.ap-south-1.amazonaws.com/Screenshot+2024-07-10+001500.png" alt="Site Access" style={{ margin: '1.5rem auto', borderRadius: '8px', overflow: 'hidden', maxWidth: '80%' }} />

## Renewing the Certificate

To check if the certificate is close to expiring, run:

```sh theme={null}
sudo certbot renew --dry-run
```

To renew the certificate, run:

```sh theme={null}
sudo certbot renew
```

This guide ensures that your Nginx server is secured with SSL, making your site accessible over HTTPS.

## Summary

In this guide, we learned how to set up SSL for Nginx on Ubuntu using Certbot. We installed Certbot and the Nginx plugin, configured Nginx, created an A record in your domain registrar, and obtained an SSL certificate. We also learned how to renew the certificate.

<Card title="Next: Deploy Node API with Nginx" icon="arrow-right" href="/devops/node-nginx-vps">
  Continue your journey by learning how to deploy a Node API with Nginx on Ubuntu server.
</Card>
