Innovative Open source WEB technologies for everyone

IOWEB Technologies

with our news and our solutions

Magento Nginx hosting in Plesk Onyx

With PLESK Onyx  it’s now possible for hosting providers to completely turn off Apache and use Nginx with PHP-FPM for all requests.

Requirements:

  1. A VPS or Dedicated server with Plesk and root access
  2. A basic understanding of SSH
  3. Knowledge of a shell based text editor (Nano or Vi)

Recommended:

  1. Good understanding of Nginx configuration directives needed  for the systems hosted

HOWTO:

First you have to actually switch to PHP-FPM mode served by NGINX. You can switch to it by going to Domain Settings -> PHP Settings and choose FPM application served by nginx

PHP Settings

Picture 1: Go to domain Settings

PHP FPM Settings plesk

Picture 2: Choose FPM application served by nginx

Secondly you need to go to Apache & nginx Settings and turn off Proxy Mode. This will cause all requests to be served by nginx.

NGINX settings plesk

Picture 3: Choose Apache & nginx Settings

Switch off Proxy mode

Picture 4: Switch off Proxy mode

As soon as you make these changes you will notice your website no longer works. You might get a 504 Gateway error or the homepage might load but Clean / SEF urls will produce a 404 error. This depends on the PLESK minor version since they revamped their VHOSTS templates quite a few times.

This problem is caused because some additional nginx directives are needed. In the case of Apache these are provided by the .htaccess file provided with Magento however nginx cannot read it.

In the case of Magento you will most likely bump into a lot of articles online suggesting a simple configuration like the following:

location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
        expires 30d; ## Assume all files are cachable
    }
 
    ## These locations would be hidden by .htaccess normally
    location ^~ /app/                { deny all; }
    location ^~ /includes/           { deny all; }
    location ^~ /lib/                { deny all; }
    location ^~ /media/downloadable/ { deny all; }
    location ^~ /pkginfo/            { deny all; }
    location ^~ /report/config.xml   { deny all; }
    location ^~ /var/                { deny all; }
 
    location /var/export/ { ## Allow admins only to view export folder
        auth_basic           "Restricted"; ## Message shown in login window
        auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
        autoindex            on;
    }
 
    location  /. { ## Disable .htaccess and other hidden files
        return 404;
    }
 
    location @handler { ## Magento uses a common front handler
        rewrite / /index.php;
    }
 
    location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
        rewrite ^(.*.php)/ $1 last;
    }
 
    location ~ .php$ { ## Execute PHP scripts
        if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
 
        expires        off; ## Do not cache dynamic content
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_param  HTTPS $fastcgi_https;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
        fastcgi_param  MAGE_RUN_TYPE store;
        include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }

ADDITIONAL NGINX DIRECTIVES

However if you try and add the snippet above you’ll bump into a bug in PLESK causing a “duplicate location /” error

Temporarily you can however work around this issue until it’s fixed by the PLESK team.

PLESK fortunately allows us to override the vhost configuration templates  by creating a file in:

/usr/local/psa/admin/conf/templates/custom/domain/nginxDomainVirtualHost.php

Copied from

/usr/local/psa/admin/conf/templates/default/domain/nginxDomainVirtualHost.php

And near line 142 comment out the offending directive like this:

#location ~ /$ {
       <?php //echo $VAR->domain->physicalHosting->proxySettings['directoryIndex'] ?>
#}

After you can enable the additional directives needed for Magento like this :

location / {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
    expires 30d; ## Assume all files are cachable
}

## These locations would be hidden by .htaccess normally
location ^~ /app/                { deny all; }
location ^~ /includes/           { deny all; }
location ^~ /lib/                { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/            { deny all; }
location ^~ /report/config.xml   { deny all; }
location ^~ /var/                { deny all; }

location /var/export/ { ## Allow admins only to view export folder
auth_basic           "Restricted"; ## Message shown in login window
auth_basic_user_file htpasswd; ## See /etc/nginx/htpassword
autoindex            on;
}

location  /. { ## Disable .htaccess and other hidden files
return 404;
}

location @handler { ## Magento uses a common front handler
rewrite / /index.php;
}

location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}

*Beware that removing the directive will likely break other systems that relied on that directive so you will have to manually add the corresponding ones for other sites you host. 

This was tried and tested at the time of writing 03/01/2017 using PLESK Onyx Version 17.0.17 Update #12 and Magento 1.9.x on Debian Jessie

Rate this item
(0 votes)

Related items

Comments powered by CComment

FIND US

Eptapirgiou 149, Sykies
ZIP: 56626
Thessaloniki

Tel: +302314012823


WORKING HOURS

Monday-Friday: 9:00 – 17:00

Search