Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5129

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T06:23:05+00:00 2024-11-27T06:23:05+00:00

Cheat Sheet: Enabling HTTPS on a Fresh Laravel Sail App with MacOS

  • 61k

When developing Laravel applications locally using Sail and Docker, you might need to enable HTTPS to integrate with third-party services like Google, Dropbox, or other OAuth2 providers. These services often require secure HTTPS callbacks for authentication and API interactions. Enabling HTTPS in your local development environment ensures you can test these integrations effectively and securely.

This guide will show you how to set up HTTPS in your Laravel Sail environment by configuring Nginx as a reverse proxy and forcing HTTPS within your application.

Table of Contents

  1. Create a Fresh Laravel Sail Application
  2. Update the .env File
  3. Add Custom Domain to /etc/hosts
  4. Configure Docker for HTTPS
  5. Create an Nginx Configuration
  6. Modify AppServiceProvider to Force HTTPS
  7. Rebuild and Restart Sail
  8. Clear Laravel Cache
  9. Test HTTPS Setup
  10. Done!

1. Create a Fresh Laravel Sail Application

curl -s "https://laravel.build/my-app" | bash cd my-app ./vendor/bin/sail up 
Enter fullscreen mode Exit fullscreen mode

This creates a fresh Laravel app with Sail and brings up the Docker containers.

2. Update the .env File

Edit your .env file to set the APP_URL to your custom domain with https://:

APP_URL=https://my-app.test 
Enter fullscreen mode Exit fullscreen mode

This ensures Laravel generates URLs using HTTPS instead of HTTP.

3. Add Custom Domain to /etc/hosts

Map your custom domain to 127.0.0.1 in /etc/hosts:

sudo nano /etc/hosts 
Enter fullscreen mode Exit fullscreen mode

Add the following line:

127.0.0.1 my-app.test 
Enter fullscreen mode Exit fullscreen mode

This allows your machine to resolve my-app.test to localhost.

4. Configure Docker for HTTPS

Edit the docker-compose.yml file to expose both 80 (HTTP) and 443 (HTTPS) ports for Nginx:

services:   laravel.test:     ports:       - '80:80'       - '443:443'  # HTTPS port    nginx:     image: nginx:alpine     ports:       - '80:80'       - '443:443'  # Expose HTTPS port for Nginx     volumes:       - .:/var/www/html       - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf  # Nginx configuration       - ./docker/nginx/ssl:/etc/nginx/ssl  # SSL certificate     networks:       - sail 
Enter fullscreen mode Exit fullscreen mode

This config ensures that both the Laravel application and Nginx are set up to handle HTTP and HTTPS traffic. Exposing port 443 allows Nginx to listen for HTTPS requests which it will then proxy to your Laravel application.

5. Create an Nginx Configuration

In this step we'll set up Nginx as a reverse proxy to handle the HTTPS requests and forward them to our Laravel application running in Docker. We'll generate a self-signed SSL certificate for local development purposes.

Create a directory for Nginx configuration:

mkdir -p docker/nginx/ssl 
Enter fullscreen mode Exit fullscreen mode

Generate a self-signed SSL certificate:

openssl req -newkey rsa:2048 -nodes -keyout docker/nginx/ssl/my-app.key -x509 -days 365 -out docker/nginx/ssl/my-app.crt 
Enter fullscreen mode Exit fullscreen mode

This creates a self-signed SSL certificate for local HTTPS support.

Create a new Nginx config file in docker/nginx/nginx.conf:

user nginx; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf;  events { worker_connections 1024; }  http { include /etc/nginx/mime.types; default_type application/octet-stream;      server {         listen 80;         listen 443 ssl;         server_name my-app.test;          ssl_certificate /etc/nginx/ssl/my-app.crt;         ssl_certificate_key /etc/nginx/ssl/my-app.key;          root /var/www/html/public;         index index.php index.html;          location / {             proxy_pass http://laravel.test:80;             proxy_set_header Host $host;             proxy_set_header X-Real-IP $remote_addr;             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;             proxy_set_header X-Forwarded-Proto $scheme;         }     }      sendfile on;     keepalive_timeout 65; } 
Enter fullscreen mode Exit fullscreen mode

This config sets up Nginx as a reverse proxy, forwarding HTTPS requests to Laravel’s built-in web server that's provided by the laravel.test Docker container.

6. Modify AppServiceProvider to Force HTTPS

Edit app/Providers/AppServiceProvider.php and add URL::forceScheme('https'):

use IlluminateSupportFacadesURL; use IlluminateSupportServiceProvider;  class AppServiceProvider extends ServiceProvider {     public function boot(): void     {         URL::forceScheme('https');  // Force HTTPS in development     } } 
Enter fullscreen mode Exit fullscreen mode

This ensures that Laravel generates all URLs with HTTPS.

7. Rebuild and Restart Sail

Rebuild the Docker containers to apply your changes:

sail down sail build --no-cache sail up -d 
Enter fullscreen mode Exit fullscreen mode

8. Clear Laravel Cache

After making changes to Laravel, clear the cache:

sail php artisan config:clear sail php artisan route:clear sail php artisan cache:clear 
Enter fullscreen mode Exit fullscreen mode

9. Test HTTPS Setup

Run the following command to test the HTTPS connection:

curl -v https://my-app.test --insecure 
Enter fullscreen mode Exit fullscreen mode

A successful result should show:

  • Connected to port 443:
  * Connected to my-app.test (127.0.0.1) port 443 
Enter fullscreen mode Exit fullscreen mode

  • Successful SSL handshake:
  * SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256 
Enter fullscreen mode Exit fullscreen mode

  • HTTP 200 OK or 302 Redirect:
  < HTTP/1.1 200 OK 
Enter fullscreen mode Exit fullscreen mode

or

  < HTTP/1.1 302 Found   < Location: https://my-app.test/login 
Enter fullscreen mode Exit fullscreen mode

These indicate that HTTPS is working and Laravel is correctly generating secure URLs.

10. Done!

Congratulations 🎉 You've successfully configured HTTPS for your Laravel Sail app! You can now visit https://my-app.test in your browser. This setup should enable you to:

  • Test integrations with OAuth2 providers that require secure HTTPS callbacks.
  • Develop and debug third-party APIs that require HTTPS.
  • Ensure your local dev environment closely mirrors production in terms of security.

dockerlaraveloauth2webdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.