Tuesday, February 21, 2012

Redirect non-www URLs to www URLs using a .htaccess file

Create a notepad file and save it as .htaccess if you do not already have an existing one.

Update the .htaccess file with the following code and save; be sure to replace domain.com with your domain name.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain.com [NC]

RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Upload the .htaccess file via ftp to the site root and now your traffic will be redirected to www.domain.com.

Configure NFS Server Shares

NFS, or Network File System, is a protocol for sharing and mounting remote file systems over a network.
Installation

To run an NFS server on Redhat or CentOS Linux, the systems package 'nfs-utils' must be installed. The 'yum' package manager can be used to ensure this is installed.

yum install nfs-utils
Configure Shares

NFS shares are created via the /etc/exports configuration file. To create a share you must specify a path to share, as well as a list of hosts to grant access and the type of access they should have. The path to share must be a full system path, and the list of hosts can be specified as a single host (IP, FQDN, or hostname), with wildcards (*.domain.com), IP networks (1.2.3.4/24), or netgroups.

For example, to share the directory '/data' with the host 10.10.1.5 (read-only) and the hosts within 10.10.1.15/29 (read-write), the following line would be added to /etc/exports.

/data 10.10.1.5(ro) 10.10.1.15/29(rw)

The list of access hosts must be separated with spaces, and no space must exist between the host address and the opening '(' of its options. If no options are specified, the host will have read only access. If options are specified without a host preceding it, they will be the share's default.

For a full list of options, please refer to the exports man page.

man exports
Starting the NFS Server

The NFS server is controlled with the service init script '/etc/init.d/nfs', or through the 'service' command. However before nfsd can run, the 'portmap' service must be running as well.

service portmap start
service nfs start

The NFS server will now be running, and shares (or 'exports') can be mounted by remote hosts that are given access. If the exports file is modified after the service is started, you can apply them with the command

exportfs -r

Also be sure to use the 'chkconfig' command to add both services to the system runlevel to run on startup.

chkconfig portmap on
chkconfig nfs on
Firewall notes

If your NFS server is behind a firewall (hardware or iptables), relative to the connecting hosts, the port "2049" must be opened for TCP and UDP.

Mounting NFS Shares

Network File System shares, or NFS exports, are easily mounted to Linux servers. Before an NFS export can be mounted, the nfs-utils package must be installed.

yum install nfs-utils

Additionally, the 'portmap' service must be running and enabled.

service portmap start
chkconfig portmap on

To mount the share, you'll need the following information.

The hostname, domain name, or IP of the NFS server.
The full path of the export on the server.

Also, be sure that the NFS server allows access from the host for the desired export.


Manually Mounting

Mounting NFS shares to the client is done using the mount command, in a similar fashion to mounting local file systems.

mount -t nfs4 -o options host:/remote/path/ /local/mount/point

For example: To mount an export '/var/data' on host '10.10.1.10' to the local directory '/remote/data', the following command would be used.

mount -t nfs4 10.10.1.10:/var/data /remote/data

The '-t nfs4' can usually be dropped completely. You'll now be able to access the remote directory under '/remote/data' as though it were part of the local filesystem.

For information on the various mount options, which can improve transfer performance and permissions, refer to the nfs man page.

man nfs
Mount via fstab

NFS mounts can also be specified within the /etc/fstab file for ease of mounting, and mounting on system startup. The format is the same as with local mounts.

host:/remote/path /local/mount/point nfs4 defaults 0 0

For the previously used example export, this would be:

10.10.1.10:/var/data /remote/data nfs4 defaults 0 0
Nginx is a lightweight, fast, and efficient web and proxy server. It is one of the fastest static conent web servers available, and can also be deployed to delivery dynamic content through a FastCGI interface. In addition, nginx is a very powerful reverse proxy (frontend) server and very capable software load balancer.

For a full listing of features, please refer to http://nginx.org.
Installation

Installation of the latest stable release of nginx can be done easily with the EPEL (Extra Packages for Enterprise Linux) package repository. To use this repository, execute the following as the root user:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

Once the EPEL repository is in use, nginx can be easily installed through yum.

yum install nginx
Configuration

Nginx uses the configuration file /etc/nginx/nginx.conf, which can be edited using nano. Virtual hosts (web sites) are configured in this file with 'server' code blocks, which are located under the main 'http' block. The default 'server' block will listen on port 80, and has a document (web root) of '/usr/share/nginx/html'.

To help explain configuration of the server block, below is a very basic entry.

server {
listen 80;
server_name www.domain.com domain.com;

location / {
root /var/www/domain.com/html;
index index.html index.htm;
}

}

listen: Specifies the port on which this virtual host listens.
server: Lists the host headers for the site.
location /: Specifies how to handle requests under the location '/', the site root.
root: The document root for the site.
index: An ordered priority list of default documents.

