WebFaction
Community site: login faq

I have a custom Nginx app serving a PHP website that periodically returns a 502 Bad Gateway response. The way I fix the issue is by restarting the php-cgi process. Has anyone else experienced this? There must be a better way to maintain communication with the front-end server.

Thanks for any help.

asked 27 Oct '13, 09:37

goobertime
1112
accept rate: 0%


The preferred way to run PHP through Nginx is with PHP-FPM. I'd suggest a move over to that method. It's well documented and rock-solid.

permanent link

answered 02 Nov '13, 22:08

HittingSmoke
4822330
accept rate: 8%

I agree. Unfortunately, running PHP-FPM with Nginx on a Webfaction server is not well-documented. It seems that this will be another custom app installation.

(03 Nov '13, 06:01) goobertime

I am actually surprised that even my custom PHP5 apps created through the Webfaction control panel and running on Apache (standard install) are not being served via PHP-FPM. Webfaction is running those with CGI/FastCGI and not FPM/FastCGI.

(03 Nov '13, 06:15) goobertime

PHP-FPM on Webfaction is very easy, you just have to compile PHP yourself with the --enable-fpm flag.

This is the script I use to install PHP 5.4 with FPM support, APC, and Imagick in ~/php5.4: http://pastebin.com/zaJDaT8z

You don't need another custom application. The only time you need to start a custom application is when you need a port open. Nginx and PHP should be communicating via a UNIX socket so you only need a listen port open for Nginx.

(03 Nov '13, 14:25) HittingSmoke

Thanks HittingSmoke! I will give that a shot. I feel like I tried this when I was setting up my Nginx environment and was unable to install with the --enable-fpm flag. I will try again. Surprising that Webfaction doesn't default to FPM.

Any chance you can share more of your knowledge here? For instance, could you tell us everything you did to set listen on the UNIX socket. I've seen some documentation on that and I believe I tried at one point but didn't make much of an effort. Are there any special config options we need to consider when developing in this environment?

Thanks again.

(03 Nov '13, 17:25) goobertime

I'm working on a new deployment/update script system that's modular with different parts of the stack you can mix and match. It's not quite ready to be posted yet but I'll give you the basics.

The UNIX socket thing is easy but it can have some quirks in some instances, particularly if you're running your own database server.

For just Nginx/PHP-FPM it's pretty simple though. That deployment script I posted sets up the PHP-FPM socket at ~/php5.4/var/run/php5-fpm.sock.

In your Nginx vhost for the site you want to serve PHP on you'd set up a location block to parse PHP files as such:

    location ~* \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/home/${USER}/php5.4/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME  $fastcgi_script_name;
    }

(replace ${USER} with your Webfaction username)

This will send all PHP files requested through that vhost to PHP-FPM via a UNIX socket.

As for why Webfaction doesn't use Nginx/PHP-FPM by default I believe mostly has to do with Nginx's inability to parse .htaccess and per-directory php.ini. Using a shared Nginx instance to serve a web site severely limits your ability to configure your web site since Nginx only supports configuration through config files. Apache is much more forgiving in this respect, but with an overhead cost.

Here is my Nginx install script wich will enable PHP through PHP-FPM (if you've installed it with my other script) in the default vhost: http://pastebin.com/abmPcxdB

Note that this installs with the Google Pagespeed module which you may or may not want. By default it enables only the default safe options. You can read more about the Pagespeed module here: https://developers.google.com/speed/pagespeed/module

(04 Nov '13, 02:42) HittingSmoke

This is fantastic. Thank you so much HittingSmoke!

(04 Nov '13, 06:47) goobertime

YES! Whoo hoo finally some good info on this. I been looking everywhere. SO me commands in shell are limited. but this pretty much sums it up. Great write up HittingSmoke.

(06 Nov '13, 10:28) MITCHY
showing 5 of 7 show 2 more comments

Try setting up a script on cron which monitors for the existence of the processes PID, this thread provides a demo script which can be modified for this purpose.

permanent link

answered 27 Oct '13, 17:13

johns
5.4k412
accept rate: 23%

Thanks for the response johns!

Unfortunately, this is not the solution I was hoping for. The site should not have these issues communicating with webfaction's front-end. The cron would be a sub-optimal solution and would open the door to the site being down for up to 1 minute.

(27 Oct '13, 19:15) goobertime

Submit a support ticket so we can investigate directly.

(28 Oct '13, 17:00) johns

Thanks again Johns. I created a ticket. It's really a request for the PHP-FPM install and some documentation about using it on Webfaction servers.

Webfaction is great. Thank you for all your help.

(03 Nov '13, 13:54) goobertime

how did you install php along with nginx? I have nginx setup but will not read any php files.

permanent link

answered 31 Oct '13, 16:36

MITCHY
1114
accept rate: 0%

You need to compile PHP with the correct configure line options and tell Nginx via a location block in the config to serve PHP files through PHP.

(02 Nov '13, 22:13) HittingSmoke

MITCHY, I am pretty confident that I did not install PHP. I did install Nginx. The key for me was to start the PHP-CGI process that listens for requests. The command:

php-cgi -b 127.0.0.1:9000 &

If you have a CGI process manager running and the Nginx server is listening on the right port and with the proper location blocks in your config, then you should be good to go.

(03 Nov '13, 14:02) goobertime
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×186
×1

question asked: 27 Oct '13, 09:37

question was seen: 15,109 times

last updated: 06 Nov '13, 10:28

                              
WEBFACTION
REACH US
SUPPORT
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM