<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Fastcgi on K-Life Hack | Seoul Gastronomy &amp; Travel Guide</title><link>https://klifehack.com/en/tags/fastcgi/</link><description>Recent content in Fastcgi on K-Life Hack | Seoul Gastronomy &amp; Travel Guide</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 01 Jun 2026 13:49:50 +0900</lastBuildDate><atom:link href="https://klifehack.com/en/tags/fastcgi/index.xml" rel="self" type="application/rss+xml"/><item><title>Implementation of Connecting Apache on a Docker Container to PHP-FPM on the Host OS via FastCGI Proxy</title><link>https://klifehack.com/en/p/docker-apache-host-php-fpm-proxy/</link><pubDate>Mon, 01 Jun 2026 13:49:50 +0900</pubDate><guid>https://klifehack.com/en/p/docker-apache-host-php-fpm-proxy/</guid><description>&lt;h1 id="building-a-php-execution-environment-with-a-hybrid-configuration-of-docker-container-apache-and-host-os-php-fpm"&gt;Building a PHP Execution Environment with a Hybrid Configuration of Docker Container (Apache) and Host OS (PHP-FPM)
&lt;/h1&gt;&lt;h2 id="1-architecture-overview-and-selection-rationale"&gt;1. Architecture Overview and Selection Rationale
&lt;/h2&gt;&lt;p&gt;This article details the implementation process of a hybrid configuration that links the Apache HTTP Server running inside a Docker container with PHP-FPM (FastCGI Process Manager) running directly on the host OS. While it is common in Docker environments to run PHP within the same container or in a sidecar configuration, this setup physically and logically separates the presentation layer (Apache) from the application processing layer (PHP).&lt;/p&gt;
&lt;h3 id="11-current-issues"&gt;1.1 Current Issues
&lt;/h3&gt;&lt;p&gt;In the containerized Apache (container name: &lt;b&gt;web2&lt;/b&gt;), while static content (HTML) delivery was functioning normally, it was observed that PHP file requests were not being parsed, resulting in the source code being exposed or the browser attempting to download the file. This is caused by the absence of a PHP interpreter within the container or Apache&amp;rsquo;s inability to properly handle PHP requests.&lt;/p&gt;
&lt;h3 id="12-adopted-solution-hybrid-proxy-model"&gt;1.2 Adopted Solution: Hybrid Proxy Model
&lt;/h3&gt;&lt;p&gt;After considering the following two options, Option 2 was adopted.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;Container Rebuild&lt;/b&gt;: Create a new image containing both Apache and PHP.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Hybrid Proxy Model&lt;/b&gt;: Connect the existing Apache container and the PHP-FPM on the host OS via a network bridge.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;By adopting Option 2, it is possible to utilize the host OS&amp;rsquo;s native hardware resources for PHP processing while preventing container image bloat. Furthermore, operational flexibility is improved because the management of PHP extension modules and configuration changes can be completed on the host side.&lt;/p&gt;
&lt;h2 id="2-host-os-configuration-building-php-fpm"&gt;2. Host OS Configuration: Building PHP-FPM
&lt;/h2&gt;&lt;p&gt;Prepare the environment on the host OS side to accept FastCGI requests from the container.&lt;/p&gt;
&lt;h3 id="21-package-installation"&gt;2.1 Package Installation
&lt;/h3&gt;&lt;p&gt;Use the host OS package manager (dnf) to install the PHP core and major modules.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;dnf install -y php-fpm php-mysqlnd php-opcache php-mbstring
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;b&gt;php-fpm&lt;/b&gt;: A FastCGI manager that processes requests from the web server.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;php-mysqlnd&lt;/b&gt;: Driver for database connections.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;php-opcache&lt;/b&gt;: Improves execution speed by keeping compiled bytecode in shared memory.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;php-mbstring&lt;/b&gt;: Essential for proper handling of multi-byte strings.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="22-modifying-php-fpm-configuration-wwwconf"&gt;2.2 Modifying PHP-FPM Configuration (www.conf)
&lt;/h3&gt;&lt;p&gt;By default, PHP-FPM listens on a Unix domain socket or 127.0.0.1:9000, and access from the outside (container) is denied. Change this to accept requests over the network.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;vi /etc/php-fpm.d/www.conf
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Configuration adjustments:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-ini" data-lang="ini"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;; Port setting to allow requests from outside&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;listen&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;8080&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;; Restriction of clients allowed to access&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a6e22e"&gt;listen.allowed_clients&lt;/span&gt; &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#e6db74"&gt;127.0.0.1, 192.168.159.10&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After changing the settings, start and enable the service.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;systemctl start php-fpm
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;systemctl enable php-fpm
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="3-docker-container-configuration-apache-fastcgi-proxy"&gt;3. Docker Container Configuration: Apache FastCGI Proxy
&lt;/h2&gt;&lt;p&gt;Next, configure the Apache container (&lt;b&gt;web2&lt;/b&gt;) to forward PHP requests to port 8080 of the host OS.&lt;/p&gt;
&lt;h3 id="31-editing-the-apache-configuration-file"&gt;3.1 Editing the Apache Configuration File
&lt;/h3&gt;&lt;p&gt;Enter the container&amp;rsquo;s shell and edit httpd.conf.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;docker exec -it web2 /bin/bash
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;vi /usr/local/apache2/conf/httpd.conf
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="311-enabling-proxy-modules"&gt;3.1.1 Enabling Proxy Modules
&lt;/h4&gt;&lt;p&gt;Uncomment the following lines to enable the FastCGI proxy functionality.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-apache" data-lang="apache"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;LoadModule proxy_module modules/mod_proxy.so
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id="312-handler-configuration"&gt;3.1.2 Handler Configuration
&lt;/h4&gt;&lt;p&gt;Add the proxy settings for PHP files to the end of the file. Here, assume the host OS IP address is 192.168.159.10.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-apache" data-lang="apache"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://192.168.159.10:8080/var/www/html/$1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With this configuration, all requests with a .php extension will be forwarded to the specified FastCGI endpoint.&lt;/p&gt;
&lt;h2 id="4-integration-testing-and-verification"&gt;4. Integration Testing and Verification
&lt;/h2&gt;&lt;p&gt;After completing the configuration, verify that communication between the container and the host is functioning correctly.&lt;/p&gt;
&lt;h3 id="41-verification-flow"&gt;4.1 Verification Flow
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;&lt;b&gt;Entry Point&lt;/b&gt;: Access index.php (HTML form) on the container.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Data Transmission&lt;/b&gt;: Send data from the form to login.php using the POST method.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Proxy Processing&lt;/b&gt;: Apache detects the request to login.php and forwards it to 192.168.159.10:8080 on the host OS.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;PHP Execution&lt;/b&gt;: PHP-FPM on the host OS executes the script and returns the result to Apache.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Response&lt;/b&gt;: Confirm that the execution result is displayed in the browser.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If the PHP code is not displayed as-is and the result processed on the server side is returned as expected, the proxy integration is successful.&lt;/p&gt;
&lt;h2 id="operational-notes"&gt;Operational Notes
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;⚠️ &lt;b&gt;Network Reachability&lt;/b&gt;: Ensure that port 8080 is open from the container to the host OS IP address in the firewall settings (e.g., firewalld).&lt;/li&gt;
&lt;li&gt;🛠️ &lt;b&gt;File Path Consistency&lt;/b&gt;: The document root seen by Apache and the script path referenced by PHP-FPM must match. If they do not match, a &lt;code&gt;File not found&lt;/code&gt; error will occur.&lt;/li&gt;
&lt;li&gt;💡 &lt;b&gt;Performance&lt;/b&gt;: Since this is a proxy over the network, overhead may occur compared to Unix domain sockets in high-traffic environments. Consider tuning php-opcache as necessary.&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>