To add PHP support to Apache2 (without using PHP-FPM, you need to install the Apache2 PHP module, commonly known as libapache2-mod-php. This module allows Apache to directly handle PHP requests.
To install PHP and the Apache PHP module (libapache2-mod-php), run the following command:
sudo apt update
sudo apt install php libapache2-mod-php
This will install both PHP and the module that enables Apache to process PHP files.
After installation, verify that PHP is installed correctly by checking the version:
php -v
You should see output indicating the PHP version, for example:
PHP 8.1.2 (cli) (built: Jan 20 2022 09:30:25) ( NTS )
By default, Apache2 will automatically load the libapache2-mod-php module after installation. You can verify that the PHP module is enabled by checking the Apache configuration.
You can check which modules are loaded in Apache2:
apache2ctl -M | grep php
You should see php_module in the list of loaded modules.
After the installation, restart Apache2 to ensure that PHP is correctly loaded:
sudo systemctl restart apache2
To ensure that Apache is correctly serving PHP files, you can create a simple PHP file in the web directory.
Create a file called info.php in the default web directory (/var/www/html/):
sudo nano /var/www/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
?>
Save the file and exit.
Open a web browser and navigate to the IP address or domain of your server followed by /info.php:
http://your-server-ip/info.php
If PHP is installed correctly, you should see a page with detailed information about your PHP installation, including the PHP version, loaded modules, and configuration settings.
Depending on your web application’s needs, you may need additional PHP modules, such as php-mysql for MySQL database interaction or php-gd for image manipulation.
You can install additional PHP modules as follows:
sudo apt install php-mysql php-gd php-xml
After installing any new PHP modules, restart Apache2:
sudo systemctl restart apache2
Once PHP is working, you might want to delete the info.php file, as it reveals sensitive information about your server’s configuration. You can remove it with the following command:
sudo rm /var/www/html/info.php
If you encounter issues with PHP not working as expected, here are some common troubleshooting steps:
Check Apache Error Logs: Review the Apache error logs for any PHP-related errors.
sudo tail -f /var/log/apache2/error.log
Check PHP Configuration: Ensure that the PHP configuration file (php.ini) is correctly set up. You can find the location of php.ini by running:
php --ini
Verify Module Loading: Ensure that the libapache2-mod-php module is loaded in Apache. You can check this with:
apache2ctl -M | grep php
Restart Services: Sometimes, simply restarting Apache can resolve issues.
sudo systemctl restart apache2
To optimize PHP for better performance, consider the following adjustments in your php.ini file:
Increase Memory Limit: Increase the memory limit to allow PHP scripts to use more memory.
memory_limit = 256M
Enable OPcache: OPcache improves PHP performance by storing precompiled script bytecode in memory.
opcache.enable = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
Adjust Max Execution Time: Increase the maximum execution time for PHP scripts.
max_execution_time = 60
Optimize Error Logging: Disable error display on production servers and log errors instead.
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
After making these changes, restart Apache to apply the new settings:
sudo systemctl restart apache2
You’ve now installed PHP with Apache2 without using PHP-FPM. Apache is using the libapache2-mod-php module to handle PHP files directly. This is ideal for smaller or less complex setups where performance is not as critical as with PHP-FPM (which is used for more advanced setups and higher performance needs).