Introduction
Setting up PHP on a freshly installed Virtualmin default website involves several steps. This guide will walk you through the process from start to finish, ensuring your default website can efficiently run PHP scripts using PHP-FPM.
Prerequisites
- A fresh Virtualmin installation.
- Root access to your server.
Getting the Default Domain Ready
The next steps involve getting the default domain ready. Virtualmin creates a numbered file for each domain that is handled on the server. You need to know which “number” corresponds to your domain name on the server. Here is a way to figure that out:
Do a directory listing of the domains directory:
ls /etc/webmin/virtual-server/domains
This will show you a list. If this is a fresh Virtualmin installation, there will only be one number in here. If you already have more than one listing in here, you will have to use nano and view the files to see which belongs to your domain. In the alternative, you can view the Apache virtual host configuration file to determine which number belongs to your domain as it is listed within that file as well.
Now do:
nano /etc/webmin/virtual-server/domains/[the number here]
This will display a very long configuration file. In mine, I changed three entries:
php_mode=fpm (this one was originally "none") emailto=support@charlesworks.com (this was originally a server name that no email existed for) emailto_addr=support@charlesworks.com (this was also originally a server name that no email existed for)
Now CTRL O to save and CTRL X to exit.
Enable the site:
a2ensite [site name].conf
Steps to Activate PHP
1. Verify PHP-FPM Installation
First, check if PHP-FPM is installed. Use the following command:
dpkg -l | grep php8.2-fpm
If PHP-FPM is not installed, install it:
apt-get install php8.2-fpm
2. Ensure PHP-FPM Service is Running
Check the status of the PHP-FPM service:
systemctl status php8.2-fpm
If it’s not running, start it:
systemctl start php8.2-fpm
Enable PHP-FPM to start on boot:
systemctl php8.2-fpm
3. Check the PHP-FPM Socket
Verify the PHP-FPM socket path in the configuration file:
cat /etc/php/8.2/fpm/pool.d/www.conf | grep listen
You should see something like this:
listen = /run/php/php8.2-fpm.sock
4. Edit Apache Virtual Host Configuration
Navigate to the Apache sites-available directory and open your site’s configuration file:
cd /etc/apache2/sites-available nano [site domain].conf
Update the configuration to include the necessary settings for PHP-FPM:
<VirtualHost [ip address]:80> ServerName [server name] DocumentRoot /home/._default_hostname/public_html ErrorLog /var/log/virtualmin/[server name]_error_log CustomLog /var/log/virtualmin/[server name]_access_log combined DirectoryIndex index.php index.htm index.html<Directory /home/._default_hostname/public_html> Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch Require all granted AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch </Directory> <Directory /home/._default_hostname/cgi-bin> Require all granted AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/" </FilesMatch> </VirtualHost> <VirtualHost [ip address]:443> ServerName [server name] DocumentRoot /home/._default_hostname/public_html ErrorLog /var/log/virtualmin/[server name]_error_log CustomLog /var/log/virtualmin/[server name]_access_log combined DirectoryIndex index.php index.htm index.html <Directory /home/._default_hostname/public_html> Options -Indexes +IncludesNOEXEC +SymLinksIfOwnerMatch Require all granted AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch </Directory> <Directory /home/._default_hostname/cgi-bin> Require all granted AllowOverride All Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch </Directory> <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost/" </FilesMatch> SSLEngine on SSLCertificateFile /etc/ssl/virtualmin/[site number]/ssl.cert SSLCertificateKeyFile /etc/ssl/virtualmin/[site number]/ssl.key SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 SSLCACertificateFile /etc/ssl/virtualmin/[site number]/ssl.ca </VirtualHost>
5. Enable Necessary Apache Modules
Ensure the required Apache modules are enabled:
a2enmod proxy_fcgi setenvif a2enconf php8.2-fpm
6. Restart Apache
After making these changes, restart Apache to apply the configuration:
systemctl restart apache2
To activate the new configuration, you need to run:
systemctl reload apache2
7. Verify the PHP Configuration
Create a PHP info file to verify PHP is working:
echo "<?php phpinfo(); ?>" > /home/._default_hostname/public_html/info.php
Troubleshooting
- Apache Error Logs: Check the Apache error logs for any configuration issues:
tail -f /var/log/apache2/error.log
- PHP-FPM Logs: Check the PHP-FPM logs for any errors or issues:
tail -f /var/log/php8.2-fpm.log
Conclusion
By following these steps, you can configure PHP-FPM on a fresh Virtualmin installation’s default website, ensuring efficient PHP script execution.
For more info on Virtualmin, please visit:
https://virtualmin.com