PHP and Azure Data Services

When we think of PHP we also think of MySQL. The perfect relationship. PHP can provide the logic and MySQL can provide the data storage. There are others like SQLite, Postgres, MariaDB, MongoDB and more. All of these provide great functionality and performance but there are a few services that Azure has that can be deployed quickly and managed really easy. We are going to look into 3 services that Azure utilizes to make deploying and building PHP apps really easy and scalable.

Using Azure Microsoft SQL Server

This is not something new. PHP has supported MS SQL for a while now but why not utilize the ability of creating a SQL DB out on Azure and using the ODBC drivers to connect to SQL. By using SQL as a Service you can build software-as-a-service (SaaS) applications that use Azure SQL Database to provide flexibility to support both explosive growth. For workloads with unpredictable database resource consumption, the elastic database model gives you the ability to pool resources to use among a group of databases. Instead of overprovisioning a virtual machine to meet peak demand, you can use an elastic database pool to let hundreds or thousands of databases use resources within a budget that you control.

You are also removing virtually all infrastructure maintenance with SQL Database, which provides automatic software patching as part of the service. Meanwhile, built-in system replicas using the quorum writes technique help deliver inherent data protection, database uptime, and system stability, which means fewer hassles for developers and architects. System replicas are automatically moved to new computers, which are provisioned on the fly as old ones fail.

Now you do not have to focus on the “database server” you can just put your focus on your business and technology.

Here is an example. Using sqlsrv_connect and a MS SQL instance out on Azure, you can be up and running in no time.

Using Azure App (Mobile) Services

Azure App Service is the only cloud service that integrates everything you need to quickly and easily build web and mobile apps for any platform and any device. Built for developers, App Service is a fully managed platform with powerful capabilities such as built-in DevOps, continuous integration with Visual Studio Online and GitHub, staging and production support, and automatic patching. Using Mobile Apps in Azure App Service, it offers a highly scalable, globally available mobile application development platform for Enterprise Developers and System Integrators that brings a rich set of capabilities to mobile developers.

Why Mobile Apps?

Mobile Apps in Azure App Service offers a highly scalable, globally available mobile application development platform for Enterprise Developers and System Integrators that brings a rich set of capabilities to mobile developers. With Mobile Apps you can create API’s in minutes. Customize those API’s to include push notifications, authentication and more.

Here is an example on how to set up a Mobile app as well as integrating the service with a PHP application. There is a Azure JavaScript Library that you can import and use for your app.

Using Azure PHP SDK

The other way to utilize manage storage is via Azure Storage (blobs, files, tables). Azure Storage is massively scalable, so you can store and process hundreds of terabytes of data to support the big data scenarios required by scientific, financial analysis, and media applications. Or you can store the small amounts of data required for a small business website. Wherever your needs fall, you pay only for the data you’re storing. Azure Storage currently stores tens of trillions of unique customer objects, and handles millions of requests per second on average.

Azure Storage is elastic, so you can design applications for a large global audience, and scale those applications as needed – both in terms of the amount of data stored and the number of requests made against it. You pay only for what you use, and only when you use it.

In order to access Azure Storage in PHP you will need to install the Azure PHP SDK and configure your application as well as set up a storage account.

Wrap up

Each of these options will provide you with managed data storage, easy setup, and highly scalable so you can focus on your code and your business.

Check out phpmssqldemo.azurewebsites.net for examples of all three scenarios.

Using MS Open Tech to Quickly Build Your Virtual Machine

Azure Marketplace is a great place to start your search for a Virtual Machine to start building your app or infrastructure on. However, these VM’s are sometimes vanilla and will need to be configured to your liking. Let’s use Rails as an example. In order to get a Rails app on a Virtual Machine there are a bunch of steps you will need to perform. First, create the operating system VM, Linux. Make sure that is up to date (sudo apt-update). Then you need to install Rails, Nginx, Unicorn, some sort of database like Postgres and dont forget permissions and users.

Having a custom install is cool but sometimes the situation does not call for that. There is MS Open Tech for that.

msopen

Microsoft Open Technologies, Inc., (MS Open Tech) is a wholly owned subsidiary of Microsoft Corporation to advance Microsoft’s investment in openness including interoperability, open standards and open source.

At MS Open Tech, we focus on providing our customers with even greater choice and opportunity to bridge Microsoft and non-Microsoft technologies together in heterogeneous environments. We believe that openness is good for our customers, good for the community and good for our business.

We’re a diverse team of engineers, standards professionals and technical evangelists who are experienced and passionate in working with an equally diverse set of technologies from Microsoft and others.

Every day, we are powering interoperability and opportunity across developers, partners, customers and competitors.

We build open source code. We create interoperability bridges. We contribute open standards specs. We work with a variety of standards organizations such as: W3CIETFOASISEcmaDMTF, and open source communities including: JQueryMongoDB,Apache CordovaRedisApache, Qpid.NET Foundation, OpenJDKEclipseNode.jsCocos2d,  Web Platform DocsSymfony, DoctrineWebKitGitHubCodePlex and more.

Our team works closely across Microsoft business groups to support our customers’ journey in a mobile first, cloud first world.

As a result, you can do more and achieve more with the openness of the Microsoft platform.

  • You can build with the tool of your choice.
  • You can work across platforms.
  • You can collaborate through code.

There are Projects that MS Open Tech builds, HTML5 Labs that contributes to the Edge platform and VM Depot where a community managed repository of Linux and FreeBSD virtual machine images for easy deployment to Microsoft Azure reside.

You can search for a open source product or platform and most likely there is an image that is ready to go and deploy. Let’s use WordPress as an example.

  1. Search for WordPress and you will see a listing of results.
  2. Choose the result that best fits your need.
    wpubu
  3. Click on Create Virtual Machine (or if you are familiar with PowerShell you can get a deployment script)
  4. Login to the MS Open Tech website using one of the 3 Authentication methods.
  5. You can fill out the minimum to create an account on the MS Open Tech site.
  6. Accept the T&C and Click Save
  7. The next step is to link your Azure account with MS Open Tech to create the deployment. If you have your publication file you can upload it, otherwise you can click the Azure Publish Settings link within the copy and it will download one for you (as long as you are currently logged into the Azure portal).
  8. Once downloaded, you can now upload it to the site.
  9. Once that is connected, you now can configure some of the VM settings. DNS Name, Username/Password as well as region, storage accounts and endpoints.
  10. Once you are happy with your configuration, click I agree and Create VM. It should take a few minutes and your new VM will show up in your Azure portal.
  11. One thing to note. If you choose a VM with configured software you might need to read the README to find out usernames and passwords to certain products.

 

Activate Your DreamSpark Subscription to use Azure

What is DreamSpark?

DreamSpark is a Microsoft Program that supports technical education by providing access to Microsoft software for learning, teaching and research purposes.

DreamSpark is simple: it’s all about giving students Microsoft professional-level developer and designer tools at no cost so that students can chase their dreams and create the next big breakthrough in technology – or just get a head start on their career.

DreamSpark helps educators teach the latest technologies and experiment in research. Microsoft knows that to make learning more motivating, relevant, and engaging for today’s students requires a diverse set of resources. DreamSpark gives educators the resources to ensure their classrooms always have the latest technologies to challenge, motivate, and keep students engaged in new ways.

DreamSpark is also a subscription for Academic Institutions: it provides a cost-effective way to put Microsoft developer tools, platforms and servers in labs and classrooms, and on their students’ and faculty’s computers for learning and research purposes. It reduces lab costs and teaching budgets.

How do I get it?

As a Student: simply create an account, verify your student status and download software through this website at no cost. If your school/university has a subscription, you can also get access to more software titles.

Here are the steps:

  1. Head on over to DreamSpark.comDreamspark.com
  2. Click on Sign In or Get Azure Now. step-2
  3. Make sure you have a Microsoft Account first and sign in to create your DreamSpark account.step-3a
  4. Once logged in, you can now create your DreamSpark account profile. Click ‘Continue to Verify’ to get to the verification option screen.step-3b
  5. The verification option screen provides several methods to verify that you are a student. Using your student email address (john.doe@college.edu), your School DreamSpark account (My College Name), Student Identity Card, Verification Code or documentation you can supply.step-3c
  6. Once you enter your verification method, click verify.
  7. If you are verified you can click continue to get to the “congratulations” screen.step-3
  8. Click continue to access the DreamSpark main page. From here you will be able to get to the store and Download Software.step-4
  9. You will now be presented with all of the software and service options within DreamSpark. Click on Azure to activate your Azure Subscription.step-5
  10. You will now be prompted to start the Azure account activation process.step-6a
  11. Once you click on Register Now you will be in the Azure Account creation funnel. Fill out the appropriate information and make sure you are verified before you click sign up.step-7step-8
  12. The Azure account creation takes a couple of minutes. In the mean time you can view a video of Azure or sit and wait until you get the all done message.step-9step-10
  13. You can now start managing your service and access the Azure Portal.step-11
  14. You can jump right in and start creating Web Apps!step-14
  15. Unfortunately some features are not active with the Student DreamSpark subscription, such as Virtual Machines. But that is ok. There is a lot you still can do within Azure.step-13

 

