Build and Run Laravel 7 in Docker Part 3 - Nginx

Build and Run Laravel 7 in Docker Part 3 - Nginx

Creating the Web Block

Below the pasted app file we now create an Nginx App. Alignment is imporant so make sure the app and web are in the same column. In your docker-compose.yml paste the following below the app.

# The Web Server
  web:
      build:
      context: ./
      dockerfile: web.dockerfile
      working_dir: /var/www
      volumes:
      - ./:/var/www
      depends_on:
      - app
      ports:
        - 80:80

Notice again we have another file referenced. web.dockerfile. Lets create it.

touch web.dockerfile

Open the file and paste the following:

FROM nginx:1.10

ADD scripts/vhost.conf /etc/nginx/conf.d/default.conf

We use the ADD notation to copy the nginx config required to run the app.

 

In your Laravel root create a folder called scripts and create a file called vhost.conf

 

mkdir scripts

cd scripts

touch vhost.conf

 

Open the file and paste the following:

server {
  listen 80;
  index index.php index.html;
  root /var/www/public;

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

  location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass app:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

 

Part 4 - Add MySQL to Docker

Categories: Posts