Category Archives: Web sites

.htaccess useful options

This is a post that I started to remind me of what the correct syntax is for the .htaccess file. But given that it has kind of grown to include a heap of .htaccess info I thought it wise to highlight the Apache official .htaccess tutorial pages. Given the information in that tutorial only use .htaccess if you really need to and preferably use the correct httpd.conf (apache2.conf or similar depending on your distro) for the commands as it will help with system performance. That said, there are a number of things that I use .htaccess for, here are some of them.

Edit: Have not read this as yet, but it seems to contain a heap more detail. http://www.askapache.com/htaccess/htaccess.html

The following came from http://www.buildwebsite4u.com/advanced/htaccess-file.shtml:

Redirecting YourSite.com to www.YourSite.com
If search engines find both www and non-www links from other sites to your site, they may treat http://YourSite.com and http://www.YourSite.com as two different websites with the same content. This means that your site can be penalized for duplicate content. Many experts recommend to set up a 301 redirect (permanent redirect) from YourSite.com to www.YourSite.com…

The code for this would be:
[php]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^YourSite.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L][/php]

Of course, using the .htaccess file and having all the correct paths enabled will help.

My default SME Server uses Apache2 and I have found that in order to use .htaccess and htpasswd effectively I had to add / enable some modules in the /etc/httpd/conf/httpd.conf file.

Setting aside the fact that SME Server needs to have specific templates updated, the Centos distro that it is based on will be similar to this (I expect).

I did enable a number of auth type modules to address a number of errors.  In each case the /var/log/httpd/error.log was where the errors were recorded.

.htaccess: Invalid command ‘AuthUserFile’, perhaps misspelled or defined by a module not included in the server configuration

was fixed by enabling

LoadModule authn_file_mod modules/mod_authn_file.so

While the error

configuration error: couldn’t check user. No user file?

needed to have the Basic Authentication module enabled in the LoadModules section of the httpd.conf

LoadModule auth_basic_mod modules/mod_auth_basic.so

needed to have Basic Authentication enabled. Note that by default Digest is enabled but given that Digest provides little security above Basic I am ok with using Basic with SSL. Note that the SSL is essential for Basic Authentication to be of use. Read the info from Apache on Basic v Digest as I think it makes it clear.

…and another error

configuration error: couldn’t check access. No groups file?:

needed to have this module added.

LoadModule authz_user_mod modules/mod_authz_user.so

at this point I could login from the browser and got a prompt to access the directory. However there was still an error in the log which stated:

/.htaccess: order not allowed here

This was because I had not completed the AllowOverride construct in the httpd.conf file.

In my httpd.conf I have sections for each virtual site / directory on the server.

Options None
Options +Indexes
Options +Includes
AllowOverride None
order deny,allow
deny from all
allow from all

in which the ‘AllowOverride None ‘ directive will ignore .htacess, while setting it to AllowOverride AuthConfig will allow it to check for a username/password it also needs the AllowOverride Limit to avoid the error about Order.

So to summarise the httpd.conf change I added a specific directory directive for the directory I am securing with htaccess/ htpasswd with the following:

AllowOverride AuthConfig Limit
order deny,allow
deny from all
allow from all

MySQL Backups from Hosted server

My hosting provider WrenMaxwell provide servers with cpanel and if you ask for it ssh access. The MySQL access is restricted to just ‘my’ database(s) and in order to include these in my remote backup solution I wanted to automate the process.

SourceForge AutoMySQLBackup is a simple but very effective script. I uploaded it, set the common settings and ran it.

Adding it as a cron job will run it daily while adding some tweaks to my website backup script on my remote server now includes the resultant backups offsite as well.

Backups using Rsync website to home server

I have remotely hosted websites and my home network. Following some recent almost disasters I figured that it was time to create some automatedbackup processes.

So I started with one remote site and my SMEServer at home and used Troy’s guide mostly.

One change was that I have alternative port settings for SSH connections, so I had to modify this line:

$ rsync -avz -e ssh remoteuser@remotehost:/remote/dir /this/dir/

to

$ rsync -avz -e ‘ssh -p 3333’ remoteuser@remotehost:/remote/dir /this/dir/

the single quotes around the ssh command separates its parameters from the rsync parameters so you can add other ssh options if needed.

Making the keys etc worked fine, but a similar setting is required when transferring via scp.

scp /home/thisuser/cron/thishost-rsync-key.pub remoteuser@remotehost:/home/remoteuser/

changes to

scp -P 3333 /home/thisuser/cron/thishost-rsync-key.pub remoteuser@remotehost:/home/remoteuser/

note that ssh uses -p (lowercase) while scp uses -P (uppercase) to change/set the port #.

I also liked Troy’s verbose ‘whoami’ line

$ echo I am now $USER at $HOSTNAME

but I also used

$ pwd

to check what directory I was actually using before continuing.

Two things tripped me up when testing just prior to the cron job configuration.

So to debug I started by stripping the authorised keys back to just the certificate and it was fine. I then added in the command string to that file and tested again only to get a password prompt.

The issue was that I had the validate-rsync script on the local host and not the remote server so I copied that file to an appropriate location on the remote host and tried again and that worked.

The second issue was with the ‘from=’ host setting in which I tried the hostname rather than ip address and rsync did not like that at and again prompted for the password. Changing it to the ip address worked. Not sure why this is as all the documentation I have read indicates that the text option should work. I’ve left it as the ip address for now.

Having done that and proved that it all worked added a new template into /etc/e-smith/templates-custom/etc/crontab called 70remotebackups and added these lines:

$OUT = “”;
# note to self as to what this is doing
$OUT .=
“05 2 * * * root” . ” /home/e-smith/files/ibays/backups/files/scripts/myremotebackup.shn”;

This is setup so that I can add another site script into the same crontab component file.Then I ran the process to include my new custom template into the crontab file.

/sbin/e-smith/expand-template /etc/crontab

No need to restart anything as cron will pick up the changes automatically.

Throughout all the testing I have already got the first initial copy of my entire website and a few changes so thats all working nicely.

The next step at some point is to prepare further copies of the same stuff in order that I can recover intermediate changes when required. But that is another day.