麒麟系统安装nginx-php7.2.32

安装依赖
1
yum install libxml2 xz-devel  libxml2-devel libjpeg-turbo-devel libpng-devel libicu-devel libxslt-devel gcc g++ cmake freetype-devel
安装 libiconv
1
2
3
4
5
wget http://mirrors.linuxeye.com/oneinstack/src/libiconv-1.16.tar.gz
tar xf libiconv-1.16.tar.gz && cd libiconv-1.16
./configure --prefix=/usr/local/libiconv
make -j 4
sudo make install
安装 openssl
1
2
3
4
5
6
7
8
9
10
11
12
wget http://mirrors.linuxeye.com/oneinstack/src/openssl-1.1.1g.tar.gz
tar xf openssl-1.1.1g.tar.gz && cd openssl-1.1.1g
# 如果不是mips架构
./config -Wl,-rpath=/usr/local/openssl/lib -fPIC --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
# 如果是mips架构
./Configure linux64-mips64 -Wl,-rpath=/usr/local/openssl/lib -fPIC --prefix=/usr/local/openssl --openssldir=/usr/local/openssl

make -j 4
sudo make install
# 添加openssl到lib
sudo echo "/usr/local/openssl/lib" >> /etc/ld.so.conf.d/openssl.conf
ldconfig
安装 curl
1
2
3
4
5
wget http://mirrors.linuxeye.com/oneinstack/src/curl-7.71.1.tar.gz
tar xf curl-7.71.1.tar.gz && cd curl-7.71.1
./configure --prefix=/usr/local/curl --with-ssl=/usr/local/openssl
make -j 4
sudo make install
安装 freetype
1
2
3
4
5
6
7
8
wget http://mirrors.linuxeye.com/oneinstack/src/freetype-2.10.1.tar.gz
tar xf freetype-2.10.1.tar.gz && cd freetype-2.10.1
./configure --prefix=/usr/local/freetype --enable-freetype-config
make -j 4
sudo make install
sudo ln -sf /usr/local/freetype/include/freetype2/* /usr/include/
sudo cp /usr/local/freetype/lib/pkgconfig/freetype2.pc /usr/lib/pkgconfig/
sudo ldconfig
安装 argon2
1
2
3
4
wget http://mirrors.linuxeye.com/oneinstack/src/argon2-20171227.tar.gz
tar xf argon2-20171227.tar.gz && cd argon2-20171227
make -j 4
sudo make install
安装 libsodium
1
2
3
4
5
wget http://mirrors.linuxeye.com/oneinstack/src/libsodium-1.0.18.tar.gz
tar xf libsodium-1.0.18.tar.gz && cd libsodium-1.0.18
./configure --disable-dependency-tracking --enable-minimal
make -j 4
sudo make install
安装 mhash
1
2
3
4
5
6
7
8
9
   wget http://mirrors.linuxeye.com/oneinstack/src/mhash-0.9.9.9.tar.gz
tar xf mhash-0.9.9.9.tar.gz && cd mhash-0.9.9.9
./configure && make -j 4 && make install
# 如果configure报找不到系统类型尝试使用 ./configure --build=arm-linux 构建
############################# 安装re2c ###############################
wget https://jaist.dl.sourceforge.net/project/re2c/1.0.1/re2c-1.0.1.tar.gz
tar xf re2c-1.0.1.tar.gz && cd re2c-1.0.1
./configure && make -j 4
sudo make install
安装 php7.2.32
1
2
wget http://mirrors.linuxeye.com/oneinstack/src/php-7.2.32.tar.gz
tar xf php-7.2.32.tar.gz && cd php-7.2.32
  • 如果是mips架构,修改
    ext/pcre/pcrelib/sljit/sljitNativeMIPS_common.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
    {
    sljit_sw fir = 0; // 这里初始化变量
    switch (feature_type) {
    case SLJIT_HAS_FPU:
    #ifdef SLJIT_IS_FPU_AVAILABLE
    return SLJIT_IS_FPU_AVAILABLE;
    #elif defined(__GNUC__)
    //sljit_sw fir; // 这里注释掉
    asm ("cfc1 %0, $0" : "=r"(fir));
    return (fir >> 22) & 0x1;
    #else
    编译php
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 如果是mips架构, 增加 --host=mips64选项
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc \
    --with-config-file-scan-dir=/usr/local/php/etc/php.d \
    --with-fpm-user=www --with-fpm-group=www --enable-fpm --enable-opcache --disable-fileinfo \
    --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
    --with-iconv-dir=/usr/local/libiconv --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib \
    --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-exif \
    --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --enable-mbregex \
    --enable-mbstring --with-password-argon2 --with-sodium=/usr/local --with-gd --with-openssl=/usr/local/openssl \
    --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp --enable-intl --with-xsl \
    --with-gettext --enable-zip --enable-soap --disable-debug

    make -j 4 && echo $?
    sudo make install && echo $?
  • 报错处理: 错误 undefined t_type
1
2
3
4
echo "/usr/local/lib
/usr/local/lib64
/usr/lib
/usr/lib64" > /etc/ld.so.conf
配置PATH
1
2
export PATH=/usr/local/php/bin/:$PATH
export PATH=/usr/local/php/sbin/:$PATH
配置php.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 复制配置文件
sudo cp php.ini-production /usr/local/php/etc/php.ini

# 内存限制
sed -i "s@^memory_limit.*@memory_limit = 128M@" /usr/local/php/etc/php.ini
# 开启缓冲区,超过4k, 刷新输出
sed -i 's@^output_buffering =@output_buffering = On\noutput_buffering =@' /usr/local/php/etc/php.ini
# 开启php短标签支持
sed -i 's@^short_open_tag = Off@short_open_tag = On@' /usr/local/php/etc/php.ini
# 取消在response header中返回php版本和安装信息
sed -i 's@^expose_php = On@expose_php = Off@' /usr/local/php/etc/php.ini
# cookie放到request全局变量,提高安全性
sed -i 's@^request_order.*@request_order = "CGP"@' /usr/local/php/etc/php.ini
# 时区
sed -i "s@^;date.timezone.*@date.timezone = Asia/Shanghai@" /usr/local/php/etc/php.ini
# post最大字节
sed -i 's@^post_max_size.*@post_max_size = 100M@' /usr/local/php/etc/php.ini
# 上传文件最大字节,不能超过post_max_size
sed -i 's@^upload_max_filesize.*@upload_max_filesize = 50M@' /usr/local/php/etc/php.ini
# 最长执行时间
sed -i 's@^max_execution_time.*@max_execution_time = 600@' /usr/local/php/etc/php.ini
# 设置文件路径缓存大小
sed -i 's@^;realpath_cache_size.*@realpath_cache_size = 2M@' /usr/local/php/etc/php.ini
# 禁用进程相关函数
sed -i 's@^disable_functions.*@disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen@' /usr/local/php/etc/php.ini
# 配置默认的ca证书
sed -i "s@^;curl.cainfo.*@curl.cainfo = \"/usr/local/openssl/cert.pem\"@" /usr/local/php/etc/php.ini
sed -i "s@^;openssl.cafile.*@openssl.cafile = \"/usr/local/openssl/cert.pem\"@" /usr/local/php/etc/php.ini
sed -i "s@^;openssl.capath.*@openssl.capath = \"/usr/local/openssl/cert.pem\"@" /usr/local/php/etc/php.ini
开启opcache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo cat > /usr/local/php/etc/php.d/02-opcache.ini << EOF
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=100000
opcache.max_wasted_percentage=5
opcache.use_cwd=1
opcache.validate_timestamps=1
opcache.revalidate_freq=60
;opcache.save_comments=0
opcache.consistency_checks=0
;opcache.optimization_level=0
EOF
新建服务
  • sudo vim /lib/systemd/system/php-fpm.service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [Unit]
    Description=The PHP FastCGI Process Manager
    Documentation=http://php.net/docs.php
    After=network.target

    [Service]
    Type=simple
    PIDFile=/usr/local/php/var/run/php-fpm.pid
    ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
    ExecReload=/bin/kill -USR2 $MAINPID
    LimitNOFILE=1000000
    LimitNPROC=1000000
    LimitCORE=1000000

    [Install]
    WantedBy=multi-user.target
    新建fpm配置文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48

    sudo cat > /usr/local/php/etc/php-fpm.conf <<EOF
    ;;;;;;;;;;;;;;;;;;;;;
    ; FPM Configuration ;
    ;;;;;;;;;;;;;;;;;;;;;
    ;;;;;;;;;;;;;;;;;;
    ; Global Options ;
    ;;;;;;;;;;;;;;;;;;
    [global]
    pid = run/php-fpm.pid
    error_log = log/php-fpm.log
    log_level = warning
    emergency_restart_threshold = 30
    emergency_restart_interval = 60s
    process_control_timeout = 5s
    daemonize = yes
    ;;;;;;;;;;;;;;;;;;;;
    ; Pool Definitions ;
    ;;;;;;;;;;;;;;;;;;;;
    [www]
    listen = /dev/shm/php-cgi.sock
    listen.backlog = -1
    listen.allowed_clients = 127.0.0.1
    listen.owner = www
    listen.group = www
    listen.mode = 0666
    user = www
    group = www
    pm = dynamic
    pm.max_children = 12
    pm.start_servers = 8
    pm.min_spare_servers = 6
    pm.max_spare_servers = 12
    pm.max_requests = 2048
    pm.process_idle_timeout = 10s
    request_terminate_timeout = 120
    request_slowlog_timeout = 0
    pm.status_path = /php-fpm_status
    slowlog = var/log/slow.log
    rlimit_files = 51200
    rlimit_core = 0
    catch_workers_output = yes
    env[PATH] = /usr/local/bin:/usr/bin:/bin
    env[TMP] = /tmp
    env[TMPDIR] = /tmp
    env[TEMP] = /tmp
    EOF

按机器内存大小修改fpm配置文件

< 3000M

1
2
3
4
5
$Mem = $((`free -m | awk '/Mem:/{print $2}'`/3/20))
sed -i "s@^pm.max_children.*@pm.max_children = $(($Mem/3/20))@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = $(($Mem/3/30))@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = $(($Mem/3/40))@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = $(($Mem/3/20))@" /usr/local/php/etc/php-fpm.conf

3000M - 4500M

1
2
3
4
sed -i "s@^pm.max_children.*@pm.max_children = 50@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 30@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 20@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 50@" /usr/local/php/etc/php-fpm.conf

4500M - 6500M

1
2
3
4
sed -i "s@^pm.max_children.*@pm.max_children = 60@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 40@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 30@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 60@" /usr/local/php/etc/php-fpm.conf

6500M - 8500M

1
2
3
4
sed -i "s@^pm.max_children.*@pm.max_children = 70@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 50@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 40@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 70@" /usr/local/php/etc/php-fpm.conf

8500以上

1
2
3
4
5
6
7
8
9
10
sed -i "s@^pm.max_children.*@pm.max_children = 80@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.start_servers.*@pm.start_servers = 60@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.min_spare_servers.*@pm.min_spare_servers = 50@" /usr/local/php/etc/php-fpm.conf
sed -i "s@^pm.max_spare_servers.*@pm.max_spare_servers = 80@" /usr/local/php/etc/php-fpm.conf

##### 启动php-fpm

```bash
systemctl enable php-fpm
systemctl start php-fpm