When setting up a web server with Nginx to host PHP applications, it’s essential to configure PHP to work seamlessly with Nginx. In this guide, we’ll walk through the steps to configure PHP for an Nginx server, ensuring that PHP scripts are processed correctly.
Prerequisites
Before proceeding, make sure you have:
- Nginx is installed on your server.
- PHP is installed on your server.
- Nginx server blocks (virtual hosts) are configured for your websites.
Step 1: Install PHP-FPM
PHP-FPM (FastCGI Process Manager) is a FastCGI implementation for PHP. It is essential for processing PHP scripts with Nginx.
sudo apt-get install php-fpm
This command installs PHP-FPM on a Debian-based system. Adjust the command according to your Linux distribution.
Step 2: Configure PHP-FPM
Edit the PHP-FPM configuration file to adjust settings:
sudo nano /etc/php/7.4/fpm/php.ini
You can adjust PHP settings based on your application requirements. For example;
Adjust Memory Limit
Increase the memory limit if your applications require more memory. Look for the memory_limit
directive
memory_limit = 256M
Adjust the Maximum Upload Size
If your application involves file uploads, adjust the maximum upload file size:
upload_max_filesize = 20M
post_max_size = 25M
Set the Timezone (Optional)
Set the default timezone
date.timezone = "Your/Timezone"
Replace "Your/Timezone"
with your timezone (e.g., “America/New_York”).
Configure Error Reporting
For a production environment, it’s recommended to log errors and not display them to users:
display_errors = Off
log_errors = On
Enable OPCache
Enable OPCache for performance improvement:
[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.save_comments = 1
Now, the entire php.ini file with the settings above would look as follows:
; php.ini
; Memory Limit
memory_limit = 256M
; Maximum Upload Size
upload_max_filesize = 20M
post_max_size = 25M
; Timezone
date.timezone = "Your/Timezone"
; Error Reporting
display_errors = Off
log_errors = On
; OPCache Settings
[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.save_comments = 1
Restart the PHP-FPM service to apply the changes:
sudo systemctl restart php7.4-fpm
Step 3: Configure Nginx to Use PHP
Edit the Nginx server block configuration file for your website:
sudo nano /etc/nginx/sites-available/mywebsite
Add or modify the location ~ \.php$
block to include the following:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
This configuration tells Nginx to pass PHP requests to the PHP-FPM process for processing.
Step 4: Test and Reload Nginx
Test the Nginx configuration for syntax errors:
sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Conclusion
Configuring PHP for an Nginx server involves installing PHP-FPM, adjusting PHP settings, and configuring Nginx to work with PHP scripts. Following these steps ensures that PHP-powered websites work seamlessly on your Nginx server, providing a reliable and performant web hosting environment. Adjust the version numbers in commands based on your PHP version.