Difference between revisions of "Nginx"

From Joomla! Documentation

(Created page with "[http://nginx.net/ Nginx] is light-weight web-server which run on more than 6% over internet web-servers[http://news.netcraft.com/]. Here are some instructions on how-to install...")
 
(Updated the configuration notation on where configs reside for Ubuntu given prior knowledge of the NGINX packages (member of the nginx team on Launchpad))
(18 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[http://nginx.net/ Nginx] is light-weight web-server which run on more than 6% over internet web-servers[http://news.netcraft.com/].
+
[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.
  
Here are some instructions on how-to install it on Gentoo Linux machine and install joomla within this server.
+
Below are instructions on how to get Joomla! running with [http://wiki.nginx.org/PHPFcgiExample nginx and PHP via FastCGI].
  
== PHP installation ==  
+
== Install nginx ==
PHP will run as fastcgi service (fpm), so Nginx server will run php separately (as different process):
+
For Ubuntu, run <tt>aptitude install nginx</tt>. For other distros, run the corresponding package manager, or see http://wiki.nginx.org/Install.
 +
 
 +
== Install PHP FastCGI ==  
 +
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:
 
<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 of php-fpm is good for most servers. For special configuration visit [http://php.net/manual/en/install.fpm.php php site].
+
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].
== Install nginx ==
 
In Linux you'll need to use emerge command. In other distros it would be apt-get, rpm, etc (depend on the distro).
 
<pre>
 
# emerge nginx
 
</pre>
 
  
 
== Configure Nginx ==
 
== Configure Nginx ==
 +
nginx configuration files reside in:
 +
* <tt>/etc/nginx/sites-available/</tt> on Ubuntu (for sites running on that nginx instance)
 +
* <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
  
The configuration of the virtual host on Gentoo lives in /etc/nginx/nginx.conf. Here is example from the author server:
 
<pre>
 
        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;
+
<pre>
                # this is the sef of Joomla
+
server {
                location / {
+
        listen 80;
                        try_files $uri $uri/ /index.php?q=$request_uri;
+
        server_name YOUR_DOMAIN;
                }
+
        server_name_in_redirect off;
  
                index index.php index.html index.htm default.html default.htm;
+
        access_log /var/log/nginx/localhost.access_log main;
                # unauthorize running scripts inside writable directories
+
        error_log /var/log/nginx/localhost.error_log info;
                location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
 
                        return 403;
 
                        error_page 403 /403_error.html;
 
                }
 
  
                location ~ .*.php$ {
+
        root PATH_ON_SERVER;
                    include /etc/nginx/fastcgi.conf;
+
        index index.php index.html index.htm default.html default.htm;
                    fastcgi_pass  127.0.0.1:9000;
+
        # Support Clean (aka Search Engine Friendly) URLs
                    fastcgi_index index.php;
+
        location / {
                    include fastcgi_params;
+
                try_files $uri $uri/ /index.php?q=$uri&$args;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+
        }
                }
 
  
                # caching of files
+
        # deny running scripts inside writable directories
                location ~* \.(ico|pdf|flv)$ {
+
        location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
                        expires 1y;
+
                return 403;
                 }
+
                 error_page 403 /403_error.html;
 +
        }
  
                location ~* \.(ico|pdf|flv)$ {
+
        location ~ \.php$ {
                        expires 1y;
+
            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;
 +
        }
  
                location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
+
        # caching of files
                        expires 14d;
+
        location ~* \.(ico|pdf|flv)$ {
                }
+
                expires 1y;
 +
        }
  
 +
        location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
 +
                expires 14d;
 
         }
 
         }
  
 +
}
 
</pre>
 
</pre>
  
 
Pay attention for few things:
 
Pay attention for few things:
# The sef of Joomla is inside.
+
# 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 argument fastcgi_pass set to 127.0.0.1:9000 as the fpm configured to listen on this port. That's mean you can run the php on separate servers. You can find this configuration in /etc/php/fpm-php5.3/php-fpm.conf/
 
 
# 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 depend on your domain and the path of Joomla on your server.
  
 
== GZip support ==
 
== GZip support ==
If you need GZip compression support add the next code into the http section (inside /etc/nginx/nginx.conf:
+
If you need GZip compression support, add the following section to the http section of the main nginx configuration file:
 
<pre>
 
<pre>
 
         gzip on;
 
         gzip on;
Line 83: Line 83:
 
         gzip_disable    "MSIE [1-6]\.";
 
         gzip_disable    "MSIE [1-6]\.";
 
</pre>
 
</pre>
 +
 +
== Sources ==
 +
* [http://en.gentoo-wiki.com/wiki/Nginx Nginx in Gentoo]
 +
* [http://www.kevinworthington.com/nginx-for-windows/ Nginx for Windows]
 +
* [http://wiki.nginx.org/Install#Ubuntu_PPA Nginx in Ubuntu]
 +
* [http://www.debianadmin.com/howto-install-nginx-webserver-in-debian.html Nginx in Debian]
 +
* [http://php.net/manual/en/install.fpm.php PHP-FPM installation and configuration]
 +
* [http://wiki.nginx.org/HttpGzipModule GZip in Nginx]
 +
* [http://wiki.nginx.org/HttpRewriteModule Rewrite in Nginx]
 +
* [http://nginx.org/en/docs/http/request_processing.html How nginx processes a request]

Revision as of 19:06, 24 October 2012

nginx is a lightweight web server that powers 11.53% 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 asa different 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 configuration 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?q=$uri&$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 for 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 depend 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-javascr$
        gzip_proxied     any;
        gzip_disable     "MSIE [1-6]\.";

Sources[edit]