PHP Configuration Changes with php.ini and phpinfo()

PHP-configuration-changes

If you do enough WordPress development, you’ll eventually find yourself in need of some PHP environment changes. These’ll look slightly different depending on circumstance, but you’ll have an issue that requires you change the way PHP behaves. Maybe you need to extend the execution time for long-running script that you really must run. Maybe you need to increase the upload limit because a client insists they let their team upload 500 megabyte PDFs.

Whatever it is, you’ll need to have some familiarity with the php.ini file to make the changes. And while it’s a pretty straight-forward file, there are some useful tips you should know about it. If you’re making those changes, another great tool to have in you back pocket is the phpinfo() function. While you probably shouldn’t expose the output of this command on a production server, access to it is a hugely valuable when doing a lot of little things. One of the most common: finding the php.ini file you need to change.

phpinfo(): Get Info About PHP

phpinfo() is really easy to use. What do you do? You create a PHP file on your server — I frequently call it info.php but it doesn’t matter the name — and you put that function call into it. That’s all. The net result is a file with about these contents:

<?php
// info.php
phpinfo();

Then you visit that page in your web browser. That’s it.

A LOT of Information

If there’s a problem beginners face with phpinfo it’s not that it’s hard to get working. It’s that it’s an incredibly large amount of information, much of which will make little or no sense. But all that information is exactly what makes it hugely valuable to someone with the stomach to not get intimidated and some facility with their browser’s ability to search a page.

Screen Shot 2016-05-02 at 3.32.33 PM

The above screenshot shows the top box from a phpinfo() function call. As I said, there’s a lot of information there, which will make a varying amount of sense to you depending on the level at which you understand computers and PHP. Most of it is, if I’m honest, noise to me. I see that it’s using FastCGI, but that doesn’t have much meaningful impact for most WordPress developers. However, the fifth line in matters a lot: it’s tells you which configuration file PHP loaded, in our case it’s the one at /etc/php5/fpm/php.ini. (The line is labelled: “Loaded Configuration File.”) Why is that useful? Because it tells us that if we want to change the way the server which rendered this page is working, we’ll want to make the changes to that file.

PHP Configuration: What and Why of php.ini

Like most software including WordPress, PHP provides some options. It also has extention or “plugins” which themselves have options. Most of those will live in “configuration files” which end in .ini. Wikipedia tells us that the prefix came from a shortened version of “initialization”. In any case, that convention pervades PHP.

These files are all pretty similar. They’ll have a number of values set in lines that are going to look pretty familiar to any programmer, like:

implicit_flush = Off
serialize_precision = 17
memory_limit = 256M

What sets a good INI file apart from a bad one is how useful and clear the comments are. A php.ini rolled out with default values is pretty solid in that regard. Rather than looking quite like the above, you’ll see lots of comments as preamble before a value is set. So rather than simply declaring the memory_limit as it did above, you’ll see explanations before values, like:

; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 256M

As you may have guessed, in a php.ini anything after a semicolon (;) isn’t parsed, and is simply treated as a comment. You’ll also see declarations, similar to the memory_limit line, commented out. When a value isn’t provided, either because it is deleted or commented out, PHP uses a default value.

PHP Config Values That Are Useful For WordPress Devs

There are a relatively small number of values inside of the php.ini file that you’ll ever need to edit as a WordPress developer (or even as an average non-WordPress PHP developer).

The ones I know that I’ve changed many times before are max_execution_time, memory_limit, upload_max_filesize, and post_max_size. There are literally hundreds of value that can and are frequently settable in PHP’s INI file and the various extensions it runs with. But only these four come up somewhat regularly as issues I have in my day-to-day work. A quick summary of each of the four:

  • max_execution_time — As you might expect, this controls how long your PHP script is allowed to run before being unceremoniously stopped cold. By default for webpages it’s something like 30 seconds, so a single bad script doesn’t lock up the whole server. On my servers when I know everything that’s going on and I know the public can only make well-formed small requests, I’ll raise this for a script that takes a bit more than thirty seconds. Just keep in mind that you are increasing the pain (on server resources) of an accidentally or maliciously long-running script.
  • memory_limit — Memory limiting and execution time limiting have a lot in common. You’re allowing a process to consume more of the server’s memory, which is a bad choice if one of your “citizen” requests starts trying to use lots more resources than it should. But it’ll help, especially when you use a full-page caching plugin like WP Super Cache, to increase this a little.
  • upload_max_filesize and post_max_size have so much in common that I’ve rolled them together. Basically, how big a file can someone upload through a web-form? If it’s not a lot, the defaults work. But if you’re looking at taking files of hundreds of megabytes you’re virtually guaranteed to have to fiddle with these values. Like we talked about with the others, this is possibly wasteful of server resources, but it’s good for a well-behaving client who needs to upload giant CSVs for you to import. The difference between them? post_max_size affects the total acceptable size for a POST HTTP request, where upload_max_filesize is the maximum size of any single file PHP should accept uploaded. Because I so frequently need both of these changed to allow a single large file submitted in a single POST request, I always change them together and to the same value.

Final Notes on Editors, File Permissions, Server Restarts

Because php.ini is a configuration file, and because it’s often on a server, you may hit a couple issues: you’ll not have easy access to the file via your usual comfortable tools, when you’ve edited the file you may not be able to save your changes, or the changes you made may seem to have no effect.

Screen Shot 2016-05-03 at 10.35.57 AM

Opened without sudo lead to warning of no write permission and the inability to save your changes… 🙁

For editing, when you’re new to the terminal and SSHing into a box for the first time: nano is the best text editor. All of its keyboard shortcuts are clearly spelled out on the screen. (^ is the CTRL key…) It’s still really the only command-line text editor I use, because I rarely need one and it’s got just enough features to keep me satisfied. So I often edit my php.ini on a remote server with a command like:

sudo nano /etc/php5/fpm/php.ini

This isn’t a thorough-going lesson on the command line — though I should probably write one of those sometime… — but I’m using nano to open the file at the path /etc/php5/fpm/php.ini.

What’s that sudo thing? Well on a well-configured server (at least from a security perspective) a common users shouldn’t be able to change complicated configuration files. sudo is the way you temporarily request “administrator” or “super user” access-levels while a normal user. (sudo is short for “super user do.”) You’ll get prompted for the password when you do that, but then you’ll be able to save your php.ini from nano. Without that, you’ll probably be unable to change the file. And that doesn’t work!

Finally, you *may* need to restart your server or the FastCGI daemon. Whether or not that’s absolutely required depends on some of the subtleties of how exactly our server is configured. If you’re making changes and it’s not having an effect — you’ll know with your phpinfo page — and you’re on a server where you have the power to restart (not shared hosting), you may need to. The specific command for that vary a fair amount on server configuration and operating system, however, so I’ll leave you to Google for them.

Configuration Buddies: php.ini and phpinfo()

There are many many other values in a php.ini file. You can see almost all of them by running the phpinfo() command. But mostly I find that I just change a very small set of them and choose to let the wise builders of PHP and other intermediary server configuration experts elsewhere make good defaults for me. But I love knowing how they work and the basics of how to work with them. Hopefully it was helpful to you. Happy hacking!

Image credit: NASA via Unspalsh