Nginx: Difference between revisions

From Joomla! Documentation

Oc666 (talk | contribs)
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..."
 
Marked this version for translation
 
(32 intermediate revisions by 15 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/].
<noinclude><languages /></noinclude>
<translate><!--T:1-->
[http://nginx.org/ Nginx] is a lightweight Web server that powers [https://en.wikipedia.org/wiki/Nginx about 33%] of Web servers across all domains. Unless you have specific requirements that demand a heavy Web server like Apache, you are much better off using Nginx.</translate>


Here are some instructions on how-to install it on Gentoo Linux machine and install joomla within this server.
<translate><!--T:2-->
Below are instructions on how to get Joomla! running with [https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/ PHP FastCGI Example].</translate>


== PHP installation ==  
<translate>
PHP will run as fastcgi service (fpm), so Nginx server will run php separately (as different process):
== Install Nginx == <!--T:3-->
</translate>
<translate><!--T:4-->
For Ubuntu, run ''aptitude install Nginx''. For other distributions, run the corresponding package manager or see the [https://www.nginx.com/resources/wiki/start/topics/tutorials/install/ Nginx Install page].</translate>
 
<translate>
== Install PHP FastCGI == <!--T:5-->
</translate>
<translate><!--T:6-->
For Ubuntu, read [https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/ PHP FastCGI Example].</translate>
 
<translate><!--T:7-->
For Gentoo, PHP will run as a FastCGI service (FPM), so the Nginx server will run PHP as a separate process:</translate>
<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].
<translate><!--T:8-->
== Install nginx ==
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].</translate>
In Linux you'll need to use emerge command. In other distros it would be apt-get, rpm, etc (depend on the distro).
 
<pre>
<translate>
# emerge nginx
== Configure Nginx == <!--T:9-->
</pre>
</translate>
<translate><!--T:10-->
Nginx configuration files reside in:</translate>
<translate><!--T:11-->
* <tt>/etc/nginx/sites-available/</tt> on Ubuntu (for sites running on that Nginx instance)</translate>
<translate><!--T:12-->
* <tt>/etc/nginx/nginx.conf</tt> on Gentoo and Raspbian (Debian optimized for Raspberry Pi)</translate>


== Configure Nginx ==
<translate><!--T:13-->
Here is an sample Nginx configuration file, ''joomla.conf'', that you can reuse over all your Nginx enabled-sites.</translate>


