FreeBSD: Ports and Disk Space

Using the Ports Collection within FreeBSD will use up disk space over time. After building and installing software from the ports, you should always remember to clean up the temporary work directories using the make clean command. You can sweep the whole Ports Collection with the following command:

# portsclean -C
You will accumulate a lot of old source distribution files in the distfiles directory over time. You can remove them by hand, or you can use the following command to delete all the distfiles that are no longer referenced by any ports:

# portsclean -D
Or to remove all distfiles not referenced by any port currently installed on your system:

# portsclean -DD

EC2 now has FreeBSD

EC2 now has FreeBSD

Good news. EC2 now has FreeBSD. Though with some quirks…In order to work with EC2, the FreeBSD AMIs are slightly modified from what you would have immediately after installing FreeBSD from a release CD:

  • The network is configured and the sshd daemon is enabled, for obvious reasons.
  • SSH logins as root are enabled, but root’s password is starred out (i.e., password logins as root will fail until a password is set).
  • The first time an AMI is booted, a script fetches the public half of an SSH keypair from EC2 and arranges for root logins using that key.
  • Every time the the AMI boots, it prints the SSH host keys to the console in the same format as Amazon Linux AMIs do.
  • The FreeBSD kernel is located on an ext2 filesystem, in order to allow Amazon’s user provided kernel functionality to work. (Everything else is on a single UFS2 filesystem.)
  • AMIs published from 2010-12-29 onwards have /etc/motd text with some comments about FreeBSD/EC2 and a link to this page.
  • AMIs published from 2010-12-29 onwards by default report kernel panics (including backtraces) to the FreeBSD/EC2 maintainer to assist him in identifying and fixing bugs. 
At the present time, FreeBSD/EC2 should be considered an experimental platform not yet ready for production use.

The following AMIs are available (though there are additional obsolete AMIs…):

FreeBSD version us-east-1 us-west-1 eu-west-1 ap-southeast-1
FreeBSD 9.0-CURRENT @ 2011-01-04
For t1.micro instances only! ami-8cce3fe5 ami-61683824 ami-e388bd97 ami-36bdc364
FreeBSD 8.2-RC @ 2011-01-08
For t1.micro instances only! ami-4a15e423 ami-376e3e72 ami-f77e4a83 ami-3eb6c86c

Note: Apparently FreeBSD will only currently work on t1.micro instances..

Resources
http://aws.amazon.com/ec2/
http://www.freebsd.org/
http://forums.freebsd.org/showthread.php?t=254

PHP: Finding the binary

The other day I was stumped, albeit rather briefly, by something I should not have been. I was scripting out some CRON jobs for a CRM system when I found out I could not locate where the hosting company had put the PHP binary. Now that is something you usually do not have to think about so it can easily fall into the ‘been a while’ category.

So if you ever find yourself asking, where is the PHP binary? or just where is PHP? Here are some steps to follow.

Telnet/Shell
If you have shell access via telnet, log in, and type “which php” and you should be given the location to use.

If you don’t have shell, try one of the following:

Create a php page with just the following: and then call the page, this should tell you the location of PHP.

echo system('which php');

If that does not work, though it should, try checking one of the paths listed under path in the ENVIRONMENT section of the output with this.Take note though that this will give you the environment path so you will still need to add /php to it…

echo phpinfo();

Getting Apache22 on FreeBSD 8.1 to work with DHCP

Apache likes to have a static IP address to work with and that is not always something you can guarantee to have. An easy way to get around this is to add the following two files to /etc

Firstly add a file called “dhclient-exit-hooks” with the following

#!/bin/sh


APINCDIR=/usr/local/etc/apache22/Includes
APPIDFILE=/var/run/httpd.pid


if [ ! -z “$new_ip_address” ]; then
    # First, configure the /etc/hosts file so Apache can start
    HOSTNAME=`grep ^hostname= /etc/rc.conf | awk -F = ‘{ print $2 }’ | sed -e ‘s/”//g;’`
    if [ ! -z “$HOSTNAME” ]; then
        echo “$new_ip_address $HOSTNAME” > /etc/hosts.dynamic
        cat /etc/hosts.boilerplate /etc/hosts.dynamic > /etc/hosts
    fi


    # Next, we’ll configure the Apache web server, if it has been installed
    if [ -d $APINCDIR ]; then
        echo “ServerName $new_ip_address” > $APINCDIR/server-name.conf


        if [ -f $APPIDFILE ]; then
            kill -HUP `cat $APPIDFILE`
        fi
    fi
fi

