Resources: Linux and Apache

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

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!

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/

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]

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!

Read more

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" ' \;

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

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]

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

1 2

Let's work together