It is hard to keep up on all the different ways to get an application out to a public facing host. You hear names like Chef, Vagrant, Puppet, Ansible, Docker, Cloudstuff… At the end of the day you just have to pick the one that makes sense for you. Hopefully you can do that without going all the way down the road of fully implementing your production environment with all this before you can decide. Here I will show you one way it can be done without too much fuss and without a 3rd party tool family so big you cant remember all their names.
Some of the old-timers may remember “poor man’s” IT tools. My flavor I like to call ‘Ghetto Hacks’, or when they are elegant I like to call them a ‘Dixie Mix’. This solution isn’t exactly the most elegant, but it is possible to run a production environment from it as it does not introduce any non-production worthy dependencies. It basically does it’s thing and can be thrown out. In fact I run a bunch of domains that I have setup this exact way.
Some people like to adopt a bunch of cute tools so they can talk ‘name soup’ at the water cooler. My first goal was to sidestep this for a more unified front. I also like to keep environments pure. What I mean is I don’t like to use a Ruby script to drive a Python script that installs a PHP script. This does not mean I wont use shell script or OS built-ins, but I like to minimize the external installs and dependencies and use native tools.
So now lets take a deep breath before we take this shallow dive. I will introduce the stack and then show you how easy it is to create a WordPress Website and WP-CLI to configure it from CMD, even if you use a Mac.
So lets start with the host. As the title states we are using Azure, which is a Windows-ish environment. I say this because many users unfamiliar with the OS in the last 10 years may be surprised how at home they can be quickly. You may be familiar with ipconfig on Windows sharing use cases with ifconfig in unix. What you may not know is that there are many things that can make you feel at home. For example in the CMD shell on Azure you can type ls -la and get a directory listing. As an interesting side note there is a shell for Azure written in Node.js called KuduExec, if that makes you feel at home. We obviously wont be using that here.
Now is where you would normally get into ‘Cheppet or ‘Vagrible. I have instead chosen to use ‘Websites’, a name I can remember. No OS management necessary. You may be asking, based on the name, what is the difference between Websites and your $1.35/month shared hosting. Without editorializing more than I already have, think “all the power and none of the ceremony” of a fully virtualized environment, a non-“Ceremonialized” environment.
I have chosen WordPress because it has a low barrier to entry for users and I think it is somewhat of a common tool. I also choose it because it is PHP and isn’t the first thing you would think to run on Azure. I must point out that programming language discussions sometimes degrade to unproductive arguments. All sides usually expose their ignorance by arguing against the negatives that existed 20 years ago (often before most participants were born or could talk). “ET was the worst game ever.” So a PHP on Azure is some what of a ‘PB and ketchup sandwich’ in the world of cloud. To set you at ease Windows and Azure has run PHP well for years, so I hope you like the simplicity of the combination.
STEPS: The Instance
The obvious first step is to get a single instance up and running. Preferably this would be a free operation so we can be sure of the option and the path. Gladly, Azure gives us a free tier to make it happen, and scale when we are ready.
- In this case I will walk you through using the portal. At a later date I will do this same tutorial via shell (cmd or PowerShell).
- From the portal home you will select ‘New’ > ‘Web + Mobile’.
- At the bottom select ‘Azure Marketplace’.
- On the right side you will see a search box where it says ‘Search Everything’. Type “wordpress” and hit enter.
- There will be two items at the top for WordPress, we want the one that is “WordPress” only with a Publisher of “WordPress” and Category of “Web Apps”. (not Scalable WordPress)
- When you click on that you will get a description. Click the ‘Create’ button at the bottom.
- The next frame will show what you want to call the resource group and some configurations. For the sake of this experiment start with ‘Web App- Configure required settings’.
- URL: you need to pick something unique
- Pricing Tier: to select the free tier:
- click on this option and in the next frame click ‘View all’.
- At the bottom click ‘F1 Free’ and ‘Select’.
- This will be good enough for testing.
- Click ‘OK’ at the bottom of that frame.
- Give your database a descriptive name by clicking ‘Database’ and changing it to something like “MyWpDb” at the top.
- To proceed you need to click ‘Leagal Terms’ and accept the terms if you agree.
- Click ‘OK’ at the bottom of the Database frame. This will return you to the ‘WordPress’ frame.
- Click Create.
When you create an Azure Website in the background a second site is created at http://.scm.azurewebsites.net which exposes the Kudu Console (Kudu is also the deployment engine for Azure Websites). This is where you will gain access to Powershell and CMD, more on that later. (but if you are ancious click ‘Debug console’ > ‘CMD’ and type ‘ls -la’ then ‘cd’-around and notice how things above the shell window change).
If you click ‘Home’ and then the tile that was created there you will see a panel that describes the resources you have created. You have created a Web app, an Applicion Insights profile and a MySQL Database. You also see various other information about the site, including ‘estimated spend’ which will help you keep ontop of the ‘for pay’ sites should you decide to upgrade for any additional features in the future.
STEPS: WP-CLI Install
Since we are going to use wp-cli to configure the site, you should install that before hitting the site. Click on your web app and right at the top click “All Settings”. Scroll down to “Extensions” and click it. Then click the add button at the top. You will then be presented with a list, from which you should choose WordPress CLI by Cory Fowler.
Now, to show you it works, visit the Diagnostic Console via http://.scm.azurewebsites.net. Click Debug console. Then, for the purposes of this post, click CMD. Then to make sure everything is working type
'wp cli version'. This should display the version of wp-cli you are using.
To make things easy for me, I like to make a wp-cli.yml file in the site root. This allows me to do operations directly on the root site without specifying it every time. So create a wp-cli.yml file with the following contents and upload to your
url: .azurewebsites.net core config: dbname: dbuser: dbpass: extra-php: | define( 'WP_DEBUG', true ); define( 'WP_POST_REVISIONS', 50 );
Obviously replacing the content in the brackets with your own. This information can be obtained from the portal (All settings > Application settings > Connection strings [you may need to click “Show connection strings”]). Note that the WP_DEBUG constant should only be used while testing.
STEPS: CMD Configuration
Now, to setup the first site, it is simply one command (replacing %name% with your values):
wp --path=%install_dir% core multisite-install --subdomains --title=%siteTitle% --admin_user=%admin% --admin_password=%adminpw% --admin_email=%adminaddr%
Breaking down this command a little, we know that the %install_dir% is going to be D:\home\site\wwwroot. Here we are doing a install for WordPress Multisite, so we can point several domains at the same piece of code which makes updates and maintenance quite a bit easier. For you to understand the rest of the options the documentation is quite good.
Wp-cli makes installing extensions easy also. For example, if you are planning on importing your previous site I would recomend generating thumbs seperate from the import process with the regenerate-thumbnails extension, which is installed like this:
plugin install regenerate-thumbnails --activate
Then say you want to install and activate a great Bootstrap based theme named after a different part of the shoe:
wp theme install toebox --activate
Perhapse you want to install the WP Theme Unit Test data to test your own theme. You could do the following:
php -r "readfile('https://raw.githubusercontent.com/manovotny/wptest/master/wptest.xml');" > wptest.xml wp import wptest.xml --authors=create --skip="image_resize" wp media regenerate --yes
Now, visit your site and see what you have. Hit the /wp-admin url and login with the username and password you specified in the install command above and see that everything is there.
You may think that this tutorial ended abruptly. Hopefully you only reach this summary if you were not following along on your own free account. I hope you were totally distracted from this post when you started playing around with the Azure Debug console and started reading the WP-CLI documentation. But in the event that you find yourself back here to finish reading this post, thank you.
As with any platform Azure has it’s quirks. Ones that you probably aren’t used to. It is developing at a break-neck pace, and they are highly attuned to their users. Managing websites without the ceremony of VMs is something that is achievable now, and I hope you see this. Personally I have moved away from these “Ceremonalized” environments because I don’t have the time to invest in DevOps or finding and paying someone else to do it for me. I build and tear-down whole sites daily for testing using little more than Composer, WP-CLI and some batch files. I hope you can remember those names. I might not be one of the cool kids speaking the slur of name soup, but my lean running, massively scalable site makes my customers money and keeps their customers happy and coming back. I hope the same or better success in your endeavor. What have you had success with? Leave a comment.