Shell script implementation launches PHP built-in fastcgi server_linux shell

Source: Internet
Author: User
Tags fpm unix domain socket

A few days ago the work platform from the Ubuntu 9.10 Karmic updated to the 10.04 Lucid, because the LUCID official from the PHP5.3.2, the former use of the source of the dotdeb can not be used, has been very fond of this source, not only provides PHP5.3 but also has a ph P5-FPM this very practical fcgi process Manager, this is not in the official source. Forced on the Dotdeb although it can, but there must be a lot of packages will be dependent on the problem, dealing with these dependencies is a very disturbing thing. Brother is not afraid of anything, afraid of trouble ~:!:

For PHP, PHP-FPM is the most appropriate, spawn-fcgi such things do not have to consider, I would prefer to use the PHP5 built-in FastCGI Server.

Start the server with the following command to listen for Port 9000:

Copy Code code as follows:

Php-cgi-q-B 127.0.0.1:9000 &

Well, with Nginx, you can continue to work, but you'll find 502 bad Gateway errors every once in a while, because the php-cgi process has the maximum number of requests (default 500) to automatically exit.

You need to set two environment variables:

php_fcgi_children– the number of derived child processes
php_fcgi_max_requests– the maximum number of requests that each child process can handle

With these two environment variables, start the built-in FastCGI Server to ensure that PHP can derive the child process to handle the request, rather than being done by the main process. Otherwise, it will appear above said situation, to 500, automatic exit.

Here's a simple script:

Copy Code code as follows:

#!/bin/bash

# # Reference:
##
# # Http://php.net/manual/en/install.unix.lighttpd-14.php
# # HTTP://KOVYRIN.NET/2006/05/30/NGINX-PHP-FASTCGI-HOWTO
##

# # php-cgi File path
Phpfcgi= ' which php-cgi '

# # PID File path
Php_pid= "/tmp/php.pid"

# # Binding TCP Addresses
Fcgi_bind_address= "127.0.0.1:9000"

# # bound to Unix domain socket
#FCGI_BIND_ADDRESS = "/tmp/php.sock"

# # How many PHP child processes derive from
# # where the main process is not included
Php_fcgi_children=16

# # The maximum number of requests per PHP process processed
php_fcgi_max_requests=4096

# # users
Userid=verdana

################## No config below this line

# Toggle Start command, depending on user
if test x$uid = x0; Then
Cmd= "/bin/su-m-C \" $PHPFCGI-Q-B $FCGI _bind_address\ "$USERID"
Else
Cmd= "$PHPFCGI-B $FCGI _bind_address"
Fi

Echo $CMD

# Related environment variables
E= "php_fcgi_children= $PHP _fcgi_children php_fcgi_max_requests= $PHP _fcgi_max_requests"

# Ignore other environment variables, start in a clean environment
Nohup env-$E sh-c "$CMD" &>/dev/null &

# to record the PID of the PHP main process
# $! Return is the PID of SH
# Find the smallest PID in all php-cgi processes, the PID of the main process
Master_pid= ' Ps-e | grep ' php-cgi ' | Sed-n ' 1p ' | awk ' {print '} '
echo $MASTER _pid > "$PHP _pid"

How to close it?
can kill all php-cgi processes at once:

Copy Code code as follows:

Ps-e | grep php-cgi | awk ' {print $} ' | Xargs Kill

The same effect can be achieved only by killing the main process, and all child processes will disappear as well:
Copy Code code as follows:

Cat/tmp/php.pid | Xargs Kill

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.