KahWee - Web Development, AI Tools & Tech Trends

Expert takes on AI tools like Claude and Sora, modern web development with React and Vite, and tech trends. By KahWee.

Upgrading WordPress to PHP 7: Configuration Changes

I upgraded my Ubuntu server to 16.04 and WordPress stopped working. The issue: Ubuntu 16.04 ships with PHP 7, and my nginx configuration was still pointing to the old PHP 5 socket path.

Ubuntu 16.04 replaced PHP 5 with PHP 7 as the default. The php-fpm socket moved from /run/php/php5-fpm.sock to /run/php/php7.0-fpm.sock. If your nginx config hardcodes the old path, requests to PHP files just hang or return errors. WordPress won't load, the admin panel breaks, and you get gateway timeout errors.

First, install the PHP 7 packages WordPress needs:

sudo apt install php-fpm php-mysql php7.0-xml

The php-fpm package provides the FastCGI process manager, php-mysql adds MySQL database support, and php7.0-xml handles XML parsing for WordPress's RSS feeds and XML-RPC.

PHP has a setting called cgi.fix_pathinfo that's enabled by default. It's a security risk—it lets attackers execute arbitrary PHP code by crafting specific URLs. Open /etc/php/7.0/fpm/php.ini, find ;cgi.fix_pathinfo=1, and change it to cgi.fix_pathinfo=0. This prevents PHP from trying to "fix" file paths, which blocks a common attack vector. Restart PHP-FPM with sudo systemctl restart php7.0-fpm.

nginx configuration

The main config change is pointing nginx to the new PHP 7 socket. Edit /etc/nginx/nginx.conf and add this upstream block inside the http section:

upstream php {
  server unix:/run/php/php7.0-fpm.sock;
}

This creates a named upstream called php that points to the PHP 7 socket. Now your site configs can reference php instead of hardcoding the socket path.

Here's a reusable WordPress config that works with the upstream we just defined. Save this as global/wordpress.conf:

# WordPress single blog rules.
# Designed to be included in any server {} block.

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
  access_log off;
  log_not_found off;
  expires max;
}

# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ [^/]\.php(/|$) {
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }
  include snippets/fastcgi-php.conf;
  fastcgi_pass php;
}

The first rewrite ensures /wp-admin URLs get the trailing slash (WordPress requires this). The second location block sets aggressive caching on static assets and stops logging 404s for missing images. The third location block handles PHP files, checking if they exist before passing to php-fpm, and using the php upstream we defined earlier.

Include this config in your WordPress site's server block with include global/wordpress.conf;, then test with sudo nginx -t and reload with sudo systemctl reload nginx. Your WordPress site should now work with PHP 7.

Beyond fixing the immediate problem, PHP 7 was a huge upgrade in 2016. It ran roughly twice as fast as PHP 5.6 and used less memory. For WordPress sites handling significant traffic, the upgrade meant lower server costs and faster page loads. Ubuntu 16.04 forcing the upgrade was painful for unprepared servers, but the performance gains made it worth the configuration hassle.

Years later, I moved away from WordPress entirely. Building a static blog turned out to be faster and simpler than managing PHP, databases, and server configurations.