dimanche 10 mars 2019

PHP7 + Laravel +Nginx is terribly slow on Docker

I have a project, that have been running OK on an EC2 instance for over a year. The response from backend to frontend is usually in 600ms. On this server, let's call it server A the frontend and backend are hosted on the same server and the database is hosted on MongoDB Atlas.

Now we are using docker and the frontend and the backend are in separate containers, each container is hosted on a different instance. The response time now has jumped to 3.36s. So from 600 milliseconds to 3.36 seconds. And I am using the same database server used with Server A.

I would really love to hear your thoughts on how to optimize PHP, Laravel and Nginx to get the fastest response times on Docker.

Here are my configuration files, the DockerFile, start.sh and nginx.conf

DockerFile

FROM nginx:mainline-alpine

COPY start.sh /start.sh
COPY nginx.conf /etc/nginx/nginx.conf
COPY supervisord.conf /etc/supervisord.conf

RUN apk add --update \
php7 \
php7-fpm \
php7-pdo \
php7-pdo_mysql \
php7-gd \
php7-mcrypt \
php7-mbstring \
php7-xml \
php7-openssl \
php7-json \
php7-phar \
php7-curl \
php7-zip \
php7-fileinfo \
php7-xmlwriter \
php7-tokenizer \
php7-mongodb \
php7-dom \
php7-session \
php7-zlib && \
php7 -r "copy('http://getcomposer.org/installer', 'composer-setup.php');" && \
php7 composer-setup.php --install-dir=/usr/bin --filename=composer && \
php7 -r "unlink('composer-setup.php');" && \
ln -s -f /usr/bin/php7 /usr/bin/php && \
ln -s -f /etc/php7/php.ini /etc/php7/conf.d/php.ini

RUN apk add --update \
bash \
openssh-client \
supervisor

RUN mkdir -p /etc/nginx && \
mkdir -p /etc/nginx/sites-available && \
mkdir -p /etc/nginx/sites-enabled && \
mkdir -p /run/nginx && \
ln -s /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf && \
mkdir -p /var/log/supervisor && \
rm -Rf /var/www/* && \
chmod 755 /start.sh

RUN php -i | grep -i gd

RUN wget -O phpunit https://phar.phpunit.de/phpunit-8.phar
RUN chmod +x phpunit
RUN ./phpunit --version

RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" \
-e "s/variables_order = \"GPCS\"/variables_order = \"EGPCS\"/g" \
/etc/php7/php.ini && \
sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" \
-e "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g" \
-e "s/user = nobody/user = nginx/g" \
-e "s/group = nobody/group = nginx/g" \
-e "s/;listen.mode = 0660/listen.mode = 0666/g" \
-e "s/;listen.owner = nobody/listen.owner = nginx/g" \
-e "s/;listen.group = nobody/listen.group = nginx/g" \
-e "s/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm.sock/g" \
-e "s/^;clear_env = no$/clear_env = no/" \
/etc/php7/php-fpm.d/www.conf

EXPOSE 80
COPY . /var/www
WORKDIR /var/www
RUN chmod -R 777 storage/
RUN echo `ls`


CMD ["/start.sh"]

start.sh

#!/bin/bash

# ----------------------------------------------------------------------
# Create the .env file if it does not exist.
# ----------------------------------------------------------------------

if [[ ! -f "/var/www/.env" ]] && [[ -f "/var/www/.env.example" ]];
then
cp /var/www/.env.example /var/www/.env
fi

# ----------------------------------------------------------------------
# Run Composer
# ----------------------------------------------------------------------

if [[ ! -d "/var/www/vendor" ]];
then
cd /var/www
composer install --no-scripts
composer update
composer dump-autoload
fi

php artisan optimize --force
php artisan config:cache

# ----------------------------------------------------------------------
# Start supervisord
# ----------------------------------------------------------------------

exec /usr/bin/supervisord -n -c /etc/supervisord.conf

nginx.conf

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log off;
    sendfile on;
    #tcp_nopush on;
    keepalive_timeout 0;
    #gzip on;

    fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=BACKEND:500m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    add_header X-Cache $upstream_cache_status;

    server {

        listen 80;

        root /var/www/public;
        index index.php index.html;
                set $no_cache 0;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
            }

        location ~ /\. {
            deny all;
            }

        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_cache BACKEND;
            fastcgi_cache_valid 200 1m;
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire