【Openresty】Openresty及php-fpm编译安装及优化调整

 

修改Openresty返回服务器名称

OPENRESTY_NAME=

sed -i "s/openresty/$OPENRESTY_NAME/g" ./bundle/nginx-1.*/src/core/nginx.h
sed -i "s/Server: openresty/Server: $OPENRESTY_NAME/g" ./bundle/nginx-1.*/src/http/ngx_http_header_filter_module.c
sed -i "s:<hr><center>openresty</center>:<hr><left>$OPENRESTY_NAME</left>:g" ./bundle/nginx-1.*/src/http/ngx_http_special_response.c
sed -i 's:center:left:g' ./bundle/nginx-1.*/src/http/ngx_http_special_response.c

基础包安装准备

yum install readline-devel pcre-devel openssl-devel gcc libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel -y

nginx.conf优化配置

worker_rlimit_nofile 12800;
提高文件句柄数,与worker数量共同决定nginx最大持有句柄数,保持小于系统句柄限制。

events {
    use epoll; #使用epoll模型
    worker_connections  10000; #提高worker_connection最大连接数
}

http {
    server_tokens      off; #关闭服务器名称返回
    underscores_in_headers on; #开启非标准header支持
}

nginx EL6服务脚本

#!/bin/bash
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
nginx="/usr/local/openresty/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/openresty/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    #make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

nginx EL7服务脚本

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecStartPost=/bin/sleep 1
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

php-fpm适用于zabbix的编译参数

./configure '--prefix=/usr/local/php' \
                 '--with-libxml-dir' \
                 '--enable-sockets' \
                 '--with-mysql=mysqlnd' \
                 '--with-mysqli' \
                 '--with-sqlite3' \
                 '--with-pdo_mysql' \
                 '--with-curl' \
                 '--enable-pcntl' \
                 '--enable-mbstring' \
                 '--with-gettext' \
                 '--enable-bcmath' \
                 '--with-png-dir' \
                 '--with-jpeg-dir' \
                 '--with-freetype-dir' \
                 '--enable-ctype' \
                 '--enable-fpm' \
                 '--with-gd' \
                 '--with-zlib' \
                 '--enable-zip'

php-fpm.conf优化配置

pm = static/dynamic/ondemand
static    响应性能最佳,内存占用最大,只有pm.max_children生效。
dynamic   响应性能略弱,pool数量取决于参数pm.max_children/pm.start_servers/pm.min_spare_servers/pm.max_spare_servers。
ondemand  响应性能最弱,pm.max_children/pm.process_idle_timeout,生效,处于idle超时就会被杀。

对于zabbix这种弱性能场景,使用ondemand可以最小化内存占用。

php.ini优化配置

Zabbix需求配置

max_execution_time = 300
max_input_time = 300
memory_limit = 256M
post_max_size = 16M
always_populate_raw_post_data = -1

php-fpm EL6服务脚本

#!/bin/bash

prefix=/usr/local/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
    try=0

    while test $try -lt 35 ; do

        case "$1" in
            'created')
            if [ -f "$2" ] ; then
                try=''
                break
            fi
            ;;

            'removed')
            if [ ! -f "$2" ] ; then
                try=''
                break
            fi
            ;;
        esac

        echo -n .
        try=`expr $try + 1`
        sleep 1

    done

}

case "$1" in
    start)
        echo -n "Starting php-fpm "

        $php_fpm_BIN --daemonize $php_opts -R

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        fi

        wait_for_pid created $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -QUIT `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if [ ! -r $php_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi

        PID=`cat $php_fpm_PID`
        if ps -p $PID | grep -q $PID; then
            echo "php-fpm (pid $PID) is running..."
        else
            echo "php-fpm dead but pid file exists"
        fi
    ;;

    force-quit)
        echo -n "Terminating php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -TERM `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    reload)

        echo -n "Reload service php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -USR2 `cat $php_fpm_PID`

        echo " done"
    ;;

    *)
        echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
        exit 1
    ;;

esac

php-fpm EL7服务脚本

[Unit]
Description=php-fpm
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecStartPost=/bin/sleep 1
ExecStop=/bin/kill -s 9 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

转载于:https://www.cnblogs.com/caizhifeng/p/10281764.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值