Pretty permalinks and search urls for WordPress on lighttpd

To make a blog Search Engine (SE) friendly, one feasible way is to make all requested URIs permanently. It is easier for Apache web service than lighttpd, because there are more resources and technical notes on the Net. This post is about how to set up the pretty permalinks and to introduce a partial solution for the unfriendly search URIs for WordPress 3.3 on lighttpd.

First, for pretty permalinks, the reference is Using Permalinks. I want to use the Day and Name format as an SEO friendly URI. Unfortunately, except for Apache’s rewrite rules, there always exist index.php as a bridge for processing. Then the format seems

/index.php/%year%/%monthnum%/%day%/%postname%/

. To eliminate the bridge string, we can still use Custom Structure and set up rewrite ules on lighttpd.

There are ways to write proper rewrite rules within lighttpd. Based on the references: lighttpd rewrite rule for wordpress 3 and other resources from Google (keywords: lighttpd, wordpress, permalinks), the final configuration is very simple. We only need to add

1
server.error-handler-404 = "/index.php"

into lighttpd. This rule indicate that the request will be passed into the index page when the page (or URI) is not exist physically. It is depend on the truth that almost all of the requests are handled by the index page (like the posts, pages, feeds).

Second, to fix the unfriendly search URIs, we can use plugins or to add a redirect rule into lighttpd. The search URIs are formatted as

/?s=terms

and we want to replace it into

/search/terms

. It is SEO friendly and is VERY IMPORTANT when we use 404 handler as rewrite rule. For example, if there are more than one page for the search term T, the first result page’s URI is

/?s=T

where the second result page is

/page/2/?s=T

. Because the request of page 2 is not exist physically, the request will be passed into index.php and the search term (?s=T) will be ignored. The reason for why search will be ignored remains unknown.

To make the search and paged view work, we can set a redirect rule to fix it by adding

1
url.redirect = ( "^/\?s=(.*)" => "/search/$1" )

into configuration on lighttpd.

Finally, the URIs are all search engine friendly and work correctness. Enjoy it. 🙂

Script to check tunnel status periodically on Windows

This post is delayed for more than 10 months because I forgot about it. Sorry about that.

To improve the availability of the reverse-tunnel, we would check up the status periodically via Scheduled Tasks. Now, we need to write a batch script and add it into Windows.

First, the script I wrote:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@ECHO OFF

SET _SERVER={Server ip}
SET _PORT={Server port}

SET _SERVICE={Your service name installed in Windows}

:TUNNEL_CHECK
start /MIN /B netstat.exe -nav -p TCP | FIND /C "%_SERVER%:%_PORT%" | FIND "1" > nul 2>&1
IF ERRORLEVEL 1 CALL :TUNNEL_RESTART
EXIT /B

:TUNNEL_RESTART
start /MIN /B net stop %_SERVICE%
start /MIN /B net start %_SERVICE%

Actually, I’m a newbie of batch script in Windows and the script may be ugly. The script first defined some variables we will use later: the destination server’s ip and port, and the service name we installed in Windows.

The main function is TUNNEL_CHECK. We start a command without creating a new window (/B) or minimizing it (/MIN). For netstat.exe, we list all active connections numerically (-an) and filtered with TCP only (-p TCP). Then we count the lines (/C) matched to our server string by FIND. We will restart the service if there is error exist. Otherwise, exit the program.

Second, we shall add this script into Windows. Open the the configure window via: Start > All Programs > Accessories > System Tools > Scheduled Tasks. Then create a new task with the script. I’ll list the settings when editing the task.

In the Schedule tab of the task, we need two schedules. One is Run at system startup and another one is ran periodically: In my settings, the script is a Repeat task for every 1 hour and its duration is 23 hours. The tasks itself is Scheduled as Daily from 12:00 AM every 1 Day.

Generally speaking, this script will run at system start-up, and every hour of every day. To avoid any problem, I uncheck all boxes in the Settings tab. Another note is to set up the task ran as a valid user. This step will make it run at system start-up successfully. If the user is password-protected, we shall also set the password.

It’s all the details of my checking script. Enjoy it. 🙂

Suppress massively SNMP notices in message log

In my CentOS, with SNMP, it arise much more SNMP logs in message periodically. Like this:

Connection from UDP: [127.0.0.1]:xxxxx
Received SNMP packet(s) from UDP: [127.0.0.1]:xxxxx

Although the notices are harmless, it may drown out other useful messages. To suppress these messages, we can modify the parameters passed to SNMP daemon.

