From Apache mod_php to php-fpm[]

Source: Internet
Author: User
Tags vps

Many articles say that mod_php cannot be shared with the Apache MPM worker or event, which may have been handed down in the Apache version 2.2 era. Since I compiled Apache 2.4 last year, I have been using the MPM event, and according to the parameters I compiled for PHP 5.5, I have made PHP a built-in module for Apache. Such a wonderful configuration existed more than a year later, I finally decided to transition from mod_php to PHP-FPM.

Since the Apache 2.4.9, the configuration required for PHP-FPM to work with Apache is not as complex as it used to be, which is one of the main reasons I decided to transition.

Recompile PHP, and the previous compilation parameters not too much, just need to remove "--with-apxs2=/usr/local/apache2/bin/apxs", add "--enable-fpm":

./configure--enable-shared--with-libxml-dir--with-gd--with-openssl--enable-mbstring--with-mcrypt--with-mysqli- -with-mysql--enable-opcache--enable-mysqlnd--enable-zip--with-zlib-dir--with-pdo-mysql--with-jpeg-dir-- With-freetype-dir--with-curl--without-pdo-sqlite--without-sqlite3--enable-fpm

Then make, make install. To enable CentOS to boot automatically php-fpm, we need:

CP sapi/fpm/init.d.php-fpm/etc/init.d/php-fpmchmod o+x/etc/init.d/php---add php-fpm

Let PHP-FPM's configuration file take effect:

Mv/usr/local/etc/php-fpm.conf. default /usr/local/etc/php-fpm.conf

According to your own needs to modify this conf file, I personally like:

user == =/dev/shm/php-==static4   2048

If it is a multi-core CPU VPS or server, the above value equals the number of CPUs, if it is a single-core VPS, then Pm.max_children = 2, you can achieve a certain optimization effect.

Again, the Apache side needs a bit of setup. Since I am from mod_php to PHP-FPM, the previous lines in httpd.conf need to be commented out:

LoadModule php5_module modules/libphp5.so<filesmatch \.php$>    SetHandler application/ x-httpd-php</FilesMatch>

Then remove the annotations before mod_proxy.so and mod_proxy_fcgi.so to make sure they are loaded by Apache.

If PHP-FPM is using a TCP socket, add the following at the end of httpd.conf:

<filesmatch \.php$>         "proxy:fcgi://127.0.0.1:9000"</filesmatch >

If you are using a UNIX socket, then add the httpd.conf at the end:

" UNIX:/DEV/SHM/PHP-FPM.SOCK|FCGI://PHP-FPM ">    proxyset disablereuse=off</proxy><filesmatch \.php$>     SetHandler proxy:fcgi://php-fpm</FilesMatch>

At this point configure PHP-FPM end on Apapche:

Service php-fpm startservice httpd Restart

A man of good character will not see the error message. If the PHP file on the access server appears "file not found." Ten is the user and group in the php-fpm.conf do not have read and write permissions, modified to Apache used by the user and group.

