Tag: php-fpm

  • CentOS 7 Apache use PHP-FPM

    Install PHP-FPM with command

    yum install php-fpm
    

    Edit www.conf

    vi /etc/php-fpm.d/www.conf
    

    Find

    listen = 127.0.0.1:9000
    

    Replace with

    listen = /var/run/php-fpm/default.sock
    

    Find

    ;listen.owner = nobody
    ;listen.group = nobody
    

    Replace with

    listen.owner = apache
    listen.group = apache
    

    Edit php.conf

    mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.old
    vi /etc/httpd/conf.d/php.conf
    

    Add

    
        	ProxySet disablereuse=off
    
    
    
    	SetHandler proxy:fcgi://php-fpm
    
    
    AddType text/html .php
    
    DirectoryIndex index.php
    
    
    	SetHandler application/x-httpd-php-source
    
    

    Restart Apache/php-fpm

    systemctl restart httpd
    systemctl restart php-fpm
    

    Method 2

    Here is php-fpm config using proxy_fcgi

    https://gist.github.com/serverok/3d2e43bb951ded9a42ce8bc0c2c3b627

  • Enable Error Logging in php-fpm

    Edit your pool file

    vi /etc/php/7.0/fpm/pool.d/www.conf
    

    Add

    catch_workers_output = yes
    php_flag[display_errors] = on
    php_admin_value[error_log] = /var/log/fpm-php.www.log
    php_admin_flag[log_errors] = on
    

    if you only enable “log_errors”, then errors go to default nginx error log.

    php-fpm

  • Install PHP 7.1 php-fpm from source

    Lets install the requirements.

    On CentOS, run

    yum -y install libxml2-devel libcurl-devel libmcrypt-devel openssl-devel
    yum -y install libjpeg-turbo-devel libpng-devel freetype-devel libtidy-devel
    

    Before you can install, you need to check MySQL socket location. To do this, run following command in MySQL command prompt.

    show variables like '%socket%;
    

    Find MySQL socket

    Download and install PHP 7.1 from source

    mkdir /usr/local/src
    cd /usr/local/src
    wget http://php.net/get/php-7.1.12.tar.gz/from/this/mirror -O php-7.1.12.tar.gz
    tar xf php-7.1.12.tar.gz
    cd php-7.1.12
    make clean
    ./configure --prefix=/usr/serverok/php-7.1.12 \
    --with-config-file-path=/usr/serverok/php-7.1.12/etc \
    --with-mysql-sock=/var/lib/mysql/mysql.sock \
    --with-pdo-mysql \
    --with-mysqli \
    --enable-cgi \
    --with-zlib \
    --with-gettext \
    --enable-ftp \
    --enable-calendar \
    --enable-bcmath \
    --enable-sockets \
    --with-curl \
    --with-gd \
    --with-jpeg-dir=/usr/local \
    --enable-mbstring \
    --with-freetype-dir=/usr/local \
    --with-mhash=/usr/local --enable-exif \
    --with-mcrypt=/usr \
    --with-tidy \
    --with-openssl \
    --enable-zip \
    --enable-fpm
    make && make install
    

    Copy the configuration files

    cp /usr/serverok/php-7.1.12/etc/php-fpm.conf.default /usr/serverok/php-7.1.12/etc/php-fpm.conf
    cp /usr/serverok/php-7.1.12/etc/php-fpm.d/www.conf.default /usr/serverok/php-7.1.12/etc/php-fpm.d/www.conf
    cp /usr/local/src/php-7.1.12/php.ini-development /usr/serverok/php-7.1.12/etc/php.ini
    

    Now you will be able to start php-fpm by running

    /usr/serverok/php-7.1.12/bin/php
    

    By default php-fpm will run using tcp port 9000, it is better to change it to use unix socket, for this

    vi /usr/serverok/php-7.1.12/etc/php-fpm.d/www.conf
    

    Find

    listen = 127.0.0.1:9000
    

    Replace with

    listen = /var/run/php-fpm/proxy.sock
    

    php-fpm user

    You need to configure the user account used to run php-fpm, if you don’t use proper account, you may get permission errors.

    The account you use vary depending on OS, Web server. For example, nginx use account nginx. Apache on Ubuntu/Debian use www-data. Apache on CentOS use nobody.

    Here is what i have on a server running Nginx web server

    [root@vps154294 ~]# cat /usr/serverok/php-7.1.12/etc/php-fpm.d/www.conf | grep nginx
    user = nginx
    group = nginx
    listen.owner = nginx
    listen.group = nginx
    [root@vps154294 ~]# 
    

    Systemd Service

    To easily manage php-fpm, lets create a php-fpm.service file.

    Before we do this, edit

    vi /usr/serverok/php-7.1.12/etc/php-fpm.conf
    

    Find

    ;pid = run/php-fpm.pid
    

    Add below

    pid = /run/php-fpm/php-fpm.pid
    

    Find

    ;error_log = log/php-fpm.log
    

    Add below

    error_log = /var/log/php-fpm/error.log
    

    Save and exit.

    Now lets create file /usr/lib/systemd/system/php-fpm.service

    vi /usr/lib/systemd/system/php-fpm.service
    

    Add following content

    [Unit]
    Description=PHP FastCGI process manager
    After=syslog.target network.target
    
    [Service]
    PIDFile=/run/php-fpm/php-fpm.pid
    ExecStartPre=/bin/mkdir --parents /run/php-fpm
    ExecStart=/usr/serverok/php-7.1.12/sbin/php-fpm --nodaemonize
    ExecReload=/bin/kill -USR2 $MAINPID
    Type=simple
    
    [Install]
    WantedBy=multi-user.target
    

    Enable php-fpm with

    systemctl enable php-fpm
    

    Now you will be able to use systemctl to start/stop/restart php-fpm

    systemctl start php-fpm
    systemctl stop php-fpm
    systemctl restart php-fpm
    

    systemctl status php-fpm