Compile PHP on Fedora

Below is an outline on how to compile a feature rich, PHP Apache module on Fedora 9. First, compile Apache on the system using these instructions. This document assumes that Apache and the APache eXtenSion Tool are installed in the PREFIX: /opt/apache2.

Once you have Apache prepared, download the PHP source from php.net. As of August, 2008 the latest version is PHP 5.2.6. So, the following examples will use the php-5.2.6.tar.gz source tarball. Use your browser to download the latest and greatest. Then, untar the source.

tar -xzvf php-5.2.6.tar.gz
cd php-5.2.6

Next, configure the source for compile. Use the configure command. You can provide –help to see the large list of options.

./configure --help

For every option you enable, make sure you have the necessary RPMs installed to build PHP. For example, if you run configure with –with-snmp, you will need the net-snmp-devel RPM installed to complete the build. To ensure that you can build PHP with a full suite of options, install the following RPMs. Trim the list to suit your needs.

yum install aspell-devel curl-devel cyrus-sasl-devel e2fsprogs-devel freetype-devel glibc-devel keyutils-libs-devel krb5-devel libgcc libidn-devel libjpeg-devel libpng-devel libselinux-devel libsepol-devel libstdc++-devel libX11-devel libXau-devel libXdmcp-devel libxml2-devel libXpm-devel mysql-devel net-snmp-devel openldap-devel openssl-devel tcp_wrappers zlib-devel

Then, configure with the following settings. Change the configure –prefix to adjust your install PREFIX. Again, trim the list to suit your needs.

./configure  --prefix=/opt/php5 --with-apxs2=/opt/apache2/apxs --with-mysql=/usr/bin/mysql_config --with-mysqli=/usr/bin/mysql_config --with-png-dir=/usr --with-gd --enable-gd-native-ttf --with-ttf --enable-safe-mode --enable-magic-quotes --with-pspell --with-gettext --with-jpeg-dir=/usr  --with-zlib --with-curl --enable-soap --with-ldap=/usr --enable-sockets --with-openssl --with-snmp --enable-mbstring --with-freetype-dir=/usr --with-xpm-dir=/usr --with-libdir=lib64

Please note that if you are on a 32bit system, remove:

--with-libdir=lib64

Finally, build PHP and test it.

make
make test

If you are comfortable with the results (a few errors are OK in make test), then install PHP.

sudo make install

The install will place the PHP module in /opt/apache2/modules and prepare /opt/apache2/conf/httpd.conf with these settings:

LoadModule php5_module        modules/libphp5.so

Then, place a php.ini in /opt/php5/lib (or your PREFIX/lib).

cp -a php.init-recommended /opt/php5/lib/php.ini

Finally, prepare your Apache Virtual Host to support index.php with the following code:

AddType application/x-httpd-php .php .inc .class
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php

Refer to PHP.net and Apache.org for reference.

Vi Search

In Vi or Vim, use the forward slash </> to search. Then type in your search string and hit <Enter>. You can navigate through occurrences of your search string using <n> to move forward and <N> to move backwards. Below is an example search for the string ‘array’.

If you do not want Vim to highlight searches, set the following in your ~/.vimrc

set nohlsearch

To wrap searches around the file when using <n> and <N>, set the following in your ~/.vimrc

set wrapscan

For more, see this article if you need to search and replace.

Vi Search and Replace

Search and Replace

To begin a search and replace in Vi or Vim, start by hitting the colon <:>. This will allow you to enter a Vi command. Then, enter a search and replace command like so:

%s/old/new/g

This will search every line for ‘old’ and replace all occurrences in the line with ‘new’. You start by mentioning which lines to affect. In my example, I use ‘%s’. This translates to all lines. Then, I trail the command with ‘g’ (for global) to affect all occurrences in the lines defined by ‘%s’. Note that global does not mean the entire file!

Limiting the Replace

You can declare the search and replace for only the current line by removing the percent sign:

s/old/new/g

It is also possible to limit the affected line numbers. The next example only affects lines 40 through 42:

40,42 s/old/new/g

Eliminating the ‘g’ from the end will only replace the first occurrence per line defined in the search. For example, the following replaces the first instance of ‘hello’ on every line in the file:

Regular Expressions

The search string can be the form of a regular expression. In the next example, I replace all numbers with the word ‘number’.

%s/[0-9]/number/g

Escaping Characters

You will have to escape out the slash </> if it is part of your search string:

%s/http:\/\//https:\/\//g

Use Any Delimiter

Alternatively, change your delimiter. It can be anything!

%s!http://!https://!g

Get Confirmation Before Changing

You can get confirmation for each replace by adding a ‘c’ to the end of your command:

For more information, see the online Vi documentation.

Bash Functions

Here are some example BASH functions. For more information, see the online BASH Function documentation.

Declaring Functions

A BASH function must first be declared. One of the two methods can be used to declare a BASH function:

Method #1: function name compound-command

function say_goodbye {
    echo "goodbye"
}

Method #2: name() compound-command

say_hello() {
    echo "hello"
}

Using Functions

Just call the name as you would any other simple command:

say_hello
hello

Passing Variables

Pass variables into the function by space delimited fields and accept parameters using $1, $2 and so on.

my_function() {
    echo "$1"
}

Example:

my_function hello this is a test
hello

To expand all variable parameters inside the function, use $@

log() {
    echo "[$(date)]: $@"
}
log this is a test
[Sun Jun 22 10:21:26 PDT 2008]: this is a test

Return Codes

Functions return an exit status to their caller:

subtest() {
return 1
}

The function will return 1:

subtest || echo "Returned: $?"
Returned: 1

Since the function returned 1, it failed the test of || (or).

Bash For Loop

Below are examples of the Bash for loop in action. If you need more help, see the official Bash documentation for an introduction to Bash programming.

Examples

Echo all jpg files in the current folder:

for JPG in *.jpg; do 
    echo $JPG
done

Count a list of numbers:

for NUM in $(seq 1 10); do
    echo $NUM
done

One liners work as well:

for n in $(seq 3); do echo $n; done

Iterate through the process ID’s for httpd and kill them:

for PID in $(ps -C httpd | awk '/httpd/ { print $1 }'); do
    kill -TERM $PID
done

List arguments to a function named showarg:

showarg() {
    for ARG in $@; do
        echo $ARG
    done
}

Loop through folders and back them up using tar:


DATE=$(date +'%Y-%m-%d')
for DIR in /etc /var /root; do
    tar -czvf /backups/$DIR_$DATE.tgz $DIR
done

Bash Arrays

Arrays in Bash are quite simple. Here are some array examples to get you going. The official Bash documentation has more details and examples.

Initialize an entire array:

bash$ DAYS=(mon tue wed thu fri sat sun)

There are two ways to reference an entire array:

bash$ echo ${DAYS[@]}
mon tue wed thu fri sat sun
bash$ echo ${DAYS[*]}
mon tue wed thu fri sat sun

Initialize array element

bash$ ARRAY[0]="Fedora"
bash$ ARRAY[1]="RedHat"
bash$ ARRAY[2]="CentOS"

Display an element

bash$ echo ${ARRAY[0]}
Fedora

Get the length of an array:

