I'm using nginx-proxy-automation to run my php application which is written using CodeIgniter 4
and the app structure is the following:
php-application
docker
php-fpm
config
php.ini
Dockerfile
src
node_modules
app
public
tests
vendor
writable
.env
composer.json
package.json
spark
docker-comopse.yml
the index.php
file is available inside the public
folder.
The docker-compose.yml
of php-application
contains the following stuff:
nginx
nginx-proxy
docker-compose.yml
php-application
docker-compose.yml
Inside the php-application/docker-compose.yml
I have this:
version: '3.7'
services:
php-fpm:
container_name: boilerplate_app
restart: always
build:
context: .
dockerfile: ./docker/php-fpm/Dockerfile
volumes:
- ./src:/var/www/html
environment:
# NGINX-PROXY ENVIRONMENT VARIABLES: UPDATE ME
- VIRTUAL_HOST=mysite.com
- VIRTUAL_ROOT=/var/www/html/public
- VIRTUAL_PORT=9000
- VIRTUAL_PROTO=fastcgi
- LETSENCRYPT_HOST=mysite.com
- [email protected]
- NETWORK=proxy
# /END NGINX-PROXY ENVIRONMENT VARIABLES
ports:
- '9000:80'
expose:
- 9000
networks:
- proxy
database:
container_name: boilerplate_db
restart: always
build:
context: ./docker/database
environment:
- MYSQL_DATABASE=boilerplate
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./docker/database/data.sql:/docker-entrypoint-initdb.d/data.sql
phpmyadmin:
container_name: boilerplate_phpmyadmin
image: phpmyadmin/phpmyadmin
restart: always
ports:
- 8088:80
environment:
- PMA_HOST=database
- MYSQL_USER=user
- MYSQL_PASSWORD=secret
- MYSQL_ROOT_PASSWORD=secret
depends_on:
- database
networks:
proxy:
external:
name: proxy
essentially I have three services:
- php-fpm: which mount the application files in the
/var/www/html
folder, and in theenvironment
section, I have specified theVIRTUAL_PORT
as 9000 'causephp-fpm
runs overfastcgi
. Then I linked theproxy
network which is the network of your image. - database: runs within the app network
- phpmyadmin: runs within the app network
The Dockerfile
content of php-fpm
contains the following stuff:
FROM php:8.0.2-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN apk add icu-dev
# Install intl
RUN docker-php-ext-configure intl && docker-php-ext-install intl
# Install curl
RUN apk add --update libzip-dev curl-dev &&\
docker-php-ext-install curl && \
apk del gcc g++ &&\
rm -rf /var/cache/apk/*
COPY docker/php-fpm/config/php.ini /usr/local/etc/php/
# Install composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
# Install nodejs
RUN apk add --update nodejs nodejs-npm
RUN npm install gulp-cli -g
RUN npm install
COPY src src/
CMD ["php-fpm"]
EXPOSE 9000
when I start the container using docker-compose up --build -d
I get this message when I visit mysite.com
(I have hidden the real domain for privacy):
File not found.
Inspecting the nginx
log using sudo docker logs -f nginx
I get:
[error] 30#30: *39 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 2.38.140.109, server: mysite.com, request: "GET / HTTP/2.0", upstream: "fastcgi://172.28.0.7:9000", host: "mysite.com"
mysite.com 2.38.140.109 - - [29/Mar/2021:17:52:31 +0000] "GET / HTTP/2.0" 404 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63"
For fix this problem, I have update the nginx.tmpl
with this one.
The problem now is that when I load my site all the static files such js / image / css return 404.
What can I do for fix this?
php-application/docker-compose.yml
environment section isVIRTUAL_ROOT=/var/www/html
, but you said it is/var/www/html/public
. Are you sure of yourphp-application/docker-compose.yml
? What if you letnginx
execute anindex.html
from/var/www/html/
to see if it's working properly? – Statertail /var/log/nginx/error.log
to see the error. Before, runls -l /var/log/nginx/error.log
. If it's pointed tostderr
, remove this file and runnginx -s reload
and run your site again, then runtail -f ...
to see errors – Stater/etc/resolve.conf
. It seems there's resolve problem. Check both on your host and container. – Stater/etc/resolve.conf
in your host with this single linenameserver 8.8.8.8
, then restart your host's network and try your task again. – Staterdocker exec -ti nginx-proxy cat /etc/nginx/fastcgi.conf
to see if you have this file. If does not exist, rundocker exec -ti nginx-proxy echo "fastcgi_index index.php;" >> /etc/nginx/fastcgi.conf
. Then rundocker exec -it nginx-proxy nginx -s reload
Or as said in the last post, create a file called/etc/nginx/fastcgi_params
and reload nginx again to see if it works. – Stater