Blog Post

Apps on Azure Blog
2 MIN READ

Configure Nginx for PHP 8 Linux Azure App Service

Hanli_Ren's avatar
Hanli_Ren
Icon for Microsoft rankMicrosoft
Jan 25, 2022

Azure App Service for Linux platform now supports customer using both PHP 7 and PHP 8 built-in docker image. 

When switching from PHP 7 to PHP 8, you may recognize the platform changed the web container from using Apache to Nginx. If you previously defined custom rules in .htaccess will be lost and will highly possible break your app.

 

But Nginx does not have an equivalent to the .htaccess file, you need to update the main configuration and reload Nginx for any changes to take effect. NGINX is designed to be efficient. While using .htaccess can do harm to performance. For more details, please refer to https://www.nginx.com/resources/wiki/start/topics/examples/likeapache-htaccess/.

 

This article shows how to deploy a customized Nginx configuration in PHP 8 Linux App Service.

 

Inside App Service built-in PHP 8 docker image, the Nginx configuration is stored in /etc/nginx/sites-enabled/default file.

The following steps can be used to create your own customized Nginx configuration based on the default one.

 

1. Firstly, we need to copy /etc/nginx/sites-enabled/default to the /home folder

By default, App Service set WEBSITES_ENABLE_APP_SERVICE_STORAGE = true.  Files stored in /home path are persisted in an Azure Storage file share, which can survive restart and shared across scale instances. So we need to save your own Nginx configure file under /home path.

Go to App Service WEBSSH via https://<AppServiceName>.scm.azurewebsites.net/webssh/host

cp /etc/nginx/sites-enabled/default /home/default

 

2. Make Nginx configuration changes to the /home/default file.

vi /home/default

For example, we can turn off absolute_redirect, change root path, add rewrite rules, etc.

 

3. Use custom startup script to overwrite original Nginx config file. So the platform can use your configuration to start the Nginx server every time the App Service being started.

Go to “Configuration” --> “General settings” in the App service Portal.
Add the following command in the “Startup Command

cp /home/default /etc/nginx/sites-enabled/default; service nginx restart

When you change the Startup Command and save the setting, the App Service will be Restarted and begin to use your customized configuration to start the Nginx server.

 

More Reference Documents:

 

 

Updated Jan 23, 2022
Version 1.0
  • OnyXerozz's avatar
    OnyXerozz
    Copper Contributor

    I am new to Wordpress on Azure and Nginx configuration.

    Php = 8.3

    Wordpress = 6.5.5
    Can anyone help with the config file?


    I did manage to be able to make changes and run this at startup like mentioned above in this article, but perlinks dont work. Perhaps an example, the main issue I have is Permalinks not working. It doesnt work with structure set to  /%postname%/ I get the 404 error.

    If I change to simple structure this works, but links look awfull with <domain>/?p=123 for example.

     

  • Andre_Ferreira's avatar
    Andre_Ferreira
    Copper Contributor

    Todd_Core_BTS thank you for your advice. Just one correction, you miss "cp" after the first "&&" :

     

    cp /home/site/wwwroot/default /etc/nginx/sites-available/default && cp /home/site/wwwroot/default /etc/nginx/sites-enabled/default && service nginx reload

     

  • Todd_Core_BTS's avatar
    Todd_Core_BTS
    Copper Contributor

    Don_Wise In trying to install a Laravel application in an Azure web app, I found that there is an inconsistency in the path to the NGINX 'default' file on Azure Linux servers.  Heck, the path changed on me overnight after my app was working and broke it.

    For some servers, the path is:

    /etc/nginx/sites-available/default


    For other servers, the path is:

     

    /etc/nginx/sites-enabled/default

     
    To deal with either, I'm using the following Startup Command that copies my custom startup file into both directories:

     

    cp /home/site/wwwroot/default /etc/nginx/sites-available/default && /home/site/wwwroot/default /etc/nginx/sites-enabled/default && service nginx reload

     

  • thinkdj's avatar
    thinkdj
    Copper Contributor

    Don_Wise Schakko has an article where he has packed the commands into a .sh file and calls the script on each restart of the container. Quite extensible.

     

    https://www.schakko.de/2021/09/08/deploying-php-8-0-applications-with-azure-app-service/

     

  • Don_Wise's avatar
    Don_Wise
    Copper Contributor

    It seems this article is stale by June of 2022. I recently create an Azure Linux VM with the following tech article: https://docs.microsoft.com/en-us/azure/app-service/quickstart-wordpress. However, this article doesn't seem to align despite it being an Azure Linux VM. Is it an inconsistency?
    Specifically, in that VM environment, /etc/nginx/sites-enabled/default does not exist.
    I have been working around this by just using "/etc/nginx/conf.d/default.conf". as the default which seems to suit the same purpose. Oddly, I see the startup command as portrayed in this article does not get called consistently in that environment. I often need to SSH in and run it manually. Still trying to figure out how I can get that to work. Just a touch frustrating this article is out to address this issue but yet I am spending time trying to resolve it due to VM inconsistencies.