Home > Mobile >  Deploying a php website using docker and nginx
Deploying a php website using docker and nginx

Time:10-28

I am trying to get a website working that uses php. First I am trying to get it working with a basic index.php file.

I don't put ports in the nginx part because traefik auto takes care of this via it's reverse proxy feature. I have other sites (blogs) running it's just having a nginx with php website that seems to be giving me problems.

version: '3'

services:

    example-website:
        container_name: example-website
        build: ./Dockerfile
        image: 'nginx:alpine'
        restart: unless-stopped
        links:
          - php
        networks:
          - proxy
          - default
        labels:
          - "traefik.enable=true"
          - "traefik.docker.network=proxy"
          - "traefik.http.routers.example-secure.entrypoints=websecure"
          - "traefik.http.routers.example-secure.rule=Host(`example.com`)"
        volumes:
          - /var/docker/test/html:/var/www/html
          
    php:
        image: php:7.1.11-fpm-alpine
        expose:
        - 9000
        volumes:
        - /var/docker/test/html:/var/www/html
        networks:
          - proxy
          - default

networks:
  proxy:
    external: true  

Here is my default.conf file:

server {

     listen 80;
     root /var/www/html;
     index index.html index.php;

     charset utf-8;

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

     location = /favicon.ico { access_log off; log_not_found off; }
     location = /robots.txt { access_log off; log_not_found off; }

     access_log off;
     error_log /var/log/nginx/error.log error;

     sendfile off;
 client_max_body_size 100m;

     location ~ .php$ {
      fastcgi_split_path_info ^(. .php)(/. )$;
      fastcgi_pass php:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors off;
      fastcgi_buffer_size 16k;
      fastcgi_buffers 4 16k;
    }

     location ~ /.ht {
      deny all;
     }
    }

Here is the index.php

  <!DOCTYPE html>  
     <head>  
      <title>Hello World!</title>
     </head>  

     <body>  
      <h1>Hello World!</h1>
      <p><?php echo 'We are running PHP, version: ' . phpversion(); ?></p>
     </body>

Dockerfile is:

FROM nginx:latest
COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf

Currently when deployed I just see:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

CodePudding user response:

I suggest doing the following:

  • check the container logs for any errors
  • log into the containers to verify everything is setup the way you expect them to be, e.g. make sure /etc/nginx/conf.d/default.conf was overwritten with yours
  • check the nginx and php logs in each container
  • you don't need image: 'nginx:alpine' in the docker-compose file since you have FROM nginx:latest in the Dockerfile
  • include a ports section in the nginx section of docker-composer to be explicit
  • In nginx.conf I always use a / in the location block: location ~ \.php$ {
  • Related