How to send emails through Azure Web Apps using Google / Gmail

How to send emails through Azure Web Apps using Google / GmailPart I – Configuring Google to use App PasswordsTwo quick notes before proceeding: This is not a ‘once and done’ secret or app generation concept, this is unique to the gmail address and will need to be done for each and every email address that you wish to send emails from. Next, thi...

Continue Reading →

Building Python Functions App that use Native Modules – Custom Docker

Prerequisites

To build and test locally, you will need to:

To install Azure Functions Core tools you will need also these requirements:

  1. Install .NET Core 2.1 for Windows.
  2. Install Node.js, which includes npm. For version 2.x of the tools, only Node.js 8.5 and later versions are supported.
  3. Install the Core Tools package: npm install -g azure-functions-core-tools

Create and activate a virtual environment

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named env. # In Bash python3.6 -m venv env source env/bin/activate # In PowerShell py -3.6 -m venv env env\scripts\activate # In CMD python -m venv env env\scripts\activate

Create a local Functions Project

To create a Functions project, it is required that you work in a Python 3.6 virtual environment. Run the following commands to create and activate a virtual environment named env. In the terminal window or from a command prompt, run the following command:

func init MyFunctionProj –worker-runtime python –docker func init . –worker-runtime python –docker

You will see something like the following output.

Installing wheel package Installing azure-functions==1.0.0a5 package Installing azure-functions-worker==1.0.0a6 package Running pip freeze Writing .funcignore Writing .gitignore Writing host.json Writing local.settings.json Writing D:\__Documents\WebAppProjects\functions-python-3\.vscode\extensions.json Writing Dockerfile

This generates a Dockerfile with the following content:

FROM mcr.microsoft.com/azure-functions/python:2.0

COPY . /home/site/wwwroot

RUN cd /home/site/wwwroot && \

pip install -r requirements.txt

Create a function

https://github.com/Azure/azure-functions-python-worker/wiki/Create-your-first-Python-function#create-a-function

Run the function locally

https://github.com/Azure/azure-functions-python-worker/wiki/Create-your-first-Python-function#run-the-function-locally

Run locally as Docker container

Using Docker, run the following command. Replace and with preferred names.

docker build . –tag : (example) docker build . – pythonfunction:v1

Once image has been build you could run the following command to start the image:

docker run -p : : (example) docker run -p 8080:80 pythonfunction:v1

Once started, you could browse to http://localhost:8080 (using example) to view your function running.

Deploy your function app

You could use Docker Hub or Azure Container Registry to deploy your Docker image.

Push image to Docker Hub

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#push-image-to-acr

First Create a container registry

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#create-a-container-registry

Log in to ACR

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#create-a-container-registry

Push image to ACR

https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal#push-image-to-acr

Create function app via Azure Portal

Select the New button found on the upper left-hand corner of the Azure portal, then select Compute > Function App. When configuring the settings, select Linux (Preview) for OS and Docker Image for Publish. Under Configure Container, select either Azure container Registry or Docker Hub depending on how you deployed your Docker image. Once deployed, you will be able to access the Function Apps page Once deployed, you can test your Function App: Be sure to change and with your functions values. https://.azurewebsites.com/api/

Restrict access to login for the WordPress running on Azure web app container

For WordPress sites that running on Apache server in Azure web app for containers, here is sample code to restrict access to login pages, such as wp-login.php or wp-admin

1. FTP to files in /home/site/wwwroot, find the file “.htaccess” (create one if it does not exist)

2. Add the code below to “.htaccess”, replace the IP “xx.xx.xx.xx” by that you allow to access wp-login.php

<Files wp-login.php>
Order Deny,Allow
Deny from all
SetENVIf X-Client-IP "xx.xx.xx.xx" AllowAccess
Allow from env=AllowAccess
</Files>

Note: Since /wp-admin will direct to wp-login.php, no need to define the rule for wp-admin

RewriteRule sample for WordPress in subdirectory

In the case you installed another instance of WordPress in a subdirectory of main WordPress site in Azure web app for Windows, your WordPress installation may be like this,

if use “Post name” Permalink setting, with default web.config, articles in subdirectory might give 404 (not found), for example,

For this problem, you can modify web.config to exclude subdirectory “blogs” from main site rewrite rules, here is sample web.config,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
  <rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^index.php$" ignoreCase="false" />
      <action type="None" />
    </rule>
    <rule name="Imported Rule 2" stopProcessing="true">
      <match url="." ignoreCase="false" />
      <conditions>
        <!--# Include in the next line all folders to exclude-->
        <add input="{URL}" pattern="(blogs)" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.php" />
    </rule>
  </rules>
  </rewrite>
</system.webServer>
</configuration>

If the WordPress sites are hosted on Apache on Azure web app for containers, modify wwwroot/.htaccess, for example,

\# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]

# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI} !(blogs) [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

How to change memory_limit on Wordpress Web App for Linux on Azure

