Generate Random string on shell

To generate a random string on the shell

<code>

cat /dev/urandom | tr -cd ‘a-zA-Z0-9-_$#%&@!*’ | head -c 15

</code>

 

Advertisements

Git Aliases “cannot exec”

I ran into a problem when attempting to use git aliases.

[git@git hello]$ git hist
fatal: cannot exec 'git-hist': Permission denied

Turns out the issue was because I had logged in as user “git” via the su command but had neglected to use the – (minus)

When I logged in directly as user git or from root using ‘su -l git‘ everything worked as advertised.

I got the clue to the problem from running strace.

[git@git hello]$ strace -f -e execve git hist
execve("/usr/bin/git", ["git", "hist"], [/* 21 vars */]) = 0
Process 1560 attached
[pid  1560] execve("/usr/libexec/git-core/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/usr/local/sbin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/usr/local/bin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/sbin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/bin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/usr/sbin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/usr/bin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 ENOENT (No such file or directory)
[pid  1560] execve("/root/bin/git-hist", ["git-hist"], [/* 21 vars */]) = -1 EACCES (Permission denied)
fatal: cannot exec 'git-hist': Permission denied

Exit Popup Message

A lot of marketing site needs exit popup message. I seen lot of bloated plugging which does the job. This one is cool and simple.

<!– Exit Message –>
<script type=”text/javascript”>
window.onload = function(){

var allowUnload = true;

window.onbeforeunload = function(e){
//allowUnload will allow us to see if user recently clicked something if so we wont allow the beforeunload.
if(allowUnload){
//message to be returned to the popup box.
var message = ‘Are you sure you want to leave the page.’,
e = e||window.event;
if(e)
e.returnValue=message; // IE
return message; // Safari
}
};
// We need this to allow us to see if user has clicked anywhere if user has clicked we wont allow beforeunload to run.
document.getElementsByTagName(‘body’)[0].onclick = function(){
allowUnload = false;
//setTimeout so we can reset allowUnload incase user didn’t leave the page but randomly clicked.
setTimeout(function(){ allowUnload = true; },100);
};

};

</script>

Split your Nginx config files up, in order to host multiple WordPress domains

If you are hosting multiple domain on your VPS, then you don’t want to have to copy and paste all the configuration lines every time. Instead keep each domain’s config file small and compact. This makes it easy to see what is going on, makes it easier to change a setting across the board (e.g. increasing the cache period), and in my opinion makes it easier to build a script to do the job for you.

To this end, I thought I’d share an example Nginx WordPress configuration, split into 4 parts that allows you to easily add another domain.

1. The main Nginx server config file (nginx.conf)

user www-data;
worker_processes 4;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}

http {
  server_names_hash_max_size 512;
  server_names_hash_bucket_size 128;

  index index.php index.html index.htm;

  include       mime.types;
  default_type  application/octet-stream;

  # Configure FastCGI cache
  fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:15m inactive=15m;

  access_log /var/log/nginx/access.log main;
  sendfile   on;

  keepalive_timeout  65;

  gzip  on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  client_max_body_size       25m;
  client_body_buffer_size    256k;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

2. Handling static resources (css, js, images and so on)

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~ /favicon.ico {
  log_not_found off;
  access_log    off;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  expires       30m;
  access_log    off;
  log_not_found on;
}

location ~ /\.ht {
  deny  all;
}

3. FastCGI handling (and caching)

try_files $uri =404;

set $nocache "";
if ($http_cookie ~ (comment_author_.*|wordpress_logged_in.*|wp-postpass_.*)) {
  set $nocache "Y";
}

fastcgi_pass  unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param CONTENT-LENGTH  $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO       $fastcgi_script_name;
include fastcgi_params;

fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key       $request_method$host$request_uri;
fastcgi_cache           WORDPRESS;
fastcgi_cache_valid     200 301 302 10m;
fastcgi_cache_valid     404 30m;
fastcgi_ignore_headers  Expires Cache-Control;
fastcgi_cache_bypass    $nocache;
fastcgi_no_cache        $nocache;

4. Example domain

server {
  listen      178.79.142.110:80;
  server_name www.maanas.co maanas.co;

  error_log  /var/log/nginx/maanas/www.maanas.co-error.log;
  access_log /var/log/nginx/maanas/www.maanas.co-access.log;

  root  /home/maanas/sites/www.maanas.co;

  include static-resources.conf;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    include wordpress-fastcgi-cache.conf;
  }

  include purge.conf;
}

The 4th config file is small but pertinent. Only the information about the domain itself is in here.

Dynamically Load content in bootstrap modal

<!-- this is the table that shows the user information -->
<table class="zebra-striped">
	<thead>
		<tr>
			<th>#</th>
			<th>First Name</th>
			<th>Last Name</th>
			<th>Email</th>
		</tr>
	</thead>
	<tbody>
		<?php
		$fields = array('id','first_name','last_name','email');
		foreach ($data as $user) { ?>
		<tr data-href="/users/edit/<?php echo $user->id; ?>/1">
			<?php foreach ($fields as $field) { ?>
			<td><?php echo $user->$field; ?></td>
			<?php } ?>
		</tr>
		<?php } ?>
	</tbody>
</table>

<!-- this is the placeholder for the modal box -->
<div id="modal-editUser" class="modal hide">
	<!-- content will go here -->
</div>

<!-- this is the code that makes it all happen -->

<script type="text/javascript">
	jQuery( function($) { 
		// find all tr's with a data-href attribute
		$('tbody tr[data-href]').click( function() {
			// copy the data-href value to the modal for later use
			$('#modal-editUser').attr('data-href',$(this).attr('data-href'));
			// show the modal window
			$('#modal-editUser').modal({show: true , backdrop : true , keyboard: true});
		}).find('a').hover( function() { 
			// unbind it in case I put some a tags in the table row eventually
			$(this).parents('tr').unbind('click'); 
		}, function() { 
			$(this).parents('tr').click( function() { 
				// rebind it
				$('#modal-editUser').attr('data-href',$(this).attr('data-href'));
				$('#modal-editUser').modal({show: true , backdrop : true , keyboard: true});
			}); 
		});

		// when the modal show event fires, load the url that was copied to the data-href attribute
		$('#modal-editUser').bind('show', function() {
			$(this).load($(this).attr('data-href'));
		});
	});
</script>