Difference between revisions of "Nginx"

From Joomla! Documentation

(Updated the configuration notation on where configs reside for Ubuntu given prior knowledge of the NGINX packages (member of the nginx team on Launchpad))
m (line has been copied from console and contains $ which indicates more unseen code. Stops javascipt being gzipped)
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[http://nginx.net/ nginx] is a lightweight web server that powers 11.53% of web servers across all domains[http://en.wikipedia.org/wiki/Nginx#Usage]. Unless you have specific requirements that demand a heavy web server like Apache, you are much better off using nginx.
+
[http://nginx.net/ nginx] is a lightweight Web server that powers about 13% of Web servers across all domains[http://en.wikipedia.org/wiki/Nginx#Description]. Unless you have specific requirements that demand a heavy Web server like Apache, you are much better off using nginx.
  
 
Below are instructions on how to get Joomla! running with [http://wiki.nginx.org/PHPFcgiExample nginx and PHP via FastCGI].
 
Below are instructions on how to get Joomla! running with [http://wiki.nginx.org/PHPFcgiExample nginx and PHP via FastCGI].
Line 9: Line 9:
 
For Ubuntu, read this excellent page on how to [http://wiki.nginx.org/PHPFcgiExample configure PHP and FastCGI for nginx].
 
For Ubuntu, read this excellent page on how to [http://wiki.nginx.org/PHPFcgiExample configure PHP and FastCGI for nginx].
  
For Gentoo, PHP will run as a FastCGI service (fpm), so the nginx server will run PHP asa  different process:
+
For Gentoo, PHP will run as a FastCGI service (fpm), so the nginx server will run PHP as a separated process:
 
<pre>
 
<pre>
 
# echo "dev-lang/php gd gd2 curl simplexml tokenizer dom tidy sqlite xml fpm cgi" >> /etc/portage/package.use
 
# echo "dev-lang/php gd gd2 curl simplexml tokenizer dom tidy sqlite xml fpm cgi" >> /etc/portage/package.use
 
# emerge php
 
# emerge php
 
</pre>
 
</pre>
The default settings of php-fpm are good for most servers. For special configuration visit the [http://php.net/manual/en/install.fpm.php PHP FPM site].
+
The default settings of php-fpm are good for most servers. For special configurations, visit the [http://php.net/manual/en/install.fpm.php PHP FPM site].
  
 
== Configure Nginx ==
 
== Configure Nginx ==
Line 21: Line 21:
 
* <tt>/etc/nginx/nginx.conf</tt> on Gentoo and Raspbian(= Debian optimized for Raspberry Pi)
 
* <tt>/etc/nginx/nginx.conf</tt> on Gentoo and Raspbian(= Debian optimized for Raspberry Pi)
  
Here is an sample nginx configuration file joomla.conf that you can reuse over all your nginx enabled-sites
+
Here is an sample nginx configuration file, joomla.conf, that you can reuse over all your nginx enabled-sites.
 
 
 
 
  
 
<pre>
 
<pre>
Line 38: Line 36:
 
         # Support Clean (aka Search Engine Friendly) URLs
 
         # Support Clean (aka Search Engine Friendly) URLs
 
         location / {
 
         location / {
                 try_files $uri $uri/ /index.php?q=$uri&$args;
+
                 try_files $uri $uri/ /index.php?$args;
 
         }
 
         }
  
Line 67: Line 65:
 
</pre>
 
</pre>
  
Pay attention for few things:
+
Pay attention to a few things:
 
# The parameter <tt>fastcgi_pass</tt> is set to 127.0.0.1:9000, corresponding to the port that fpm is configured to listen to. This means you can run the PHP processes on separate servers. On Gentoo, you can find this configuration in <tt>/etc/php/fpm-php5.3/php-fpm.conf/</tt>
 
# The parameter <tt>fastcgi_pass</tt> is set to 127.0.0.1:9000, corresponding to the port that fpm is configured to listen to. This means you can run the PHP processes on separate servers. On Gentoo, you can find this configuration in <tt>/etc/php/fpm-php5.3/php-fpm.conf/</tt>
# Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER depend on your domain and the path of Joomla on your server.
+
# Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER above depending on your domain and the path of Joomla on your server.
  
 
== GZip support ==
 
== GZip support ==
Line 79: Line 77:
 
         gzip_min_length 1100;
 
         gzip_min_length 1100;
 
         gzip_buffers 4 8k;
 
         gzip_buffers 4 8k;
         gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascr$
+
         gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
 
         gzip_proxied    any;
 
         gzip_proxied    any;
 
         gzip_disable    "MSIE [1-6]\.";
 
         gzip_disable    "MSIE [1-6]\.";

Revision as of 14:02, 6 September 2014

nginx is a lightweight Web server that powers about 13% of Web servers across all domains[1]. Unless you have specific requirements that demand a heavy Web server like Apache, you are much better off using nginx.

Below are instructions on how to get Joomla! running with nginx and PHP via FastCGI.

Install nginx[edit]

For Ubuntu, run aptitude install nginx. For other distros, run the corresponding package manager, or see http://wiki.nginx.org/Install.

Install PHP FastCGI[edit]

For Ubuntu, read this excellent page on how to configure PHP and FastCGI for nginx.

For Gentoo, PHP will run as a FastCGI service (fpm), so the nginx server will run PHP as a separated process:

# echo "dev-lang/php gd gd2 curl simplexml tokenizer dom tidy sqlite xml fpm cgi" >> /etc/portage/package.use
# emerge php

The default settings of php-fpm are good for most servers. For special configurations, visit the PHP FPM site.

Configure Nginx[edit]

nginx configuration files reside in:

  • /etc/nginx/sites-available/ on Ubuntu (for sites running on that nginx instance)
  • /etc/nginx/nginx.conf on Gentoo and Raspbian(= Debian optimized for Raspberry Pi)

Here is an sample nginx configuration file, joomla.conf, that you can reuse over all your nginx enabled-sites.

server {
        listen 80;
        server_name YOUR_DOMAIN;
        server_name_in_redirect off;

        access_log /var/log/nginx/localhost.access_log main;
        error_log /var/log/nginx/localhost.error_log info;

        root PATH_ON_SERVER;
        index index.php index.html index.htm default.html default.htm;
        # Support Clean (aka Search Engine Friendly) URLs
        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        # deny running scripts inside writable directories
        location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
                return 403;
                error_page 403 /403_error.html;
        }

        location ~ \.php$ {
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi.conf;
        }

        # caching of files 
        location ~* \.(ico|pdf|flv)$ {
                expires 1y;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
                expires 14d;
        }

}

Pay attention to a few things:

  1. The parameter fastcgi_pass is set to 127.0.0.1:9000, corresponding to the port that fpm is configured to listen to. This means you can run the PHP processes on separate servers. On Gentoo, you can find this configuration in /etc/php/fpm-php5.3/php-fpm.conf/
  2. Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER above depending on your domain and the path of Joomla on your server.

GZip support[edit]

If you need GZip compression support, add the following section to the http section of the main nginx configuration file:

        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 6;
        gzip_min_length 1100;
        gzip_buffers 4 8k;
        gzip_types text/plain application/xhtml+xml text/css application/xml application/xml+rss text/javascript application/javascript application/x-javascript
        gzip_proxied     any;
        gzip_disable     "MSIE [1-6]\.";

Sources[edit]