Programming

Category: Programming

Setting up SSL on Amazon Linux Instance under EC2

Hopefully if you do it right, you too will see a positive Qualys report after you follow these steps.

Previously, the easiest way to add an SSL certificate to an EC2 instance (and still the Amazon recommended way it appears) has been to create a certificate (free) from Amazon through the certificate manager and then adding that cert to a CloudFront or ELB instance (both of which cost money).

I’ve been successful in using LetsEncrypt to provide SSL for websites running under Windows Server in EC2 using the fantastic Windows ACME Simple (WACS) (previously known as letsencrypt-win-simple (LEWS)). If this is your need, you can always grab the lastest instance on GitHub.

I never seemed to find the correct set of steps to do this on the Amazon Linux instances. However, due to the work of Lawrence McDaniel, I discovered this blog post. He walks you through step by step on how to install mod24_ssl, CertBot (from EFF) and then how to run CertBot to create and install a LetsEncrypt SSL cert on one or more sites on your Apache-served Amazon Linux instance. He also shows you how to use crontab to set up a recurring job to renew the certificates on a regular basis (a necessity since LetsEncrypt certificates expire after 90 days.

Well done Lawrence!

BTW – crontab defaults to vi as the editor. If you aren’t used to using this beast (I know, some people love it) what you really need to know is that you press the letter i on your keyboard to enter INSERT mode so you can edit the file, hit ESC to exit the INSERT mode and then enter “:wq” (without the quotes of course) to write and quite (save and exit) or just “:q” to quit without saving your changes.

SQL Tips: Insert Value using Identity from Previous Insert

Ever find yourself needing to write SQL code to run on multiple tables where one of the values in a secondary INSERT statement is dependent upon the identity value from previous insert? Well, if so, this should work for you…

DECLARE @NewApiId int;
INSERT INTO APIs (Name, Description) VALUES ('Social Links','The Social Links API, available only to corporate clients, will output a report all of the clients for the client''s ClientTypeID that includes all of the recorded social media properties');
SELECT @NewApiId = CAST(scope_identity() AS int);
INSERT INTO ApiSubscriptions (ApiId, ClientId) VALUES (@NewApiId, 666);
select * from APIs;
Select * from APISubscriptions WHERE ClientId=666;

Pretty simple, no?

Insert From Table on Remote SQL Server

Ever have the need to populate a table from data that you have in a table on a different SQL server? This is a very handy thing to do when you are trying to add data to your dev or staging SQL databases from the live server.
Previously I would have exported the data from the one server and then imported that data into the new one.
Well brother (and sister) I’m her to tell you there is a better way!
Through the magic of Linked Servers (SQL Server 2005+ I believe) it can be done. And through the use of Synonyms, you can even make it less cumbersome if it is something that you do regularly.
How does this work you ask? Well here is how I did it.

  1. Ensure that in SQL Mangler (I mean SSMS) you are connected to both servers with a user that has access to both databases.
  2. Open a query window for your target database
  3. Run the following command replacing server.domain.com with the FQDN of the source server
    exec sp_addlinkedserver [server.domain.com];
  4. Run your INSERT INTO statement as follows:
    INSERT INTO targetTableName ([Col1], [Col2], [Col3], [etc])
    SELECT [Col1], [Col2], [Col3], [etc]
    FROM [server.domain.com].databaseName.dbo.tableName
  5. If this is something you are going to be doing a lot of, you can make a Synonym of to save you some typing:
    CREATE SYNONYM nickname FOR [server.domain.com].databaseName
    Which would give you the ability to shorten your FROM selector.

Of course, your results may vary, always test on a backup database, blah, blah blah. Enjoy coders!

Wget and URLs with Ampersands

I have been working around some issues with Wget recently and after modifying the URLs, I found that it would start failing if the URL was a querystring with more than one variable. This means that you would have an & in the string.

Well, Wget sees this as an additional command so you need to delimit it somehow. Googling turned up this article that indicates either enclosing the URL in single-quotes (‘) or delimiting the ampersand character with a backslash. The comments on this post all praise this solution.

Only problem… Neither worked for me. Then I thought, perhaps this is another “oddity” of running it on the Windows port. So, I tried enclosing the URL in double quotes and voila it worked! So, let that be a lesson to you: Don’t assume every ported program from Linux runs as you’d expect when under Windows.

Did You Know: Bootstrap Grid Breakpoints

One of the nice things about Bootstrap is it’s grid system and how it can be leveraged to do some pretty cool things as you move from device to device. Aside from knowing that the standard measure is for 12 columns wide, it is also important to know what the breakpoints are for each size.
Bootstrap uses 4 different classes to accomplish this.

  • Extra Small has a breakpoint for displays of less than 768px (many/most phones in portrait mode)
  • Small has a breakpoint between 768px and 991px (appropriate for most tablets)
  • Medium has a breakpoint between 992px and 1199px (laptops and desktops with small screens)
  • Large has a breakpoint greater than 1200px (suitable for the modern laptop or desktop)

If you don’t apply a breakpoint to a column,then the default behavior is to stack the columns.
You can use multiple breakpoint classes in a div so that you can have different behaviors for different sized screens but just remember, the lowest appropriate breakpoint is going to win.