
Published on: September 24, 2020
As a developer, you often need to build and manage your own hosting environments. Running Nginx virtual host for Laravel sites using Docker provides an excellent solution for creating isolated and consistent web application setups. Once Docker is installed, you're ready to set up your local file structure and configurations to build a robust local development environment.
File Structure
Here's the essential file structure you'll need for running Nginx virtual host for Laravel sites using Docker:
- 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 Setup
To begin running Nginx virtual host for Laravel sites using Docker, pull the necessary Docker images for Nginx and PHP:
$ docker pull nginx:latest $ docker pull webdevops/php-apache-dev:centos-7-php7
Laravel Project Initialization
Navigate to your project directory and create a new Laravel project, which will be the foundation for running Nginx virtual host for Laravel sites using Docker:
$ cd virtual-host/app/php $ composer create-project --prefer-dist laravel/laravel www "5.4"
Configuration Setup
docker-compose.yml
This `docker-compose.yml` file orchestrates your Nginx and PHP services, essential for running Nginx virtual host for Laravel sites using Docker:
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"
nginx.conf
This Nginx configuration (`nginx.conf`) defines global settings for your web server, facilitating running Nginx virtual host for Laravel sites using Docker:
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
This is your Nginx virtual host configuration (`default.conf`), specifically tailored for running Nginx virtual host for Laravel sites using Docker. It directs traffic to your Laravel application's public directory and handles PHP processing:
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
For proper PHP execution when running Nginx virtual host for Laravel sites using Docker, download the default PHP 7.0 `php.ini` file and place it in the specified location within your Docker volume setup:
Download php.ini 7.0 default
Building and Running Docker Images
With all configurations in place, build and run your Docker containers to begin running Nginx virtual host for Laravel sites using Docker:
$ docker-compose up -d
Composer Installation
Once your containers are up, install your Laravel project dependencies:
$ composer install
Editing Hosts File on a PC
Finally, to access your Laravel site by its custom domain, add an entry to your PC's hosts file. Replace `SERVER_IP_ADDRESS` with the IP address of your Docker host:
SERVER_IP_ADDRESS teguh.arief.com
Conclusion
You're all set! Now you can access your Laravel site by navigating to `http://teguh.arief.com` in your browser. This completes the process of running Nginx virtual host for Laravel sites using Docker successfully.
Related Posts

NestJS with PostgreSQL, TypeORM, JWT auth, and Docker
Discover the complete steps to set up and run a NestJS application with PostgreSQL, TypeORM, JWT authentication, and Docker, all managed via Darwin Terminal.
Read More
React Native CLI vs. Expo: Battle of Mobile Devs
Dive deep into the core differences between React Native CLI and Expo for mobile development. Discover which tool fits your project and workflow best.
Read More
Tailwind CSS to React Native, NativeWind: a Savior
Boost your React Native app's performance. Convert Tailwind to StyleSheet with NativeWind for faster development and incredible results.
Read More