Finally attached on the virtual machine AB test Apache mod_php and PHP-FPM results, the test page is the homepage of my blog. CPU 100% is a performance bottleneck during the test, with more memory:

    • Mod_php:requests per second:54.38 [#/sec]
    • Php-fpm:requests per second:67.55 [#/sec]

The performance improvement of the PHP-FPM is still obvious, and the memory footprint is significantly less than mod_php.

----

PHP-FPM Start-up parameters

#测试php-FPM configuration /usr/local/php/sbin/php-fpm-t/usr/local/php/sbin/php-fpm-c/usr/local/php/etc/ Php.ini-y/usr/local/php/etc/php-fpm.conf-t #启动php-fpm/usr/local/php/sbin/php-fpm/usr /local/php/sbin/php-fpm-c/usr/local/php/etc/php.ini-y/usr/local/php/etc/php-fpm.conf #关闭php-- INT ' cat/usr/local/php/var/run/php-fpm.pid ' #重启php--usr2 ' cat/usr/local/php/var /run/php-fpm.pid '

php-fpm.conf Important Parameters Detailed

PID = run/php-fpm.pid#pid settings, default in the installation directory of Var/run/php-Fpm.pid, it is recommended to open Error_log= log/php-fpm.log# error log, default in the installation directory of Var/log/php-Fpm.log Log_level=notice# error level. The available levels are: alert (must be processed immediately), error (Error condition), warning (warning condition), notice (general important information), debug (debug Info). Default: Notice. emergency_ Restart_threshold= -Emergency_restart_interval=60s# means PHP with SIGSEGV or sigbus errors within the values set by Emergency_restart_interval-cgi number of processes if more than Emergency_restart_threshold, php-The FPM will be gracefully restarted. These two options generally leave the default values. Process_control_timeout=0#设置子进程接受主进程复用信号的超时时间. Available units: s (seconds), M (min), H (Hours), or D (days) default units: s (seconds). Default:0. Daemonize=The yes# background performs FPM, the default is yes, and if for debugging you can change to No. In FPM, you can use different settings to run multiple process pools. These settings can be set individually for each process pool. Listen=127.0.0.1:9000#fpm监听端口, the address that PHP handles in Nginx, the general default value can be. Available formats are:'Ip:port','Port','/path/to/unix/socket'. Each process pool needs to be set. Listen.backlog= -1#backlog数,1 means no limit and is determined by the operating system, and this line is commented out. Backlog meaning reference: http://www.3gyou.cc/?p=41listen.allowed_clients=127.0.0.1#允许访问FastCGI进程的IP, set any to No limit IP, if you want to set other hosts Nginx can also access this FPM process, listen to set the cost of access to the IP. The default value is any. Each address is separated by commas. Allows any server to request a connection if it is not set or is empty Listen.owner=Wwwlisten.group=Wwwlisten.mode=0666#unix Socket Settings option, if accessed using TCP, Comment here. User=Wwwgroup=www# Start the process account and group PM=Dynamic#对于专用服务器, the PM can be set to static. #如何控制子进程, the options are static and dynamic. If Static is selected, the number of fixed child processes is specified by Pm.max_children. If dynamic is selected, it is determined by the following argument: Pm.max_children #, maximum number of child processes Pm.start_servers #, number of processes at startup Pm.min_spare_servers #, minimum number of idle processes guaranteed, If the idle process is less than this value, a new child process Pm.max_spare_servers # is created, guaranteeing the maximum number of idle processes, which is cleaned up if the idle process is larger than this value pm.max_requests= +#设置每个子进程重生之前服务的请求数. It is useful for third-party modules that may have a memory leak. If set to'0'The request is always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value:0. Pm.status_path= /the URL of the STATUS#FPM status page. If there is no setting, the status page cannot be accessed. Default value: None. Munin monitoring will be used to Ping.path= /ping#fpm the ping URL of the monitoring page. If not set, the ping page cannot be accessed. This page is used to externally detect whether FPM is alive and can respond to requests. Note that you must start with a slash (/). Ping.response=The pong# is used to define the return of the ping request accordingly. Return as HTTP $The text/Plain formatted text. Default value: Pong. Request_terminate_timeout=0#设置单个请求的超时中止时间. This option may be in the php.ini settings'Max_execution_time'Scripts that are not aborted for some special reasons are useful. Set to'0'Said'Off'You can try changing this option when a 502 error occurs frequently. Request_slowlog_timeout=10s# When a time-out is requested for this setting, the corresponding PHP call stack information is written to the slow log. Set to'0'Said'Off'Slowlog= log/$pool. log.slow# Slow-Request logging, with request_slowlog_timeout use Rlimit_files=1024x768#设置文件打开描述符的rlimit限制. Default value: System-defined value the default open handle is 1024, you can use Ulimit-N View, Ulimit-N 2048 modification. Rlimit_core=0#设置核心rlimit最大限制值. Available values:'Unlimited', 0, or a positive integer. Default value: System-defined value. chroot=#启动时的Chroot目录. The defined directory needs to be an absolute path. If not set, Chroot is not used. ChDir=#设置启动目录, the directory is automatically chdir on startup. The directory you define needs to be an absolute path. Default value: Current directory, or/directory (chroot) Catch_workers_output=yes# redirects the stdout and stderr to the primary error log file during the run. If there are no settings, stdout and stderr will be redirected to the following fastcgi rules./dev/NULL. Default value: Empty.

From Apache mod_php to php-fpm[]

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.