次の方法で共有


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

  1. 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.
    1. Go to Debug Console -> cmd

    2. Click on the globe icon, then navigate to config folder, PHP version you are using

    3. Click the edit button and you get an editor that has all the content of the php.ini used for your site.

       Note: these config files files are read only.

  2. 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.

In the portal or the preview portal, you can do all of these steps but for the purpose of that post I will use only the preview portal.

  1. Go to your website -> All settings, you will find a section called App settings.

     

  2. 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.

  3. 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!

 Now restart the site and refresh the index.php and here you go, the empty ini shows up.

 
Now let’s add another ini file that really does something, I will add a file  NewConfig.ini and the content will be

 mysql.max_links=5

and again use Kudu magic to just drop it in D:\home\site. Here is the output of phpinfo before and after

Before:

After adding the file and restarting the site:

Then I added this to my new ini file

 upload_tmp_dir=D:\home\site\uploads

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

 zend_extension=D:\home\site\php-ext\php_xdebug-2.3.1-5.4-vc9-nts.dll

or

 extension=D:\home\site\php-ext\php_gd2.dll

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:
https://microsoftazurewebsitescheatsheet.info/