Source

Setup Docker with NGINX for Laravel PART 1

#[Pragmatic(Kiwi)]
2 min readMay 9, 2022

--

Our folder structure will be as follows:

root
|__ docker
| |_ dev
| |_ nginx.dockerfile
| |_ prod
| |_ nginx.prod.dockerfile
|__ nginx
|_ default.conf
|_ default.prod.conf
|_ laravel-docker.test.pem
|_ laravel-docker.test-key.pem

We will create configurations for both DEV and PROD environments, including HTTP and HTTPS with SSL certificates and key.

SETUP FOR DEV

nginx.dockerfile

FROM nginx:stable-alpine — we are going to use the latest nginx version from alpine linux

ENV — creates a variable inside the file

RUN mkdir -p /var/www/html/public — we are creating a public folder in the NGINX filesystem

ADD ../../nginx/default.conf /etc/nginx/conf.d/default.conf — we are copying our local nginx configuration to NGINX

RUN sed -i “s/user www-data/user ${NGINXUSER}/g” /etc/nginx/nginx.conf — we are searching for “user www-data” and replace it with “user laravel”

RUN adduser -g ${NGINXGROUP} -s /bin/sh -D ${NGINXUSER} — we are creating a new user (“laravel”) and add its profile to the shell and set no password (-D for Alpine)

default.conf

This is just a boilerplate configuration for NGINX web servers. You can create yours easily here.

On this configuration only HTTP is set up.

SETUP FOR PROD

nginx.prod.dockerfile

This configuration is similar as the one before, the only difference is that we ADD both laravel-docker.test.pem and laravel-docker.test-key.pem from our local machine to the NGINX web server.

Here is how you can generate your SSL certificates.

default.prod.conf

And now we add HTTPS as well for the PROD configuration and a server name to access the server from.

We also tell NGINX that for HTTPS we are going to use SSL and where to get the files.

Checkout PART2 to setup PHP.

--

--