0

I have searched for days on this topic and read almost every SO thread on Nginx + PHP-FPM configuration. My configuration seems accurate. I am using Docker. Unfortunately, this site is old and I am the new maintainer of it. As you'll see, the .htm/.html files are not super easy to follow in their present format, but it does work on a remote server so I am relatively certain my local configuration is simply missing something.

My local site loads the homepage just fine, but any PHP within the index.htm file is printed directly:

enter image description here

Other pages that rely more heavily on PHP to render any HTML appear as pure PHP code printed to an otherwise blank document.

My docker-compose.yml and Dockerfile:

docker_compose.yml

version: '3'
volumes:
  persistent-db:
services:
  php:
    build:
      context: ./docker-config/php-fpm
    ports:
      - 9000:9000
    volumes:
      - ./:/var/www/html
      - ./docker-config/php-fpm/www.conf:/usr/local/etc/php-fpm.d/www.conf
      - ./docker-config/php-fpm/php-fpm.conf:/usr/local/etc/php-fpm.d/php-fpm.conf
    environment:
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_HOST: ${MYSQL_HOST}

  nginx:
    image: nginx:1.13.8
    ports:
      - 80:80
    volumes:
      - ./:/var/www/html
      - ./docker-config/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php

  mysql:
    image: mysql:5.7
    ports:
      - 3312:3306
    volumes:
      - persistent-db:/var/lib/mysql:delegated
    depends_on:
      - php
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

Dockerfile

FROM php:7.2-fpm

RUN docker-php-ext-install mysqli

And the configuration in my default.conf file, for Nginx:

server {
    listen 80 default_server;

    root /var/www/html/docs;
    charset utf-8;

    index index.php index.html index.htm;

    server_name dev.local;

    location ~ \.php$ {
        try_files $uri =404;
        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_param PATH_INFO $fastcgi_path_info;
    }

    location ~ \.htm$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.htm)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.htm;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log /var/log/nginx/local_error.log;
    access_log /var/log/nginx/local_access.log;
}

And, finally, the actual template file that is trying to be rendered on the homepage:

<?
include "../inc/OLconfig.php";
include_once ("$docRoot/inc/common.php");
if(is_file("$docRoot/inc/droplistrental.html")){
    include_once ("$docRoot/inc/droplistrental.html");
}
session_start();

$con = getDBCon($db,$sqlServer);

$query = "SELECT MAX(bedroom), MIN(bedroom), MAX(fullBath), MIN(fullBath), MAX(sleeps), MIN(sleeps) FROM {$dbrental} where isActive='1'";
$result = mysql_query($query, $con) or die();
list($DEFAULT_MAXBEDS,$DEFAULT_MINBEDS,$DEFAULT_MAXBATHS,$DEFAULT_MINBATHS,$DEFAULT_MAXOCC, $DEFAULT_MINOCC) = mysql_fetch_row($result);
if (!$DEFAULT_MINBEDS) {
    $DEFAULT_MINBEDS = 1;
}
$DEFAULT_MINSTAY = 7;
$DEFAULT_MAXSTAY = 28;
if($DEFAULT_MINSTAY % 7 == 0 && $DEFAULT_MAXSTAY % 7 == 0){
    //assume weekly stays
    $DEFAULT_STAYINCREMENT = 7;
}else{
    $DEFAULT_STAYINCREMENT = 1;
}
if($DEFAULT_MAXSTAY > 21){
    //don't allow search for more than 3 weeks at this time
    $DEFAULT_MAXSTAY = 21;
}
$DEFAULT_MINOCC = $DEFAULT_MINBEDS;
$DEFAULT_MAXOCC = $DEFAULT_MAXBEDS * 2;
$DEFAULT_MINBATHS = $DEFAULT_MINBEDS;
$DEFAULT_MAXBATHS = ceil($DEFAULT_MAXBEDS / 2);

$Arrival = qryGenWeekendDropDown();
$Stay = qryStayDropDown('Stay',28,7,7);
$bedroom = qryNumericDropDown("Min_bedroom","9","1","1","Bedrooms");
$fullBath = qryNumericDropDown("Min_fullBath","6","1","1","Bathrooms");
$town = qryDropDown("city",$dbrental,"Town");
$location = qryDropDown("location",$dbrental,"Village");
$area = qryDropDown("area",$dbrental,"Distance to Beach");

$Units = "<select name='ID' class='qsrchfield' onChange='javascript:parent.document.location.href=this.value'><option value=''>Find A Specific Property</option>";
$query = "SELECT UnitID,address FROM $dbrental where isActive='1' and address<>'' ORDER BY address";
$result = mysql_query($query,$con) or die ();
while(list($UnitID,$name) = mysql_fetch_row($result)){
    $Units .= "<option value='/rental/house.html?ID=$UnitID'>$name</OPTION>";
}
$Units .= "</select>";