bash$ echo ${#ARRAY[@]}
3

Get the length of an array value based on index:

bash$ echo ${ARRAY[0]} 
Fedora
bash$ echo ${#ARRAY[0]} 
6

Get the subset of an array through trailing substring extraction:

bash$ echo ${ARRAY[@]:0} 
Fedora RedHat CentOS
bash$ echo ${ARRAY[@]:1} 
RedHat CentOS
bash$ echo ${ARRAY[@]:2} 
CentOS

Fedora SRC RPM

So, you want to rebuild a Fedora package from source using a src.rpm file? Maybe make some tweaks along the way? This is how.

Quick Prerequisites Check

First, make sure you have yum-utils and rpmdevtools installed

sudo yum -y install yum-utils rpmdevtools 

Next, configure your user’s .rpmmacros file. Use your username, not root! Edit ~/.rpmmacros and add these lines:

%_topdir      %(echo $HOME)/rpmbuild
%__arch_install_post   /usr/lib/rpm/check-rpaths   /usr/lib/rpm/check-buildroot

The first line, indicates the rpm build top directory. Then, we add some some post install commands. Systems with SMP enabled should also add the following to enable parallel compiles during builds. Adjust the job number to be the number of CPU cores available. In my example, two.

%_smp_mflags  -j2

I also like to format my rpm query string so that architecture is part of the name:

%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}

To review, here is my .rpmmacros file:

%_topdir      %(echo $HOME)/rpmbuild
%_smp_mflags  -j2
%__arch_install_post   /usr/lib/rpm/check-rpaths   /usr/lib/rpm/check-buildroot
%_query_all_fmt %%{name}-%%{version}-%%{release}.%%{arch}

Get the Source

Build the rpmbuild folder and it’s subfolders using rpmdev-setuptree from the rpmdevtools package. Note that you can also wipe ~/rpmbuild clean with /usr/bin/rpmdev-wipetree.

/usr/bin/rpmdev-setuptree

Then, cd into ~/rpmbuild/SRPMS and use yumdownloader (part of yum-utils) to download the source code. In the following example, I am downloading httpd.

cd ~/rpmbuild/SRPMS/
yumdownloader --source httpd

Build the Source

Use yum-builddep as root to install all packages needed to build the source RPM. The yum-builddep tool is part of the yum-utils package.

sudo yum-builddep httpd-2.2.8-3.src.rpm

You have two options when compiling the RPM from source. One, you just rebuild the src.rpm as-is. This will drop a binary RPM in ~/rpmbuild/RPMS/$(arch):

rpmbuild --rebuild httpd-2.2.8-3.src.rpm

Otherwise, if you want to make changes to the source RPM’s spec file, first install the source RPM:

rpm -ivh httpd-2.2.8-3.src.rpm

Now you can edit the spec file in ~/rpmbuild/SPECS and build a binary package using the edited spec file:

cd ~/rpmbuild
vim SPECS/httpd.spec
rpmbuild -bb SPECS/httpd.spec

To also build a new source RPM from your edited SPEC, use rpmbuild -bs

rpmbuild -bs SPECS/httpd.spec

Again, binary RPMs are produced in ~/rpmbuild/RPMS/$(arch).

Sources

FedoraProject.org: Docs/CustomKernel
HowToForge.com: How To Compile A Kernel – The Fedora Way

Fedora 9 Perlcc

As of version 5.10.0-RC2, Perlcc is no longer distributed with Perl. This means that Fedora 9 does not include perlcc. Here is the official statement regarding perlcc from CPAN.org:

Removal of the bytecode compiler and of perlcc

perlcc, the byteloader and the supporting modules (B::C, B::CC, B::Bytecode, etc.) are no longer distributed with the perl sources. Those experimental tools have never worked reliably, and, due to the lack of volunteers to keep them in line with the perl interpreter developments, it was decided to remove them instead of shipping a broken version of those. The last version of those modules can be found with perl 5.9.4.

However the B compiler framework stays supported in the perl core, as with the more useful modules it has permitted (among others, B::Deparse and B::Concise).

Sources
CPAN.org: perldelta – what is new for perl 5.10.0

Fedora 9 NVIDIA

Fedora 9 comes with two drivers that can be used in /etc/X11/xorg.conf to support your Nvidia card.

  1. NV: http://xorg.freedesktop.org/wiki/nv
  2. Nouveau: http://nouveau.freedesktop.org/wiki/

Although these two drivers work, they have their limitations. It is best to install the Nvidia driver from Nvidia.com. You can either build the source from Nvidia’s Driver Website, or use Livna.org to install a binary kernel module.

Livna’s Binary Nvidia Driver

To use Livna.org‘s binary driver, install Livna’s release package for Fedora 9. This will provide you with access to Livna.org’s Fedora 9 YUM repository using /etc/yum.repos.d/livna.repo.

rpm -ivh http://rpm.livna.org/livna-release-9.rpm

Next, install the Nvidia kernel module and all of it’s dependencies:

sudo yum install kmod-nvidia

Then, use the nvidia-xconfig script to configure /etc/X11/xorg.conf. This will configure /etc/X11/xorg.conf with the ‘nvidia’ driver and it’s options.

sudo /usr/sbin/nvidia-xconfig

Finally, reboot your system. You should see the Nvidia driver loading during boot. If you have any X errors, check /var/log/Xorg.log. If you want to tweak your driver, OpenGL or display settings, use nvidia-settings.

sudo /usr/bin/nvidia-settings

Vim Spell Check

Place these settings in ~/.vimrc to enable and configure spell checking in Vim. Comments start with double quotes. For more, open vim and type :help spell

Enable Spelling

set spell

Enable Spellfile

set spell spelllang=en_us
" zg to add word to word list 
" zw to reverse
" zug to remove word from word list 
" z= to get list of possibilities
set spellfile=~/.vim/spellfile.add

Set Colors

highlight clear SpellBad
highlight SpellBad term=standout ctermfg=1 term=underline cterm=underline
highlight clear SpellCap
highlight SpellCap term=underline cterm=underline
highlight clear SpellRare
highlight SpellRare term=underline cterm=underline
highlight clear SpellLocal
highlight SpellLocal term=underline cterm=underline