{"id":316,"date":"2024-11-11T04:43:04","date_gmt":"2024-11-11T04:43:04","guid":{"rendered":"https:\/\/technese.com\/?p=316"},"modified":"2024-11-14T04:50:18","modified_gmt":"2024-11-14T04:50:18","slug":"to-create-a-separate-fastcgi-socket-for-each-virtual-host-vhost-youll-typically-need-to-configure-a-separate-php-fpm-pool-for-each-vhost-this-allows-each-vhost-to-have-its-own-isolated-php-fpm-p","status":"publish","type":"post","link":"https:\/\/technese.com\/index.php\/2024\/11\/11\/to-create-a-separate-fastcgi-socket-for-each-virtual-host-vhost-youll-typically-need-to-configure-a-separate-php-fpm-pool-for-each-vhost-this-allows-each-vhost-to-have-its-own-isolated-php-fpm-p\/","title":{"rendered":"Configure a separate PHP-FPM pool for each vhost. This allows each vhost to have its own isolated PHP-FPM process, which communicates over its own unique socket"},"content":{"rendered":"\n<p>To create a separate FastCGI socket for each virtual host (vhost), you&#8217;ll typically need to configure a separate PHP-FPM pool for each vhost. This allows each vhost to have its own isolated PHP-FPM process, which communicates over its own unique socket. Here\u2019s how you can set it up:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Configure Separate PHP-FPM Pools<\/h3>\n\n\n\n<p>PHP-FPM allows you to define multiple pools, each with unique settings, including its own socket. By creating a separate pool for each vhost, you can control resources independently and enhance security and isolation.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Locate the PHP-FPM Pool Configuration Directory:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP-FPM pool configurations are usually found in <code>\/etc\/php\/&lt;version>\/fpm\/pool.d\/<\/code> (replace <code>&lt;version><\/code> with your PHP version, e.g., <code>7.4<\/code>).<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a New Pool Configuration File for Each Vhost:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Copy the default pool configuration file to create new files for each vhost. For example:<br><code>cp \/etc\/php\/7.4\/fpm\/pool.d\/www.conf \/etc\/php\/7.4\/fpm\/pool.d\/vhost1.conf<br>cp \/etc\/php\/7.4\/fpm\/pool.d\/www.conf \/etc\/php\/7.4\/fpm\/pool.d\/vhost2.conf<\/code><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Edit Each Pool Configuration File:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open each pool file (e.g., <code>vhost1.conf<\/code>) in a text editor and configure the following settings to make each pool unique.<\/li>\n\n\n\n<li><strong>Set a Unique Pool Name:<\/strong> <code>[vhost1]<\/code><\/li>\n\n\n\n<li><strong>Specify a Unique Socket for Each Pool:<\/strong> <code>listen = \/var\/run\/php\/php7.4-fpm-vhost1.sock<\/code><\/li>\n\n\n\n<li><strong>Optionally Configure Additional Settings:<\/strong>\n<ul class=\"wp-block-list\">\n<li>You can configure other settings independently, such as <code>user<\/code>, <code>group<\/code>, <code>pm.max_children<\/code>, etc., to control resource usage for each vhost.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Repeat for Each Vhost:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Change the pool name and socket path in each configuration file to ensure they are unique (e.g., <code>vhost2<\/code> with <code>php7.4-fpm-vhost2.sock<\/code>).<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Restart PHP-FPM:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Once all pools are configured, restart PHP-FPM to apply the changes.<br><code>sudo systemctl restart php7.4-fpm<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. Configure Apache for Each Vhost to Use the Correct FastCGI Socket<\/h3>\n\n\n\n<p>In your Apache configuration, you need to configure each vhost to use its respective FastCGI socket.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Define Each Vhost in Apache<\/strong> (in <code>\/etc\/apache2\/sites-available\/<\/code>):<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each vhost configuration file, use the <code>FastCgiExternalServer<\/code> directive to point to the unique socket created for that vhost&#8217;s PHP-FPM pool.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Example Apache Configuration:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &lt;VirtualHost *:80&gt;\n       ServerName vhost1.example.com\n\n       # FastCGI configuration for vhost1\n       FastCgiExternalServer \/var\/www\/vhost1\/cgi-bin\/php-fcgi -socket \/var\/run\/php\/php7.4-fpm-vhost1.sock -pass-header Authorization -pass-header Content-Type\n       DocumentRoot \/var\/www\/vhost1\n\n       &lt;Directory \/var\/www\/vhost1&gt;\n           Options +ExecCGI\n           Require all granted\n       &lt;\/Directory&gt;\n   &lt;\/VirtualHost&gt;\n\n   &lt;VirtualHost *:80&gt;\n       ServerName vhost2.example.com\n\n       # FastCGI configuration for vhost2\n       FastCgiExternalServer \/var\/www\/vhost2\/cgi-bin\/php-fcgi -socket \/var\/run\/php\/php7.4-fpm-vhost2.sock -pass-header Authorization -pass-header Content-Type\n       DocumentRoot \/var\/www\/vhost2\n\n       &lt;Directory \/var\/www\/vhost2&gt;\n           Options +ExecCGI\n           Require all granted\n       &lt;\/Directory&gt;\n   &lt;\/VirtualHost&gt;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this setup, each vhost has a separate <code>FastCgiExternalServer<\/code> directive pointing to its dedicated PHP-FPM socket.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reload Apache:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>After configuring each vhost, reload or restart Apache to apply the changes.<br><code>sudo systemctl restart apache2<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<p>By configuring a separate PHP-FPM pool and socket for each vhost, each site operates in isolation with its own dedicated FastCGI process. This setup not only enhances security but also allows for resource control per vhost, preventing one site from monopolizing resources at the expense of others.<\/p>\n\n\n\n<p>Source: ChatGPT<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a separate FastCGI socket for each virtual host (vhost), you&#8217;ll typically need to configure a separate PHP-FPM pool for each vhost. This allows each vhost to have its own isolated PHP-FPM process, which communicates over its own unique socket. Here\u2019s how you can set it up: 1. Configure Separate PHP-FPM Pools PHP-FPM allows [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-316","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/posts\/316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/comments?post=316"}],"version-history":[{"count":4,"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":320,"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions\/320"}],"wp:attachment":[{"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/technese.com\/index.php\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}