$unitCodes = "<select name='ID' class='qsrchfield' onChange='javascript:parent.document.location.href=this.value'><option value=''>Find A Property by Number</option>";
$query = "SELECT UnitID,unitNumber FROM $dbrental where isActive='1' and unitNumber<>'' ORDER BY unitNumber";
$result = mysql_query($query,$con) or die ();
while(list($UnitID,$name) = mysql_fetch_row($result)){
    $unitCodes .= "<option value='/rental/house.html?ID=$UnitID'>$name</OPTION>";
}
$unitCodes .= "</select>";

$price = "
<select name='Min_Price'>
    <option value=''>Price Range</option>
    <option value=''>Any</option>
    <option value='1000'>&lt; $1000</option>
    <option value='2000'>$1000 - $2000</option>
    <option value='3000'>$2000 - $3000</option>
    <option value='5000'>$3000 - $5000</option>
    <option value='6000'>$5000 +</option>
</select>
";

print "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<title>$clientName</title>
<link href='/css/stylesheet.css' rel='stylesheet' type='text/css'>
</head>
<body>
<div id='quickSearch'>
  <form id='search_form' name='search_form' action='/rental/query_p.html' method='post' target='_top'>
    <span id='weekend'>
    $Arrival
    </span>
    <span id='stay'>
    $Stay
    </span>
    <span id='bedRooms' class='floatLeft'>
    $bedroom
    </span>
    <span id='bathRooms' class='floatRight'>
    $fullBath
    </span>
    <span id='priceRange' class='clearFloat'>
    $price
    </span>
    <span id='town'>
    $town
    </span>
    <span id='location'>
    $location
    </span>
    <span id='beachDistance'>
    $area
    </span>
    <input type='submit' name='Submit' value=' Find My Rental ' class='button qsrch'>
  </form>
  <div id='qsrchLinks'><a href='/rental/query.html' target='_top'>Advanced Search</a> | <a href='/rental/alpha.html' target='_top'>View All</a></div>
   <span id='findName'>
    $Units
    </span>
  <div id='propIdWrapper'>
    $unitCodes
<!--
  <form action='/rental/query_p.html' method='get' target='_top'>
    <input type='text' name='PMSKey' placeholder='Enter Property Number' value='' id='propID'>
    <input type='image' src='/images/bttn-tiny.png' alt='Submit' class='floatRight'>
  </form>
-->
 <div class='clearFloat'></div>
  </div>
</div>
</body>
</html>
";
exit();

function qryDropDown($sendName,$dbtable,$Label=""){
    $outText = "";
    global $con;
    $chkVal = $GLOBALS["url_".$sendName];
    if(! $colspan){
        $colspan='1';
    }
    if($Label){
        $Label = "<option value=''>$Label</option>";
    }
    $outText .= "<select name='$sendName' class='qsrchfield'>$Label<option value=''>Any</option>";
    $result = mysql_query("SELECT DISTINCT $sendName from $dbtable WHERE destination='".$GLOBALS['DESTINATION']."' AND $sendName <>'' AND $sendName <>'N/A' ORDER BY $sendName",$con);
    while(list($opt) = mysql_fetch_row($result)){
        $outText .= "<OPTION VALUE=".urlencode($opt);
        if($chkVal == $opt){
            $outText .= " SELECTED ";
        }
        $outText .= ">$opt</OPTION>";
    }
    $outText .= "</select>";
    return $outText;
}

function qryNumericDropDown($sendName,$end,$start=1,$step=1,$Label=""){
    $outText = "";
    $chkVal = $GLOBALS["url_".$sendName];
    if(! $step){
        $step = 1;
    }
    if(($start > $end && $step > 0) || ($start < $end && $step < 0)){
        return;
    }
    if($Label){
        $Label = "<option value=''>$Label</option>";
    }
    $outText .= "<select name='$sendName' class='qsrchfield'>$Label<option value=''>Any</option>";
    for($i=$start;$i<=$end;$i += $step){
        if($i == $start && $i > 1){
            $sendVal = "_".$i;
            $displayVal = "Up to ".$i;
        }elseif($i == $start){
            $sendVal = "_".$i;
            $displayVal = $i;
        }elseif($i == $end){
            $sendVal = $i."_";
            //$displayVal = $i." or More";
            $displayVal = $i;
        }else{
            $sendVal = $i."_".$i;
            $displayVal = $i;
        }
        $outText .= "<OPTION VALUE='$sendVal'";
        if($chkVal == $sendVal){
            $outText .= " SELECTED ";
        }
        $outText .= ">$displayVal</OPTION>";
    }
    $outText .= "</select>";
    return $outText;
}

function qryStayDropDown($sendName,$end,$start=1,$step=1)
{
    $outText = "";
    $chkVal = $GLOBALS["url_".$sendName];
    if(! $step){
        $step = 1;
    }
    if(($start > $end && $step > 0) || ($start < $end && $step < 0)){
        return;
    }

    $outText .= "<select name='$sendName' class='qsrchfield-sm'><option value=''>Length of Stay</option>";
    for($i=$start;$i<=$end;$i += $step){
        $outText .= "<OPTION VALUE='$i'";
        if($chkVal == $i){
            $outText .= " SELECTED ";
        }
        $outText .= ">$i</OPTION>";
    }
    $outText .= "</select>";
    return $outText;
}

