Tuesday, June 12, 2012

Install Nginx, PHP-FPM and Varnish on FreeBSD 8.2

Install Nginx, PHP-FPM and Varnish on FreeBSD 8.2

# cd /usr/ports/www/nginx

# make install clean

Note: make sure you select important option such as:
HTTP_REWRITE_MODULE=on
HTTP_SSL_MODULE=on

and others as per your requirements.

# echo '### Nginx' >> /etc/rc.conf
# echo 'nginx_enable="YES"' >> /etc/rc.conf

# vim /usr/local/etc/nginx/nginx.conf

gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server {
            server_name mydomain1.com mydomain2.com mydomain3.com;
            root /usr/local/www/nginx/drupal7; ## <-- Your only path reference.

            client_max_body_size 20M; # Maximum allowed size for uploaded files

            location = /favicon.ico {
                    log_not_found off;
                    access_log off;
            }

            location = /robots.txt {
                    allow all;
                    log_not_found off;
                    access_log off;
            }

            # This matters if you use drush
            location = /backup {
                    deny all;
            }

            # Very rarely should these ever be accessed outside of your lan
            location ~* \.(txt|log)$ {
                    allow 192.168.0.0/24;
                    deny all;
            }

            location ~ \..*/.*\.php$ {
                    return 403;
            }

            location / {
                    # This is cool because no php is touched for static content
                    try_files $uri @rewrite;
            }

            location @rewrite {
                    # Some modules enforce no slash (/) at the end of the URL
                    # Else this rewrite block wouldn't be needed (GlobalRedirect)
                    rewrite ^/(.*)$ /index.php?q=$1;
            }

            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    ### NOTE: If you are using drupal 7, you should have "cgi.fix_pathinfo = 0" in php.ini (but wait, according to PHP manual, we should modify the code instead?).
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    fastcgi_intercept_errors on;
                    #fastcgi_pass 127.0.0.1:9000;  #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                    fastcgi_pass unix:/tmp/php-fpm.sock;
            }

            ### disable PHP execution on upload attachement directory
            location /sites/default/files/ {
              location ~ .*\.(php)?$
              {
                 deny all;
              }
            }

            # Fighting with ImageCache? This little gem is amazing.
            location ~ ^/sites/.*/files/imagecache/ {
                    try_files $uri @rewrite;
            }
            # Catch image styles for D7 too.
            location ~ ^/sites/.*/files/styles/ {
                    try_files $uri @rewrite;
            }

            location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                    expires max;
                    log_not_found off;
            }

            # nginx status
            location /NginxStatus {
              stub_status on;
              access_log on;
              auth_basic NginxStatus;
              auth_basic_user_file conf/htpasswd;
            }
    }

# /usr/local/etc/rc.d/nginx start

Change this line for nginx:

# vim /usr/local/etc/php.ini
cgi.fix_pathinfo = 0

Note: If you are using drupal 7, you should have "cgi.fix_pathinfo = 0" in php.ini (but wait, according to PHP manual, we should modify the code instead?).

Install php and php-fpm:

// For PHP5.3
# cd /usr/ports/lang/php5 ; make install

// For PHP5.2
# cd /usr/ports/lang/php52 ; make install

===> The following configuration options are available for php5-5.3.8:
CLI=on "Build CLI version"
CGI=on "Build CGI version"
FPM=on "Build FPM version (experimental)"
APACHE=off "Build Apache module"
AP2FILTER=off " Use Apache 2.x filter interface (experimental)"
DEBUG=off "Enable debug"
SUHOSIN=on "Enable Suhosin protection system"
MULTIBYTE=on "Enable zend multibyte support"
IPV6=off "Enable ipv6 support"
MAILHEAD=on "Enable mail header patch"
LINKTHR=on "Link thread lib (for threaded extensions)"
===> Use 'make config' to modify these settings

// For PHP5.3
# cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

// For PHP5.2
# cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini

Install PHP Extensions
// For PHP5.3
# cd /usr/ports/lang/php5-extensions ; make install

