my production server is running Docker with a classic structure
Db-Container, Server-Container and Php-Fpm container.
What i would like to do is to split the sources in order to have different containers for the 3 main parts of the project. Now they work the old way like mydomain.com/index for the main site, mydomain.com/api and mydomain.com/adm for other services.
How i have to setup the Apache virtual host in order to map requests like this?
mydomain.com -> fcgi://siteFpm:9000
mydomain.com/api -> fcgi://apiFpm:9000
mydomain.com/cms -> fcgi://cmsFpm:9000
Thanks
Use docker-compose expose and FastCgiExternalServer in conf file or vhosts file
(Note this is an approach I will be taking and am still researching. I will update as I know more. But it should hopefully give you an outline of what to do too. I do update my answers, not an empty promise, see this example here: Getting a LAMP stack running on a Vagrant VM (under windows 7 host), full instructions?)
Install docker-compose which provides an official standardised way to batch/automate running of your docker containers, using a docker-compose.yml
file, rather than using the command line docker
command to start up each command individually.
In the docker-compose.yml
file, define your php-fpm service, e.g.:
services:
use the EXPOSE
keyword statement/instruction to make available the port of your php-fpm to apache.
An example of expose
is shown in this article: Multiple versions of AMP in One Host where in the example docker-compose.yml
contains this expose statement:
expose:
- "3306"
- which enables the sql database to be available to other docker containers.
You will also need to ensure that the php files are available to both php and apache containers - CREDIT: https://stackoverflow.com/a/40449377/227926
Then, that same expose:
port, together with the service name will need to be referenced from the Apache FastCgiExternalServer
directive in the vhosts file or conf file. Choosing a vhosts file or conf file to put the directive in is, I think, what seems to be a personal preference though research may reveal differences where one or the other suits your circumstance more.
An example in the conf file would look something like:
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization
What conf file should I put this in?
Answer: there are several options (as indicated above), in a little more detail these are (assuming Ubunut/Debian Linux is the OS that Apache is running on top of):
- httpd.conf
- 000-default.conf (the default vhost) (in sites-available)
- yoursite.conf (in sites-available)
- and (e.g.) /etc/apache2/conf-available/php5.6-fpm.conf
Where would these conf files be located?
Answer: inside your apache docker container. You will need to use the docker-compose.yml file to add (inject) the setup described into the container, once you have defined the services. You can execute, from the docket-compose.yml the standard linux commands to insert text into config files.
You should automate the adding of these settings rather than go in an manually edit Apache config files inside the container, because: 1) automate means that the setup is repeatable and can therefore be used for different platforms in the development workflow: dev, qa, uat, live/prod 2) no manual work required 3) Docker containers are intended to be ephemeral in that they can be destroyed and recreated. Any persistent data should be kept outside of them - in the host -(config in Dockerfiles, docker-composer files, assets (images) in separate folders, database store outside of the container and on the host too.
Examples of FastCgiExternalServer directive:
- Apache 2.4 + PHP-FPM and Authorization headers
- https://www.cyberciti.biz/tips/rhel-fedora-centos-apache2-external-php-spawn.html
- https://www.howtoforge.com/using-php5-fpm-with-apache2-on-centos-6.2-p2
- Difference between FastCgiExternalServer and FastCgiServer in Apache FastCGI PHP?
- Apache 2.4.6 on Ubuntu Server: Client denied by server configuration (PHP FPM) [While loading PHP file]
- https://www.digitalocean.com/community/questions/apache-2-4-with-php5-fpm?answer=12056
- https://www.howtoforge.com/tutorial/apache-with-php-fpm-on-ubuntu-16-04/#-making-phpfpm-use-a-tcp-connection-optional
References to Debian/Ubuntu conventions for conf files and Apache
- https://serverfault.com/questions/216252/how-to-configure-apache-sites-available-vs-httpd-conf
- https://forum.owncloud.org/viewtopic.php?t=30157
- https://askubuntu.com/questions/378734/how-to-configure-apache-to-run-php-as-fastcgi-on-ubuntu-12-04-via-terminal
Discussions about PHP-FPM
- https://serverfault.com/questions/645755/differences-and-dis-advanages-between-fast-cgi-cgi-mod-php-suphp-php-fpm
Useful related information about docker-file.yml statements
- Difference between 'image' and 'build' within docker compose
- https://docs.docker.com/compose/compose-file/#build
Similar discussions on running seperate containers together
- https://medium.com/docker-captain/multiple-versions-of-amp-in-one-host-6e107c836cd8
- php docker link apache docker
- Multi Docker container with PHP7 fpm and nginx
- Docker - Run Apache on host and container for different websites
- Linking nginx and php-fpm container together for fast interaction in docker prod
- How to correctly link php-fpm and Nginx Docker containers?