How to Install and Examples of Using Swoole PHP on FreeBSD

Swoole is a framework for PHP based on asynchronous co-routine. Swoole is specifically designed for building concurrent systems at scale. Swoole is written using the C or C++ programming language and installed as a PHP extension. If you want to build Swoole PHP on a local computer, you have to install it via pecl. Meanwhile, on FreeBSD systems, Swole can be easily installed, the repository is available.

Swoole PHP originates from neighboring China, developed primarily by Chinese developers working on large-scale applications for the Chinese market. Therefore, this product has been tested and proven in high-traffic production environments. Swoole is an application that you can definitely rely on to help you with your work, and is fun to use.


1. Advantages of Swoole PHP

Compared to other asynchronous programming frameworks or software such as Apache, Nginx and Node.js, Swoole supports more asynchronous programming via multi-threaded I/O modules (HTTP Server, WebSockets, TaskWorkers). Apart from that, Swoole PHP also supports Redis, CURL, protocol TCP/UDP and Unix socket.

With the advantages that Swoole has, you can freely create applications with PHP on a large scale (webserver), create applications with APIs, create chat systems, CMS platforms, and even create applications with real time web services.



Swoole has a number of advantages that other applications like HodeJS don't have, including multiple web workers and separate task workers, coroutine support, and the ability to significantly increase request limits. Here are some of the advantages of Swoole PHP:

* event-driven
* full asynchronous non-blocking
* multi-thread reactor
* Supports Separate Task Workers
* millisecond timer
* Supports Multiple Web Workers
* async MySQL
* async task
* async read/write file system
* Can Increase Request Ceiling
* async dns lookup
* No Web Server Needed
* support IPv4/IPv6/UnixSocket/TCP/UDP
* Coroutine Support

At first glance, Swoole's features are similar to NodeJS, both have an Event Loop feature, provide asynchronous HTTP, network and socket clients, you can create a network server, and much more.


2. Install Swoole

On FreeBSD, installing Swoole is very easy, no configuration files, no rc.d start up. To run it you only need to add a PHP script. The following is how to install Swoole on.
root@ns3:~ # cd /usr/ports/devel/pecl-swoole
root@ns3:/usr/ports/devel/pecl-swoole # make config
When you run the "make config" command, a menu will appear that you must activate together with Swoole. You must adjust this choice to your needs and FreeBSD system.



After that, continue with the command "make install clean".
root@ns3:/usr/ports/devel/pecl-swoole # make install clean


3. Create Swoole App

As a complement, in this article we will provide an example of using Swoole on FreeBSD. The first step you have to do is create a working directory. We will place the Swoole project in "/var/freebsd-swoole-app".
root@ns3:~ # cd /var
root@ns3:/var # mkdir -p freebsd-swoole-app
root@ns3:/var # cd freebsd-swoole-app
root@ns3:/var/freebsd-swoole-app #
Continue by creating an "index.html" file, using your favorite editor.
root@ns3:/var/freebsd-swoole-app # touch index.html
root@ns3:/var/freebsd-swoole-app # chmod -R 775 index.html
In the "/var/freebsd-swoole-app/index.html" file, type the script below.

