A Guide to Instal Magento with nginx and Zend Server in a production

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

Hi All, Welcome to my first post on the Invent blog! Something I have been reading a lot about recently and have seen come up a lot on forums as something you guys want to know how to do is how to install Magento with nginx and Zend Server on a production environment. So in this post I aim to talk you through the process in a few simple steps.

Installation

1.   Download the Zend Server installer file.

         2.   Go to http://www.zend.com and login.

3.   Go to downloads and download the install script and continue. In this case I downloaded version 6.1.0.

4.   Extract the tar file: tar -xf ZendServer-6.1.0-RepositoryInstaller-linux.tar.gz

5.   Change directory to the extracted file: cd ZendServer-RepositoryInstaller-linux/

6.   Run the Zend Server installer: sudo sh install_zs.sh 5.3 nginx

      a. You may need to check if the part of install_zs.sh that installs nginx needs sh prepended to it.

7.   Then answer Y to all questions.

8.   This should complete and install Zend in the following location: /usr/local/zend.

9.   Access the Zend Web console, to complete installation. If you are doing this on a remote box make sure that you configure SSH with Port redirect: ssh This email address is being protected from spambots. You need JavaScript enabled to view it. -L 10081:localhost:10081.

10.   Finally access the site with: http://localhost:10081/ZendServer.

 

Configuration

1.   If this is a fresh installation of Linux then you will need to add a directory to hold each of the sites (even if there is just one). As root run: mkdir /sites

2.    For each site you will need to create a user: adduser --home /sites/<USER> --gid nginx <USER>

3.   Make sure you change the password (as this is a production system make sure it is complex) on this user: passwd <USER>

4.   Change the permissions on the user home to 775: chmod 775 /sites/<USER>

5.   Edit /usr/local/zend/etc/php-fpm.conf and find user = nobody and group = nobody. Change this to user = <USER> group = nginx

6.   Now restart the Zend Server so the php-fpm details can be loaded.

7.   Edit /etc/nginx/nginx.conf like follows (note the bold parts):

 

           user <USER> nginx;        

          worker_processes  2;


           error_log  /var/log/nginx/error.log warn;

           pid        /var/run/nginx.pid;


           events {

           worker_connections  1024;

           }

           http {

           include       /etc/nginx/mime.types;

           default_type  application/octet-stream;

           log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

           access_log  /var/log/nginx/access.log  main;

           sendfile        on;

           #tcp_nopush     on;

           client_max_body_size 8m;

           map $http_host $magesite{

              default default;

           }

           autoindex off;

              map $scheme $fastcgi_https { ## Detect when HTTPS is used

              default off;

              https on;

           }

           keepalive_timeout 60;

           gzip on;

           gzip_comp_level 2;

           gzip_proxied any;

           gzip_types text/plain text/css application/x-javascript text/xml application/xml                   application/xml+rss text/javascript;


       include /etc/nginx/conf.d/*.conf;

           

            #ZEND-{3C23C0FCBD20D5A139D33950F63865C8}

            include /usr/local/zend/etc/sites.d/globals-*.conf;

            include /usr/local/zend/etc/sites.d/vhost_*.conf;

            #ZEND-{3C23C0FCBD20D5A139D33950F63865C8}

       }

 

At this point you have the option to change the number of workers to the amount of processors on the box. 

 

8.   Firstly please note that in the previous step if you have a multi-store setup you can map the domains to the store codes. This instance sets a default for the map.

9.   Create a new file called /etc/nginx/conf.d/<USER>.conf

10.   Paste the following in the file and edit the bold parts:

server {

       listen 80;

       server_name localhost;


       access_log /sites/<USER>/access.log main;

       error_log /sites/<USER>/error.log error;

       root /sites/<USER>/;

       proxy_cache off;

       location / {

              include /etc/nginx/fastcgi.conf;

              index index.php index.html index.htm;

              try_files $uri $uri/ @handler;

              expires off;

       }

       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 ~* \.(jpe?g|gif|css|png|js|ico|pdf|zip|tar|t?gz|mp3|wav|swf)$ {

            access_log off; ## Do not log access to these files

            expires 1y;

            log_not_found off;

        }

       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;

              include fastcgi_params; ## See /etc/nginx/fastcgi_params

              location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler

                     rewrite ^(.*.php)/ $1 last;

              }

       }

       #ZEND-{C07594BEBCC4ACA073E4964EF9A576A1}

#             include "/usr/local/zend/etc/sites.d/zend-default-vhost-80.conf";

       #ZEND-{C07594BEBCC4ACA073E4964EF9A576A1}

}

11.   Add the following two lines to /etc/nginx/fastcgi_params
fastcgi_param MAGE_RUN_CODE $magesite; 
fastcgi_param MAGE_RUN_TYPE store;

12.   Remove /etc/nginx/conf.d/default.conf or rename it as something that will not be loaded

13.   Restart nginx: service nginx restart

14.   Install your magento site at /sites/<USER>

And there you have it! Installing Magento with nginx and Zend Server on a production environment in a few easy steps. I hope you find this useful, stay tuned for more posts from me. Coming soon is a follow up post on how to Configure Magento with Memcache and Redis (For Zend Server).

 

 


© 2019 SQLI LTD. All Rights Reserved.