How to install PHP extensions on nginx?

How to install PHP extensions on nginx?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Misconception of PHP Extensions on Nginx

It is a common stumbling block for developers migrating from Apache to Nginx: the belief that enabling a PHP extension requires modifying the web server configuration. If you are installing Drupal and seeing gd listed as disabled despite having php5-gd installed via apt-get, the issue almost certainly lies within your PHP runtime, not your web server.

Nginx is a reverse proxy; it handles the HTTP request but hands off execution to a separate process manager, typically PHP-FPM or an integrated interpreter like php-embedded. When you install a package like php5-gd on a Debian-based system, the extension binary is compiled into the PHP core, but it is not automatically loaded. The default configuration for Nginx projects usually relies on the standard php.ini file located in /etc/php/.

To resolve the "Disabled" status, you must explicitly load the extension. Open your main PHP configuration file using a text editor:

sudo nano /etc/php/5.x/fpm/php.ini
# OR for CLI and CGI setups
sudo nano /etc/php/5.x/cli/php.ini

Search for the line corresponding to the gd extension. You will likely find it commented out with a semicolon at the beginning:

;extension=gd

Remove the semicolon to uncomment the line. Save the file and restart your PHP-FPM service to apply changes without affecting Nginx directly:

sudo systemctl restart php5-fpm
# Or on older systems
sudo service php5-fpm restart

Once restarted, create a new phpinfo() file in your web root to verify the fix. If the GD section appears, the extension is active. However, simply having the binary installed isn't enough for frameworks like Drupal; you must also ensure the extension is enabled in the SAPI (Server API) context.

The Missing Link: php.ini vs. fpm.d

On modern Linux distributions, particularly those using PHP 7 and higher, the configuration hierarchy has shifted. If you are running a newer version of PHP, the standard /etc/php/7.x/fpm/php.ini might be read-only or minimal, relying instead on a drop-in configuration directory at /etc/php/7.x/fpm/conf.d/.

In this architecture, extensions are managed via individual .ini files. Look for a file named 20-gd.ini in the conf.d directory. If it is missing, you can create one manually to ensure the extension loads regardless of the main php.ini state:

sudo nano /etc/php/7.x/fpm/conf.d/20-gd.ini

Add the following line:

extension=gd.so

Restart PHP-FPM again. This method is crucial because web servers like Nginx often spawn multiple worker processes. If you only edit the main php.ini, some workers might pick up an older configuration if they were forked before your changes took effect, though a restart usually mitigates this.

Understanding the Trade-offs

Why does Nginx feel different from Apache here? With Apache, you often use .htaccess to tweak configurations per directory, but PHP extensions generally cannot be controlled via .htaccess. They are loaded at the process level by the PHP binary itself.

The primary pitfall here is assuming that apt-get install php-extension-name is a complete operation. On many distributions, this command installs the compiled module but leaves it disabled by default for security and performance reasons (loading unused extensions increases memory footprint).

Another potential issue arises if your application expects specific GD functions like imagecreatefromjpeg. Even if the extension loads, older PHP versions might lack these functions if they were not compiled with support. Verify this by checking the output of phpinfo() for the "Supported image file formats" section. If it says "none," the library itself wasn't compiled in, and you may need to recompile PHP from source or switch packages.

If you are managing multiple sites or using containerized environments (like Docker), ensure that your build context includes these extensions in the Dockerfile. A line like RUN apt-get install -y php-gd followed by COPY --from=... /etc/php/.../conf.d/20-gd.ini . ensures consistency across your deployment pipeline.

For Laravel projects or other modern frameworks, this same logic applies. Whether you are using PHP-FPM with Nginx or the built-in server for development, the mechanism to enable extensions remains consistent: configure the loader in the php.ini or conf.d hierarchy and restart the process manager. The web server itself is merely the front door; it doesn't hold the keys to the PHP runtime's internal modules.

Our network