Adjusting RAM for an OpenVZ VPS

Here are commands to help adjust the RAM for an OpenVZ VPS:

64MB Guaranteed, 128MB Burstable

cid=1000
vzctl set ${cid} --vmguarpages $((64 * 64)) --save
vzctl set ${cid} --privvmpages $((64 * 128)) --save

256MB Guaranteed, 512MB Burstable

cid=1000
vzctl set ${cid} --vmguarpages $((256 * 256)) --save
vzctl set ${cid} --privvmpages $((256 * 512)) --save

512MB Guaranteed, 1024MB Burstable

cid=1000
vzctl set ${cid} --vmguarpages $((512 * 512)) --save
vzctl set ${cid} --privvmpages $((512 * 1024)) --save

1024MB Guaranteed, 2048MB Burstable

cid=1000
vzctl set ${cid} --vmguarpages $((1024 * 1024)) --save
vzctl set ${cid} --privvmpages $((1024 * 2048)) --save

Adding NFS support to an OpenVZ VPS

I still use a lot of NFS connections on my equipment and when I create OpenVZ VPS systems I need them to have access to NFS. Here are the steps I use:

From the Host Node (HN):

modprobe nfs
vzctl set 101 --features "nfs:on" --save

From the VPS:

yum -y install nfs-utils nfs-utils-lib
chkconfig --levels 345 portmap on
/etc/init.d/portmap start

Setting up an OpenVZ VPS DNS Server in CentOS 5.3

Here is my quick-and dirty way to build DNS servers using OpenVZ, CentOS and Bind/Named. This assumes you are creating a new server via OpenVZ. Although Bind is easy to admin with the configuration files recently I have found it is easier to simply admin the zones with Webmin. This setup will create the VPS, install Bind and install Webmin.

Create the VPS:

cid=1161
cd /vz/template/cache/
wget http://download.openvz.org/template/precreated/centos-5-x86_64.tar.gz
vzctl create ${cid} --ostemplate centos-5-x86_64 --config vps.basic
vzctl set ${cid} --hostname [HOSTNAMEHERE] --save
vzctl set ${cid} --ipadd [IP] --save
vzctl set ${cid} --nameserver [IP] --save
vzctl start ${cid}
vzctl exec ${cid} passwd
vzctl enter ${cid}

From inside the VPS I install bind and webmin

yum -y install bind bind-chroot bind-libs bind-utils caching-nameserver
cd /root
wget http://prdownloads.sourceforge.net/webadmin/webmin-1.480-1.noarch.rpm
rpm -Uvh webmin-1.480-1.noarch.rpm

Now I simply visit webmin’s panel and tap in any new zones (or copy over our zones from another box).

OpenVZ and virtual serving makes this time-consuming task of bringing new boxes up a simple task!

Installing OpenVZ in CentOS 5.3 (64bit)

There are a few tutorials out there on installing OpenVZ in CentOS 5.3. Here are the steps I use to install OpenVZ on a brand new installation of CentOS 5.3:

Note: this tutorial / walkthrough is for use 64 bit only

  1. Update the box via yum
  2. Install the OpenVZ Repository and grab the GPG key
  3. Install OpenVZ
  4. Install OpenVZ Controller and Quota system
  5. Configure the local system for the OpenVZ kernel
  6. Update OpenVZ’s ARP Settings
  7. Disable SELINUX
  8. Reboot
yum -y update
cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ
yum -y install ovzkernel.x86_64
yum -y install vzctl.x86_64 vzquota.x86_64

Now; we need to configure the sysctl.conf file for OpenVZ

perl -pi -e 's/net\.ipv4\.ip_forward = 0/net\.ipv4\.ip_forward = 1/' /etc/sysctl.conf
perl -pi -e 's/kernel\.sysrq = 0/kernel\.sysrq = 1/' /etc/sysctl.conf
echo -e "\n\nnet.ipv4.conf.default.proxy_arp = 0\nnet.ipv4.conf.all.rp_filter = 1\nnet.ipv4.conf.default.send_redirects = 1\nnet.ipv4.conf.all.send_redirects = 0\nnet.ipv4.icmp_echo_ignore_broadcasts=1\nnet.ipv4.conf.default.forwarding=1\nkernel.ve_allow_kthreads=1\n" >> /etc/sysctl.conf
perl -pi -e 's/NEIGHBOUR_DEVS=detect/NEIGHBOUR_DEVS=all/' /etc/vz/vz.conf