Do you need to edit the memory_limit and post_max_size PHP settingson your Linux Web App hosted with MS Azure? I can show you how to do this without needing to install anything via SSH (which by the way is not recommended. Read up on why here). Step 1: Login to the portal and go to the “Advanced Tools” section and go to the SSH option. Step 2: Copy these entries and place them in the .htaccess file located at /home/site/wwwroot/ using your preferred method. I prefer to use vi. php_value memory_limit 256M php_value post_max_size 256M php_value upload_max_filesize 128M Step 3: Save the file using vi to add these entries into the .htaccess file. It should look something like this once you are done: Step 4: Confirm they have been set by creating a phpinfo.php file and make sure the settings are correct. Or, you can check in the Wordpress Media Upload tool: You can see that these settings have now been updated and no need to make a custom Docker image just to edit a few PHP variables. Let me know what other interesting settings work in our .htaccess files in the comments below!

How to set node version for Windows node.js webapp from Azure Portal

Create the Azure WebApp Create new WebApp using Azure portal Set the App name, Resource Group and click “Create” Wait for deployment to succeed and then click on Application Settings for the new Webapp. Add a new setting with App Setting Name “WEBSITE_NODE_DEFAULT_VERSION” and Value “8.9.4” and click Save. If you would like to use a diffe...

Continue Reading →

Configuring node.js application on Azure App Service with Azure Blob Storage

In this article, we will learn about configuring the node js applications deployed on Azure App Services with the Azure blob storage.

If you would like to store the application logs in a blob storage on azure for the node.js applications, you would need to follow the steps below. It basically includes 2 main steps:

  1. Enabling from Portal
  2. Configuring from the code

Enabling from Portal

· Navigate to your webapp from the azure portal

· Choose the Diagnostic Logs blade. For application level logging, choose Blob storage as shown below. If you would like to choose file system, please check out the blog post here

clip_image002

Configuring from the code

  • In your application, install the packages winston & winston-azure-blob-transport by the running the commands below

npm install winston

npm install winston-azure-blob-transport

  • Now, use the code below in order to configure them from the application

    var winston = require(“winston”);
    
    require(“winston-azure-blob-transport”);
    
    var logger = new (winston.Logger)({
    
        transports: [
    
        new (winston.transports.AzureBlob)({
    
            account: {
    
            name: process.env.ACCOUNT_NAME,
    
            key: process.env.ACCOUNT_KEY
    
            },
    
            containerName: process.env.CONTAINER_NAME,
    
            blobName: “test.log“,
    
            level: “info”
    
        })
    
        ]
    
    });
    
    logger.info(“Hello!”);
    
  • test.log in the above code snippet is the name of the blob where the logs get stored.

  • You would be able to get the Account name, Account key and the container name from the Azure storage account view
  • You would need to make use of Storage Explorer in order to check the log file as shown below.

clip_image004

Debugging Node.js applications on Azure App Services Linux/containers

This is the continuation of the earlier post where I had explained how to enable logging for node applications on App Service Windows. In this blog, you will learn how to debug the node.js applications that are hosted on Azure App Service Linux / Azure webapp for containers. Enabling logs on linux offerings of AppService is fairly easier and pretty straight forward when compared to Windows. Enable Logging on Linux

  • Navigate to your webapp from the azure portal

  • Choose the Diagnostic Logs blade. Here we have only one option to enable logs which is docker container logs unlike in Windows, where we have the storage option as well. As of today, we do not have the option of storing the logs to blob storage in App Service Linux.

  • Choose File System, specify the retention period of your choice. This will delete the logs that are older than specified number of days

clip_image001

  • This will gather all the STDOUT and STDERR logs of the container into /home/LogFiles folder
  • Once you have configured using above step, you will be able to access these log files by following the steps below
  1. Go to your file system by browsing https://{webapp_name}.scm.azurewebsites.net url (called as kudu site)
  2. Click on Debug Console –> Bash. If it is app service Linux, you can alternatively choose SSH option
  3. Please note that SSH works only for App Service Linux. But for Webapp Containers, you would need to configure it by following the official documentation here
  4. Navigate to the LogFiles folder by running the command “cd LogFiles
  5. Now run the command “ls -lrt” to list all the log files with the latest one in the bottom. The stderr and stdout logs are stored in the file that ends with default_docker.log as shown below

clip_image002

  • You can view this by running the command cat . If your application has any stdout/stderr configured, it will be seen in this log file.

clip_image003

  • You can also download it using FTP and steps to follow are specified here
  • If you are only looking for the latest docker logs, the quickest way is to go to Kudu console and click on Current Docker Logs option as shown below. However, this will only give the information related to the container, but not the stdout/stderr logs.

clip_image004

  • Once you click on this link, you will be redirected to a new page with all the details related to the latest docker logs

clip_image005

  • Now, copy paste the link that is highlighted into the new tab and you should see the output as below

clip_image006

© 2019 Azure OSS Developer Support All Rights Reserved.