Thursday, 19 March 2015

Nginx: 301 Redirect To A Domain Name


I am a new Nginx web server user. How do I redirect to a different domain using nginx (say example.org to example.com) permanently?

You need to use HttpRewriteModule module to change URI using regular expressions (PCRE), and to redirect and select configuration depending on variables.


rewrite directive

This directive changes URI in accordance with the regular expression and the replacement string. Directives are carried out in order of appearance in the configuration file. You can use this directive in the following context:
  • server
  • if
  • location

Nginx 301 rewrite syntax

The syntax is:

rewrite regex replacement [ flag ]

Flags can be any of the following:
  • last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.
  • break - completes processing of current rewrite directives and non-rewrite processing continues within the current location block only.
  • redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
  • permanent - returns permanent redirect with code 301

Example: Redirect to a different domain with nginx

In this example, redirect all traffic as follows:

Edit nginx.conf or your virtual host config and update server context as follows:

server {
  server_name .example.org;
  return 301 $scheme://example.com$request_uri;
}
server {
  server_name example.com;
  # rest of config, if any goes below ... #
}
Finally, restart/reload your nginx server, type:
# nginx -t && nginx -s reload

How do I test and verify HTTP 301 redirect?
You need to use curl command:
$ curl -I http://example.org/
$ curl -I http://example.org/foo/bar.html

Monday, 16 March 2015

Create Excel Spreadsheets in PHP

3 Easy Steps to Create Excel Spreadsheets in PHP
Spreadsheet

Generating Spreadsheets

Since computers and automation have become second nature, spreadsheet generation isn’t out of the ordinary. With MySQL databases, it becomes even more imperative, as spreadsheets are perfect to represent the data located in these structures.
Upon searching for how to create excel spreadsheets using PHP, most of the results came back with libraries that do the job. This seemed to be too much of a hassle. That’s why I’ve created 3 easy steps to create excel spreadsheets without any libraries.

Step 1: The Content-Type

The first thing you have to do is add an application/vnd.ms-excel content-type to your PHP file:
header("Content-Type: application/vnd.ms-excel");
Easy enough? Let’s move on to the actual data.

Step 2: Adding the Data

Data is just as simple. Separate cells/columns with tabs (“\t” in PHP) and move to the next row with a newline (“\n” in PHP).
Here are a few PHP echo statements that will do the trick:
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
I’ve separated the tabs (\t) and the new lines (\n) so you can easily see the structure. The above would create a spreadsheet that looks like this:
First Name     Last Name     Phone
John           Doe           555-5555

Step 3: Downloading the Spreadsheet

Now that you’ve set the content-type and created the data, just open up the PHP file in a browser. You’ll be asked to download the spreadsheet. If you would like to give a name to the spreadsheet, just add the following:
header("Content-disposition: attachment; filename=spreadsheet.xls");
All you need to do is change spreadsheet.xls to the name of your spreadsheet. Note that the above header also ensures that Internet Explorer will ask you to download the file rather than trying to display it.

Conclusion

That’s really all there is to create a spreadsheet. For those of you who are using Microsoft Excel 2007, you might see the following message when opening the spreadsheet:
The file you are trying to open, ‘filename.xls’, is in a different format than specified by the file extension… [message condensed]
If so, just hit the “Yes” button and you’re good to go!

Always open Google Chrome in Incognito: Mac

Most Chrome users use the Incognito browsing mode every now and then, while others use it quite often. If you prefer incognito browsing over normal browsing sessions (we’re not judging) but are vexed with the fact that there is no one-click method to launch Chrome in Incognito by default, you can do so with a little script. This particular method is meant to work in OS X only, as Windows users can always directly launch Chrome in Incognito mode from its Jumplist (just as Mac users can do so from the Dock icon’s options). The script is meant to launch Chrome in Incognito mode with one click using a dedicated shortcut created in form of an AppleScript app.
Open AppleScript Editor and paste the following script in it:
do shell script "open -a /Applications/Google\\ Chrome.app --args --incognito"
Save the script as an application and move it to Launchpad for easier access. If you want to be able to search and run it from Spotlight search, you should add it to the Applications folder. If you’re only ever going to use Chrome in Incognito mode, all you need to do now is to remove the app’s icon from the Launchpad and keep only this script there.
Chrome not running
This script has a little catch to it though – it won’t work if Chrome is already running. So if you’re looking for a method that will open Chrome in Incognito mode regardless of whether it’s already running in normal mode or not, open AppleScript Editor and save the following script as an application. The catch with this method is that it will close all other Chrome windows when launched.

tell application "Google Chrome"
    close windows
    make new window with properties {mode:"incognito"}
    activate
end tell
Chrome incognito
Both methods should give you an application that can be added to Launchpad, but the apps’ icons will be the default script one, and that’ll make it hard to be recognized. What you can do is modify the icon to something that is easier to identify. Find a nice PNG image, preferably one that’s small in size, that you can identify with as Chrome’s Incognito mode. Open that image in Preview and from the Edit menu, click ‘Select All’, followed by ‘Copy’. You can select the image by clicking and dragging your cursor over it, though there is a chance you might not select all of it.
Right-click the application you created using AppleScript Editor and select ‘Get Info’. In the window that opens, click the icon at the very top next to the script’s name and hit Ctrl+V. Make sure the padlock icon at the bottom right is unlocked so that the changes take effect. Enter your password if you are prompted to do so.
script icon
You now have a perfectly functioning and easily recognisable app that opens Chrome in Incognito mode.

Friday, 13 March 2015

SSL Certificate Installation in Nginx

Nginx Server SSL Certificate Installation

  1. Primary certificate and intermediate certificate.
    You should have received a your_domain_name.pem file from DigiCert in an email when your certificate was issued. This .pem file contains both your primary certificate and the intermediate certificate. If you have that .pem file you can skip to step 4.
    If you still need to concatenate your primary certificate and your intermediate certificate in to a single file, start with step 2.
  2.  Copy the Certificate files to your server.
    Log in to download your Intermediate (DigiCertCA.crt) and Primary Certificates (your_domain_name.crt) from within your DigiCert Customer Account. Copy them, along with the .key file you generated when you created the CSR, to the directory on your server where you will keep your certificate and key files. Make them readable by root only to increase security.
  3. Concatenate the primary certificate and intermediate certificate.
    You need to concatenate the primary certificate file (your_domain_name.crt) and the intermediate certificate file (DigiCertCA.crt) into a single pem file by running the following command:
    cat your_domain_name.crt DigiCertCA.crt >> bundle.crt
  4. Edit the Nginx virtual hosts file.Now open your Nginx virtual host file for the website you are securing. If you need your site to be accessible through both secure (https) and non-secure (http) connections, you will need a server module for each type of connection. Make a copy of the existing non-secure server module and paste it below the original. Then add the lines in bold below:
    server{

    listen 443;

    ssl    on;

    ssl_certificate /etc/ssl/your_domain_name.pem;
    (or bundle.crt)

    ssl_certificate_key /etc/ssl/your_domain_name.key;

    server_name your.domain.com;

    access_log /var/log/nginx/nginx.vhost.access.log;

    error_log /var/log/nginx/nginx.vhost.error.log;
    location / {
    root   /home/www/public_html/your.domain.com/public/;
    index  index.html;}}

    Adjust the file names to match your certificate files:
    >ssl_certificate should be your primary certificate combined with the intermediate certificate that you made in the previous step (e.g. your_domain_name.crt).
    >ssl_certificate_key should be the key file generated when you created the CSR.
  5. Restart Nginx.
    Run the following command to restart Nginx:
            sudo /etc/init.d/nginx restart