Secondly add a file as referenced above called “hosts.boilerplate” with the following content:
# $FreeBSD: src/etc/hosts,v 1.16 2003/01/28 21:29:23 dbaker Exp $
#
# Host Database
#
# This file should contain the addresses and aliases for local hosts that
# share this file.  Replace ‘my.domain’ below with the domainname of your
# machine.
#
# In the presence of the domain name service or NIS, this file may
# not be consulted at all; see /etc/nsswitch.conf for the resolution order.
#
#
::1 localhost localhost.my.domain
127.0.0.1 localhost localhost.my.domain
#
# Imaginary network.
#10.0.0.2 myname.my.domain myname
#10.0.0.3 myfriend.my.domain myfriend
#
# According to RFC 1918, you can use the following IP networks for
# private nets which will never be connected to the Internet:
#
# 10.0.0.0 –   10.255.255.255
# 172.16.0.0 –   172.31.255.255
# 192.168.0.0 –   192.168.255.255
#
# In case you want to be able to connect to the Internet, you need
# real official assigned numbers.  Do not try to invent your own network
# numbers but instead get one from your network provider (if any) or
# from your regional registry (ARIN, APNIC, LACNIC, RIPE NCC, or AfriNIC.)
#
Then reboot, restart service, whatever your preference is, and test it out.

Creating symbolic links in FreeBSD

Sometimes when you FTP into a FreeBSD environment you need to bounce around to directories acroos the file system. One way to simply this process when working in a FTP client is to create a symbolic link as a file. It is really easy.

To go from point A to point B where point A is a click able file and B is a target folder just run:

ln -s /path/to/folder/B /where/you/want/the/file/to/exist/B

How to install MemCached with Apache, PHP, and MySQL on FreeBSD

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.

Notes

  • There are definitely steps in this that can be changed, extended, other, but these will get you what you want in a good learning fashion. And learning is key!
  • You can just as easily install memcached on an IIS server.
  • Yes you can download binaries with pkg_add etc. but building will help you literally see what is going where. One just has to make sure that some housekeeping is performed during the build process. This is done with the clean parameter in the make command.
  • This appears to be quite a popular post. Please keep in mind that version numbers change so update the strings below.
To Start

First off build a FreeBSD box. It’s easy, fast, and best of all free once you have the hardware – real or virtualized. Don’t forget to install the ports. You’re going to need them going forward.

For dependency issues we’re going to install all the items in this order:

  1. MySQL Server 5.x
  2. Apache 2.x
  3. PHP5
  4. PDF-Lite
  5. PHP Extensions
  6. MemCached

1: MySQL

Move to the ports folder, specify some build parameters that will helps speed things up a tad.
cd /usr/ports/databases/mysql51-server
make BUILD_OPTIMIZED=yes BUILD_STATIC=yes
make install clean
Edit /etc/rc.conf and add mysql_enable=”YES”
Apply the configuration file: cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf
Assign the master password: /usr/local/bin/mysqladmin -u root password ‘new-password’
Start MySQL: /usr/local/etc/rc.d/mysql-server start

2: Apache

cd /usr/ports/www/apache22
make install clean
Edit /etc/rc.conf and add apache22_enable=”YES”
Edit /boot/loader.conf and add accf_http_load=YES

3: PHP

cd /usr/ports/lang/php5
make install clean  !!In the popup at the least check off “Build Apache Module”
4: PDF Lite
fetch ftp://ftp.swin.edu.au/gentoo/distfiles/PDFlib-Lite-7.0.2.tar.gz
sudo mv PDFlib-Lite-7.0.2.tar.gz /usr/ports/distfiles/

5: PHP Extensions
cd /usr/ports/lang/php5-extension 
make install clean !!Confirm that sockets is selected.
Apply the configuration file: cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini
Enabling Apache to run PHP
Edit the Apache configuration file /usr/local/etc/apache22/httpd.conf and add
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Change DirectoryIndex index.html to DirectoryIndex index.php index.html
Start Apache /usr/local/etc/rc.d/apache22 start
6: MemCached
cd /usr/ports/databases/pecl-memcache
make install clean
cd /usr/ports/databases/memcached
make install clean
Edit /etc/rc.conf and add memcached_enable=”YES”
Start MemCached with /usr/local/bin/memcached -d -u nobody
Confirming MemCached is running
Run netstat -an to confirm that a process is listening on TCP 11211
It may also be useful to run /usr/local/bin/php -i | grep -i ‘memcache’

Speed up your PHP like Facebook does with HipHop for PHP

It’s a fact that script based languages and frameworks slow you down. PHP and its huge amount of frameworks is no exception to that rule. Until you realize that it is used on the busiest site out there – Facebook.
Scripted languages typically are embraced as they have advantages in being simple to learn, code, and debug while counterbalanced with a definite sacrificing of speed. On the other side of the spectrum, compiled languages usually have a steeper learning curve and are much more strict; counter balanced with the clear fact that they are considerably faster and better optimized for handling memory. Facebook seems to have found a balance between both worlds with HipHop for PHP.
Facebook developers write lots of their core services in other languages, and then PHP is used as the glue that queries those services and builds the actual pages itself. Still, they have found that that really isn’t fast enough for what they want, so they went and wrote their own compiler, or rather an optimizer, called HipHop, which compiles PHP down to C++ which can then be compiled by G++.
You can download HipHop from here: http://github.com/facebook/hiphop-php

If you’re interested in using/trying some PHP frameworks you can start by looking at: