Magento Ubuntu Nginx Sample Installation

by: Dirk Siemers | posted: January 11th, 2011

PHP und nginx

PHP5 und nginx installieren:
sudo aptitude install php5-cgi nginx

Erestelle ein PHP5 FastCGI start-up script mit
sudo nano /etc/init.d/php-fastcgi

Folgenden Inhalt einfügen:

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

Das Startscript ausführbar machen:
sudo chmod +x /etc/init.d/php-fastcgi

PHP starten:
sudo /etc/init.d/php-fastcgi start

(Optional) Beim Booten ausführen:
sudo update-rc.d php-fastcgi defaults

Testen

Trage in der nginx server config folgendes ein:

    location ~ \.php$ {
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
        include         fastcgi_params;
    }

nginx neu starten:
sudo /etc/init.d/nginx restart

Erstelle eine Datei in deinem Web-Verzeichnis:

        <?php
          phpinfo();
        ?>

Die erstellte Seite im Browser aufrufen. Nun sollte die PHP Info Seite erscheinen.

Magento installieren:

PHP Pakete für Magento installieren:
sudo apt-get install php5-mcrypt php5-curl php5-gd

Limitierung von Ressourcen. Maximalwert des Speichers erhöhen.
sudo nano /etc/php5/cgi/php.ini
memory_limit = 64M;

Magento aus dem SVN importieren:

        sudo apt-get install subversion 
        cd /var/www
        svn checkout http://svn.magentocommerce.com/source/branches/1.4
        sudo mv 1.4 magento 
        sudo chown -R www-data magento

Magento Datenbank anlegen und Beispieldaten importieren:

        wget http://www.magentocommerce.com/downloads/assets/1.2.0/magento-sample-data-1.2.0.tar.gz
        tar xvfz magento-sample-data-1.2.0.tar.gz
        cd magento-sample-data-1.2.0
        
        mysqladmin -u root -p create magento
        mysql -u root -p magento < magento_sample_data_for_1.2.0.sql
        mv media/* /var/www/magento/media/

Server Config anpassen:

user              nginx;
worker_processes  1;
error_log         /var/log/nginx/error.log;
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;
    autoindex off;
    map $scheme $fastcgi_https { ## Detect when HTTPS is used
        default off;
        https on;
    }

    keepalive_timeout  10;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;

}

Magento für nginx einrichten:

server {
    listen 80 default;
    server_name www.DOMAIN.com *.DOMAIN.com; ## Domain is here twice so server_name_in_redirect will favour the www
    root /var/www/vhosts/DOMAIN.com;
    
    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
    }
    location /minify/ { ## Needed for Fooman Speedster
        rewrite ^/minify/([0-9]+)(/.*\.(js|css))$ /lib/minify/m.php?f=$2&d;=$1 last;
    }
    
    ## These locations would be hidden by .htaccess normally
    location /app/                { deny all; }
    location /includes/           { deny all; }
    location /lib/                { deny all; }
    location /lib/minify/         { allow all; }  ## Deny is applied after rewrites so must specifically allow minify
    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
        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
    }
}

Quellen:

Fork me on GitHub