The configuration of the virtual host on Gentoo lives in /etc/nginx/nginx.conf. Here is example from the author server:
<pre>
<pre>
        server {
server {
                listen 80;
  listen 80;
                server_name YOUR_DOMAIN;
    server_name YOUR_DOMAIN;
                server_name_in_redirect off;
    server_name_in_redirect off;


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


                root PATH_ON_SERVER;
    root PATH_ON_SERVER;
                # this is the sef of Joomla
    index index.php index.html index.htm default.html default.htm;
                location / {
                        try_files $uri $uri/ /index.php?q=$request_uri;
                }


                index index.php index.html index.htm default.html default.htm;
    # Support API
                # unauthorize running scripts inside writable directories
    location /api/ {
                location ~* /(images|cache|media|logs|tmp)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
try_files $uri $uri/ /api/index.php?$args;
                        return 403;
    }
                        error_page 403 /403_error.html;
                }


                location ~ .*.php$ {
    # Support Clean (aka Search Engine Friendly) URLs
                    include /etc/nginx/fastcgi.conf;
    location / {
                    fastcgi_pass  127.0.0.1:9000;
        try_files $uri $uri/ /index.php?$args;
                    fastcgi_index index.php;
    }
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                }


                # caching of files
    # add global x-content-type-options header
                location ~* \.(ico|pdf|flv)$ {
    add_header X-Content-Type-Options nosniff;
                        expires 1y;
                }


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


                location ~* \.(js|css|png|jpg|jpeg|gif|swf|xml|txt)$ {
    location ~ \.php$ {
                        expires 14d;
      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;
    }
}
</pre>
</pre>


Pay attention for few things:
<translate><!--T:14-->
# The sef of Joomla is inside.
Pay attention to a few things:</translate>
# 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/
<translate><!--T:15-->
# Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER depend on your domain and the path of Joomla on your server.
# 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 the ''/etc/php/fpm-php5.3/php-fpm.conf/'' file.</translate>
<translate><!--T:16-->
# Don't forget to replace YOUR_DOMAIN & PATH_ON_SERVER above depending on your domain and the path of Joomla on your server.</translate>
 
<translate>


== GZip support ==
== GZip Support == <!--T:17-->
If you need GZip compression support add the next code into the http section (inside /etc/nginx/nginx.conf:
</translate>
<translate><!--T:18-->
If you need GZip compression support, add the following section to the ''http'' section of the main Nginx configuration file:</translate>
<pre>
<pre>
        gzip on;
gzip on;
        gzip_http_version 1.1;
gzip_http_version 1.1;
        gzip_comp_level 6;
gzip_comp_level 6;
        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]\.";
</pre>
</pre>
<translate>
=== Module ngx_http_gzip_static_module === <!--T:20-->
<!--T:21-->
The '''ngx_http_gzip_static_module''' module allows sending precompressed files with the “.gz” filename extension instead of regular files. The directive enables Nginx to stream directly the pre-gzipped files that Joomla offers. It means less processing power is needed and the files are delivered with a far better compression ratio.
<!--T:22-->
To use it, update the gzip parameters in the ''http'' section of the main Nginx configuration file:
</translate>
<pre>
gzip on;
gzip_static 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]\.";
</pre>
<translate>
<!--T:23-->
'''Note:''' This module is not built by default, it should be enabled with the '''--with-http_gzip_static_module''' configuration parameter. Refer to [https://nginx.org/en/docs/http/ngx_http_gzip_static_module.html ngx_http_gzip_static_module] and [https://nginx.org/en/docs/configure.html Building Nginx from Sources]
</translate>
<translate>
== Sources == <!--T:19-->
</translate>
* [https://wiki.gentoo.org/wiki/Nginx Nginx in Gentoo]
* [https://kevinworthington.com/nginx-for-windows/ Nginx for Windows]
* [https://ubuntu.com/tutorials/install-and-configure-nginx#1-overview Nginx in Ubuntu]
* [https://www.debianadmin.com/howto-install-nginx-webserver-in-debian.html Nginx in Debian]
* [https://www.php.net/manual/en/install.fpm.php PHP-FPM installation and configuration]
* [https://docs.nginx.com/nginx/admin-guide/web-server/compression/ Compression and Decompression]
* [https://www.nginx.com/blog/creating-nginx-rewrite-rules/ Creating NGINX Rewrite Rules]
* [http://nginx.org/en/docs/http/request_processing.html How Nginx processes a request]
[[Category:Server configurations]]

Latest revision as of 15:01, 23 July 2024

Nginx is a lightweight Web server that powers about 33% of Web servers across all domains. 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 PHP FastCGI Example.

Install Nginx

For Ubuntu, run aptitude install Nginx. For other distributions, run the corresponding package manager or see the Nginx Install page.

Install PHP FastCGI

For Ubuntu, read PHP FastCGI Example.

For Gentoo, PHP will run as a FastCGI service (FPM), so the Nginx server will run PHP as a separate 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

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;
    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 API
    location /api/ {
	try_files $uri $uri/ /api/index.php?$args;
    }

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

    # add global x-content-type-options header
    add_header X-Content-Type-Options nosniff;

    # 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 the /etc/php/fpm-php5.3/php-fpm.conf/ file.
  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

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]\.";

Module ngx_http_gzip_static_module

The ngx_http_gzip_static_module module allows sending precompressed files with the “.gz” filename extension instead of regular files. The directive enables Nginx to stream directly the pre-gzipped files that Joomla offers. It means less processing power is needed and the files are delivered with a far better compression ratio.

To use it, update the gzip parameters in the http section of the main Nginx configuration file:

gzip on;
gzip_static 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]\.";

Note: This module is not built by default, it should be enabled with the --with-http_gzip_static_module configuration parameter. Refer to ngx_http_gzip_static_module and Building Nginx from Sources


Sources