skip to Main Content

I have a Nginx, PHP-FPM and MySQL running inside a docker containers. I want install Opencart 3 in subfolder “public” for future developement.
But when I try to install OC3, I get a 404 error from nginx. At the same time, the server is working, the containers are pinging with each other, I can display the phpinfo() and there it is clear that everything is working properly.
Where is the mistake?

docker-compose.yml

version: '3.7'

services:
  ktt_nginx:
    build: ./docker/nginx
    container_name: ktt_nginx
    ports:
      - '86:80'
    volumes:
      - ./docker/nginx/conf:/etc/nginx/conf.d
      - .:/var/www/html
      - ./docker/nginx/log/:/var/log/nginx
      - /tmp:/tmp
    depends_on:
      - ktt_php
    networks:
      - ktt-net

  ktt_php:
    build: ./docker/php
    container_name: ktt_php
    volumes:
      - .:/var/www/html
      - /tmp:/tmp
      - ~/.composer:/var/www/.composer
    depends_on:
      - ktt_mysql
    networks:
      - ktt-net

  ktt_mysql:
    image: mysql:5.7
    container_name: ktt_mysql
    ports:
      - "3386:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=kttdb
      - MYSQL_USER=opencart
      - MYSQL_PASSWORD=root
    volumes:
      - ktt_mysql_data:/var/lib/mysql
    networks:
      - ktt-net

  ktt_pma:
    image: phpmyadmin/phpmyadmin
    container_name: ktt_pma
    environment:
      - PMA_ARBITRARY=1
    restart: always
    ports:
      - 8086:80
    volumes:
      - /sessions
    depends_on:
      - ktt_mysql
    networks:
      - ktt-net

networks:
  ktt-net:

volumes:
  ktt_mysql_data:

server.conf

server {
    server_name localhost;
    root /var/www/html/public;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index.php(/|$) {
        fastcgi_pass ktt_php:9000;
        fastcgi_split_path_info ^(.+.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/index.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ .php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

nginx Dockerfile

FROM nginx:latest

RUN apt-get update && apt-get install mc -y
RUN usermod -u 1000 nginx && groupmod -g 1000 nginx

3

Answers


  1. Chosen as BEST ANSWER

    I again had to deal with configuring Opencart to work inside a Docker container. And now I was able to do the correct system configuration. So, an example of a configuration for OpenCart 3.

    docker-compose.yml

    version: "3.3"
    
    services:
      cart30_nginx:
        image: nginx:latest
        container_name: cart30_nginx
        ports:
          - "83:80"
        volumes:
          - .:/var/www/html
          - ./docker/nginx/conf:/etc/nginx/conf.d:rw
          - ./docker/nginx/log/:/var/log/nginx
          - /tmp:/tmp
        depends_on:
          - cart30_mysql
        networks:
          - cart30-net
    
      cart30_fpm:
        build: ./docker/php-fpm
        container_name: cart30_fpm
        ports:
          - "9000"
          - "80:80"
        volumes:
          - .:/var/www/html
          - ./docker/php-fpm/php.ini-production:/usr/local/etc/php/php.ini:rw
        depends_on:
          - cart30_mysql
        networks:
          - cart30-net
    
      cart30_mysql:
        build: ./docker/mysql
        cap_add:
          - SYS_NICE
        container_name: cart30_mysql
        ports:
          - "3306:3306"
        volumes:
          - ./docker/mysql/data:/var/lib/mysql:rw
          - ./docker/mysql/sql:/tmp/sql:rw
        environment:
          - MYSQL_ROOT_PASSWORD=123456
          - MYSQL_DATABASE=opencart
          - MYSQL_USER=root
          - MYSQL_PASSWORD=123456
        networks:
          - cart30-net
    
    networks:
      cart30-net:
    

    docker/mysql/Dockerfile

    FROM mysql:5.7.32
    
    RUN usermod -u 1000 mysql
    

    docker/nginx/conf/server.conf

    server {
        listen 80;
        listen [::]:80;
        root /var/www/html;
        index index.php index.html index.htm;
        server_name opencart30.loc;
    
        location / {
        try_files $uri $uri/ =404;
        }
    
        location ~ [^/].php(/|$) {
            fastcgi_pass cart30_fpm:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
    }
    

    docker/php-fpm/Dockerfile

    FROM php:7.4-fpm
    
    RUN apt-get update && apt-get install -y 
    curl 
    wget 
    unzip 
    git 
    libfreetype6-dev 
    libjpeg62-turbo-dev 
    libxslt-dev 
    libicu-dev 
    libmcrypt-dev 
    libpng-dev 
    zlib1g-dev 
    libzip-dev 
    libxml2-dev 
    mediainfo 
    libonig-dev 
    libpq-dev 
    graphviz 
    mc 
    sudo 
    && docker-php-ext-configure gd --with-freetype --with-jpeg 
    && docker-php-ext-install -j$(nproc) gd 
    && docker-php-ext-install pdo_mysql 
    && docker-php-ext-install mysqli 
    && docker-php-ext-install zip 
    && docker-php-ext-install bcmath 
    && docker-php-ext-install sockets 
    && docker-php-source delete 
    && usermod -u 1000 www-data && groupmod -g 1000 www-data
    
    RUN docker-php-ext-configure intl
    RUN docker-php-ext-install intl
    RUN docker-php-ext-install xsl
    RUN docker-php-ext-install soap
    RUN yes | pecl install xdebug-2.9.8
    
    RUN chsh -s /bin/bash www-data
    
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=1.10.19
    
    ADD php.ini /usr/local/etc/php/conf.d/40-custom.ini
    #RUN echo "www-data:root" | chpasswd $$ && adduser www-data sudo
    
    WORKDIR /var/www/html
    USER www-data
    
    CMD ["php-fpm"]
    

    docker/php-fpm/php.ini

    memory_limit = 2G
    
    always_populate_raw_post_data = -1
    
    post_max_size=30M
    
    upload_max_filesize=30M
    
    cgi.fix_pathinfo = 1
    
    fastcgi_split_path_info = 1
    
    max_execution_time = 18000
    
    flag session.auto_start = off
    
    zlib.output_compression = on
    
    suhosin.session.cryptua = off
    
    display_errors = On
    
    max_input_vars = 2000
    
    zend_extension=xdebug.so
    xdebug.remote_enable=1
    xdebug.remote_connect_back=0
    xdebug.remote_handler=dbgp
    xdebug.remote_autostart=1
    xdebug.remote_port=9000
    xdebug.idekey=PHPSTORM
    xdebug.remote_host=172.17.0.1
    

  2. Make sure you have correct permission to the folder containing opencart

    chmod -R 777 /var/www/html

    opencart folder under html

    /var/www/html/opencart

    Now try to run opencart from browser.

    this works with me.

    Login or Signup to reply.
  3. In my case was the $document_root wrong, because the document root of nginx was different to php-fpm.

    Document root of nginx: /usr/share/nginx/html/

    Document root of php:8-fpm: /var/www/html

    fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search