<div style="text-align: justify;">The Apache web server has evolved over the years to serve various needs and tasks related to the HTTP/HTTPS protocol. Like other web servers, Apache must solve one important problem, namely how to serve various processes to serve HTTP/HTTPS protocol requests. This work includes opening sockets, handling requests, keeping connections open for a certain period of time, handling new events that occur over those connections, and returning content generated by programs written in a particular language (such as PHP, Perl or Python). These tasks are executed and controlled by the Multi-Processing Module (MPM).</div><div style="text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-AN2uAdlqKrA5mTD8ICiZsQwPNCJSeHD58nsQIA1hXbwv64JxRTV2b4mliBNZ8UtAs4qHv1dSuKRPjh9UuUMsxK8KZc_TBXzbyGlz2J7fs_4xthAAyG0RXbm96zJ-h_-vVL6LI1inNrlY-8_u2ns1vASW3BDeB5h-MPY3jkHR2wpJD8Q9GpLyKKo4Ba4/s1024/php%20fpm.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="768" data-original-width="1024" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-AN2uAdlqKrA5mTD8ICiZsQwPNCJSeHD58nsQIA1hXbwv64JxRTV2b4mliBNZ8UtAs4qHv1dSuKRPjh9UuUMsxK8KZc_TBXzbyGlz2J7fs_4xthAAyG0RXbm96zJ-h_-vVL6LI1inNrlY-8_u2ns1vASW3BDeB5h-MPY3jkHR2wpJD8Q9GpLyKKo4Ba4/s320/php%20fpm.jpg" width="320" /></a></div><br /><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><div><div><b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;">1. PGP-FPM Apache</span></b></div><div><br /></div></div><div>On the Apache Web server there are 3 MPM:</div><div><ol><li><b>mpm_prefork:</b>&nbsp;A new process is created for each incoming connection that reaches the server. Each process is isolated from the others, so there is no shared memory between them, even if they make identical calls at some point in their execution. This method is a safe way to run applications that are linked to libraries that do not support multithreading, usually older applications or libraries.</li><li><b>mpm_worker:</b>&nbsp;The parent process is responsible for starting a collection of child processes, some of which listen for new incoming connections, while others serve requested content. Each process is multi-threaded (each thread serves one connection), so one process can handle multiple requests at once. This method of handling connections promotes better resource usage while maintaining stability. This is a result of the pool of available processes, which often have free threads ready to serve new connections immediately.</li><li><b>mpm_event:</b>&nbsp;While these processes depend on their workers, this MPM goes a step further by optimizing the way the parent process schedules tasks for child processes and the threads associated with them. By default, the connection remains open for 5 seconds and is closed if no new activity occurs. This process is the default value of the keep-alive directive, which keeps the stream associated with it. MPM events allow a process to manage threads so that some threads are free to process new incoming connections while others remain tied to direct connections. This process will allow the redistribution of assigned tasks to threads, thereby improving resource usage and performance.</li></ol></div><div><div><div>On FreeBSD systems by default Apache24 uses the MPM (Multi-Processing Module) prefork. In some cases these modules are very good and offer the most compatibility, MPM provides better performance on most systems. However, because PHP is not thread-safe, the regular mod_php method (PHP as an Apache module) is only compatible with preforks.</div><div><br /></div><div>As a solution PHP-FPM (FastCGI Process Manager) is an alternative that provides a CGI daemon that can be used by any version of Apache and offers its own performance benefits over regular mod_php (PHP as an Apache module).</div><div><br /></div><div>PHP-FPM is a FastCGI process manager for PHP. The FastCGI protocol is based on the Common Gateway Interface (CGI), a protocol created between applications and web servers such as Apache HTTP. This allows developers to write applications separately from the web server behavior. The program carries out its processes independently and transfers its products to the web server using this protocol. Each new connection that the application needs to handle will create a new process.</div><div><br /></div><div>By combining the Apache HTTP MPM module with PHP's FastCGI, process Manager (PHP-FPM), websites can load faster and handle more concurrent connections using fewer resources.</div></div><div><br /></div><div><br /></div><div><b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;">2.&nbsp;</span></b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;"><b>System Requirements</b></span></div><div><br /></div><div>OS: FreeBSD 13.2</div><div>Web Server: Apache24</div><div>Modul: PHP82 dan mod_php</div><div><br /></div><div>Before you continue reading this article, it is recommended that you read the previous article which discusses the configuration and installation of apache24, PHP82 and the mod_php module.</div><div><br /></div><div><b style="color: #2b00fe; font-family: Unna; font-size: x-large;"><a href="https://www.unixwinbsd.site/2023/11/freebsd-phpmyadmin-starter-with-apache24.html">FreeBSD phpMyAdmin Starter With Apache24</a></b></div><div><span style="color: #0000ee; font-family: Unna; font-size: large;"><b><u><a href="https://www.unixwinbsd.site/2023/11/wordpress-on-freebsd-installation-and.html">Wordpress on Freebsd - Installation and Configuration For Blog</a></u></b></span></div><div><br /></div><div><br /></div></div><div><br /></div><div style="text-align: left;"><b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;">3.&nbsp;</span></b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;"><b>Apache24 PHP-FPM Configuration</b></span></div><div><br /></div><div>The main PHP-FPM configuration file is located in the /usr/local/etc/php-fpm.d/www.conf folder, edit the file as in the example below.</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">ee /usr/local/etc/php-fpm.d/www.conf</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">user = www<br />group = www<br />listen = 192.168.5.2:9000<br />listen.owner = www<br />listen.group = www<br />listen.mode = 0660</span></code></pre></div><div><br /></div><div>Remove the "#" sign in the script above, listen = 192.168.5.2 is the FreeBSD Server IP Address. After the main PHP-PFM configuration file has been edited, continue by creating the file /usr/local/etc/apache24/Includes/php-fpm.conf and type the script below in that file.</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">ee /usr/local/etc/apache24/Includes/php-fpm.conf</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">&lt;IfModule proxy_fcgi_module&gt;<br />   &lt;IfModule dir_module&gt;<br />       DirectoryIndex index.php<br />   &lt;/IfModule&gt;<br />   &lt;FilesMatch "\.(php|phtml|inc)$"&gt;<br />       SetHandler "proxy:fcgi://192.168.5.2:9000"<br />   &lt;/FilesMatch&gt;<br />&lt;/IfModule&gt;</span></code></pre></div><div><br /></div><div><div>The script above is to activate the PHP-FPM module. What you need to pay attention to when creating the script above is that the IP Address in SetHandler must be the same as the IP Address File /usr/local/etc/php-fpm.d/www.conf.</div><div><br /></div><div>The core of the PHP-FPM configuration lies in the /usr/local/etc/apache24/httpd.conf file. Edit several scripts in the file by removing the "#" sign in front of the script file /usr/local/etc/apache24/httpd.conf. Follow the example below to enable the PHP-FPM module on apache24.</div></div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">ee /usr/local/etc/apache24/httpd.conf</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">#LoadModule mpm_event_module libexec/apache24/mod_mpm_event.so<br />LoadModule mpm_prefork_module libexec/apache24/mod_mpm_prefork.so<br />#LoadModule mpm_worker_module libexec/apache24/mod_mpm_worker.so<br />LoadModule authnz_fcgi_module libexec/apache24/mod_authnz_fcgi.so<br />LoadModule proxy_module libexec/apache24/mod_proxy.so<br />LoadModule proxy_http_module libexec/apache24/mod_proxy_http.so<br />LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so<br />LoadModule proxy_scgi_module libexec/apache24/mod_proxy_scgi.so</span></code></pre></div><div><br /></div><div>According to the explanation above in this article, there are 3 MPM modules, you can choose one according to your needs. In this article, the mpm_event module is used. After that, continue by editing the /etc/make.conf file and enter the script below in the /etc/make.conf file.</div><div><br /></div><div><b>DELETE</b> the script below in the /usr/local/etc/apache24/httpd.conf file</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><div style="font-family: &quot;Times New Roman&quot;; font-size: medium; text-align: justify; white-space: normal;"><span style="color: #ffa400;">&lt;FilesMatch "\.php$"&gt;</span></div><div style="font-family: &quot;Times New Roman&quot;; font-size: medium; text-align: justify; white-space: normal;"><span style="color: #ffa400;">&nbsp; &nbsp; SetHandler application/x-httpd-php</span></div><div style="font-family: &quot;Times New Roman&quot;; font-size: medium; text-align: justify; white-space: normal;"><span style="color: #ffa400;">&lt;/FilesMatch&gt;</span></div><div style="font-family: &quot;Times New Roman&quot;; font-size: medium; text-align: justify; white-space: normal;"><span style="color: #ffa400;">&lt;FilesMatch "\.phps$"&gt;</span></div><div style="font-family: &quot;Times New Roman&quot;; font-size: medium; text-align: justify; white-space: normal;"><span style="color: #ffa400;">&nbsp; &nbsp; SetHandler application/x-httpd-php-source</span></div><div style="font-family: &quot;Times New Roman&quot;; font-size: medium; text-align: justify; white-space: normal;"><span style="color: #ffa400;">&lt;/FilesMatch&gt;</span></div></pre></div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">ee /etc/make.conf</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">WITH_MPM=event</span></code></pre></div><div><br /></div><div><br /></div><div style="text-align: left;"><b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;">4.&nbsp;</span></b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;"><b>Test PHP-FPM Apache24</b></span></div><div><br /></div><div>After all the configurations above are complete, it's time for us to do the test. Create a test file called php.info and enter the script below in the file /usr/local/www/apache24/data/info.php.</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">ee /usr/local/www/apache24/data/info.php</span><span style="color: #1f2328;"><br /><br /></span><span style="color: #ffa400;">&lt;?php phpinfo(); ?&gt;</span></code></pre></div><div><br /></div><div>Give ownership rights to the file and also file permissions.</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">chown -R www:www /usr/local/www/apache24/data/info.php</span><span style="color: #1f2328;"><br />root@ns1:~ # </span><span style="color: red;">chmod +x /usr/local/www/apache24/data/info.php</span></code></pre></div><div><br /></div><div>The next step is to activate the PHP-FPM module so that it can run automatically. Edit the /etc/rc.conf file and enter the script below.</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">ee /etc/rc.conf</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">php_fpm_enable="YES"</span></code></pre></div><div><br /></div><div>Before we do the test, restart or reboot the apache24 and PHP-FPM web server.</div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">service apache24 restart</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">Performing sanity check on apache24 configuration:<br />Syntax OK<br />Stopping apache24.<br />Waiting for PIDS: 2437.<br />Performing sanity check on apache24 configuration:<br />Syntax OK<br />Starting apache24.</span></code></pre></div><div><br /></div><div><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><code style="background: transparent; border-radius: 6px; border: 0px; box-sizing: border-box; display: inline; font-family: ui-monospace, SFMono-Regular, &quot;SF Mono&quot;, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-size: 13.6px; line-height: inherit; margin: 0px; overflow-wrap: normal; overflow: visible; padding: 0px; word-break: normal;"><span style="color: #1f2328;">root@ns1:~ # </span><span style="color: red;">service php-fpm restart</span><span style="color: #1f2328;"><br /></span><span style="color: #ffa400;">Performing sanity check on php-fpm configuration:<br />[20-Aug-2023 13:20:07] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful<br /><br />Stopping php_fpm.<br />Waiting for PIDS: 2105.<br />Performing sanity check on php-fpm configuration:<br />[20-Aug-2023 13:20:07] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful<br /><br />Starting php_fpm.</span></code></pre></div><div><br /></div><div>The way to do the PHP-FPM test is to type the IP Address of the FreeBSD Server, because in this article the FreeBSD server uses IP 192.168.5.2, type the IP Address in a Web Browser such as Yandex or Google Chrome, and the results should appear as shown in the image below.</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://raw.githubusercontent.com/iwanse1977/iwanse1977/main/php%20fpm.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="600" data-original-width="800" height="600" src="https://raw.githubusercontent.com/iwanse1977/iwanse1977/main/php%20fpm.jpg" width="800" /></a></div><br /><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><b style="text-align: left;"><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium;">5.&nbsp;</span></b><span style="color: #bf9000; font-family: Roboto Slab; font-size: medium; text-align: left;"><b>Apache24 PHP-FPM With Unix Socket</b></span></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><div class="separator" style="clear: both;">The method above is to connect Apache to the PHP-FPM server using the TCP/IP protocol. In this section we will learn how to connect an Apache server to a PHP-FPM server using a UNIX socket.</div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;">Open the file /usr/local/etc/php-fpm.d/www.conf, and change the script "listen = 192.168.5.2:9000" to "listen = /tmp/php-fpm.sock".</div><div class="separator" style="clear: both;"><br /></div><div class="separator" style="clear: both;"><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><span face="ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace"><span style="font-size: 13.6px;"><span style="color: #1f2328;">listen = 192.168.5.2:9000

