File: php

package info (click to toggle)
nginx 1.18.0-6.1%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,344 kB
  • sloc: ansic: 250,653; perl: 7,548; sh: 1,408; ruby: 879; python: 358; makefile: 338; awk: 36; cpp: 18
file content (119 lines) | stat: -rw-r--r-- 3,708 bytes parent folder | download | duplicates (14)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
##
#  File:
#    php
#  Description:
#    This file is meant to help users get a basic understanding of a PHP stack
#    with Nginx as the web server.
##

# Note: This is not a configuration sample and comment lines will be userd differently.


== PHP Options ==

There are a number of options that can be used to provide PHP. The two most
common methods are php-cgi and php-fpm. The php-fpm option is relatively new
and is not yet a standard option. This package is stable however and is moving
toward being a standard option in distribution repositories.

== PHP-FPM ==

The php-fpm option is considerably harder to debug. However, the hardest issues
to debug should be solved by including that fastcgi_params file provided by
this package. It should at a minimum remove all silent errors.

# sudo apt-get install php5-fpm

If you do not have php5-fpm available, you will want to add the repository for
the package.  https://launchpad.net/~nginx/+archive/php5

In php5-fpm, you will want to edit the php pool.
Edit /etc/php5/fpm/pool.d/www.conf

The listen directive is the most important piece in this file. It is suggested
to listen to a local unix socket. This listen directive will be used in your
nginx configuration.
Example: listen = /tmp/phpfpm.socket

The rest of this file can be tweaked to your liking.

== PHP-CGI ==

The simplest and easiest method to run PHP is to use php-cgi. It does not offer
the ability to monitor and restart processes that hang or die however.

# sudo apt-get install php5-cgi

To make php5-cgio work, you will need to create an init script.

===== FILE: /etc/init.d/phpcgi =====
#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          php-fcgi
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-cgi processes
# Description:       starts php-cgi using start-stop-daemon for each user
### END INIT INFO

# Number of PHP processes to be able to handle connections.
CHILD=10
# Maximum number of requests each child should handle before being regenerated
MAX_REQS=750

start() {
        start-stop-daemon --quiet --start --background --chuid "www-data" \
          --exec /usr/bin/env \
          -- - USER="www-data" \
          PATH=/usr/bin PHP_FCGI_CHILDREN=$CHILD PHP_FCGI_MAX_REQUESTS=$MAX_REQS \
          php-cgi -b /tmp/phpcgi.socket &
}

stop() {
        killall -w php-cgi
        rm /tmp/phpcgi.socket
        sync
        sleep 1
}

case "$1" in
        start) start;;
        stop) stop;;
        restart) stop; start;;
        *) echo "Usage: php-fastcgi {start|stop|restart} [user]"; exit 1;;
esac
===== END FILE =====

# Make file executable
chmod +x /etc/init.d/phpcgi

# Add file to startup
update-rc.d phpcgi defaults

== Using PHP in Nginx ==

In order to use the sockets you created (/tmp/phpfpm.socket or /tmp/phpcgi.socket)
you will need to add a php block to your Nignx configuration.

# This block adds a little security.
# See /usr/share/doc/nginx/examples/drupal for context
location ~ \..*/.*\.php$ {
	return 403;
}

# This is basic PHP block that can be used to handle all PHP requests.
# See /usr/share/doc/nginx/examples/drupal for context
location ~ \.php$ {
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	include fastcgi_params;
	# Intercepting errors will cause PHP errors to appear in Nginx logs
	fastcgi_intercept_errors on;
	fastcgi_pass unix:/tmp/phpcgi.socket;
}

# The above example will use php5-cgi which is bound to /tmp/phpcgi.socket.
# If you choose to use php5-fpm the example above will bind to /tmp/phpcgi.socket
# instead and this should be used for fastcgi_pass instead.