How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)

How to deploy php-fpm on docker container and apache/nginx on localhost (Ubuntu)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Bridging Host Apache with Dockerized PHP-FPM

Running a web server and application logic in separate containers is a common pattern, but it often leads to friction when you need to iterate quickly on your host machine. A frequent scenario involves installing Apache directly on the Ubuntu host using apt-get while keeping PHP running inside a Docker container managed by PHP-FPM. This hybrid approach allows you to leverage system-level networking and SSL certificates from the host while maintaining the isolation and version control benefits of containers for your runtime environment.

The core challenge lies in communication: how does the Apache process listening on the host communicate with the PHP-FPM socket exposed by the container? The answer is almost exclusively the Unix domain socket. Unlike HTTP requests which traverse the network stack, FastCGI processes in this configuration exchange data through a socket file located at /var/run/php-fpm.sock.

To get this working, you must ensure both services are listening on the same interface and that the host Apache knows exactly where to find the PHP-FPM socket. When configuring your docker-compose.yml or running the container directly via docker run, you need to mount the necessary directories from the host to the container. Specifically, you will map the PHP configuration directory (typically /etc/php/.../fpm) and ensure the socket path exists on the host so Apache can write to it.

On the host Ubuntu system, after installing Apache2, you must create a virtual host configuration file that points to your Docker socket. In the standard sites-available directory, you would define a location block or simply configure the global settings to handle PHP requests by forwarding them to the container's socket. A critical detail often overlooked is permissions. The Apache worker process runs as the www-data user on the host. Consequently, the directory containing the socket and the parent directories must be owned by www-data (or a group that includes it) with appropriate read/write permissions. If the socket is created inside the container with root ownership but not properly mapped or permissioned on the host, Apache will fail to connect with an error indicating it cannot connect to the upstream server.

You can achieve this by mounting specific volumes in your container definition:

volumes:
  - /var/run/php-fpm.sock:/var/run/php-fpm.sock:rw
  - ./your-app-code:/var/www/html:ro

In this setup, the host provides a writable socket file, and the application code is mounted read-only to prevent accidental overwrites. You must ensure that when the container starts, PHP-FPM creates the socket at /var/run/php-fpm.sock. If you are using a specific PHP version (e.g., 8.1), the path might vary slightly depending on your Dockerfile base image, such as /run/php/php8.1-fpm.sock.

This architecture offers distinct advantages over running everything in Docker. It allows you to utilize existing host certificates for HTTPS without complex port forwarding tricks inside the container. Furthermore, it simplifies debugging because you can use standard tail -f on the host logs while still benefiting from the isolated PHP error reporting and dependency management within the container.

However, there are trade-offs to consider. Managing hybrid environments requires a bit more manual configuration than a pure Docker setup. If you switch your entire stack to containers later, you will need to reconfigure Apache or use docker exec to restart services inside a larger orchestration system. Additionally, ensuring the correct user IDs match between the host and container can sometimes be tricky if your application code relies on specific file ownership.

For developers looking for production-grade examples of this exact setup, particularly regarding Laravel applications running in this hybrid mode, resources like https://laravelcompany.com often provide detailed deployment guides that walk through the specific docker-compose configurations and Apache virtual host setups required to keep the stack stable.

Our network