nginx+php-fpm+varnish+mysqld+php+drupal+unison+bind-roundrobin on fbsd 9.1

I got two machine that will act as web and name server.

Both servers running with same services and will replicate each other using unison.

The name server configured with round robin.

1. Bind Setup

# cd /usr/ports/dns/bind99 && make install clean
# mkdir -p /var/chroot/named/etc/namedb/log
# mkdir -p /var/chroot/named/dev
# mkdir -p /var/chroot/named/var/run
# cd /var/chroot
# chown -R bind:bind named
# chmod 700 named
# cp /etc/localtime /var/chroot/named/etc
# cp /etc/namedb/named.root /var/chroot/named/etc/namedb/
# cd /var/chroot/named/dev
# mknod zero c 2 12
# ln -s /dev/random .
# mknod null c 2 2
# chmod 666 zero random null
# cd /etc
# mv namedb old.namedb
# ln -s /var/chroot/named/etc/namedb .
# rndc-confgen -a -c /etc/namedb/rndc.conf -k rndc-key -b 512
# edit /etc/rc.conf
named_enable=”YES”
named_program=”/usr/local/sbin/named”
named_chrootdir=”/var/chroot/named”
named_flags=”-c /etc/namedb/named.conf”
named_pidfile=”/var/run/named.pid”

2. Nginx Setup

# cd /usr/ports/www/nginx# make install clean; rehash

Required module for nginx
i. HTTP_MODULE
ii HTTP_ADDITION_MODULE
iii. HTTP_CACHE_MODULE
iv. HTP_GEOIP_MODULE
v. HTTP_GZIP_STATIC_MODULE
vi. HTTP_PERL_MODULE
vii. HTTP_REALIP_MODULE
viii. HTTP_REWRITE_MODULE
x. HTTP_STATUS_MODULE

3. Setup PHP-FPM

# cd /usr/ports/lang/php5
# make install clean; rehash

Select Build Option – CLI , CGI , FPM

edit /etc/rc.conf

php_fpm_enable=”YES”
nginx_enable=”YES”

cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

edit php.ini with this settings:

error_reporting = E_ALL | E_STRICT
cgi.fix_pathinfo=1 <– change 0 for drupal
expose_php = Off
upload_max_filesize = 200M
post_max_size = 200M
max_execution_time = 600
max_input_time = 600
memory_limit = 256M
mysql.allow_persistent = Off
register_argc_argv = On
date.timezone = Asia/Kuala_Lumpur
register_globals = Off
allow_url_fopen = Off
magic_quotes_gpc = Off
magic_quotes_runtime = Off
/usr/local/etc/rc.d/php-fpm start
Install Maxmind GeoIP
# cd /opt/conf
# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# gunzip ./GeoIP.dat.gz

edit /usr/local/etc/nginx/nginx.conf

user www www;
worker_processes 4;
error_log /var/log/nginx/error.log crit;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
geoip_country /opt/conf/GeoIP.dat;
include /usr/local/etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
server_tokens off;
sendfile on;
client_max_body_size 200m;
client_body_buffer_size 1m;
keepalive_timeout 1;
port_in_redirect off;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
gzip_disable “MSIE [1-6].(?!.*SV1)”;
include /usr/local/etc/nginx/conf.d/*.conf;
}

# mkdir /usr/local/etc/nginx/conf.d

# mkdir /var/www/domain.com
# chown www:www /var/www/domain.com
# chmod 755 /var/www/domain.com

# sh -c ‘echo “<?php phpinfo(); ?>” > /var/www/domain.com/info.php’

create /usr/local/etc/nginx/conf.d/domain_com.conf

server {
listen 80;
server_name www.domain.com;
rewrite ^ http://domain.com$request_uri?;
}
server {
listen 80;
server_name domain.com;
server_name_in_redirect off;
root /var/www/domain.com;
location ~* ^.+\.(ico|js|gif|jpg|jpeg|png|bmp)$ {
expires 30d;
}
location / {
index index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param SCRIPT_FILENAME /var/www/domain.com$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

# mkdir -p /var/tmp/nginx/client_body_temp
# touch /var/log/nginx/error.log

3. Install some more packages that required by drupal.

# cd /usr/ports/databases/mysql-server# make install clean; rehash

edit rc.conf

mysql_enable=”YES”

# /usr/local/etc/rc.d/mysql-server start

# cd /usr/ports/databases/php5-mysql
# make install clean

# cd /usr/ports/www/php5-session
# make install clean

# cd /usr/ports/graphics/php5-gd
# make install clean

# cd /usr/ports/security/php5-hash
# make install clean

# cd /usr/ports/security/php5-mcrypt
# make install clean

# cd /usr/pors/devel/php5-json
# make install clean

# cd /usr/ports/textproc/php5-dom
# make install clean

# cd /usr/ports/security/filter/php5-filter
# make install clean

# cd /usr/ports/databases/php5-pdo
# make install clean

# cd /usr/ports/textproc/php5-simplexml
# make install clean

# cd /usr/ports/textproc/php5-xml
# make install clean

# cd /usr/ports/converters/php5-mbstring
# make install clean

# cd /usr/ports/databases/php5-pdo_mysql
# make install clean

4. Drupal setup

Upload drupal content to /var/www/domain.com

Run the URL and setting up the installer.

5. Varnish setup

# cd /usr/ports/www/varnish
# make install clean

change nginx port to 8080

edit /usr/local/etc/varnish/default.vcl

backend default {

.host = “127.0.0.1”;

.port = “8080”;

}

edit rc.conf

varnishd_enable=”YES”

varnishd_listen=”:80″

varnishd_config=”/usr/local/etc/varnish/default.vcl”

varnishd_storage=”malloc,4G”

varnishd_admin=”:6082″

/usr/local/etc/rc.d/varnishd start

6. Setup unison for replication

Why unison and not rsync ? unison can deal with upate from both replicas.

# cd /usr/ports/net/unison# make install clean

Directory to sync :
i. /var/www/domain.com
ii. /var/db/mysql/drupal

Create sync.sh
#!/usr/local/bin/bash
# set paths / dirs
_paths=”/var/www/domain.com/ \
/var/db/mysql/drupal/”
# binary file name
_unison=/usr/local/bin/unison
# server names
_rserver=”ns2.matnet.com.my”
# sync it
for r in ${_rserver}
do
for p in ${_paths}
do
${_unison} -batch “${p}”  “ssh://${r}/${p}”
done
done

Run the script on the crontab.
Finish 🙂

Thanks to :
http://www.cyberciti.biz/faq/unison-file-synchronizer-tool/
http://bin63.com/how-to-install-nginx-and-php-fpm-on-freebsd
http://www.freebsdonline.com/content/view/504/506/
BIND 9 FreeBSD Installation
http://blog.ijun.org/2012/01/install-nginx-php-fpm-and-varnish-on.html
http://bin63.com/how-to-install-varnish-cache-on-freebsd

This article was written by matn0t.