Run Magento under nginx instead of apache (CentOS Linux)

Question: Why we’d want to use Nginx instead of Apache?

nginx is an HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoev. For a long time, it has been running on many heavily loaded sites. frankly speaking it is more resources cost efficient than apache httpd, as well as the performance.

Accroding to Magento ECG’s test, with nginx, Magento should serve 10 – 15% more concurrent users than apache httpd. Let’s enjory Magento nginx power…

And HOW?

php-fpm is recommended for production environment because it’s faster than any other ones. Let’s take a look how to step-by-step configuration tutorial.

Step 1 – install nginx 1.0.x, php 5.3 (with php-fpm) and mysql 5.5.x

I personally prefer to install them by using yum, it’s really convenient to manage your package, you can choose to compile them by yourself or install by RPM package, if you were like me, just run following command should be enough, to do it, you will need to have CentOS / Redhat Enterprise 6 at least:

yum -y install php php-fpm php-mcrypt php-gd mysql-server nginx

Step 2 – change nginx configuration files

 edit /etc/nginx/conf.d/default.conf:

uncomment following lines in the file:

#location ~ \.php$ {
#     root html;
#     fastcgi_pass;
#     fastcgi_index index.php;
#     fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#     include fastcgi_params;

Within that same block also change:

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;


fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;

It will allow you to execute php now, however, you will need to make a bit more configuration to make Magento to be able to work:

find following lines in the same configuration file:

location / {
    root /usr/share/nginx/html;
    index index.html index.htm
  1. and add index.php into index line
  2. add  try_files $uri $uri/ @handler; after index line
  3. add following lines out of the above bracket:
    location @handler { ## Magento uses a common front handler
        rewrite / /index.php;

Step 3 – check it out

run following command to start both php-fpm and nginx ( remember to stop apache httpd first):

chkconfig php-fpm on
chkconfig nginx on

service php-fpm start
service nginx start

That’s your Magento nginx and enjoy it… Appendix – full content of default.conf:

# The default server

server {
    listen       80;
    server_name  _;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
        try_files $uri $uri/ @handler;

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

    #error_page  404              /404.html;
    #location = /404.html {
    #    root   /usr/share/nginx/html;

    # redirect server error pages to the static page /50x.html
    #error_page   500 502 503 504  /50x.html;
    #location = /50x.html {
    #    root   /usr/share/nginx/html;

    # proxy the PHP scripts to Apache listening on
    #location ~ \.php$ {
    #    proxy_pass;

    # pass the PHP scripts to FastCGI server listening on

    location ~ \.php$ {
        root           html;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        fastcgi_param MAGE_RUN_CODE default;
        fastcgi_param MAGE_RUN_TYPE store;
        include        fastcgi_params;

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one

    location ~ /\.ht {
        deny  all;
Posted in PHP
4 comments on “Run Magento under nginx instead of apache (CentOS Linux)
  1. Nataraj says:

    Its working fine…

  2. magentokey says:

    hello,thanks for sharing it!but i got a error in the step 3: service php-nginx start ,it said no such service。please help me to solve this problem,thanks!

  3. Eddy says:

    Yeah. that work. Perfect,
    thank you

Leave a Reply

Your email address will not be published. Required fields are marked *