There is a default configuration in /etc/sysconfig/snmpd.options with a comment out option line:

# OPTIONS=”-Lsd -Lf /dev/null -p /var/run/snmpd.pid -a”

This line indicate SNMP’s log facility via syslog is for daemon (-Lsd) and it will log all levels (there are 7 levels). To suppress the notices, the configuration is modified as:

1
OPTIONS="-LS0-4d -Lf /dev/null -p /var/run/snmpd.pid -a"

. The parameter -LS is to specify the level we want to log, and we set level 0 to 4 (emergency, alert, critical, error, warning) for it. Whereas the level 5 is for notice, 6 for info and 7 for debug are not what we need.

Finally, the log is clearly as usual. 😛

Disable automount of GNOME

In GNOME Shell, automount is convenient while devices inserting into computer. But it is annoying for me because I’m booting from an USB stick. The rest partitions in USB will be mounted and popup a notification to ask what action should be taken.

To disable the auto-mounting behavior built-in Nautilus (the file manager of GNOME), we need to change the configurations via dconf-editor or gsettings with the path:

org.gnome.desktop.media-handling automount

For example, to DISABLE the behavior via command-line:

1
$ gsettings set org.gnome.desktop.media-handling automount false

. Other wise, just set as true to enable it. Enjoy it. 🙂

The reference is: How to disable automount in nautilus’s preferences.

Set MySQL default character set as UTF8 on CentOS

The default character set of MySQL installed in CentOS 5 is latin while a feasible one for CJKs is UTF-8.

mysql> SHOW VARIABLES LIKE '%character%';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+

To enable whole system UTF-8, we need to add the following codes in /etc/my.cnf:
[client]
...
default-character-set = utf8

[mysqld]
...
character-set-server = utf8

mysql> SHOW VARIABLES LIKE '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

There are lots of additional settings in my Gentoo box, but it seems we don’t need right now. Maybe it can be use later. enjoyt it. 😉

UPDATE
[120302] The parameter default-character-set = utf8 for server configuration in 5.1.61 (or 5.1) is deprecated. We should use character-set-server = utf8 instead.

Use RTL8192SE wireless card in Gentoo with kernel 3

Although I had solve this problem earlier, I forgot the solution when I built a new system today. The configuration in kernel’s menuconfig is:

1
2
3
4
5
6
7
8
9
10
Symbol: RTL8192SE [=m]
Type  : tristate
Prompt: Realtek RTL8192SE/RTL8191SE PCIe Wireless Network Adapter
Defined at drivers/net/wireless/rtlwifi/Kconfig:13
Depends on: NETDEVICES [=y] && WLAN [=y] && MAC80211 [=m] && EXPERIMENTAL [=y]
Location:
-> Device Drivers
-> Network device support (NETDEVICES [=y])
-> Wireless LAN (WLAN [=y])
Selects: FW_LOADER [=y] && RTLWIFI [=m]

But after reboot, it still not loaded. Via dmesg, it caused by firmware missing. There’s a package named rtl8192se-firmware in portage tree but it is NOT the one we need. What we need to do is to:
# emerge linux-firmware.
After this step, it works. 🙂

Use PHP 5.3 and Lighttpd on CentOS 5

The default packages built-in my CentOS box are Apache HTTP Server 2.2 and PHP 5.1. If we want to replace them with Lighttpd and PHP 5.3, we need some modification to the system. This post is an annotation of the procedure.

1. The Lighttpd package is not provided by official repository. We need to add the support via EPEL x86_64 repository (I’m using 64-bit CentOS): # rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

2. After update, we can install lighttpd: # yum install lighttpd lighttpd-fastcgi

3. Update PHP from 5.1 to 5.3, first we need to list all installed 5.1 packages: # rpm -qa | grep php and then to remove them all.

4. Install the PHP53 and related packages we need: # yum install php php-cli php-mysql.
Note: The PHP53 packages also install httpd packages which we cannot removed individually.

5. Enable fastcgi support for Lighttpd in /etc/lighttpd/lighttpd.conf as this reference: lighttpd + FastCGI in CentOS.
Note: The cgi.fix_pathinfo=1 configure is installed by default in /etc/php.d/lighttpd.ini and we can skip to set it up.

6. Finally, the most important step for some application use sessions. We need to set up the permissions by: # chown -R root:lighttpd /var/lib/php because the web server we use is not httpd. If we do not set this, some application like phpMyAdmin we raise error while login.

7. Restart the Lighttpd and enjoy it. 🙂