Azure WebSites: how to change PHP ini settings.
Azure Websites aims at creating a web hosting platform that balances between simplicity and flexibility. Especially for PHP development this is challenging, as the technology includes lots of configurations that enables different scenarios. In this post, I will go through the process of overwriting and/or adding PHP.ini configurations.
In Azure web sites, there are many ways you can change PHP default configurations. You can enable custom extensions, you can use a .user.ini to change the non system-level-only directives or you can even bring in your own runtime bits of PHP. For more details read this article.
This post is to add another way to customize your PHP.ini configuration. To understand how it works there are 2 concepts we need to go through
- Azure Websites is a shared hosting environment, where multiple sites can exist in the same host machine. When a site comes up, a dedicated PHP.ini file gets created. This file is used only by that site. If you want to go through that file to see the default values you can do so through your scm site. Go to <your site name>.scm.azurewebsites.net, sign in with your Microsoft account, then you get to Kudu interface (Kudu is an awesome tool that comes with Azure Websites, if you do not know about it, here is a brief intro in this post). Using Kudu you can navigate to the location where your PHP.ini exists.
- In PHP you can specify a location, and PHP runtime will look at all .ini files in that directory and append it to the configurations in the main PHP.ini. I really could not find any documentation whatsoever to that feature, but it exists :)
Combining this knowledge with the knowledge on how to add appSettings to the Azure Websites, we can make advantage of that PHP hidden treasure and here is how.
Add this app setting PHP_INI_SCAN_DIR with a value that is a directory in your site, in this case I decided to put it in D:\home\site which is a directory that is not in the serve-able wwwroot folder, but you still can publish to it either using FTP or by using the awesome magic of Kudu. Do not forget to save the changes.
publish a simple site with an index.php that has this content
<?php phpinfo(); ?>
This will print out PHPInfo page
You can see that now it shows the “scan this dir for additional .ini files” is pointing to D:\home\site.
Now you can put any .ini files in that location and they will be simply appended to the PHP ini configurations. Let’s play with that a little
I will first try to drop an empty .ini file make sure it was loaded by PHP runtime. I do this by opening Kudu and then drag and drop the file in the folder I want to publish to, cool huh!
and again use Kudu magic to just drop it in D:\home\site. Here is the output of phpinfo before and after
After adding the file and restarting the site:
Then I added this to my new ini file
restarted the site, then I see the change reflected in phpinfo page.
Using this method you can even enable new extensions by adding an ini file that has the content
Make sure you upload the extensions in the right directory and that would effectively enable your custom extensions.
In conclusion, hosting a PHP site on Azure Websites platform is easy and reliable. In addition, you can get higher level of control on your PHP site by setting the PHP configurations you like.
Related resources that I found useful: