Nginx

From wiki.techunit.org
Jump to: navigation, search

Introduction

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

NGINX is one of a handful of servers written to address the C10K problem. Unlike traditional servers, NGINX doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load. Even if you don’t expect to handle thousands of simultaneous requests, you can still benefit from NGINX’s high-performance and small memory footprint. NGINX scales in all directions: from the smallest VPS all the way up to large clusters of servers.

  • Realized on :
    • Debian 8
    • Ubuntu 14.04
  • nginx versions :
    • 1.6.2
    • 1.4.6

Simple configuration

Install packages

aptitude install nginx -y

Files location

  • Configuration : /etc/nginx/*

Virtualhosts

Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries.
httpd.apache.org

minimal

  • Create your site directory and index
mkdir -p /var/www/www.example.com
echo "Hello b*tches!" > /var/www/www.example.com/index.html
chown www-data: /var/www/www.example.com
  • Create the Vhost config file : /etc/nginx/sites-available/www.example.com
tee /etc/nginx/sites-available/www.example.com << EOF &> /dev/null
server {
    listen 80;
    server_name www.example.com;
    charset utf-8;
    root /var/www/html;
    index index.html;
}
EOF
  • Enable www.example.com
ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/
service nginx reload

Configure php-fpm with Nginx

Configure PHP-FPM

Use PHP-FPM in virtualhost

  • Edit /etc/nginx/sites-available/www.example.com
server {
    listen 80;
    server_name www.example.com;
    charset utf-8;
    root /var/www/html;
    index index.html;

    # Add following block
    location ~ \.php(?:$|/) {
        fastcgi_pass unix:/var/run/php5-fpm-www.sock;                       # path to php-fpm unix socket
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}
  • Enable virtualhost and restart nginx