Disable SELINUX

vi /etc/sysconfig/selinux

Reboot the machine

reboot

Setting up a simple Web/Mail Server in OpenVZ

Here are the steps I continue to use over and over to build utilities/project servers in OpenVZ. Utilities servers host web apps, mailing applications, etc for my company. We usually build them as self-contained little appliance-like servers. My vision and goal is simple == better every day!

First, we create the virtual machine:

cid=1164
cd /vz/template/cache/
wget http://download.openvz.org/template/precreated/centos-5-x86_64.tar.gz
vzctl create ${cid} --ostemplate centos-5-x86_64 --config vps.basic
vzctl set ${cid} --hostname [HOSTNAMEHERE] --save
vzctl set ${cid} --ipadd [IP] --save
vzctl set ${cid} --nameserver [IP] --save
vzctl start ${cid}
vzctl exec ${cid} passwd
vzctl enter ${cid}

Now that we are in the virtual machine and away from the HN we add the REMI repo and apply the updates directly on the box.

wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
rpm -Uvh remi-release-5*.rpm epel-release-5*.rpm
/bin/rm epel-release-5-3.noarch.rpm remi-release-5.rpm
 
yum --enablerepo remi -y update
yum --enablerepo remi -y install httpd php php-devel php-pear php-gd php-xsl php-mbstring php-mcrypt php-mysql mysql

Add Postfix and switch it on via system-switch-mail

yum --enablerepo remi -y install postfix system-switch-mail
system-switch-mail

Now… customer configurations and setups…

Browser.php updated to v1.5

In the Browser.php project John pointed out (in the comments) a terrible typo in the source. I imagine no one has encountered this before as it only effects v1 or v1.5 of Internet Explorer.

Typo updated and version 1.5 of Browser.php released: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

Removing Images in a WordPress Post (Revisited)

A while back I wrote a semi-popular post on removing images from a WordPress post — today I am revisiting it. The original solution used the_content() and the output buffer to remove the images out of the post. Now that I have used WordPress a bit longer and candidly had to use the solution again and thought “what was I thinking” I thought I would share the cleaner solution:

.
.
.
<?php echo preg_replace('/<img[^>]+./','',get_the_content()); ?>
.
.
.

Browser.php updated to v1.4

I released v1.4 of the Browser.php project today to address a few features suggested by readers. This new release adds support for GoogleBot, Yahoo! Slurp, and the W3C Validator.

Check out version 1.4: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

Browser.php updated to v1.3

Based on a comment on the Browser.php page I added support for the iPod today. I opted to place both the iPhone and iPod as browsers even though they are likely using the same instance of Safari. Additionally I added support for the iPod and iPhone as platforms. Thanks for the comments; enjoy!

Backup an OpenVZ VPS

There are a lot of ways to backup an OpenVPS without powering them down. I have two critical VPS systems both operating phone/PBX apps (asterisk) which I need to backup and I cannot get them to backup correctly while powered up (driving me nuts). So weekly (early on Sunday mornings) I backup them up by quickly powering them off; backing them up and powering them back up.

Side Note: Yes, yes, begin the hate mail: I power down the boxes; which creates all of the 65sec of down time. If anyone is calling my office at 2AM on Sunday morning and can’t leave a message please email me and I’ll adjust our backup schedule!

The script also pushes the backup images to a mount at /nfs/backup (a backup NAS system we have in place)

Here is how I do it:

#!/bin/sh
 
if mount|grep -q ' nfs ' && df -T | grep -w nfs | grep -w "\/nas\/backup" | grep -q -wv "100%";then
 
        # date in YYYYMMDD format
        today=`date +%Y%m%0e`;
 
        echo -e "Stopping VZ 105"
        /usr/sbin/vzctl stop 105
 
        echo -e "Dumping VZ 105"
        /usr/bin/vzdump --suspend 105
 
        echo -e "Starting VZ 105"
        /usr/sbin/vzctl start 105
 
        echo -e "Compressing Output"
        /bin/gzip -9 /vz/dump/vzdump-105.tar
 
        echo -e "Backing up - moving file to NAS"
        /bin/mv /vz/dump/vzdump-105.tar.gz /nas/backup/__hostname__here__/vzdump-105-$today.tar.gz
 
else
        echo Error: the NFS mount for the backup NAS does not appear to be correct
fi