fideloper / docker-nginx-php

An Ubuntu, Nginx and PHP stack Built in Docker
361 stars 142 forks source link

PHP files not getting executed #8

Closed gayanhewa closed 9 years ago

gayanhewa commented 9 years ago

I cant seem to get a php file to run.

I ran the container , with a simple php file that does a die changes the path to my file which was sitting on my host machine. Is there anything else I need to be doing ?

alherd-by commented 9 years ago

Same problem

johnstontrav commented 9 years ago

same issue.

fideloper commented 9 years ago

I don't really have time at the moment to check into this, I'm really sorry! I suggest checking out some other docker setups in the docker registry.

When I get back into the swing of Docker (in a few months? or something, depends on work), I likely won't be using the Phusion base image, as the Docker philosophy isn't really about using containers like mini VM's (as this base-image for this is).

I suggest checking out other options and docker containers built from other base images!

nidheeshdas commented 9 years ago

do you have any suggestions for other options and docker containers built from other base images ?

fideloper commented 9 years ago

Check out hub.docker.com for some. There are some official Nginx ones like https://registry.hub.docker.com/u/dockerfile/nginx/ and https://registry.hub.docker.com/_/nginx/.

Here are some DockerFiles I have working currently to create my own (unpublished) base images:

PHP-FPM:

Dockerfile:

FROM ubuntu:14.04

RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN apt-get update -y
RUN apt-get install -y software-properties-common

RUN add-apt-repository -y ppa:ondrej/php5-5.6

RUN apt-get update
RUN apt-get -y install php5-fpm php5-mysql php5-pgsql php5-sqlite \
    php5-curl php5-gd php5-gmp php5-mcrypt php5-memcached \
    php5-imagick php5-intl php5-imap php5-tidy

RUN sed -i "s/;date.timezone =.*/date.timezone = UTC/" /etc/php5/fpm/php.ini
RUN sed -i "s/;date.timezone =.*/date.timezone = UTC/" /etc/php5/cli/php.ini
RUN sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php5/fpm/php.ini
RUN sed -i "s/;daemonize = yes/daemonize = no/" /etc/php5/fpm/php-fpm.conf
RUN sed -i "s%listen = /var/run/php5-fpm.sock%listen = 0.0.0.0:9000%" /etc/php5/fpm/pool.d/www.conf

RUN mkdir /var/www
RUN chown www-data:www-data /var/www

VOLUME ["/var/www"]
EXPOSE 9000

CMD ["php5-fpm", "-c", "/etc/php5/fpm"]

Build and Run:

#Build it, naming the new image "fideloper/phpfpm":
docker build -t fideloper/phpfpm ./

# Run it in the background, name the container "phpfpmapp":
docker run -d -v /home/core/share/www:/var/www:rw \
        --name="phpfpmapp" fideloper/phpfpm

Nginx

files/default.tmpl:

server {
    listen  80 default_server;

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

    # Make site accessible from http://set-ip-address.xip.io
    server_name localhost;

    access_log /var/log/nginx/localhost.com-access.log;
    error_log  /var/log/nginx/localhost.com-error.log error;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

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

    # pass the PHP scripts to php5-fpm
    # Note: \.php$ is susceptible to file upload attacks
    # Consider using: "location ~ ^/(index|app|app_dev|config)\.php(/|$) {"
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # With php5-fpm:
        fastcgi_pass FPM_PORT_9000_TCP_ADDR:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_param HTTPS off;
    }

    # Deny .htaccess file access
    location ~ /\.ht {
        deny all;
    }
}

files/start-container:

#!/usr/bin/env bash

# IP address of linked container
# Assumes alias of "FPM"
if [ -z "$FPM_PORT_9000_TCP_ADDR" ]; then
    echo "Linked container of alias 'FPM' not found. Must expose port 9000."
    exit 1
fi

# Copy template file and replace with linked container env var
rm /etc/nginx/sites-available/default
cp /etc/nginx/sites-available/default.tmpl /etc/nginx/sites-available/default
sed -i "s/FPM_PORT_9000_TCP_ADDR/$FPM_PORT_9000_TCP_ADDR/" /etc/nginx/sites-available/default

# Start nginx
nginx -g "daemon off;"

Finally, the dockerfile:

FROM ubuntu:14.04

RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN apt-get update -y
RUN apt-get install -y software-properties-common

RUN add-apt-repository -y ppa:nginx/stable
RUN apt-get update
RUN apt-get -y install nginx

ADD files/default.tmpl /etc/nginx/sites-available/default.tmpl

ADD files/start-container /usr/local/bin/start-container
RUN chmod +x /usr/local/bin/start-container

VOLUME ["/var/www"]
EXPOSE 80

CMD ["start-container"]

Then build this container and link the volumes and network to the PHP-FPM container:

# Build it, naming the new image "fideloper/nginx"
docker build -t fideloper/nginx ./

# Run it, naming the container "nginxapp"
# This shares the /var/www volume from container "phpfpmapp"
# And is linked to "phpfpmapp"'s network
# And exposes port 80 to the host, so we can access it in the browser
docker run -d  --name="nginxapp" --volumes-from=phpfpmapp \
       --link phpfpmapp:fpm -p 80:80 fideloper/nginx

I've skipped over any explanation here, so if you're not familiar with linking or sharing volumes, read up on that.

These (and explanation) will get published somewhere eventually, but this is what I have working for now. Note that there's now PHP-FPM and Nginx running in separate containers.

Adding a database would involve at least one more container.

I'm packing this week and moving to Texas so I may be too busy to get any deeper with this. Hope this helps!

fideloper commented 9 years ago

Also note how the sharing of volumes is accomplished.

The PHP-FPM container shares the hosts /home/core/share/www directory (change as needed for yourself) with the container's /var/www directory.

Then the Nginx container takes the --volumes-from the PHP-FPM container, so effectively the /home/core/share/www directory on your host is now shared in /var/www in both containers.

That way php-fpm can find PHP files and Nginx can find other static files, all of which appear in each container's /var/www directory, and all of which exist in the host's shared volume.