Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

How to: Install PHP+Memcached+GD on FreeBSD / CentOS

Dawn Rossi, 05-09-2009
This weekend I had to provision 6 new servers for a web hosting client. 3 of the servers were FreeBSD and the remaining 3 CentOS machines.

For FreeBSD, we're going with NGinx web server, PHP 4.9, Memcached and GD. For CentOS, Apache2 web server and PHP 5.

This step by step tutorial includes everything you need to create a similar setup.

Nginx with PHP+Memcached+GD on FreeBSD:

1. Install FreeBSD packages and ProFTPD


cd
/usr
mkdir tmp
cd tmp
setenv PACKAGESITE ftp
://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-6-stable/Latest/
pkg_add -r libevent
pkg_add
-r libtool
pkg_add
-r m4
pkg_add
-r pcre
pkg_add
-r pdftk
pkg_add
-r rsync
pkg_add
-r vim
pkg_add
-r wget

fetch http
://softwareprojects.com/files/auto/proftpd-1.3.1.tar.gz
tar xvfz proftpd-1.3.1.tar.gz
cd proftpd
-1.3.1
./configure --prefix=/usr/local
make
make install
cd
..
echo
"PermitRootLogin yes" >> "/etc/ssh/sshd_config"
echo "RootLogin On" >> "/usr/local/etc/proftpd.conf"
/usr/local/sbin/proftpd

2. Install NGinx


fetch http
://softwareprojects.com/files/auto/autoconf-2.61.tar.gz
fetch http://softwareprojects.com/files/auto/nginx-0.7.38.tar.gz
tar xvfz nginx-0.7.38.tar.gz
cd nginx
-0.7.38
./configure --with-http_ssl_module --without-mail_pop3_module --without-mail_imap_module  --without-mail_smtp_module
make
make install
mkdir
/usr/local/etc/rc.d
echo "/usr/local/nginx/sbin/nginx" >> /usr/local/etc/rc.d/nginx.sh
chmod 755
/usr/local/etc/rc.d/nginx.sh
/usr/local/etc/rc.d/nginx.sh
cd
..

3. Install CURL+LibXML


fetch http
://softwareprojects.com/files/auto/libxml2-2.7.2.tar.gz
fetch http://softwareprojects.com/files/auto/curl-7.19.1.tar.gz     
tar xvfz libxml2-2.7.2.tar.gz
cd libxml2
-2.7.2
./configure
make
make install
cd
..
tar xvfz curl-7.19.1.tar.gz
cd curl
-7.19.1
./configure
make
make install
cd
..

4. Install MySQL Client


fetch http
://softwareprojects.com/files/auto/mysql-5.1.31.tar.gz
pw group add mysql
pw user add
-n mysql -d /usr/local/mysql -s /usr/sbin/nologin
tar xvfz mysql
-5.1.31.tar.gz
cd mysql
-5.1.31
./configure --with-innodb --prefix=/usr/local/mysql
make all
make install
cd
..

5. Install GD


fetch http
://softwareprojects.com/files/auto/libpng-1.2.29.tar.gz
tar xvfz libpng-1.2.29.tar.gz
cd libpng
-1.2.29
./configure
make all
make install
cd
..
fetch http://softwareprojects.com/files/auto/jpegsrc.v6b.tar.gz
tar xvfz jpegsrc.v6b.tar.gz
cd jpeg
-6b
./configure --enable-shared --enable-static
make
make install
cp
-f libjpeg.* /usr/lib/
cd ..

6. Install PHP 4.9 with FPM


fetch http
://softwareprojects.com/files/auto/php-4.4.9-fpm-0.5.10.diff.gz
fetch http://softwareprojects.com/files/auto/php-4.4.9.tar.gz
tar xvfz php-4.4.9.tar.gz
gzip
-cd php-4.4.9-fpm-0.5.10.diff.gz | patch -d php-4.4.9 -p1
cd php
-4.4.9
./configure --enable-fastcgi --enable-fpm --enable-calendar --enable-ftp --enable-mbstring --enable-mysql --with-curl --with-dom --with-gd --with-iconv --with-jpeg-dir=/usr/lib --with-mysql=/usr/local/mysql --with-openssl --with-soap --with-sockets --with-zlib
make all install
cp
-f /usr/local/bin/php-cgi /usr/local/bin/php
cd
..
echo
"/usr/local/sbin/php-fpm start" >> /usr/local/etc/rc.d/php.sh
chmod 755
/usr/local/etc/rc.d/php.sh
fetch
-o /usr/local/etc/php-fpm.conf "http://www.softwareprojects.com/files/auto/php-fpm.conf"
/usr/local/etc/rc.d/php.sh start

7. Install Memcached


fetch http
://softwareprojects.com/files/auto/memcached-1.2.6.tar.gz
fetch http://softwareprojects.com/files/auto/memcache-2.2.4.tgz     
tar xvfz memcached-1.2.6.tar.gz
cd memcached
-1.2.6
./configure --prefix=/usr/local/memcached
make
make install
cd
..
echo
"extension=memcache.so" >> "/usr/local/lib/php.ini"
echo "extension=libmcrypt.so" >> "/usr/local/lib/php.ini"
echo "extension=mbstring.so" >> "/usr/local/lib/php.ini"
echo "extension=mcrypt.so" >> "/usr/local/lib/php.ini"

8. Setup Rsync for replication

The 3 servers are designed to mimic each other, for performance and reliability.

More information about using Rsync for web server replication.

One server is designated as the master and the remaining two servers have this line on their cronjob.

Be sure to replace mwHost with the master server to rsync from, mwUser with the master username and mwPass with the master password. Replace myHost with the current machine hostname.

Remove root from /etc/ftpusers before running this script

Run this on all slave servers

 
# Make sure the certificate doesn't exist
rm -rf /root/.ssh/id_rsa*
 
# Generate a new certificate
ssh-keygen -N "" -t rsa -f /root/.ssh/id_rsa

echo "open $mwHost"      >> ftp.txt
echo "user $mwUser $mwPass"  >> ftp.txt
echo "bin"          >> ftp.txt
echo "cd /root/.ssh"      >> ftp.txt
echo "get authorized_keys"  >> ftp.txt
echo "cd /etc/ssh"      >> ftp.txt
echo "get ssh_host_dsa_key.pub" >> ftp.txt
echo "bye"          >> ftp.txt

ftp
-n < ftp.txt

# Remove my own hostname from authorized_keys
cp authorized_keys authorized_keys.org
cat authorized_keys
| grep -v $myHost >tmp.$$
mv -f tmp.$$ authorized_keys

# Append my certficate to authorized_keys
cat /root/.ssh/id_rsa.pub >>authorized_keys

# Remove the host from known_hosts
cp -f /root/.ssh/known_hosts known_hosts.org
cat
/root/.ssh/known_hosts | grep -v "^$mwHost " >tmp.$$
mv -f tmp.$$ known_hosts

# Add the host to known_hosts
echo "$mwHost `cat ssh_host_dsa_key.pub | cut -d' ' -f1-2`" >>known_hosts

# Upload the NEW authorized_keys on the remote machine

echo "open $mwHost"    >>ftp.$$
echo
"user $mwUser $mwPass" >>ftp.$$
echo
"bin"        >>ftp.$$
echo
"cd /root/.ssh"    >>ftp.$$
echo
"del authorized_keys" >>ftp.$$
echo
"put authorized_keys" >>ftp.$$
echo
"bye"        >>ftp.$$

ftp -n < ftp.$$

# Install the NEW known_hosts
mv -f known_hosts /root/.ssh/known_hosts

Add this line to the crontab of all slave servers

Again - replace $mwHost with the ip address of the master server


/usr/local/bin/rsync --exclude temp/ --exclude tmp/ --exclude log/ --progress --stats --archive -z --compress -t $mwHost:/usr/local/nginx/html/ /usr/local/nginx/html/


Apache2 with PHP+Memcached+GD on CentOS:

1. Install Apache


yum install httpd
yum install httpd
-devel
yum install openssl
-devel
yum install mod_ssl
yum install libtool
-ltdl-devel

2. Install CentOS modules and ftp


wget
"http://www.softwareprojects.com/files/libevent-1.3e.tar.gz"
tar zxpfv libevent*
cd libevent*
./
configure
make install
cd
..
yum install vsftpd
chkconfig vsftpd on
service vsftpd start
echo "PermitRootLogin yes" >> "/etc/ssh/sshd_config"

3. Install CURL+LibXML

-- Same as FreeBSD instructions above (replace fetch with wget)

4. Install MySQL client


yum install mysql
yum install mysql
-devel

5. Install GD

- Same as FreeBSD instructions above (replace fetch with wget)

6. Install PHP5


wget
"http://www.softwareprojects.com/files/libmcrypt-2.5.8.tar.gz"
tar xvzf libmcrypt-2.5.8.tar.gz
cd libmcrypt
-2.5.8
./configure
make
make install
cd
..
wget http://softwareprojects.com/files/auto/php-5.2.9.tar.gz
tar xvfz php-5.2.9.tar.gz
cd php
-5.2.9
make clean
rm
-f /root/.pearrc
rm
-fdr /usr/local/lib/php
rm
-fdr /usr/lib/php
./configure --enable-fastcgi --enable-ftp --enable-calendar --enable-mbstring --with-curl --with-gd --with-mcrypt -disable-posix-threads --with-iconv --with-mysql --with-mysqli --with-mysqli=/usr/bin/mysql_config --with-jpeg-dir=/usr/lib --with-apxs2=/usr/local/apache/bin/apxs --enable-zip --with-mysql=/usr/local/mysql --with-openssl --with-zlib
make all
make install
cp
-f -p .libs/libphp5.so /usr/local/apache/modules
cp
-f /usr/local/bin/php-cgi /usr/local/bin/php
cd
..
/
etc/init.d/httpd restart


Note: If this command fails with an error about apxs, change --with-apxs2=/usr/local/apache/bin/apxs to --with-apxs2=/usr/sbin/apxs

7. Install Memcached


wget http
://www.monkey.org/~provos/libevent-1.3e.tar.gz
tar zxvf libevent-1.3e.tar.gz
cd libevent
-1.3e
./configure
make
make install
cd
..
wget http://danga.com:80/memcached/dist/memcached-1.2.5.tar.gz
tar zxvf memcached-1.2.5.tar.gz
cd memcached
-1.2.5
./configure
make
make install
cd
..
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
killall memcached
ln
-s /usr/local/lib/libevent-1.3e.so.1 /usr/lib/libevent-1.3e.so.1
memcached
-u nobody -d -m 2048 -l 127.0.0.1 -p 11211
cd
..
wget http://pecl.php.net/get/memcache-2.1.2.tgz
tar xvfz memcache-2.1.2.tgz
cd memcache
-2.1.2
phpize
&& ./configure --enable-memcache && make
make install
echo "extension=memcache.so" >> "/usr/local/lib/php.ini"
echo "extension=libmcrypt.so" >> "/usr/local/lib/php.ini"
echo "extension=mbstring.so" >> "/usr/local/lib/php.ini"
echo "extension=mcrypt.so" >> "/usr/local/lib/php.ini"
/etc/init.d/httpd restart

8. Setup Rsync for replication

-- Same as FreeBSD instructions above, but use /usr/sbin/rsync instead of /usr/local/bin/rsync

Mike Peters, 06-22-2009
To install PHP-fpm 5.2.8 on FreeBSD, replace step 6 with this:


fetch
"http://www.softwareprojects.com/files/libmcrypt-2.5.8.tar.gz"
tar xvzf libmcrypt-2.5.8.tar.gz
cd libmcrypt
-2.5.8
./configure
make
make install

fetch
"http://softwareprojects.com/files/auto/php-5.2.8-fpm-0.5.10.tar.gz"
fetch "http://softwareprojects.com/files/auto/php-5.2.8-fpm-0.5.10.diff.gz"
cp php-5.2.8-fpm-0.5.10.diff.gz /usr/ports/distfiles/
cp /lib/libm.so.5 /lib/libm.so.4
cp
/lib/libcrypt.so.4 /lib/libcrypt.so.3
cp
/lib/libutil.so.7 /lib/libutil.so.5
cp
/lib/libc.so.7 /lib/libc.so.6
tar xvfz php
-5.2.8-fpm-0.5.10.tar.gz
cd php5
-fpm
make
cd work
cd php
-5.2.8
./configure --with-config-file-path=/usr/local/lib/ --enable-fastcgi --enable-fpm --enable-calendar --enable-ftp --enable-mbstring --enable-mysql --with-curl --with-dom --with-mcrypt --with-gd --with-iconv --with-jpeg-dir=/usr/lib --with-mysql=/usr/local/mysql --enable-apc --with-openssl --with-soap --with-sockets --with-zlib --enable-zip
make all install
cp
-f /usr/local/bin/php-cgi /usr/local/bin/php
cd
..
echo
"/usr/local/sbin/php-fpm start" >> /usr/local/etc/rc.d/php.sh
chmod 755
/usr/local/etc/rc.d/php.sh
fetch
-o /usr/local/etc/php-fpm.conf "http://www.softwareprojects.com/files/auto/php-fpm.conf"
/usr/local/etc/rc.d/php.sh start

Dawn Rossi, 07-06-2009
To create a new user on CentOS use this:


useradd
-d /home/username -s /bin/bash -c "Name FamilyName" username
mkdir
/home/username &amp;&amp; chown username /home/username &amp;&amp; passwd username

Mike Peters, 08-06-2009
If you're installing on a FreeBSD machine that doesn't have the FreeBSD ports on it, use this to fetch the ports:

cd /usr
mkdir ports
fetch "ftp://ftp.freebsd.org/pub/FreeBSD/ports/ports/ports.tar.gz"
tar xvfz ports.tar.gz

Mike Peters, 01-06-2010
If you get an error while installing proftpd about g4, install it here:

cd /usr/tmp
fetch "http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz"
tar xvfz m4-1.4.9.tar.gz
cd m4-1.4.9
./configure
make all
make install

Sebastian Zavadschi, 02-12-2010
> Mike Peters
On FreeBSD 6.0 and more recent versions, consider using Portsnap.

http://www.freebsd.org/doc/en/books/handbook/portsnap.html
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions