Saturday, March 9, 2013

Installing NGINX engine X with LAMP

Debian OS - upgrade to latest packages

    # apt-get update 
    # apt-get upgrade 

Packages installation

    # apt-get install apache2
    # a2enmod rewrite
    # /etc/init.d/apache2 restart

    # nano /etc/apache2/sites-enabled/000-default
      (default webroot directory: /var/www/)  

check configuration:
    # apachectl -t  

After enabling, disabling, or modifying any part of your Apache configuration, you will need to reload or restart the Apache configuration again with command:
    # /etc/init.d/apache2 reload or 
    # /etc/init.d/apache2 restart

    # apt-get install php5 php-pear php5-suhosin php5-mysql

configuration: edit /etc/php5/apache2/php.ini Make sure that the following values are set, and relevant lines are uncommented (comments are lines beginning with a semi-colon (;)):
max_execution_time = 60
memory_limit = 128M
display_errors = Off
log_errors = On
error_log = /var/log/php5.log
register_globals = Off

To apply PHP configuration changes Apache need to be restarted:
    # /etc/init.d/apache2 restart

Advanced server setup - NGINX


Using repo for Nginx 1.0.11 last stable. For the main Dotdeb repository add these two lines to: /etc/apt/sources.list file
    # deb stable all
    # deb-src stable all

Then fetch the appropriate GnuPG key
    # wget
    # cat dotdeb.gpg | sudo apt-key add -

    # apt-get update    
    # apt-get install nginx


Stop the Nginx server if it was started automatically by the package manager and create a new nginx.conf configuration file – installed in /etc/nginx/ by default – by pasting the following and adjusting the paths to those of your installation:
user www-data; #change to the same user apache runs as
worker_processes 8; #change to the number of your CPUs/Cores
worker_rlimit_nofile 8192;

error_log /var/log/nginx/error.log;
pid /var/run/;

events {
  worker_connections 1024;
  use epoll;
  accept_mutex off;

http {
  server_names_hash_bucket_size 64;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 65;

  # reverse proxy options
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  # gzip compression options
  gzip on;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_min_length 0;
  gzip_buffers 16 8k;
  gzip_proxied any;
  gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
  gzip_disable "MSIE [1-6]\.";
  gzip_vary on;

  # include virtual hosts configuration
  include /etc/nginx/virtual.d/*.conf;

Nginx should run as the same user Apache runs, to avoid file permission problems.
Besides the proxy setup this configuration file includes some generic performance tuning, such as use epoll as the event model method, which works effectively on Linux 2.6+ kernels. This works in tandem with the next line, accept_mutex off, to improve performance a bit more. Enabling sendfile allows nginx to use the kernel’s sendfile support to send files to the client regardless of their contents. This can help with large static files, such as images, that have no need for a multiple request/confirmation system to be served. Enabling gzip compression for static files can make a big performance difference. The lines starting with gzip enable compression for common web files, such as .css and .js files, on supported browsers.

Apache reverse proxy forward module(mod_rpaf)

If you check the Apache access log files you should see that all incoming requests are coming from To fix this you need to install mod_rpaf, the reverse proxy add forward module for Apache.
    # apt-get install libapache2-mod-rpaf

check content of /etc/apache2/mods-enabled/rpaf.conf :
<IfModule mod_rpaf.c>
RPAFenable On
RPAFsethostname On

restart apache:
    # /etc/init.d/apache2 restart

Apache configuration (behind Nginx)

Nginx now acts as the front-end web server – waiting for requests on port 80 – you need to configure Apache to listen on a different port (8080 in this case) and preferably only on localhost, open the file /etc/apache2/ports.conf and change the line Listen 80 to Listen, if you use name-based virtual hosts you should have a lineNameVirtualHost *:80 in the same file. Change that to NameVirtualHost *:8080.
If you have configured Keep-Alive support in Apache you should disable it since it is already enabled in Nginx. Change KeepAlive On to KeepAlive Off in/etc/apache2/apache2.conf . You can also disable the mod_deflate module since Nginx already provides gzip compression.

nginx referer denial

In /etc/nginx/nginx.conf there is a list of words to deny in URLs. If URL contains these words, all referred links will not load. This causes missing images and stylesheets, and every link from that page to another on the same site will come up blank.
  ## Deny certain Referers (case insensitive)
  ## The ~* makes it case insensitive as opposed to just a ~
  if ($http_referer ~* (babes|...|zippo) ) {
        return 444;
Just remove a word if you notice a problem and restart nginx with /etc/init.d/nginx restart