Introduction
I am a PHP developer. I use Linux, Apache, MySQL, and PHP (LAMP) to further that goal.
I have published a number of YouTube videos and blog posts that document configuring a LAMP server for development and testing.
<<<===== ADD VIDEO HERE =======>>
I am using an older HP laptop that is running Windows 10. On that, I have installed VirtualBox.
VirtualBox is a type two hypervisor that allows me to create virtual machines.
In this context, Linux is the operating system of the web server. Most PHP websites run on Linux web servers and are configured as a LAMP stack.
Apache serves web pages to users who request them via browsers, handling HTTP requests and delivering the corresponding web content (HTML files, images, etc.) from a server to a client (usually a web browser).
MySQL is an open-source relational database management system (RDBMS) that uses Structured Query Language (SQL) to manage and interact with databases. It is widely used for storing and retrieving data in a variety of applications, from web development to enterprise-level solutions.
PHP (Hypertext Preprocessor) is a popular general-purpose scripting language that is especially suited to web development. It is a server-side language, meaning that it runs on a web server rather than in the user’s browser, allowing developers to create dynamic web pages and other content.
I have created a private network in my home office. This network uses private non-routable IP addresses and private non-routable domain names.
I have configured two virtual hosts on my server:
1) default.internal.
2) lamp.internal.
I have created two users:
1) default.
2) lamp.
The user named default is the owner of the virtual host that is created by Apache when Apache is installed. This virtual host is incomplete. I completed the virtual host configuration so I can use it as a model for the virtual hosts I will build in the future. This virtual host can be accessed by a browser by using the private domain, default.internal.
The user named lamp is the owner of the lamp virtual host and is accessed by a browser using the private domain lamp.internal.
Both users were configured to allow them to use SSH (Secure Shell).
I am configuring my server this way so I can access the virtual host remotely with the ability to create, read, update, and delete files and directories on the remote server.
To complete the goal of remote editing we need to install and configure PHP-FPM. PHP-FPM stands for PHP FastCGI Process Manager.
SPM will enhance the performance of an Apache web server, however I am using it to enable the ability for remote editing.
My daily driver is a 10 year old Dell that is running Kunbuntu (Linux). Native to Kunbuntu is the Linux terminal that allows me to access a remote server. I also have installed Visual Studio Code on this computer which allows for remote editing of PHP code.
Even though VirtualBox is running on my laptop running Windows 10, any virtual server created using VirtualBox is remote to that Windows configuration.
We start by installing and configuring PHP-FPM. This install is only required once.
In the 2nd part, I will configure an Apache virtual host to take advantage of PHP-FPM.
After completing part 1, any time you want to configure an Apache virtual host to take advantage of the PHP-FPM install, you will only need to follow the 2nd part – where I configure an Apache host to use PHP-FPM.
**********************************************************
**********************************************************
*** Install and Activate PHP-FPM ONLY ***
**********************************************************
**********************************************************
To install, activate, start, and configure PHP-FPM on Ubuntu 24.04 LTS without modifying Apache2 configuration or vhost settings, you can follow these steps. This will ensure that PHP-FPM is running and available for use without interfering with Apache’s default settings.
Step 1: Upgrade and Update all of the server packages
– sudo apt update
– sudo apt upgrade
– sudo apt autoremove
– sudo reboot
Step 2: Install PHP-FPM:
– sudo apt install php-fpm
Step 3: Enable the proxy_fcgi and setenvif modules:
– sudo a2enmod proxy_fcgi setenvif
– sudo systemctl restart apache2
Step 4: Enable the configuration file for PHP-FPM (FastCGI Process Manager):
– sudo a2enconf php8.3-fpm
Step 5: Reload apache2:
– sudo systemctl reload apache2
Step 6: Check PHP-FPM Service Status:
– sudo systemctl status php8.3-fpm
Step 7: Enable it to start on boot:
– sudo systemctl enable php8.3-fpm
Step 8: Restart Apache and PHP-FPM for the changes to take effect:
– sudo systemctl restart apache2
– sudo systemctl restart php8.3-fpm
Step 9: Verify PHP-FPM is Working:
– sudo systemctl status php8.3-fpm
– – – – – – – – – – – – – – – – – –
– – – – – END – – – – – – – – –
– – – – – – – – – – – – – – – – – –
*****************************************************************************
*****************************************************************************
***** Configure Apache Virtual Host to Use PHP-FPM *****
*****************************************************************************
*****************************************************************************
Step 1: Define how a PHP file should be handled by the server.
– sudo vi /etc/apache2/sites-available/lamp.internal.conf
– ADD :
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.lamp.sock|fcgi://localhost/"
</FilesMatch>
Step 2: Create the configuration file for the lamp user:
– sudo vi /etc/php/8.3/fpm/pool.d/lamp.conf
Add the following content:
[lamp] ; Define the pool name, which will be referenced by PHP-FPM
user = lamp ; Set the user under which PHP-FPM will run
group = lamp ; Set the group under which PHP-FPM will run
listen = /run/php/php8.3-fpm.lamp.sock ; Define the socket path for the pool (for communication with the web server)
listen.owner = www-data ; Set the owner of the socket file
listen.group = www-data ; Set the group of the socket file
listen.mode = 0660 ; Set the permissions for the socket file (660 = read/write for owner/group)
pm = dynamic ; Enable dynamic process management (adjusts worker count based on demand)
pm.max_children = 50 ; Maximum number of child processes to handle requests
pm.start_servers = 5 ; Number of child processes created on startup
pm.min_spare_servers = 5 ; Minimum number of idle servers to keep alive
pm.max_spare_servers = 10 ; Maximum number of idle servers to keep alive
pm.max_requests = 5000 ; The maximum number of requests a child process should handle before being restarted
php_admin_value[error_log] = /var/log/php8.3-fpm/lamp_error.log ; Set a custom error log for this pool
php_admin_value[log_errors] = on ; Enable error logging
env[PATH] = /usr/local/bin:/usr/bin:/bin ; Define the environment variables (add more if needed)
** The bolded values should be changed to the user for the virtual host you are configuring.
** Remove all spaces from the left of this code or you will receive an error.
Step 3: Set ownership, permissions, and home directory
– sudo chown -R lamp:lamp /var/www/lamp.internal
– sudo find /var/www/lamp.internal -type d -exec chmod 755 {} \;
– sudo find /var/www/lamp.internal -type f -exec chmod 644 {} \;
– sudo usermod -d /var/www/lamp.internal/public_html lamp
Step 4: Enable the configuration for this virtual host:
– sudo a2ensite lamp.internal.conf
Step 5: Restart PHP-FPM and Apache to apply the changes.
– sudo systemctl restart php8.3-fpm
– sudo systemctl restart apache2
Step 6: Test the Apache configuration:
– sudo apache2ctl configtest
Step 7: Ensure that both domains are configured correctly and accessible in your browser:
– Make sure the site is viewable in a browser : http://lamp.internal/info.php
– Look for “Server API” with the value: “FPM/FastCGI”
– Connect via Filezilla.
– Use PuTTy or a Linux Konsole to verify the SSH connection is working.
– Use Visual Studio Code to verify that code can be edited.
– Use Visual Studio to create a PHP script that will create a text file, proving PHP is working correctly under PHP-FPM: Using Visual Studio code create a new file and name it : php-tester.php. Copy and paste the following code into that file and save it. Then run it in your browser : http://lamp.internal/php-tester.php
<?php
// The name of the file to be created
$filename = "test_file.txt";
// Open the file for writing (creates file if it
doesn't exist)
$file = fopen($filename, "w");
// Check if the file is created successfully
if ($file) {
fwrite($file, "This file was created by PHP!");
fclose($file);
echo "File '$filename' was successfully created!";
} else {
echo "Failed to create the file.";
}
?>
– Then use your SSH tool to ssh into the docroot for lamp.internal.
– If all is working as expected you should find a file by the name
of test_file.txt with the contents of “This file was created by PHP!”.
Conclusion
In this article we covered how to install PHP-FPM and configure it to work with an Apache virtual host.
PHP-FPM serves two purposes: 1) is it a performance enhancer, 2) it provides a way for us to remote edit our PHP code directly on the server.
Installing and configuring PHP-FPM is only required once. That would be the first part which consists of 9 steps.
The second set of 7 steps has to be completed every time you want to configure a new virtual host.