DEV Community

vimuth
vimuth

Posted on

Add a Domain, SSL For a Laravel project

In previous tutorial we added a Laravel project to EC2 server and get it running. Here is the link to previous tutorial.
Deploy Laravel application with Ubuntu Nginx in EC2 server

This tutorial is extension of that.

First we need to connect our IP address to the domain we purchased. That can be done from the dashboard of DNS service. I use Namecheap and I have bought a domain. For that you have to go to managesection of your domain and then Advance DNS section add an A Record.

An A record in DNS (Domain Name System) is a type of DNS record used to map a domain name to its corresponding IPv4 address.

Image description

Please make note that I have already added a site to this domain. So I used a subdomain project to show our site. And also we are hoping to use test to create test site also.

Now if you goes to the url in browser (since with subdomain it should be http://project.yourdomain.com/) you will see default nginx page.

Image description

If you want to use your domain without subdomain you can replace "project" with "@" when creating the A record in dashboard

Then we need to modify the Nginx server block (sometimes referred to as a "virtual host") to tell Nginx to serve your Laravel project's public directory when requests for your domain come in.

sudo vi /etc/nginx/sites-available/project

Now we remove our ip address and add the domain like this

server {
    listen 80;
    server_name your_domain.com www.your_domain.com;
Enter fullscreen mode Exit fullscreen mode

I added 'project.yourdomain.com' and now inside browser I see,

Image description

Add SSL with Let's Encrypt

Step 1: Install Certbot and Its Nginx Plugin

First, install Certbot and the Certbot Nginx plugin:

sudo apt update
sudo apt install certbot python3-certbot-nginx -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Obtain a Let's Encrypt SSL Certificate

Run Certbot to automatically obtain and install an SSL certificate for your domain. Replace your_domain.com and www.your_domain.com with your actual domain name(s). Certbot will modify your Nginx configuration to serve your site over HTTPS.

sudo certbot --nginx -d your_domain.com -d www.your_domain.com

During the process, you'll be prompted to enter an email address for important account notifications, agree to the terms of service, and choose whether to redirect HTTP traffic to HTTPS (which is recommended).

Step 3: Verify Certbot Auto-Renewal

Let's Encrypt certificates are valid for 90 days, but Certbot sets up automatic renewal out of the box. This happens using a cronjob automatically created by Certbot. Verify that automatic renewal is working with:

sudo certbot renew --dry-run

Step 4: Reload Nginx

sudo systemctl reload nginx

Top comments (0)