Replacing Apache with NGINX on an AWS Linux server

So I had a huge amount of pain with this and felt it would make sense to collate the action points I’d taken to transition to NGINX.

This was for a working Magento setup, and I just needed to replace the memory-bleeding Apache with something friendlier.

yum install nginx
yum install php-fpm php-common
yum install php-pdo php-mysqlnd php-mysql

Now we have nginx, php-fpm etc installed. Great stuff! Let’s get it running now

chkconfig php-fpm on
service php-fpm start
(NOTE: config at /etc/php-fpm.conf and /etc/php-fpm.d/ if you need it)

Now php-fpm is running. If you like you can telnet localhost:9000 and test it – be sure not to open this port to outsiders as it can be a potential security risk… My AWS config blocks port 9000 outside.

Editing: /etc/nginx/nginx.conf
removed comment from the line "gzip on"
changed the port number from 80 to 81 (for initial testing)
chkconfig nginx on (because I'm sure this is going to work :)
service nginx start
(NOTE: config at /etc/nginx/nginx.conf if you need it)

AND THEN, IT FAILED!! Rofl, so I needed to figure out some serious stuff – which took as much as 20-25 minutes!

What caught me off guard was the nginx.conf server {} config part

listen 81;
 server_name localhost;
 root /var/www/html;

location / {
 try_files $uri $uri/ @handler;
 expires 30d;

# Good security measures
 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 ~ \.php$ {
# if you uncomment this you will be in hell 
  #root html;
 fastcgi_index index.php;

# the line below does not contain document_root by default - you NEED to add it in there!!
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;

# the following 2 pieces are very important too - not there by default
 location @handler {
 rewrite / /index.php;

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