Magento on Nginx Issue
Small back story, I initially moved from Dreamhost last year to Linode for more flexibility and ran this StackScript to get the ball rolling "Better WordPress with WP SuperCache & Nginx" StackScript at revision 14407 - and I really dig it as most of my sites are custom rolled PHP or Wordpress.
Now, the issue, I'm trying to stand up Magento for a small shop (that will eventually grow to a larger catalog) and getting 502 Bad Gateway. I've googled LEMP+Magento, Nginx+Magento and various other flavors but none of the responses seem to get at the issue properly.
My first attempt was to follow this guide:
Other attempts led me around this post:
So I thought I'd shoot this over to the Linode side as the Magento folks (from dealing with in the past) have always seemed snarky and generally not into being helpful.
My current config: (domain sanitized for safety, not sure if this config would really be dangerous however)
server {
listen 80;
rewrite / $scheme://$host$request_uri permanent; ## Forcibly remove a www
}
server {
listen 80 default;
server_name my.domain.com;
root /srv/www/my.domain.com/public_html;
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 /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; ## Removed for now as PayPal will handle secure transactions, and it threw an error
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
There you have it, any Nginx wizards see what I'm doing wrong, or have a better resource I can comb through perhaps?
Thanks all in advance!
20 Replies
@Net-burst:
It looks like that you dont have any backend at 127.0.0.1:9000. What backend do you use for php: apache or php-fpm?
Here is where my ineptitude will shine, I'd go with php-fpm if those are my only two choices, I never installed apache on the box. But I do have /etc/php5/fpm/ if that helps.
Sorry for my lack of knowledge in this, I'm not by nature a sysadmin I just muddle through as best I can.
If you have a little insight to what you'd be my hero!
Thanks for any help!
Also what does aptitude show php5-fpm say?
@obs:
To be honest you might as well upgrade and get it out of the way now then all your site disruption is in one go, once upgraded you'll be set for the next five years.
Also what does aptitude show php5-fpm say?
The command gives me:
Package: php5-fpm
New: yes
State: installed
Automatically installed: no
Version: 5.3.3-1ubuntu9.6
Priority: optional
Section: universe/php
Maintainer: Ubuntu Developers <
Uncompressed Size: 7,811k
Depends: libbz2-1.0, libc6 (>= 2.11), libcomerr2 (>= 1.01), libdb4.8,
libevent-1.4-2 (>= 1.4.13-stable), libgssapi-krb5-2 (>= 1.6.dfsg.2),
libk5crypto3 (>= 1.6.dfsg.2), libkrb5-3 (>= 1.6.dfsg.2), libpcre3 (>=
7.7), libssl0.9.8 (>= 0.9.8m-1), libxml2 (>= 2.7.4), zlib1g (>=
1:1.1.4), mime-support, php5-common (= 5.3.3-1ubuntu9.6), libmagic1,
ucf, tzdata
Suggests: php-pear
Provides: phpapi-20090626+lfs
Description: server-side, HTML-embedded scripting language (FPM-CGI binary)
This package provides the Fast Process Manager interpreter that runs as a
daemon and receives Fast/CGI requests. Note that MOST Apache users probably
want the libapache2-mod-php5 package. The following extensions are built in:
bcmath bz2 calendar Core ctype date
dba dom ereg exif fileinfo filter ftp gettext hash iconv json libxml
mbstring mhash openssl pcre Phar posix Reflection session shmop SimpleXML
soap sockets SPL standard sysvmsg sysvsem sysvshm tokenizer wddx xml
xmlreader xmlwriter zip zlib.
PHP5 is an HTML-embedded scripting language. Much of its syntax is borrowed
from C, Java and Perl with a couple of unique PHP-specific features thrown in.
The goal of the language is to allow web developers to write dynamically
generated pages quickly. This version of PHP5 was built with the Suhosin patch.
Homepage:
Also, in regards to the upgrading, I'm just not sure how to do that. I have a enough Linux knowledge to get myself into and out of trouble in most cases but I've never upgraded one via shell. I will try and do some research on the process but I'm just in the unknown camp right now, like would I have to set up all of my sites again and so on?
Here is my config. I do have fpm installed (I actually have magento standing on apache on my linode right now, having the issue on a local 12.04 vmware install for testing)
The issue is I get "Error parsing body - doesn't seem to be a chunked message"
What does the output of
ps aux | grep php
and
netstat -lpnt
say? Let's see if PHP running and listening.
@obs:
Ok to upgrade to 12.04 check out
https://help.ubuntu.com/community/PreciseUpgrades I've not done it myself but I've heard that upgrading's been pretty painless from the forums.What does the output of
ps aux | grep php
andnetstat -lpnt
say? Let's see if PHP running and listening.
Okay: ps aux | grep php
root 2532 0.0 0.2 39328 1180 ? Ss 2011 76:22 /usr/sbin/php5-fpm --fpm-config /etc/php5/fpm/main.conf
www-data 31561 0.0 5.9 61744 30212 ? S 12:46 0:17 /usr/sbin/php5-fpm --fpm-config /etc/php5/fpm/main.conf
www-data 31562 0.0 7.0 67328 35796 ? S 12:46 0:13 /usr/sbin/php5-fpm --fpm-config /etc/php5/fpm/main.conf
www-data 31563 0.0 6.6 65224 33696 ? S 12:46 0:12 /usr/sbin/php5-fpm --fpm-config /etc/php5/fpm/main.conf
www-data 31564 0.0 6.4 64140 32612 ? S 12:47 0:14 /usr/sbin/php5-fpm --fpm-config /etc/php5/fpm/main.conf
root 31917 0.0 0.1 4008 752 pts/0 S+ 21:35 0:00 grep php
And netstat -lpnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 2149/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24017/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2068/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2433/exim4
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 24017/nginx
tcp6 0 0 :::22 :::* LISTEN 2068/sshd
tcp6 0 0 ::1:25 :::* LISTEN 2433/exim4
Hopefully that helps narrow down the issue, stabbing at it the 0.0.0.0:80 for Nginx seems a tad off but I have no issues with any other sites or software so I'm just not sure. Again, n00b with some of this stuff.
I dug around a little on the related info and php-fpm, loopback and the likes and followed the concept a bit from
Speaking of which, I'm prepping backups incase an upgrade goes completely wrong for me. I have my current Linode set up with three partitions, but cannot find a way to access the third partition I created for rsync backup purposes, I'd love to be able to utilize that to "clone" my current server configs and such in case something went awry, we'll see if I figure that one out.
Back to this issue as I've gone off topic. When I try to restart php-fpm I get this, which seems as though something could be awry elsewhere.
/etc/init.d/php5-fpm restart
* Stopping PHP5 FPM... [ OK ]
* Starting PHP5 FPM... PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626+lfs/xml.so' - /usr/lib/php5/20090626+lfs/xml.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning: Module 'curl' already loaded in Unknown on line 0
PHP Warning: Module 'xmlrpc' already loaded in Unknown on line 0
May 04 21:50:56.678053 [WARNING] [pool www] pm.start_servers is not set. It's been set to 3.
As for php-fpm what's the contents of /etc/php5/fpm/main.conf
@obs:
You can turn on linode backups to backup your node before upgrading it's probably the easiest solution (and not expensive) you can submit a ticket to turn them off when you don't need them anymore.
As for php-fpm what's the contents of /etc/php5/fpm/main.conf
I did that last night and am now running on 12.04 with no issues as far as content goes, my php is throwing some errors in the cron job ever 30 minutes so I'll need to look into that.
Error
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626+lfs/xml.so' - /usr/lib/php5/20090626+lfs/xml.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning: Module 'curl' already loaded in Unknown on line 0
PHP Warning: Module 'xmlrpc' already loaded in Unknown on line 0
And my fpm/main.conf
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;
;include=/etc/php5/fpm/*.conf
;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;
[global]
pid = /var/run/php5-fpm.pid
error_log = /var/log/php5-fpm.log
;log_level = notice
;emergency_restart_threshold = 0
;emergency_restart_interval = 0
;process_control_timeout = 0
;daemonize = yes
;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;
include=/etc/php5/fpm/pool.d/*.conf
I eliminated the commented stuff as that's nothing truly relevant. A lot of variables are commented out so I assume they're using defaults.
The other two are trying to load a module twice.
As for PHP fpm…not helpful the configs have changed since I last looked what's do all the files in /etc/php5/fpm/pool.d/*.conf contain?
@obs:
The php errors will be from borked php.ini files, the first one is trying to load a module that doesn't exist (did you install any via pecl by any chance?)
The other two are trying to load a module twice.
As for PHP fpm…not helpful the configs have changed since I last looked what's do all the files in /etc/php5/fpm/pool.d/*.conf contain?
I've never messed with pecl, I had to do a quick search just now (by nature not a server admin but command line cozy).
I did locate the references in the php.ini file (of which there's two, and I'll investigate this further) and comment out the xml.so and the two that were loading twice so hooray for not getting a cron email of errors every thirty minutes, lol.
The file I have in that directory is
; Start a new pool named 'www'.
[www]
listen = /var/run/php-fpm.sock
listen.backlog = 1024
;listen.allowed_clients = 127.0.0.1
listen.owner = www-data
listen.group = www-data
listen.mode = 0600
user = www-data
group = www-data
pm = dynamic
pm.max_children = 12
;pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 1024
;pm.status_path = /status
;ping.path = /ping
;ping.response = pong
;request_terminate_timeout = 0
;request_slowlog_timeout = 0
;slowlog = /var/log/php-fpm.log.slow
;rlimit_files = 1024
;rlimit_core = 0
;chroot =
chdir = /var/www
;catch_workers_output = yes
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M
Also you may want to drop pm.max_children = 12 in your fpm
@obs:
Ok in your nginx config file where it says fcgi_pass change it to /var/run/php-fpm.sock then restart nginx.
Also you may want to drop pm.max_children = 12 in your fpm
www.conf to say 6, if you have some high memory php processes it could cause problems.
IT WORKED, YOU ARE A GOD!!!
Thank you so very much I was pulling out my hair on this one. You rock!