function qryGenDateDropDown()
{
    $outText = "";
        $outText .= "<select name='ArrMonth' class='qsrchfield-sm'><option value=''>Mo</option>";
    for($i=1;$i<=12;$i++){
        $tmp = sprintf("%02d",$i);
        $outText .= "<option value='$tmp'";
        if($GLOBALS['url_ArrMonth'] == $tmp){$outText .= " selected";}
        $outText .= ">$i</option>";
    }
        $outText .= "</select>&nbsp;<select name='ArrDay' class='qsrchfield-sm'><option value=''>Dy</option>";
    for($i=1;$i<=31;$i++){
        $tmp = sprintf("%02d",$i);
        $outText .= "<option value='$tmp'";
        if($GLOBALS['url_ArrDay'] == $tmp){$outText .= " selected";}
        $outText .= ">$i</option>";
    }
    $outText .= "</select>";
        $outText .= "&nbsp;<select name='ArrYear' class='qsrchfield-sm'><option value=''>Yr</option>";
    $yy = date('Y');
    for($i=0;$i<=1;$i++){
        $tmp = sprintf("%4d",$yy + $i);
        $outText .= "<option value='$tmp'";
        if($GLOBALS['url_ArrYear'] == $tmp){$outText .= " selected";}
        $outText .= ">'".sprintf("%02d",($tmp % 100))."</option>";
    }
    $outText .= "</select>";
    return $outText;
}

function qryGenWeekendDropDown(){
    global $url_Avail;
    $outText = "";
    $outText .= "<select name='Weekend'><option value=''>Arrival Weekend</option><option value=''>No Preference</option>";
    $dow = date('w');
    $dayOffset = 6 - $dow;//find next saturday date
    $month = date('m');
    $day = date('d') + $dayOffset;
    $thisyear = date('Y');
    $year = $thisyear + 1;
    $beginDateMonth = "05";
    $endDateMonth = "09";
    if($url_Avail){
        $Arrival = date("Y-m-d",strtotime("$url_Avail"));
    }
    if ( $dayOffset > 2){
        //if greater than 2 days out then show this saturday.
        $arriveDate = date("Y-m-d",strtotime("+$dayOffset day"));
        $arriveDateMonth = date("m",strtotime($arriveDate));
        if($arriveDateMonth > $beginDateMonth && $arriveDateMonth < $endDateMonth){
            $outText .= "<option value='$arriveDate'";
            if($arriveDate == $Arrival){
                $outText .= " selected='selected'";
            }
            $outText .= ">" . strftime("%m/%d/%y",strtotime("+$dayOffset day")) . "</option>";
        }
    }
    for($i=1;$i<51;$i++){
        //For Saturday Date
        $dayOffset += 7;
        print "<!-- test  $i -->";
        if(date("Y",strtotime("+$dayOffset day")) == $year || date("Y",strtotime("+$dayOffset day")) == $thisyear){
            $arriveDate = date("Y-m-d",strtotime("+$dayOffset day"));
            $arriveDateMonth = date("m",strtotime($arriveDate));
            if($arriveDateMonth < $beginDateMonth || $arriveDateMonth > $endDateMonth)continue;
            $outText .= "<option value='$arriveDate'";
            if($arriveDate == $Arrival){
                $outText .= " selected='selected'";
            }
            $outText .= ">" . strftime("%m/%d/%y",strtotime("+$dayOffset day")) . "</option>";
        }
    }//end for $i
    $outText .= "
    </select>
    ";
    return $outText;
}

?>

0

2 Answers 2

2

For those who find this in the future, I found the solution. I had to add a link in my docker-compose.yml file to the php container:

 nginx:
    image: nginx:latest
    ports:
      - 80:80
    volumes:
      - ./:/var/www/html
      - ./docker-config/nginx/site.conf:/etc/nginx/conf.d/site.conf
    links:
      - php

Then my www.conf:

[www]

user = www-data
group = www-data

listen = nginx:9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

And, php-fpm.conf:

security.limit_extensions = .php .html .htm .js

And, finally, my site.conf file for Nginx:

server {
    listen 80 default_server;

    root /var/www/html/docs;
    charset utf-8;

    index index.php index.html index.htm;

    server_name dev.local;

    location ~ \.php$ {
        try_files $uri =404;
        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_param PATH_INFO $fastcgi_path_info;
    }

    # Had to add for both .html and .htm
    location ~ \.html$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.html)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.html;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    # Had to add for both .html and .htm
    location ~ \.htm$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.htm)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.htm;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    error_log /var/log/nginx/local_error.log;
    access_log /var/log/nginx/local_access.log;
}

Sign up to request clarification or add additional context in comments.

Comments

0

I had index.php file in my php-fpm container. Everything was working fine until I added index.html. I was using the below configuration in my nginx container

server {
    listen 80;

    server_name localhost;

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

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Once I added index.html it gave me this file not found error.

But yeah your configuration file makes sense. nginx is forwarding the request with .php extention to my php-fpm container through fastcgi but not with .html extensions. So the .html extensions would be searched in nginx container, that's where the error arises. Thank you so much for the question and answer !!

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.