0%

VPSTOOLBOX依赖篇--PHP-FPM

VPSTOOLBOX依赖篇–PHP-FPM

PHP-FPM是VPSTOOLBOX的基础依赖之一。

本文将展示从零开始自己搭建一个PHP-FPM伺服器 ,开启NGINX反向代理的方法,因此限于篇幅以及本人的知识水平,我会展示我所学会的,欢迎各位在评论区交流。


一。 前情提要

问一: 为什么用PHP-FPM ?

答: 因为有些软件是PHP写的(比如TT-rss,roundcube Webmail等),需要这个。

问二: PHP-FPM有什么用 ?

答: 基础依赖罢了。

问三: PHP-FPM对新手友好吗?

答: 是的,安装和配置都不难。


二。 准备工作

准备工作很简单,有一台有独立公网ip的非中国大陆LINUX伺服器即可。


三。 开始搭建

  1. 连接伺服器,我不想教这个,SSH教程请自己搜。

  2. 我个人不使用Centos,因此本教程只涉及Debian/Ubuntu,想看Centos的可以关掉本页面了。

    安装依赖

    1
    apt-get install sudo gnupg2 ca-certificates lsb-release -y

    添加APT源,本文不涉及编译,故deb-src不需要。

    Debian

    1
    2
    3
    wget --no-check-certificate -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    apt-get update

    Ubuntu(我只在18.04上测试过,20.04请自己Google)

    1
    2
    add-apt-repository ppa:ondrej/php -y
    apt-get update

    安装PHP-FPM。

    1
    2
    3
    4
    5
    apt-get -y install php7.4
    systemctl disable --now apache2
    apt-get install php7.4-fpm -y
    apt-get install php7.4-common php7.4-mysql php7.4-ldap php7.4-xml php7.4-json php7.4-readline php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl php7.4-bcmath -y
    apt-get purge apache2* -y
  3. 至此,安装完成,接下来我们需要配置PHP-FPM。

注:你确实可以直接apt install php-fpm,但这样的话就不会是最新版,因此我选择使用上述安装方法。


四。 配置PHP-FPM

  1. 配置PHP-FPM时区

    1
    sed -i "s/;date.timezone.*/date.timezone = UTC/" /etc/php/7.4/fpm/php.ini
  2. 安装PHP-composer

1
2
3
4
cd /etc/php/7.4/
curl -sS https://getcomposer.org/installer -o composer-setup.php
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
cd
  1. 设置PHP-FPM运行用户以及权限,本文使用NGINX,故PHP-FPM以NGINX用户运行。
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
cat > '/etc/php/7.4/fpm/pool.d/www.conf' << EOF
[www]

;prefix = /path/to/pools/$pool

user = nginx
group = nginx

listen = /run/php/php7.4-fpm.sock
; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
;listen.backlog = 511

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. The owner
; and group can be specified either by name or by their numeric IDs.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = nginx
listen.group = nginx
;listen.mode = 0660
;listen.acl_users =
;listen.acl_groups =

; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
; listen.allowed_clients = 127.0.0.1

pm = dynamic

pm.max_children = $(($(nproc --all)*5))

pm.start_servers = $(($(nproc --all)*4))

pm.min_spare_servers = $(($(nproc --all)*2))

pm.max_spare_servers = $(($(nproc --all)*4))

; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
;pm.process_idle_timeout = 10s;

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500

;pm.status_path = /status

;ping.path = /ping

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

; The timeout set by 'request_terminate_timeout' ini option is not engaged after
; application calls 'fastcgi_finish_request' or when application has finished and
; shutdown functions are being called (registered via register_shutdown_function).
; This option will enable timeout limit to be applied unconditionally
; even in such cases.
; Default Value: no
;request_terminate_timeout_track_finished = no

catch_workers_output = no

;security.limit_extensions = .php .php3 .php4 .php5 .php7

; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

; Note: path INI options can be relative and will be expanded with the prefix
; (pool, global or /usr)

; Default Value: nothing is defined by default except the values in php.ini and
; specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f [email protected]
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M
EOF
  1. 重启PHP-FPM服务以重载配置文件
1
2
systemctl restart php7.4-fpm
systemctl enable php7.4-fpm

五。NGINX反向代理配置

1
sudo nano /etc/nginx/conf.d/default.conf

在需要PHP的location里面添加以下配置即可。

1
2
3
4
5
6
7
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

六。总结篇

現在,您已經安裝了PHP-FPM,您可以安装其他以PHP-FPM为依赖的软件了。