// For PHP5.2
# cd /usr/ports/lang/php52-extensions ; make install

Edit php-fpm.conf:
# vim /usr/local/etc/php-fpm.conf
; comment out following line and add the line below.
;listen = 127.0.0.1:9000
listen = /tmp/php-fpm.sock

request_terminate_timeout=30s

Edit rc.conf
# vim /etc/rc.conf
### php-fpm
php_fpm_enable="YES"

Change this line for nginx

# vim /usr/local/etc/php.ini

date.timezone = America/Vancouver
magic_quotes_gpc = Off
max_execution_time = 120
max_input_time = 60
memory_limit = 256M
upload_max_filesize = 20M
post_max_size = 30M
cgi.fix_pathinfo = 0
log_errors = On
error_log = /var/log/php_errors.log

Note: If you are using drupal 7, you should have "cgi.fix_pathinfo = 0" in php.ini (but wait, according to PHP manual, we should modify the code instead?).

# touch /var/log/php_errors.log
# chown www:www /var/log/php_errors.log
# chmod 660 /var/log/php_errors.log

# /usr/local/etc/rc.d/php-fpm start
# /usr/local/etc/rc.d/nginx restart

# tail /var/log/nginx-error.log

# tail /var/log/php-fpm.log

# tail /var/log/messages

Rotate Nginx and PHP log files:
# vim /etc/newsyslog.conf
/var/log/nginx-access.log 600 7 100000 * JC /var/run/nginx.pid
/var/log/nginx-error.log 600 7 100000 * JC /var/run/nginx.pid
/var/log/php-fpm.log 600 7 100000 * JC /var/run/php-fpm.pid
/var/log/php_errors.log 600 7 100000 * JC

# /etc/rc.d/newsyslog restart

Install Varnish

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

Reconfigure your web server to listen on localhost:8080

# echo 'varnishd_enable="YES"' >> /etc/rc.conf
# echo 'varnishd_flags="-s malloc,1G -a 127.0.0.1:80 -b 127.0.0.1:8080"' >> /etc/rc.conf
# echo 'varnishlog_enable="YES"' >> /etc/rc.conf

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

# /usr/local/etc/rc.d/varnishncsa onestart

# tail /var/log/varnishncsa.log

# /usr/local/bin/varnishtest

# /usr/local/bin/varnishstat

# ls -lh /usr/local/varnish/`hostname`

Install webbench
# cd /usr/ports/benchmarks/webbench ; make install clean

# webbench -c 3000 -t 60 http://test.local/index.php

Reference:
Install Nginx, PHP-FPM and Varnish on FreeBSD 8.2
http://blog.ijun.org/2012/01/install-nginx-php-fpm-and-varnish-on.html

Apache MPM Worker + mod_fastcgi + PHP-FPM
http://gala4th.blogspot.com/2011/07/apache-mpm-worker-modfastcgi-php-fpm.html

Web Performance Tuning Tips Solutions for Drupal Sites
http://gala4th.blogspot.com/2010/12/web-performance-tuning-tips-solutions.html

Running Drupal with Nginx
http://wiki.nginx.org/Drupal

high performance caching reverse proxy: Varnish (安裝架設篇)
http://gala4th.blogspot.com/2010/12/high-performance-caching-reverse-proxy.html

[FreeBSD & Linux]網站分流:簡易架設 HAProxy 伺服器
http://blog.wu-boy.com/2008/06/freebsd-linux%E7%B6%B2%E7%AB%99%E5%88%86%E6%B5%81%EF%BC%9A%E7%B0%A1%E6%98%93%E6%9E%B6%E8%A8%AD-haproxy-%E4%BC%BA%E6%9C%8D%E5%99%A8/

Scaling Rails Site:Reading Material # 1
http://wp.xdite.net/?p=1597

1 comment:

James Nesbitt said...

Your installation is very clearly drupal specific (not just the comments but the rewrite implementation).
You should probably reflect this in your title.