Library of Congress

Managing ConnectionStrings and Environment Variables with WordPress on Azure

One of the cool features of Azure is the ability to have a “deployment slot” for your web app.  Web app content and configurations elements can be swapped between two deployment slots, including the production slot. Deploying your application to a deployment slot has the following benefits:

  • You can validate web app changes in a staging deployment slot before swapping it with the production slot.
  • Deploying a web app to a slot first and swapping it into production ensures that all instances of the slot are warmed up before being swapped into production. This eliminates downtime when you deploy your web app. The traffic redirection is seamless, and no requests are dropped as a result of swap operations. This entire workflow can be automated by configuring Auto Swap when pre-swap validation is not needed.
  • After a swap, the slot with previously staged web app now has the previous production web app. If the changes swapped into the production slot are not as you expected, you can perform the same swap immediately to get your “last known good site” back.

As a web developer, this makes your life easy. Production and Staging can be exactly the same. How many times have you used a staging environment that was a different  OS version than production. “It works on staging but not on production” is the common quote.

When I’m told that my code is broken in production

Setting up deployment slots are pretty straightforward. You can read more about them on this Azure post. However, how do you handle the environment variables for each slot? I need my production code to access my production database.

There are 3 ways that this can be accomplished.

1. The RCP Way

The RCP Way stands for Rename, Copy and Paste. You would rename your connection string or variable file to connstring.stage or connstring.prod and then copy and paste the appropriate values in them. Works fine. However, when deploying from Git or some version tracker can get a bit tricky.

2. The If Then Way

IF “Production” THEN production values ELSE staging values. You can either get the ip address, domain name, server name or even a boolean value that if it signals that you are on production you load in the production values. This works fine but now your one file has 2 or more sets of variables that will need to be maintained.

3. Environment Variables

This is part of Azure which makes deployment slots work so well. I configure my values within Azure per environment. This way my code does nto have to have any usernames or passwords just the variables to store them in.

I am going to show a quick example of how to set up your WordPress Blog using the connection string variables within Azure. This can be applied to any application but I will use WordPress as the default.

Step 1. Create your web app.

I am not going into detail on how to create a wordpress web app but you can follow this tutorial to get that up and running.

Step 2. Find your connection string

This can vary. You might have a Virtual Machine that is custom hosting your database or you might use the default database within Azure. Either way finding your connection string can be done by determining the Username for the DB. the Password for the DB. The data source and DB name. You can also get this info from the wp-config.php file within your root of your WordPress website. You might want to look in there since we will be modding that file in a moment.

Step 3. Build your string

Once you have those 4 parts you can build together your connection string. Your string can look like this.

Step 4. Add your string to the portal

Go into your web app and choose All Settings then Application Settings.

Application Settings

The Application Settings blade will appear. Scroll down a bit until you see the section called “Connection Strings”.

There are 4 form elements that you can configure.

  • Name: This is your name of your connection string. MyClientDB or AnalyticsDB as examples. Whatever you name it, Azure will add a prefix to it that I will explain in a moment.
  • Value: This is your value. You connection string. See step 3.
  • Type: You can choose Custom, SQL Server, SQL DB or MySQL. When you choose any of these Azure appends the following text to the front of your “name”
    • SQL Server: SQLCONNSTR_
    • MySQL: MYSQLCONNSTR_
    • SQL Database: SQLAZURECONNSTR_
    • Custom: CUSTOMCONNSTR_

Note:

For .NET apps, these connection strings are be injected into your .NET configuration connectionStrings settings at runtime, overriding existing entries where the key equals the linked database name.

For PHP, Python, Java and Node applications, these settings will be available as environment variables at runtime, prefixed with the connection type. 

Now if I need to access my MySql connection string called MyClientDB, it would be accessed through the environment variable MYSQLCONNSTR_MyClientDB.

I then can create a deployment slot with the same connection string “name” but different values and my code will use those values to connect to staging or production data.

You can see how this would be valuable when you are moving from dev to test to prod. You can also use App Settings to store “app specific” data in there as well per environment.

Step 5. Update our WordPress

Remember I said we are going to need that wp-config.php, well if it is open great, if not please open it in your favorite code editor, mine is Visual Studio Code, a free download and works great.

The first couple of lines are comments but then there are a few “define” constants that the application will use.

Your file might have actual data in your file but that is fine. We will be replacing that with the values from our environment variable that we just created in Azure. To access it’s data you can call the get_env function to retrieve it.

At this point you have your connection string and you can $echo it out to see what comes back. To make it a bit easier to get each element of the connection string you can add this function to the top of the wp-config.php file.

This is a handy little function that coverts a string into an array with a predefined delimiter.

You can var_dump the returned value to see what is returned.

Then you can replace the 4 defined constants with the following:

Done!

Now you can deploy stage and prod and have your WordPress blog access different databases very easily.

Here is the link to the full file on GitHub

BUILD 2015 Recap

Azure App Services

Azure Internet of Things

Azure Resource Manager

Azure Marketplace

Office

 Windows Store

Visual Studio Tooling

Universal Windows Platform

UX/Xaml/Graphics

Platform Bridges

Hartford Area Microsoft Events

sign-me-up-2

I wanted to share these upcoming events in the Hartford and Danbury area.

Building Websites in the Cloud

Learn how to use Microsoft Azure, our global cloud solution, to power your websites

This workshop will show you how to quickly create web applications using popular frameworks such as WordPress, Joomla, NodeJS, PHP and more. You will learn how to create and deploy a website using Visual Studio Online. Other features such as cloud services will be discussed how to take your website to the next level.

Time: Monday, March 16, 2015 from 6:00pm to 8:00pm
Location: Microsoft Store, 211 Westfarms Mall, Farmington CT (located on the upper level between Center Court and Macy’s)

Time: Wednesday, April 1, 2015 from 6:00pm to 8:00pm
Location: Microsoft Store, Danbury Fair Mall, Danbury CT

btn-register

These workshops  are “hands-on” optional. You can attend and learn without participating in the labs. The labs will be provided at the end of the workshop. If you are particpating in the labs you will need the following:

 

Microsoft Cloud Dev Camp

Master the new Microsoft Azure features and services to build, deploy and move apps to the cloud

Roll up your sleeves and get ready to master the latest Microsoft Azure development tools and technologies. Join us at an expert-led Microsoft Cloud DevCamp and leave with code running in the cloud! DevCamps are fun, FREE events for developers, by developers. That means no fluff or filler – just valuable coding skills you can immediately put into action.
More information here.

Time: Thursday, March 26, 2015 from 9:00am to 5:00pm
Location: Microsoft’s Office, Hartford at 280 Trumbull Street, Hartford CT

btn-register

The Cloud Dev camp is intended to be “hands-on”. The following is required for attendance:

Note – you MUST bring a laptop to participate in this event. In addition:

 

Cloud DevCamps

sign-me-upJoin us for a FREE hands-on day of Microsoft Azure demos and labs in a city near you!

Roll up your sleeves and get ready to learn the latest Microsoft Azure dev tools and technologies. Join Microsoft experts at your local Microsoft Cloud DevCamp and leave with code running in the cloud!

From your first cup of coffee until we call it a day, you’ll learn how to use new Microsoft Azure features and services to build and move a variety of applications to the cloud – including websites, mobile apps, and enterprise-class applications.

Whether you use Linux, Java, Node.js, Python, Ruby or just about anything else, we speak your language. With Microsoft Azure, you can use practically any language, framework or tool to build or move existing applications to the cloud.

Don’t miss the live Cloud DevCamp in your local area. Save your seat today!

reg-today

We’ll also give away a $100 Microsoft Store gift card* to one lucky attendee at each event!

See All Events
Sessions run from 9:00am – 5:00pm.

Attendance is limited, so save your seat today.

Linux Powered By Azure – 5 Part Series

If you are a modern day developer you do not have the luxury to know just one type of technology or language. Sure, you can have your specialty, but the more knowledge you have the more valuable you can become. When I worked in the digital agency world, we did not have the luxury to use just one type of technology stack. We had to support all types. Java, Struts, LAMP, .NET, ASP and more. Based on our clients needs we had to switch from using C# with .NET to coding with PHP and CodeIgniter. At Microsoft as an evangelist, it works the same way. I do not have the “traditional” clients, but you do. I support you as a developer and you have many clients with different technology stacks.

This series is to explain how to run Linux VM’s out on Azure, configure and edit and debug as well. The other big thing to note, is this can all be done on a non-Microsoft system, ie. Mac.

  1. Getting Started
  2. Turn on the LAMP
  3. WordPress Installation
  4. Remote Editing with NetBeans
  5. Remote Debugging with NetBeans

(more…)

Linux Powered By Azure – NetBeans Debugging

Now that we got NetBeans communicating with the remote server, we need to configure the remote server to enable debugging.

Prerequisites

We need to SSH to our remote machine. I am not going to go over that since that was covered in the setting up the Linux VM tutorial.

(more…)