Resources: Linux and Apache
Diagnose VirtualHost rules on Apache
23 July 2014
When diagnosing problems with Apache and VirtualHosts, especially when using SSL on a shared IP address, it makes life a lot easier to be able to show all the calculated priorities and _default_ servers. Running this command will show all:
apache2ctl -S
Removing www from your site
26 July 2013
Personally I prefer web URL's to exclude www, and so I implement the following code on all project's .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.jamesbarnsley.co.nz$ [NC]
RewriteRule ^(.*)$ http://jamesbarnsley.co.nz/$1 [R=301,L]
Another reason this is important is that www.yoursite.co.nz and yoursite.co.nz are treated as two unique websites. Any SEO rankings for yoursite.co.nz does not apply to www.yoursite.co.nz!
Rewrite based on domain
24 July 2013
Recently I needed to redirect traffic when the domain was NOT www.website.co.nz. This is often handy if you have several domains hinged off one virtual host.
RewriteCond %{HTTP_HOST} !^www\.website\.co\.nz$ [NC]
RewriteRule .* http://%{HTTP_HOST} [R=301,L]
Often you'll need to transfer a file from your webserver to your local environment. This can be a bit tricky if your local environment is a headless webserver (or you just don't like FTP). With this command you can transfer your files over an SSH connection, when your remote server is running a non-standard SSH port.
rsync -av --progress --rsh='ssh -p1234' [email protected]:/remote/file /local/folder/
Avoid locking your web-uploaded files
29 May 2013
Out of the box, most webservers will run Apache processes as user 'www-data', which has it's own group. If you then try to move/delete/edit any files created by this user via FTP or SSH, you are likely to be denied access. This often happens when files are uploaded with a CMS. Login via SSH as root and drop in this command which will add your user to the www-data group. This will allow your user to edit files that are shared within the www-data group. Win!
sudo usermod -a -G www-data [yourusernamehere]
Running uTorrent from command line on Ubuntu
23 January 2013
You can relatively easily set up uTorrent to run as a webUI from your webserver. This requires Ubuntu 9+ and a download of uTorrent. Make sure you also install PeerGuardian!
Convert file extensions to lowercase
10 December 2012
Having issues with upper and lowercase file names? Use this script to programatically convert all extensions to lowercase. It will affect all files within the current folder.
find . -name '*.*' -exec sh -c ' a=$(echo {} | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "{}" ] && mv "{}" "$a" ' \;
Track down the largest files and folders with 1 command
24 October 2012
Struggling to track down where your hard drive or web server space is going? This command prints out the 20 largest files and folders:
du -a /home | sort -n -r | head -n 20
Htaccess temporary redirect while undergoing maintenance
4 October 2012
Developing a site and you want to have a nice temporary page for visitors? Drop in this code at the top of your .htaccess file and make your pretty 'Coming Soon' page in the root called 'offline.html'.
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.co\.nz [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://example.co.nz/offline.html [L,R=302]
Extracting and Compressing Tar.gz via command line
3 October 2012
With explaining the inner workings of all the switches, you can extract / compress .tar.gz archives easily with the following codes:
Compress .tar.gz
$ tar cvzf archive_name.tar.gz dirname/
Extract .tar.gz
$ tar xvfz archive_name.tar.gz