Saturday, June 24, 2006

Sub-Subdomains using Mod_rewrite and Wildcard DNS

Domain GlobeIn an internet-savvy world, subdomains can play a big role in user awareness. Descriptive subdomains are easy to remember, strengthen the credibility of a website and encourage a visitor to bookmark it.


Quick example:
a) http://tomcat.apache.org
b) http://www.jguru.com/faq/home.jsp?topic=Tomcat
Which one would you prefer?

Even better, subdomains and sub-subdomains can be pointed to dynamic content using Apache's mod_rewrite and Wildcard DNS entries.
Called: http://topic.domain.com
Served: http://www.domain.com/generator.php?topic (invisible)
The visitor requests http://topic.domain.com while the actual content is dynamically served by a PHP script in the background.

Requirements
To realize this on your server,
- a DNS server with support for wildcard DNS entries (eg. BIND) has to be available,
- this DNS server must be the authorative DNS server for your domain,
- Apache with mod_rewrite has to be installed.

Authorative Nameserver for your domain
This means that you need to setup a NS entry and point it to your Nameserver. The corresponding line for your zone file is

domain.com NS ns.domain.com
Add a Wildcard DNS record to your Nameserver
Wildcard DNS records match all requests for domain names without a record. You can set up a wildcard record to match all subdomains of your domain like this:

*.domain.com. A 1.2.3.4
where 1.2.3.4 is the public IP address of your webserver. Be sure to include the last dot in your FQDN.

Configure Apache Wildcard support
Assuming your Apache config uses vHosts, your new configuration would look like this:

<virtualhost>
ServerName domain.com:80
ServerAlias *.domain.com
...
..
</virtualhost>

The ServerAlias statement enables Wildcard support for your domain. From now on, your Apache vHost will react on non-existent subdomains.

Configure redirects using mod_rewrite
To activate proper subdomain redirects, save the text below, put it into a .htaccess file and upload it into the the http root directory of your domain.

RewriteEngine On
#prevents rewrite loops
RewriteCond %{REQUEST_URI} !^/generator.php?(.*)?$
# match the subdomain
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$ [NC]
# append the path
RewriteRule ^(.*)$ /generator.php?%2/$1 [QSA,L]

Test your redirect
This PHP code captures the name of your subdomain. You can save it and upload it as generator.php to test your subdomain setup.
<?php $server_host = explode('.', $_SERVER['HTTP_HOST']); $subdomain = strtolower($server_host[0]); echo 'Subdomain name is: ' . $subdomain; ?>
Visit an imaginary subdomain, eg. http://test223.domain.com.
If everything works, you'll see "test223" on your screen which is dynamically generated by generator.php.

Same setup for Sub-Subdomains
If you plan to use sub-subdomains instead, you can apply this guide with a couple of changes to an existing subdomain:
- Setup an authorative NS for your Subdomain (sub.domain.com NS ns.domain.com)
- Add a wildcard DNS record for your Subdomain (*.sub.domain.com. A 1.2.3.4)
- Add a Wildcard alias in the Apache vHost of your existing subdomain (ServerAlias *.sub.domain.com)
- Change the mod_rewrite code accordingly

Thursday, June 22, 2006

Tuning a vServer/VPS for performance

Virtuozzo Logo

vServers are virtual "stand-alone" servers on one shared, physical server. On a vServer, you enjoy the privilege of unrestricted (root) access at a fraction of the cost of a dedicated server. These virtual servers share the actual hardware resources with each other. The concept is often referred to as "VPS (Virtual Private Server) architecture".


On the downside, VPS performance often suffers because of a bad default configuration, leading the whole idea ad-absurdum.
This page deals with fixing performance issues by changing the system configuration underneath Virtuozzo, as you will often find yourself limited by Virtuozzo hard limits set by your web host. I assume a standard "Virtuozzo plus Plesk" standard configuration in this tutorial. The screenshots stem from a "Virtual-Server L" VPS plan hosted at 1und1.de, the german division of 1&1.


Follow-up:
In the meantime, 1und1 fixed the "privvmpages" alarming issues by raising the memory limit to 100,000.



From a performance point of view, there are five VPS resources which tend to become problematic:
- memory (RAM) (mainly "privvmpages", "shmpages" and "vmguarpages" parameters),
- CPU ("cpuunits", "cpulimit" in Virtuozzo),
- Processes ("numproc"),
- Sockets ("numtcpsock" and "numothersock" parameters) as well as
- Disk usage ("diskspace").

Analysis

To get a first idea what might be causing performance issues, you can log into the Virtuozzo panel and check your vServer "Resources" tab. I suggest checking the Quality of Service alerts first of all as they provide a good, first hint.

Quality of Service Alerts

Quality of Service Alerts
As a general rule of thumb: Black alerts are "deadly" and should be your first action point. Yellow alerts can occur under heavy load. If they tend to show up regularly, look into it.
In this case, we can see a huge amount of privvmpages alerts which means there is a memory issue. This is a very common problem on a VPS since most companies have very strict limits on memory usage.
Once you have identified a first issue, a further look at the server resources is required to evaluate the degree of the problem.


Action

Memory
If RAM is a problem, you can stop all unnecessary System Services and disable their Autostart in your control panel.
You can configure server without a GUI? Deactivate Plesk.
You don't need any fileserver abilities? Disable smb.
No database required? Disable Mysql/PostgreSQL.
And so on.
You can also change the Apache tuning settings in /etc/apache2/server-tuning.conf and deactivate Keep-Alive using the "KeepAlive Off" option to save some memory at the cost of performance.
Sometimes, this is not enough. In my case, the virtual server was still struggling just running Joomla on Apache,PHP,MySQL.
On this specific 1und1 vServer, logging in as root and changing the available memory for MySQL by editing the /etc/my.cnf turned out to be successful. Furthermore, you can find additional memory intensive processes using the "ps -aux" command.

CPU
Key questions:
Do you run any CPU intensive scripts, eg. search engine scripts, which are called frequently (via Cron or by actual users)?
Can you implement a caching mechanism?
Is it possible to optimize the scripts?
Would it be a viable solution to outsource certain scripts or programs to another server?

Processes
On a Linux server, use the command "ps -aux" to view all system processes. On a Windows server, use the task manager.
A frequent reason for hitting the maximum allowed number of processes on a VPS is an Apache misconfiguration. Apache launches more child processes than the "numproc" parameter allows, thus a new process cannot be created anymore.
To fix this common issue, log into your server and edit your Apache config file (/etc/apache2/server-tuning.conf mostly) to allow less processes.

Sockets
Check for possible malware such as DDOS scripts.
Change the connection timeout and keep-alive parameters so old connections can be closed in-time to make room for new ones.
Do not use too many services at once.

Disk usage
Why are you running out of disk space? You shouldn't! Lack of space can bring your complete server to halt. Find the files in question, delete them and make sure they never come back.
If log files are filling up the server, implement a faster log rotation using file compression. I would not suggest disabling logging to ensure that debugging is still possible.

Restart the relevant processes and you'll have a server which is "all green", even under load.
Ordinary VPS usage

Thursday, June 15, 2006

Joomla: Include PHP scripts on content pages

Joomla CMS LogoJoomla, derived from the popular Mambo, is a fully fledged open-source content management system based on PHP and MySQL. And despite its flexibility and powerful administration, it is a setup nightmare for the average webmaster.

Unlike pseudo-CMS solutions like Postnuke, it is geared towards larger projects and does not allow "Hack and Slash" PHP code to be inserted into content pages on-the-fly. Joomla does include a wrapper module which allows for the inclusion of "foreign" content originating from other websites - but only via the use of iFrames. Out of the box, Joomla cannot include PHP scripts like forums on content pages.

The options are limited to
- writing your own component/module,
- hacking the Joomla core or
- installing a mod.

If it was a larger project, a dedicated component for each use would be a viable solution. Hacking the CMS core to allow the integration of PHP code is a very bad idea on the other side because of future compatibility problems.
A mod to distinguish PHP code from actual content is usually the best idea.

kl_php listens for signal words in the actual content fields and processes anything in between {kl_php} brackets as PHP.
You can download it and install it straight from the ZIP file via the admin interface:
Install New Mambots

After the installation, you have to activate (publish) the mambot.
Install New Mambots
kl_php has to be the first Mambot in order or the PHP output will come up be above or below the actual content. Parameters can be set according to personal preference.

From now on, you can include PHP code on Joomla pages using
{kl_php}
include("php_file.php");
{/kl_php}

The mod can also be used to catch and process form input via PHP as explained over at MamboServer.com.

Furthermore you should deactivate the Joomla WYSIWYG editor when including PHP code because HTML entities can cause problems.

Friday, May 26, 2006

test

testt

 
eXTReMe Tracker