Installing NGINX in a PHP/WordPress container


FROM wordpress:php7.4-fpm-alpine

# Install NGINX
RUN apk update && apk add -f nginx

# NGINX configurations
COPY ./config/nginx/ /etc/nginx/

# Validate NGINX configurations
RUN nginx -t

# Early create file to change its permission
RUN touch /var/run/

# Update NGINX temp folders permissions
RUN chown -R www-data:www-data /var/lib/nginx/ && \
	chown -R www-data:www-data /var/run/

# Forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log

# Source files
ADD . /project

# Update the permissions
RUN chown -R www-data:www-data /project/ && \
    find /project/ -type f -exec chmod 644 {} \; && \
    find /project/ -type d -exec chmod 755 {} \;

# Project folder is working directory
WORKDIR /project

# Expose both secure & insecure
EXPOSE 80 443

CMD php-fpm | nginx -g 'daemon off;'

# Switch to 'www-data'
USER www-data
Code language: Dockerfile (dockerfile)

NGINX default.conf:

server {

    listen 80;
    listen [::]:80;
    listen 443 http2;
    listen [::]:443 http2;

    server_name _;

	client_body_timeout 3s;
	client_header_timeout 3s;

    root /project/web;
    index index.php index.htm index.html;

    # Prevent PHP scripts from being executed inside the uploads folder.
    location ~* /app/uploads/.*.php$ {
       deny all;

    location / {
        try_files $uri $uri/ /index.php?$args;

    # Pass PHP scripts to FastCGI server
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass localhost:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param HTTP_PROXY "";
        if (!-f $document_root$fastcgi_script_name) {
            return 404;

        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 16k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;

}Code language: Nginx (nginx)



, , ,




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.