</span><span style="color: red;">change with</span><span style="color: #1f2328;">

listen = /tmp/php-fpm.sock</span></span></span></pre></div></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">Then in the file /usr/local/etc/apache24/Includes/php-fpm.conf, type the script below.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><span face="ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace" style="color: #ffa400;"><span style="font-size: 13.6px;">&lt;IfModule proxy_fcgi_module&gt;
   &lt;IfModule dir_module&gt;
       DirectoryIndex index.php
   &lt;/IfModule&gt;

   &lt;FilesMatch "\.(php|phtml|inc)$"&gt;
SetHandler proxy:unix:/tmp/php-fpm.sock|fcgi://localhost/
   &lt;/FilesMatch&gt;
&lt;/IfModule&gt;</span></span>
</pre><div style="text-align: start;"><br /></div></div><div class="separator" style="clear: both; text-align: justify;">Restart the apache server and PHP-FPM server</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><span face="ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace"><span style="font-size: 13.6px;"><span style="color: #1f2328;">root@ns:~ # </span><span style="color: red;">service apache24 restart</span><span style="color: #1f2328;">
</span><span style="color: #ffa400;">Performing sanity check on apache24 configuration:
Syntax OK
Stopping apache24.
Waiting for PIDS: 3258.
Performing sanity check on apache24 configuration:
Syntax OK
Starting apache24.</span></span></span></pre></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><pre class="notranslate" style="background-color: #f6f8fa; border-radius: 6px; box-sizing: border-box; line-height: 1.45; margin-bottom: 0px; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 16px; text-align: start;"><span face="ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace"><span style="font-size: 13.6px;"><span style="color: #1f2328;">root@ns:~ # </span><span style="color: red;">service php-fpm restart</span><span style="color: #1f2328;">
</span><span style="color: #ffa400;">Performing sanity check on php-fpm configuration:
[21-Nov-2023 20:57:00] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Stopping php_fpm.
Waiting for PIDS: 3211.
Performing sanity check on php-fpm configuration:
[21-Nov-2023 20:57:01] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.</span></span></span></pre></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">Pay attention to line 5 where it says Server API FPM/FastCGI, that means the PHP-FPM module is RUNNING or running on the apache24 web server.</div><div><div><br /></div><div>PHP-FPM is an efficient method on how to minimize memory consumption and improve performance for websites with heavy traffic. This is significantly faster than traditional CGI-based methods in multi-user PHP environments. If your main goal is to host web applications and to achieve optimal performance and security, then PHP-FPM is the solution.</div></div></div>

