Nginx + PHP + Docker: How To Get PHP Page Up With Local Domain Name

Written by ifomin | Published 2020/01/21
Tech Story Tags: nginx-docker | nginx | docker | php | programming | software-development | domain-name | backend | web-monetization

TLDR Nginx + PHP + Docker: How To Get PHP Page Up With Local Domain Name. 20,393 reads: How to get PHP page up with local domain name. Igor Fomin Full stack web developer, tech lead, project manager by Igor Fom. I will setup a very simple php page with docker and nginx. I use my own php.ini file, that I copy to php container, so if I need to change some settings, I simply do changes in php.ino and restart container.via the TL;DR App

I will setup a very simple php page with docker and nginx.
Source files can be found here:

1. Create a project folder setup:

  • /var/www/docker-study.loc/recipe-02 
  •     -> /docker 
  •     -> /php
In "php" folder I will create index.php file that executes phpinfo().

2. Create nginx config site.conf in "docker" folder:

fastcgi_pass php:9000;
- this is what tells nginx how to connect to php container

3. Edit /etc/hosts file on host machine, and add a record:

127.0.0.1       myapp.loc

4. Create docker-compose.yml file in "docker" folder:

Here I do several things:
  • I use my own php.ini file, that I copy to php container, so if I need to change some settings, I simply do changes in php.ini and restart container
  • If I do changes in php code in index.php file, there is no need to restart container, changes get applied immediately with browser page reload.
  • "depends_on" - prevents container to start before other container, on which it depends

5. Go to /var/www/docker-study.loc/recipe-02/docker/ and execute:

docker-compose up -d
If I now try
myapp.loc/
in browser, I will see php info results.

Written by ifomin | Full stack web developer, tech lead, project manager
Published by HackerNoon on 2020/01/21