Running Nginx Virtual Host for Laravel sites using Docker

Teguh Arief

Sep 24, 2020

Share article to

Illustration of Laravel sites using Docker.

Sometimes, a developer is required to build their own host. Running a virtual host with containers needed for web applications. Once Docker is installed, you’re ready to set up your local file structure, with some configuration to build a local development environment.

Table of structure:




  • virtual-host/


    • app/


      • php/


        • www/


          • ... laravel 5.4 files

          • public/

            • ...

            • index.php

            • ...









    • docker-composer.yml

    • etc/

      • nginx/

        • conf.d/

          • default.conf



        • nginx.conf



      • php.ini







Docker Container




$ docker pull nginx:latest
$ docker pull webdevops/php-apache-dev:centos-7-php7


Laravel




$ cd virtual-host/app/php
$ composer create-project --prefer-dist laravel/laravel www "5.4"


Setting up



docker-compose.yml


version: '2'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- "./app:/var/www/html"
- "./etc/nginx/nginx.conf:/etc/nginx/nginx.conf"
  - "./etc/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
links:
- php
php:
image: webdevops/php-apache-dev:centos-7-php7
volumes:
- "./app:/var/www/html"
- "./etc/php.ini:/usr/local/etc/php/php.ini"


conf.conf


user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;

keepalive_timeout 65;

gzip on;
gzip_disable "MSIE [1-6].(?!.*SV1)";

include /etc/nginx/conf.d/*.conf;
}


default.conf


server {
listen 80;
server_name teguh.arief.com;
root /var/www/html/php/www/public;
  index index.html index.htm index.php;

  try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}

  location ~ .php$ {
root /var/www/html/php/www/public;
fastcgi_pass php:9000;
  fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/php/www/public/$fastcgi_script_name;
include fastcgi_params;
}
}


php.ini


Download php.ini 7.0 default


Build Image




$ docker-compose up -d


Composer Install




$ composer install


Edit Hosts File on a PC




SERVER_IP_ADDRESS teguh.arief.com


Final



Then you can hit http://teguh.arief.com in your browser. Running Nginx Virtual Host for Laravel sites using Docker