Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker architecture for hosting a PHP website

I've been looking into Docker and how I could host multiple websites on a server "the Docker way". As far as I understand, ideally everything should be running its own containers and these containers should not be shared.

My question is whether the overview below is a valid container setup with the requests coming into a single reverse proxy which relays the requests to underlaying webservers to handle the actual requests.

Also, in the three situations below I've detailed different ways of including the source files for the application and would like to know which of the three is the most common / best to use.

My wish is to be able to run different websites on a single server in Docker. The websites can have different requirements as illustrated by the different PHP-FPM versions in the diagram. The deployment of the website itself is a bit unclear to me as I'm unsure whether I should include the source files with nginx / PHP-FPM or deploy it seperately.

Docker architecture overview

My current setup is as follows:

docker-compose.yml

version: '2'
services:
    nginx:
        image: nginx:alpine
        restart: always
        ports:
            - '80:80'
        links:
            - example_com
            - example_org
            - example_net
        volumes:
            - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro

    example_com:
        image: php:7.0-fpm-alpine
        restart: always
        volumes:
            - ./sites/example_com:/var/www/example_com:ro

    example_org:
        image: php:7.0-fpm-alpine
        restart: always
        volumes:
            - ./sites/example_org:/var/www/example_org:ro

    example_net:
        image: php:7.0-fpm-alpine
        restart: always
        volumes:
            - ./sites/example_net:/var/www/example_net

nginx/conf.d/default.conf

server {
    listen       80;
    server_name  www.example.com example.com;

    location ~ \.php$ {
        fastcgi_pass   example_com:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example_com$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen       80;
    server_name  www.example.org example.org;

    location ~ \.php$ {
        fastcgi_pass   example_org:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example_org$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen       80;
    server_name  www.example.net example.net;

    location ~ \.php$ {
        fastcgi_pass   example_net:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/example_net$fastcgi_script_name;
        include        fastcgi_params;
    }
}

This is not as illustrated by the overview above, but the situation in the overview is the one that seems to be the ideal situation to me.

How do I go about setting this up with the reverse proxy and the different PHP-FPM containers, and how do I deploy my websites and their subsequent updates?

Any input on this would be greatly appreciated!

like image 921
Revell Avatar asked Oct 10 '16 21:10

Revell


People also ask

Can I run PHP in Docker?

You can use docker run to create a container and execute PHP. You just need to add some volumes to the container. These volumes should include the paths to your code.

Can Docker be used for hosting?

for hosting docker, and you pay only for the resources you needed. This platform is very developer-friendly. It gives you complete access to the environment; you get root access so you can even edit server files according to your need. You can even change the operating system, start/start/reboot the system.


1 Answers

I think the three nginx servers connected to the main nginx server are not necessary. The php-fpm process listens to a socket so you can directly connect to each php-fpm process from the main nginx server

like image 58
Nadir Latif Avatar answered Oct 10 '22 06:10

Nadir Latif