Git Deployments 22 Aug 2013

For the past year or so I have been using rsync to deploy my sites, which has worked perfect for me until recently.

I have recently switched to using a simple post-receive hook using a bare repo on the server. I find this works a lot better for me now as it means all I need to do is “push”.

First you need to setup the repo on your server, I normally set it up in /var/git/site.com for example. This will be a bare git repo, so it will not contain any of your own code. To set this up simply run the following inside your /var/git/site.com folder:

git init --bare

This will initialize the repository, allowing you to push your code to it.

The next step is to tell git where to check your code out too. I usually use /var/www/site.com. For this, I use a very simple post-receive hook that checks the code out to the folder, and runs composer install. You would place the following in /var/git/site.com/hooks/post-receive:

WORKING_DIR=/var/www/site.com;
GIT_WORK_TREE=$WORKING_DIR git checkout -f;
cd $WORKING_DIR && composer install -o;

One thing to remember is to make the file executable by running the the following command:

chmod +x /var/git/site.com/hooks/post-receive

Now that the repo and post receive hooks are setup, we need to setup your development machine’s git remotes. The easiest way to do this is:

git remote add production user@site.com:/var/git/site.com

This will mean that you can simply run the following command to deploy your website:

git push production master

You can obviously add anything else you wish to the post-receive hook, such as clearing cache, flushing cdn etc. I currently have most of mine set to restart php-fpm after composer install.

service php5-fpm restart

If you have any questions, or i have missed anything, then please let me know below.


Route all .dev URL's to 127.0.0.1 18 Apr 2013

If you’re like me, you always forget to edit the /etc/hosts file when you want to view a local site.

To get around this, you can use the following script to route all your .dev urls to 127.0.0.1 (or any other IP). So you could go to “romhut.dev” and that would load from 127.0.0.1.

brew install dnsmasq
mkdir -pv $(brew --prefix)/etc/
echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'

If you wish to use another IP, only change the first 127.0.0.1, otherwise it will look for a name server on the host.


Select A Character in a String with PHP 01 Feb 2013

I love finding little thing in PHP that i did not know before. They are generally not important, or advanced but it’s still very interesting. Here are a few i found recently:

Select a character with a string without using substr etc:

1
2
3
<?php
$string = '1234';
echo $string[1]; // 2

Another one i found was “__halt_compiler”, it’s one of the ones i have no real use for but interesting nonetheless. It basically stops the PHP compiler so you can just output HTML from then on without any more server resources being used.

1
2
<?php
__halt_compiler();

I shall no doubt find a lot more of these, and i will have no use for any of them more than likely.


Google's Mod_Pagespeed 23 Oct 2012

“mod_pagespeed speeds up your site and reduces page load time. This open-source Apache HTTP server module automatically applies web performance best practices to pages, and associated assets (CSS, JavaScript, images) without requiring that you modify your existing content or workflow.”

When I was in the process of setting up my new VPS, I decided I would try out mod_pagespeed (conveniently it had just came out of beta a few days earlier). It turns out, its very easy to setup and very easy to use.

First of all, you need to download the correct package over at:

https://developers.google.com/speed/docs/mod_pagespeed/download

Once you have downloaded it, its as simple as running:

dpkg -i mod-pagespeed-*.deb
apt-get -f install
Or for CentOS you run
yum install at  # if you do not already have 'at' installed
rpm -U mod-pagespeed-*.rpm

The default install includes things such as the minification of css and js files and also the combining of css files (js can be enabled too). Inline images, css, and js is also enabled by default, which allows these types of files to be included in the main HTML layout if they are small enough to justify doing so. This will save requests to the server for more files.

As well as the defaults, i decided to enable a few none default options in the config file:

/etc/apache2/mods-available/pagespeed.conf
The ones i decided to enable where the following:
ModPagespeedEnableFilters inline_preview_images     # Load low quality images before loading high quality ones
ModPagespeedEnableFilters resize_images             # Resize the images before serving them (These are then cached in Varnish)
ModPagespeedEnableFilters insert_image_dimensions   # If any image does not have width, height set, then insert them
ModPagespeedEnableFilters combine_javascript        # Combine javascript files into one file
ModPagespeedEnableFilters collapse_whitespace       # Minify the HTML output of the page
ModPagespeedEnableFilters resize_mobile_images      # Resize smaller images on mobile
ModPagespeedEnableFilters remove_comments           # Remove comments from JS,CSS and HTML files.

Some are not 100% ready for production but the risk value of them is quite low.

From initial tests, it seems to have reduced the page load by about 1/3. Which on a small site may not be a lot, but on a big site with a lot of images etc, this could be a huge saving in bandwidth costs, and also a lot nicer experience for the user.


Copy SSH Keys to Server 12 Oct 2012

When you wish to login to a server without having to enter a password, all you need to do is login with SSH. However I do not know why, but it took me hours to find a simple way to do this a few years ago.

First of all, you need to generate your SSH keys using the following:

ssh-keygen

From this point, i generally just hit enter and use the defaults. You can set a password, but that can get annoying.

Now you need to copy the SSH keys to the server you wish to login to. I have found many ways to do this, but none have been as simple as the following:

ssh-copy-id username@server.com

You will then be asked for your password for the last time. Once this has complete, you will be able to login to the server using just:

ssh username@server.com