Create a server.php file.
root@ns3:/var/freebsd-swoole-app # touch server.php
root@ns3:/var/freebsd-swoole-app # chmod -R 775 server.php
In the "/var/freebsd-swoole-app/server.php" file, type the script below.

#!/usr/bin/php
<?php
$server = new \Swoole\Http\Server('192.168.5.2', 3000);
$server->on('Request', function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
    $response->write(file_get_contents('index.html'));
    $response->status(200);
    $response->end();
});
$server->start();

Pay attention to the blue script, in this script Swoole runs with
IP = 192.168.5.2 (IP private your freeBSD server)
Ports = 3000

Run the "server.php" file.
root@ns3:/var/freebsd-swoole-app # php server.php
To see the results, you open "Google Chrome" type the command "http://192.168.5.2:3000/".

As additional learning, you create a file called "app.php" and enter the following script in that file.
root@ns3:/var/freebsd-swoole-app # touch app.php
root@ns3:/var/freebsd-swoole-app # chmod 775 app.php
root@ns3:/var/freebsd-swoole-app # ee app.php

<?php
$http = new swoole\Http\Server('192.168.5.2', 3000);
$http->on('Request', function ($request, $response) {
    $response->header('Content-Type', 'text/html; charset=utf-8');
    $response->end('<h1>Hello FreeBSD Swoole' . rand(1, 9999) );
});

$http->start();
?>
Run Swoole "app.php".
root@ns3:/var/freebsd-swoole-app # php app.php
Open "Google Chrome", type "http://192.168.5.2:3000/", look at the results, it will say "Hello FreeBSD Swoole".

Apart from that, you use Apache, Nginx and Node.js to support large-scale websites. You can also use Swoole PHP. Swoole PHP is specifically designed for building large-scale and concurrent systems.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post