For further details and examples on nginx's configuration, please refer to their offical wiki page and core documentation.

Note: If your server currently is configured with another web server, you'll likely need to have Nginx listen on a port other than 80. This is done simply by editing the 'listen' setting in the default server block, as well as any additional server blocks that are created.

To test your configuration, execute the following and it will report on any errors.

/usr/sbin/nginx -t
Starting and Testing

Once you've setup a working configuration, you can start the nginx server.

/etc/init.d/nginx start

If using the default document root (/usr/share/nginx/html), visiting your server's IP in a browser should yield the default server page.

nginx_test

Now that Nginx is running successfully, you'll want to be sure it's added to the default run level so it will start automatically at boot time.

chkconfig nginx on
The .htaccess file is pretty handy as it can also allow you to set various PHP flags that you may not want enable for all of you websites and in turn will let you run just about every PHP flag or directive on a one on one website basis.

You will need to log into your Linux server directly using SSH. If you are not familiar on how to do this, we have articles on how to implement this on both Windows and Mac;

Windows - http://www.hosting.com/support/linux/general/sshwindows
Mac - http://www.hosting.com/support/linux/general/sshmac

To create the .Htaccess file, you meerly need to create a text file with any Linux editor. The file must be called .htaccess and it must exist in the root directory of the website you want override the Php global directives for.

The format of the .htaccess is simple, however you will want to comment out exactly what flag or directive you are enabling for the site. An example of this would be as seen below;

#Turn register goabls of
php_value register_globals off

#Php max upload size
php_value upload_max_filesize 12M
php_value post_max_size 12M

#Enable another version of php if you have two installed (Enable Php 5)
AddType application/x-httpd-php5 .htm .html .php

Make sure you save the file when you are done editing it.

Just about any directive or flag can be added to the .htaccess file so you can commently customize the php functions of the website instead of changing it globally in the php.ini file. For a list of what you can use, please review the PHP manual at http://php.net/manual/en/index.php.
Using a .htaccess file will grant you greater control over your website, such as safe guarding it from hacking attempts or even keeping out spammers who may frequent your website. A great feature of using a .htaccess file is the ability to populate it with single IP addresses or entire IP ranges, effectively blocking those IP's from being able to access your server. This article will explain how to implement this via the .htaccess file.

You will need to log into your Linux server directly using SSH. If you are not familiar on how to do this, we have articles on how to implement this on both Windows and Mac.

To create the .htaccess file, you need to create a text file with any Linux editor. The file must be called .htaccess and it must exist in the root directory of the website you want to deny access to.

In the file, there is a specify format you must adhere to. To block both single IP's and IP ranges, you must include the following;

order allow,deny - The rule set
deny from 192.168.1 - IP you want to block
deny from 24.0.0.0/23 - IP Range you want to block

Please note to block an IP Range, you must know the subnet.

You can also specify a deny all and allow rule set as well;

order deny,allow - The rule set
deny from all - Deny access from all IP's.
Allow from 192.12.4.1 - IP you want to allow.

An example of a file blocking IP address 1.2.3.4 and a subnet 2.0.x.x is below:


order allow,deny
allow from all
deny from 1.2.3.4
deny from 2.0.

Monitoring Bandwidth with VNstat

VNstat is a bandwidth monitoring tool that will let you monitor your bandwidth and provide daily, weekly and monthly tracking metrics.

To install and setup Vnstat, you will need to follow the steps in this article.

Log into your linux server and type the following command.wget http://humdi.net/vnstat/vnstat-1.10.tar.gz

Next to uncompress the file you will need to run the following command. tar -zxvf vnstat-1.10.tar.gz

Now that you have the file uncompressed, you will need to install Vnstat. To do this, you will need to navigate to the VNstat directory and run the following command.
make & make install

Now that it has been installed, you will first need to run "vnstat --iflist" on your server, so you know the name of your network adaptors. vnstat --iflist

You will now be presented with the any available adaptors on your server. Available interfaces lo eth0 sit0

Now that you know what adaptors you have on your server, you will need to tell VNstat to create a small database for that adapator. To do this, simply type /usr/bin/vnstat -u -i then the adaptor name. So for our example, we would run the following /usr/bin/vnstat -u -i eth0 If you have multiple adaptors, you will need to do this for each adaptor. However you do not have to if you only want to monitor specific adaptors.

Next, make sure that Vnstat restarts whenever you reboot your server. To do this, add the service to your server's "Chkconfig" list by running the following command.

chkconfig --add vnstat
chkconfig vnstat on

Now you can access Vnstat by logging into the server at anytime and typing "vnstat". Doing so will give you a realtime status of your bandwidth.