diff options
author | Piotr Rogoża | 2015-06-14 15:06:00 +0200 |
---|---|---|
committer | Piotr Rogoża | 2015-06-14 15:06:00 +0200 |
commit | 719ead9a2e785c67f7701bd56326f9cb8bceb66a (patch) | |
tree | b122af7ad80d83dba48756859490255d8ca14893 | |
download | aur-719ead9a2e785c67f7701bd56326f9cb8bceb66a.tar.gz |
Initial import
-rw-r--r-- | .SRCINFO | 28 | ||||
-rw-r--r-- | PKGBUILD | 53 | ||||
-rw-r--r-- | nginx.conf.example | 138 | ||||
-rw-r--r-- | public_html.conf.example | 17 | ||||
-rw-r--r-- | server.conf.example | 51 | ||||
-rw-r--r-- | spawn-fcgi-php.conf | 60 | ||||
-rwxr-xr-x | spawn-fcgi-php.rc.d | 463 | ||||
-rwxr-xr-x | spawn-fcgi-php.script | 430 | ||||
-rw-r--r-- | spawn-fcgi-php.service | 14 |
9 files changed, 1254 insertions, 0 deletions
diff --git a/.SRCINFO b/.SRCINFO new file mode 100644 index 000000000000..4ec2584e9eea --- /dev/null +++ b/.SRCINFO @@ -0,0 +1,28 @@ +pkgbase = spawn-fcgi-php + pkgdesc = Script to provide simple cgi-php support for nginx http daemon + pkgver = 1.2 + pkgrel = 1 + url = http://aur.archlinux.org/packages.php?ID=37439 + arch = any + license = GPL + depends = php-cgi + depends = spawn-fcgi + options = !strip + backup = etc/conf.d/spawn-fcgi-php.conf + source = spawn-fcgi-php.rc.d + source = spawn-fcgi-php.conf + source = spawn-fcgi-php.service + source = spawn-fcgi-php.script + source = server.conf.example + source = nginx.conf.example + source = public_html.conf.example + md5sums = fefc431b8fc180b3a68e597cbff5cb3a + md5sums = 58e5db0b9d10b2e2fb4784a42daffd03 + md5sums = 300a6f174a552ab1981ccf7607615904 + md5sums = 44e4278c673e63f1033f5fe23463b26e + md5sums = f7d68bcf67a05b39ead0c9ec8f87d863 + md5sums = 8f0aa0bd87e43e3684e0367c6d8682b1 + md5sums = f3bf2492b92224d71cc441e439721adc + +pkgname = spawn-fcgi-php + diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 000000000000..618cb2535be0 --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,53 @@ +# Maintainer: Piotr Rogoża <rogoza dot piotr at gmail dot com> +# Contributor: Piotr Rogoża <rogoza dot piotr at gmail dot com> +# vim:set ts=2 sw=2 et ft=sh tw=100: expandtab + +pkgname=spawn-fcgi-php +pkgver=1.2 +pkgrel=1 +pkgdesc="Script to provide simple cgi-php support for nginx http daemon" +arch=('any') +url="http://aur.archlinux.org/packages.php?ID=37439" +license=('GPL') +depends=('php-cgi' 'spawn-fcgi') +backup=('etc/conf.d/spawn-fcgi-php.conf') +source=( + 'spawn-fcgi-php.rc.d' + 'spawn-fcgi-php.conf' + 'spawn-fcgi-php.service' + 'spawn-fcgi-php.script' + 'server.conf.example' + 'nginx.conf.example' + 'public_html.conf.example' +) +options=(!strip) + +build() { + # Install script and configuration file + install -Dm755 "$srcdir"/spawn-fcgi-php.rc.d \ + "$pkgdir"/etc/rc.d/spawn-fcgi-php + install -Dm644 "$srcdir"/spawn-fcgi-php.conf \ + "$pkgdir"/etc/conf.d/spawn-fcgi-php.conf + + # Install systemd's files + install -Dm755 "$srcdir"/spawn-fcgi-php.script \ + "$pkgdir"/usr/lib/systemd/scripts/spawn-fcgi-php + install -Dm644 "$srcdir"/spawn-fcgi-php.service \ + $pkgdir/usr/lib/systemd/system/spawn-fcgi-php.service + + + # Install examples of configuration + install -Dm644 "$srcdir"/public_html.conf.example \ + "$pkgdir"/etc/nginx/public_html.conf.example + install -Dm644 "$srcdir"/server.conf.example \ + "$pkgdir"/etc/nginx/conf/server.conf.example + install -Dm644 nginx.conf.example \ + "$pkgdir"/etc/nginx/conf/nginx.conf.example +} +md5sums=('fefc431b8fc180b3a68e597cbff5cb3a' + '58e5db0b9d10b2e2fb4784a42daffd03' + '300a6f174a552ab1981ccf7607615904' + '44e4278c673e63f1033f5fe23463b26e' + 'f7d68bcf67a05b39ead0c9ec8f87d863' + '8f0aa0bd87e43e3684e0367c6d8682b1' + 'f3bf2492b92224d71cc441e439721adc') diff --git a/nginx.conf.example b/nginx.conf.example new file mode 100644 index 000000000000..63dac0ed3c26 --- /dev/null +++ b/nginx.conf.example @@ -0,0 +1,138 @@ +# vi: ft=nginx +#user http; +worker_processes 1; + +#error_log logs/error.log; +#error_log logs/error.log notice; +#error_log logs/error.log info; + +#pid logs/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include mime.types; + default_type application/octet-stream; + + #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + # '$status $body_bytes_sent "$http_referer" ' + # '"$http_user_agent" "$http_x_forwarded_for"'; + + #access_log logs/access.log main; + + sendfile on; + #tcp_nopush on; + + #keepalive_timeout 0; + keepalive_timeout 65; + + #gzip on; + + # the file with mapping username per port + include /etc/nginx/map_user2port.conf; + #or only + #include map_user2port.conf + + ## + # nginx-naxsi config + ## + + #include /etc/nginx/naxsi_core.rules; + + ## + # nginx-passenger config + ## + + #passenger_root /usr; + #passenger_ruby /usr/bin/ruby; + + Include server.conf.example; + + server { + listen 80; + server_name localhost; + + #charset koi8-r; + + #access_log logs/host.access.log main; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + #location ~ \.php$ { + # root html; + # fastcgi_pass 127.0.0.1:9000; + # fastcgi_index index.php; + # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; + # include fastcgi_params; + #} + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} + } + + + # another virtual host using mix of IP-, name-, and port-based configuration + # + #server { + # listen 8000; + # listen somename:8080; + # server_name somename alias another.alias; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + + + # HTTPS server + # + #server { + # listen 443; + # server_name localhost; + + # ssl on; + # ssl_certificate cert.pem; + # ssl_certificate_key cert.key; + + # ssl_session_timeout 5m; + + # ssl_protocols SSLv2 SSLv3 TLSv1; + # ssl_ciphers HIGH:!aNULL:!MD5; + # ssl_prefer_server_ciphers on; + + # location / { + # root html; + # index index.html index.htm; + # } + #} + +} diff --git a/public_html.conf.example b/public_html.conf.example new file mode 100644 index 000000000000..9f21c5e9c1d9 --- /dev/null +++ b/public_html.conf.example @@ -0,0 +1,17 @@ +# vi: ft=nginx +location ~ ^/~([^/]+)(/?.*)$ { + set $username $1; + alias /home/$1/public_html/$2; + autoindex on; + location ~ \.php { + #fastcgi_pass 127.0.0.1:9000; + # or for multiusers, one port per user + fastcgi_pass 127.0.0.1:$port; + # or for unix domain sockets + #fastcgi_pass unix:/run/spawn-fcgi/spawn-fcgi.$username.sock; + fastcgi_index index.php; + include /etc/nginx/fastcgi_params; + #moved to /etc/nginx/fastcgi_params + #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } +} diff --git a/server.conf.example b/server.conf.example new file mode 100644 index 000000000000..27af7a7f422a --- /dev/null +++ b/server.conf.example @@ -0,0 +1,51 @@ +# vi: ft=nginx +server { + listen 80; + server_name domain.tld; + + #charset koi8-r; + + #access_log logs/host.access.log main; + #error_log logs/error.log; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + + #error_page 404 /404.html; + + # redirect server error pages to the static page /50x.html + # + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + + # proxy the PHP scripts to Apache listening on 127.0.0.1:80 + # + #location ~ \.php$ { + # proxy_pass http://127.0.0.1; + #} + + # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 + # + location ~ \.php { + # TCP + fastcgi_pass 127.0.0.1:9000; + # or SOCKET + #fastcgi_pass unix:/run/spawn-fcgi/spawn-fcgi.sock; + fastcgi_index index.php; + include /etc/nginx/fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } + + # deny access to .htaccess files, if Apache's document root + # concurs with nginx's one + # + #location ~ /\.ht { + # deny all; + #} + + include public_html.conf.example; +} diff --git a/spawn-fcgi-php.conf b/spawn-fcgi-php.conf new file mode 100644 index 000000000000..1c1a2fa5967c --- /dev/null +++ b/spawn-fcgi-php.conf @@ -0,0 +1,60 @@ +# vi: ft=sh +# run PHPCGI as +USER=http +GROUP=http + +# Name of daemon +DAEMON="/usr/bin/spawn-fcgi" +PIDFILE="/run/spawn-fcgi-php/spawn-fcgi-php.pid" +# spawn the process +PHPCGI="/usr/bin/php-cgi" + +#------------------------------------------------------------------------------- +# Options for php-cgi +#------------------------------------------------------------------------------- +# (PHP only) Number of children to spawn +PHP_FCGI_CHILDREN=4 +# or export PHP_FCGI_CHILDREN +# PHP FastCGI proccess exit after requests: +export PHP_FCGI_MAX_REQUESTS=1000 +# Choose ONE of PORT/IPADDR (for TCP) OR SOCKET (for unix domain sockets) +IPADDR=127.0.0.1 +PORT=9000 +#SOCKET=/run/spawn-fcgi-php/spawn-fcgi.sock +# Options only for socket +#MODE=0644 +#SOCKET_USER=other +#SOCKET_GROUP=other + +#------------------------------------------------------------------------------- +# Options for spawn-fcgi +#------------------------------------------------------------------------------- +# Number of children to fork, defaults to 1. +SPAWN_FCGI_CHILDREN=1 + + +#------------------------------------------------------------------------------- +# For many users +#------------------------------------------------------------------------------- +# Run separate processes for users with UIDs from the range RANGEUID +# uncomment STARTMULTI if you want this +STARTMULTI=yes +# Base user's socket, uncomment to use socket instead IPADDR:USERPORT +#USERSOCKET='/run/spawn-fcgi-php/spawn-fcgi' +# file with mapping user to port +MAPUSER2PORT='/etc/nginx/map_user2port.conf' +# Base user's pidfile +#USERPIDFILE='/run/spawn-fcgi-php/spawn-fcgi-php' +# range for UIDs, use the dash for range and split groups by spaces e.g. 1000-1003 1005 1010-1020, use parenthis for all +RANGEUID=(1000) +# (PHP only) Number of children to spawn +USER_PHP_FCGI_CHILDREN=1 +# for status, comment if you don't want print status for users +#USERSSTATUS=yes + +OPTIONS="-u $USER -g $GROUP -f $PHPCGI -C ${PHP_FCGI_CHILDREN:-4} ${PIDFILE:+-P $PIDFILE} -F ${SPAWN_FCGI_CHILDREN:-1} " +if [ -n "$SOCKET" ]; then + OPTIONS+="-s $SOCKET ${MODE:+-M $MODE} ${SOCKET_USER:+-U $SOCKET_USER} ${SOCKET_GROUP:+-G $SOCKET_GROUP} " +else + OPTIONS+="-a $IPADDR -p $PORT " +fi diff --git a/spawn-fcgi-php.rc.d b/spawn-fcgi-php.rc.d new file mode 100755 index 000000000000..b5b814c5ae61 --- /dev/null +++ b/spawn-fcgi-php.rc.d @@ -0,0 +1,463 @@ +#!/bin/bash +# vi: ft=sh + +. /etc/rc.conf +. /etc/rc.d/functions + +NAME=spawn-fcgi-php +CONF="/etc/conf.d/spawn-fcgi-php.conf" +#------------------------------------------------------------------------------- +# Return value for: +# for check_process +PROCNOPIDEXIST=-1 # none process is running and pidfile exist +PROCNOPIDNO=0 # none process is running +PROCRUNPIDEXIST=1 # process is running, pidfile exist and has size >0b +PROCRUNPID0=2 # process is running, pidfile has size 0b +PROCRUNPIDNO=3 # process is running, pidfile doesn't exist +OTHERERROR=255 +# check_daemon +DAEMONEXIST=0 +DAEMONNOEXIST=1 +#------------------------------------------------------------------------------- + +# Reading configuration +if [ -r "$CONF" ]; then + . $CONF +else + stat_fail + stat_busy "File $CONF not found" + stat_die +fi +check_config(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: check_config +# DESCRIPTION: Check configuration file +# PARAMETERS: +# RETURNS: 0 if all right +#=============================================================================== + # testing DAEMON + if [ ! -x "$DAEMON" ]; then + stat_fail + stat_busy "File $DAEMON isn't executable" + stat_die + fi + # testing USER and GROUP + if [ -z "$USER" -o -z "$GROUP" ]; then + stat_fail + stat_busy "The variable USER/GROUP isn't defined. I cann't run process $PHPCGI as root." + stat_die + fi + # testing directory of socket + if [ -n "$SOCKET" ]; then + local sockdir=$(dirname "$SOCKET") + if [ ! -d "$sockdir" ]; then + mkdir -p "$sockdir" + fi + fi + # testing directory for user's socket + if [ -n "$USERSOCKET" ]; then + local socksdir=$(dirname "$USERSOCKET") + if [ ! -d "$socksdir" ]; then + mkdir -p "$socksdir" + fi + fi + # testing directory of pidfile + if [ -n "$PIDFILE" ]; then + if [ ! -d $(dirname $PIDFILE) ]; then + mkdir -p $(dirname $PIDFILE) + fi + fi + # testing the program php-cgi + if [ -z "$PHPCGI" ]; then + stat_fail + stat_busy "The variable PHPCGI isn't defined" + stat_die + else + if [ ! -x "$PHPCGI" ]; then + stat_fail + stat_busy "The program $PHPCGI isn't executable" + stat_die + fi + fi + return 0 +} #}}} +check_process(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: check_process +# DESCRIPTION: Check that process is alive +# PARAMETERS: name of process and/or pidfile +# RETURNS: look at the beginning of the script +#=============================================================================== + local process + local pidfile + if [ $# -eq 2 ]; then + process=$1 + pidfile=$2 + elif [ $# -eq 1 ]; then + process=$1 + else + return $OTHERERROR + fi + + if get_pid $process >/dev/null; then + # process is running + if [ -s "$pidfile" ]; then + return $PROCRUNPIDEXIST + elif [ -r "$pidfile" ]; then + return $PROCRUNPID0 + else + return $PROCRUNPIDNO + fi + else + # process doesn't runnig + if [ -r "$pidfile" ]; then + return $PROCNOPIDEXIST + else + return $PROCNOPIDNO + fi + fi +} #}}} +get_userpid(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: get_userpid +# DESCRIPTION: looking for pid of process belong to user +# PARAMETERS: name of user, name of process +# RETURNS: pid of process or 1 if error +#=============================================================================== + local user=$1 + local process=$2 + if [ -n "$user" -a -n "$process" ]; then + local pid=$(ps aux | \ + awk -vuser=$user -vprocess=$process \ + '$1 == user && $8 ~ "s" && $11 == process {print $2}') + if [[ "$pid" =~ ^[0-9]+$ ]]; then + echo $pid + else + return 1 + fi + else + return 1 + fi +} #}}} +check_daemon(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: check_daemon +# DESCRIPTION: check that daemon exist in directory +# PARAMETERS: name of daemon +# RETURNS: look at the beginning of the script +#=============================================================================== + if ck_daemon $NAME; then + return $DAEMONEXIST + else + return $DAEMONEXIST + fi +} #}}} +start(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: start +# DESCRIPTION: check configuration, if a process is already running and running php-cgi daemon +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + stat_busy "Checking configuration" + check_config && + stat_done || \ + stat_die + + stat_busy "Starting server $NAME" #{{{ + local error + check_process $PHPCGI $PIDFILE + error=$? + if [ $error -ge $PROCRUNPIDEXIST -a $error -le $PROCRUNPIDNO ]; then + stat_fail + case $error in + $PROCRUNPIDEXIST) + stat_busy "The process $PHP is running" + ;; + $PROCRUNPID0) + stat_busy "The process $PHP is running and pidfile has size 0b" + ;; + $PROCRUNPIDNO) + stat_busy "The process $PHP is running and pidfile doesn't exist" + ;; + esac + stat_die $error + elif [ $error -eq $OTHERERROR ]; then + stat_fail + stat_busy "Something went wrong..." + stat_die $returnvalue + fi + + # run the main daemon + $DAEMON $OPTIONS >/dev/null + if [ $? -eq 0 ]; then + add_daemon $NAME + stat_done + else + stat_die + fi + #}}} + #{{{ start daemons for multiusers + if [ -n "$STARTMULTI" ]; then + stat_busy "Starting server $NAME for multiusers" + gen_all_uid + if [ -z "$USERSOCKET" ]; then + # Generate file map user 2 port + :>$MAPUSER2PORT + cat >> $MAPUSER2PORT <<- EOF + # Map username to port + map \$username \$port { + default $PORT; + + EOF + local userport=$PORT + local uid gid login group userpidfile + for uid in ${ALLUID[@]}; do + login=$(uid2login $uid) + if [ $? -eq 0 ]; then + userport=$((userport+1)) + echo -e "$login $userport;" >> $MAPUSER2PORT + gid=$(id -g $login) + group=$(gid2group $gid) + if [ -n "$USERPIDFILE" ]; then + userpidfile=${USERPIDFILE}.${login}.pid + fi + USEROPTIONS="-a $IPADDR -p $userport -u $login ${group:+-g $group} -f $PHPCGI -C ${USER_PHP_FCGI_CHILDREN:-1} ${userpidfile:+-P $userpidfile}" + $DAEMON $USEROPTIONS &>/dev/null + if [ $? -ne 0 ]; then + stat_die + fi + fi + done + echo '}' >> $MAPUSER2PORT + stat_done + elif [ -n "$USERSOCKET" ]; then + local uid gid login group userpidfile usersocket + for uid in ${ALLUID[@]}; do + login=$(uid2login $uid) + if [ $? -eq 0 ]; then + gid=$(id -g $login) + group=$(gid2group $gid) + if [ -n "$USERPIDFILE" ]; then + userpidfile=${USERPIDFILE}.${login}.pid + fi + usersocket=${USERSOCKET}.${login}.sock + USEROPTIONS="-s ${usersocket} -u $login ${group:+-g $group} -f $PHPCGI -C ${USER_PHP_FCGI_CHILDREN:-1} ${userpidfile:+-P $userpidfile}" + $DAEMON $USEROPTIONS &>/dev/null + if [ $? -ne 0 ]; then + stat_die + fi + fi + done + stat_done + fi + fi + #}}} +} #}}} +stop(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: stop +# DESCRIPTION: check whether a process is running, stop the daemon php-cgi +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + stat_busy "Stopping server $NAME" #{{{ + # for main daemon + if ! get_pid $PHPCGI &>/dev/null; then + stat_fail + stat_busy "None process $PHPCGI isn't running" + stat_die + else + #process php-cgi is running + if [ -s "$PIDFILE" ]; then + # pidfile exists + kill $(<$PIDFILE) + if [ $? -eq 0 ]; then + rm -f $PIDFILE + rm_daemon $NAME + stat_done + else + stat_die + fi + else #pidfile doesn't exist + if [ -r "$PIDFILE" ]; then + #pidfile exists and has size 0b + stat_busy "Pidfile $PIDFILE exists and has size 0b.Removing it" + rm -f "$PIDFILE" && \ + stat_done || \ + stat_fail + fi + killall -9 $PHPCGI + if [ $? -eq 0 ]; then + stat_done + rm_daemon $NAME + else + stat_die + fi + fi + fi + #}}} + #{{{ for multiusers + if [ -n "$STARTMULTI" ]; then + # waiting to kill the main daemon + sleep 1 + if get_pid $PHPCGI &>/dev/null; then + stat_busy "Stopping server $NAME for multiusers" + killall $PHPCGI + # cleaning pidfiles + if [ -n "$USERPIDFILE" ]; then + gen_all_uid + local login + for UserID in ${ALLUID[@]}; do + login=$(uid2login $UserID) + if [ $? -eq 0 ]; then + rm -f ${USERPIDFILE}.${login}.pid + fi + done + fi #$USERPIDFILE + stat_done + fi + fi + #}}} +} #}}} +restart(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: restart +# DESCRIPTION: stop and start service +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + stat_busy "Restarting server $NAME" + stop + sleep 1 + start +} #}}} +status(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: status +# DESCRIPTION: check status of service +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + if ! get_pid $PHPCGI>/dev/null; then + stat_busy "None process $PHPCGI isn't running" + stat_done + else + stat_busy "Process $PHPCGI is running" + stat_done + if [ -s "$PIDFILE" ]; then + stat_busy "It's spawned by process: $(cat $PIDFILE|tr '\n' ' ')"; + stat_done + elif [ -r "$PIDFILE" ]; then + stat_fail + stat_busy "Pidfile $PIDFILE exists but has size 0b" + else + stat_busy "Pidfile $PIDFILE doesn't exist" + stat_done + fi + if [ -n "$STARTMULTI" -a -n "$USERSSTATUS" ]; then + gen_all_uid + local uid login userpidfile pid + for uid in ${ALLUID[@]}; do + login=$(uid2login $uid) + if [ $? -eq 0 ]; then + if [ -n "$USERPIDFILE" ]; then + # USERPIDFILE defined + userpidfile=${USERPIDFILE}.${login}.pid + if [ -s "$userpidfile" ]; then + stat_busy "Process $PHPCGI of user $login is spawned by process: $(cat $userpidfile|tr '\n' ' ')"; + stat_done + fi + else + pid=$(get_userpid $login $PHPCGI) + if [ $? -eq 0 ]; then + stat_busy "Process $PHPCGI for user $login is running and it's spawned by: $pid"; + stat_done + fi + fi + fi + done + fi + fi +} #}}} +uid2login(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: uid2login +# DESCRIPTION: convert uid to username +# PARAMETERS: uid +# RETURNS: name of user for uid +#=============================================================================== + local uid=$1 + if [ ! $uid -gt 0 ]; then + return 1 + fi + local username=$(getent passwd | awk -vuid=$uid -F: '$3 == uid {print $1}') + if [ -z "$username" ]; then + return 1 + else + echo $username + fi +} #}}} +gid2group(){ #{{{ + #=== FUNCTION ================================================================ + # NAME: uid2group + # DESCRIPTION: convert gid to name of group + # PARAMETERS: gid + # RETURNS: name of group for gid + #=============================================================================== + local gid=$1 + if [ ! $gid -gt 0 ]; then + return 1 + fi + local groupname=$(getent group | awk -vgid=$1 -F: '$3 == gid {print $1}') + if [ -z "$groupname" ]; then + return 1 + else + echo $groupname + fi +} #}}} +gen_all_uid(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: gen_all_uid +# DESCRIPTION: Generate array of all UID +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + ALLUID=() + if [ -n "$RANGEUID" -a ${#RANGEUID[@]} -gt 0 ]; then + for line in ${RANGEUID[@]/,/ }; do + (( line )) || stat_die # not a number + if [[ ${line/-/} != $line ]]; then + for ((i=${line%-*}; i<=${line#*-}; i++)); do + ALLUID+=($i) + done + else + ALLUID+=($line) + fi + done + elif [ $FIRSTUID -gt 1 -a $LASTUID -gt 1 -a $LASTUID -ge $FIRSTUID ]; then + for ((line=FIRSTUID; line<=LASTUID; line++)); do + (( line )) || stat_die #not a number + ALLUID+=($line) + done + else + echo "Error: Wrong range UID. Change RANGEUID or FIRSTUID and LASTUID" + exit 1 + fi +} #}}} +case $1 in + start) + start + ;; + stop) + stop + ;; + restart) + restart + ;; + status) + status + ;; + *) + echo "Usage: $0 start|stop|restart|status" +esac diff --git a/spawn-fcgi-php.script b/spawn-fcgi-php.script new file mode 100755 index 000000000000..80ec27462cfa --- /dev/null +++ b/spawn-fcgi-php.script @@ -0,0 +1,430 @@ +#!/bin/bash +# vi: ft=sh + +NAME=spawn-fcgi-php +CONF="/etc/conf.d/spawn-fcgi-php.conf" +#------------------------------------------------------------------------------- +# Return value for: +# for _check_process +PROCNOPIDEXIST=4 # none process is running and pidfile exist +PROCNOPIDNO=0 # none process is running +PROCRUNPIDEXIST=1 # process is running, pidfile exist and has size >0b +PROCRUNPID0=2 # process is running, pidfile has size 0b +PROCRUNPIDNO=3 # process is running, pidfile doesn't exist +OTHERERROR=255 +# _check_daemon +DAEMONEXIST=0 +DAEMONNOEXIST=1 +#------------------------------------------------------------------------------- + +# Reading configuration +if [ -r "$CONF" ]; then + . $CONF +else + echo "File $CONF not found" + exit 1 +fi +_check_config(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: _check_config +# DESCRIPTION: Check configuration file +# PARAMETERS: +# RETURNS: 0 if all right +#=============================================================================== + # testing DAEMON + if [ ! -x "$DAEMON" ]; then + echo "File $DAEMON isn't executable" + exit 1 + fi + # testing USER and GROUP + if [ -z "$USER" -o -z "$GROUP" ]; then + echo "The variable USER/GROUP isn't defined. I cann't run process $PHPCGI as root." + exit 1 + fi + # testing directory of socket + if [ -n "$SOCKET" ]; then + local sockdir=$(dirname "$SOCKET") + if [ ! -d "$sockdir" ]; then + mkdir -p "$sockdir" + fi + fi + # testing directory for user's socket + if [ -n "$USERSOCKET" ]; then + local socksdir=$(dirname "$USERSOCKET") + if [ ! -d "$socksdir" ]; then + mkdir -p "$socksdir" + fi + fi + # testing directory of pidfile + if [ -n "$PIDFILE" ]; then + if [ ! -d $(dirname $PIDFILE) ]; then + mkdir -p $(dirname $PIDFILE) + fi + fi + # testing the program php-cgi + if [ -z "$PHPCGI" ]; then + echo "The variable PHPCGI isn't defined" + exit 1 + else + if [ ! -x "$PHPCGI" ]; then + echo "The program $PHPCGI isn't executable" + exit 1 + fi + fi + return 0 +} #}}} +_check_process(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: _check_process +# DESCRIPTION: Check that process is alive +# PARAMETERS: name of process and/or pidfile +# RETURNS: look at the beginning of the script +#=============================================================================== + local process pidfile + if [ $# -eq 2 ]; then + process=$1 + pidfile=$2 + elif [ $# -eq 1 ]; then + process=$1 + else + return $OTHERERROR + fi + + if pidof $process >/dev/null; then + # process is running + if [ -s "$pidfile" ]; then + return $PROCRUNPIDEXIST + elif [ -r "$pidfile" ]; then + return $PROCRUNPID0 + else + return $PROCRUNPIDNO + fi + else + # process doesn't runnig + if [ -r "$pidfile" ]; then + return $PROCNOPIDEXIST + else + return $PROCNOPIDNO + fi + fi +} #}}} +_get_userpid(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: _get_userpid +# DESCRIPTION: looking for pid of process belong to user +# PARAMETERS: name of user, name of process +# RETURNS: pid of process or 1 if error +#=============================================================================== + local user=$1 + local process=$2 + if [ -n "$user" -a -n "$process" ]; then + local pid=$(ps aux | \ + awk -vuser=$user -vprocess=$process \ + '$1 == user && $8 ~ "s" && $11 == process {print $2}') + if [[ "$pid" =~ ^[0-9]+$ ]]; then + echo $pid + else + return 1 + fi + else + return 1 + fi +} #}}} +_check_daemon(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: _check_daemon +# DESCRIPTION: check that daemon exist in directory +# PARAMETERS: name of daemon +# RETURNS: look at the beginning of the script +#=============================================================================== + echo "Function obsolete" + return + if ck_daemon $NAME; then + return $DAEMONEXIST + else + return $DAEMONEXIST + fi +} #}}} +start(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: start +# DESCRIPTION: check configuration, if a process is already running and running php-cgi daemon +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + echo "Checking configuration" + _check_config || exit 1 + + echo "Starting server $NAME" #{{{ + local error + _check_process $PHPCGI $PIDFILE + error=$? + if [ $error -ge $PROCRUNPIDEXIST -a $error -le $PROCRUNPIDNO ]; then + case $error in + $PROCRUNPIDEXIST) + echo "The process $PHP is running" + ;; + $PROCRUNPID0) + echo "The process $PHP is running and pidfile has size 0b" + ;; + $PROCRUNPIDNO) + echo "The process $PHP is running and pidfile doesn't exist" + ;; + esac + exit 1 + elif [ $error -eq $OTHERERROR ]; then + echo "Something went wrong..." + exit 1 $returnvalue + fi + + # run the main daemon + $DAEMON $OPTIONS >/dev/null + if [ $? -ne 0 ]; then + exit 1 + fi + #}}} + #{{{ start daemons for multiusers + if [ -n "$STARTMULTI" ]; then + echo "Starting server $NAME for multiusers" + _gen_all_uid + local uid gid login group userpidfile usersocket + if [ -z "$USERSOCKET" ]; then + # Generate file map user 2 port + :>$MAPUSER2PORT + cat >> $MAPUSER2PORT <<- EOF + # Map username to port + map \$username \$port { + default $PORT; + + EOF + local userport=$PORT + for uid in ${ALLUID[@]}; do + login=$(_uid2login $uid) + if [ $? -eq 0 ]; then + userport=$((userport+1)) + echo -e "$login $userport;" >> $MAPUSER2PORT + gid=$(id -g $login) + group=$(_gid2group $gid) + if [ -n "$USERPIDFILE" ]; then + userpidfile=${USERPIDFILE}.${login}.pid + fi + USEROPTIONS="-a $IPADDR -p $userport -u $login ${group:+-g $group} -f $PHPCGI -C ${USER_PHP_FCGI_CHILDREN:-1} ${userpidfile:+-P $userpidfile}" + $DAEMON $USEROPTIONS &>/dev/null + if [ $? -ne 0 ]; then + exit 1 + fi + fi + done + echo '}' >> $MAPUSER2PORT + elif [ -n "$USERSOCKET" ]; then + for uid in ${ALLUID[@]}; do + login=$(_uid2login $uid) + if [ $? -eq 0 ]; then + gid=$(id -g $login) + group=$(_gid2group $gid) + if [ -n "$USERPIDFILE" ]; then + userpidfile=${USERPIDFILE}.${login}.pid + fi + usersocket=${USERSOCKET}.${login}.sock + USEROPTIONS="-s ${usersocket} -u $login ${group:+-g $group} -f $PHPCGI -C ${USER_PHP_FCGI_CHILDREN:-1} ${userpidfile:+-P $userpidfile}" + $DAEMON $USEROPTIONS &>/dev/null + if [ $? -ne 0 ]; then + exit 1 + fi + fi + done + fi + fi + #}}} +} #}}} +stop(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: stop +# DESCRIPTION: check whether a process is running, stop the daemon php-cgi +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + echo "Stopping server $NAME" #{{{ + # for main daemon + if ! pidof $PHPCGI &>/dev/null; then + echo "None process $PHPCGI isn't running" + exit 1 + else + #process php-cgi is running + if [ -s "$PIDFILE" ]; then + # pidfile exists + kill $(<$PIDFILE) + if [ $? -eq 0 ]; then + rm -f $PIDFILE + else + exit 1 + fi + else #pidfile doesn't exist + if [ -r "$PIDFILE" ]; then + #pidfile exists and has size 0b + echo "Pidfile $PIDFILE exists and has size 0b.Removing it" + rm -f "$PIDFILE" + fi + killall -9 $PHPCGI + if [ $? -ne 0 ]; then + exit 1 + fi + fi + fi + #}}} + #{{{ for multiusers + if [ -n "$STARTMULTI" ]; then + # waiting to kill the main daemon + sleep 1 + if pidof $PHPCGI &>/dev/null; then + echo "Stopping server $NAME for multiusers" + killall $PHPCGI + # cleaning pidfiles + if [ -n "$USERPIDFILE" ]; then + _gen_all_uid + for UserID in ${ALLUID[@]}; do + local login + login=$(_uid2login $UserID) + if [ $? -eq 0 ]; then + rm -f ${USERPIDFILE}.${login}.pid + fi + done + fi #$USERPIDFILE + fi + fi + #}}} +} #}}} +restart(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: restart +# DESCRIPTION: stop and start service +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + echo "Restarting server $NAME" + stop + sleep 1 + start +} #}}} +status(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: status +# DESCRIPTION: check status of service +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + if ! pidof $PHPCGI>/dev/null; then + echo "None process $PHPCGI isn't running" + else + echo "Process $PHPCGI is running" + if [ -s "$PIDFILE" ]; then + echo "It's spawned by process: $(cat $PIDFILE|tr '\n' ' ')"; + elif [ -r "$PIDFILE" ]; then + echo "Pidfile $PIDFILE exists but has size 0b" + else + echo "Pidfile $PIDFILE doesn't exist" + fi + if [ -n "$STARTMULTI" -a -n "$USERSSTATUS" ]; then + _gen_all_uid + local uid login userpidfile pid + for uid in ${ALLUID[@]}; do + login=$(_uid2login $uid) + if [ $? -eq 0 ]; then + if [ -n "$USERPIDFILE" ]; then + # USERPIDFILE defined + userpidfile=${USERPIDFILE}.${login}.pid + if [ -s "$userpidfile" ]; then + echo "Process $PHPCGI of user $login is spawned by process: $(cat $userpidfile|tr '\n' ' ')"; + fi + else + pid=$(_get_userpid $login $PHPCGI) + if [ $? -eq 0 ]; then + echo "Process $PHPCGI for user $login is running and it's spawned by: $pid"; + fi + fi + fi + done + fi + fi +} #}}} +_uid2login(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: _uid2login +# DESCRIPTION: convert uid to username +# PARAMETERS: uid +# RETURNS: name of user for uid +#=============================================================================== + local uid=$1 + if [ ! $uid -gt 0 ]; then + return 1 + fi + local username=$(getent passwd | awk -vuid=$uid -F: '$3 == uid {print $1}') + if [ -z "$username" ]; then + return 1 + else + echo $username + fi +} #}}} +_gid2group(){ #{{{ + #=== FUNCTION ================================================================ + # NAME: uid2group + # DESCRIPTION: convert gid to name of group + # PARAMETERS: gid + # RETURNS: name of group for gid + #=============================================================================== + local gid=$1 + if [ ! $gid -gt 0 ]; then + return 1 + fi + local groupname=$(getent group | awk -vgid=$1 -F: '$3 == gid {print $1}') + if [ -z "$groupname" ]; then + return 1 + else + echo $groupname + fi +} #}}} +_gen_all_uid(){ #{{{ +#=== FUNCTION ================================================================ +# NAME: _gen_all_uid +# DESCRIPTION: Generate array of all UID +# PARAMETERS: - +# RETURNS: - +#=============================================================================== + ALLUID=() + if [ -n "$RANGEUID" -a ${#RANGEUID[@]} -gt 0 ]; then + for line in ${RANGEUID[@]/,/ }; do + (( line )) || exit 1 # not a number + if [[ ${line/-/} != $line ]]; then + for ((i=${line%-*}; i<=${line#*-}; i++)); do + ALLUID+=($i) + done + else + ALLUID+=($line) + fi + done + elif [ $FIRSTUID -gt 1 -a $LASTUID -gt 1 -a $LASTUID -ge $FIRSTUID ]; then + for ((line=FIRSTUID; line<=LASTUID; line++)); do + (( line )) || exit 1 #not a number + ALLUID+=($line) + done + else + echo "Error: Wrong range UID. Change RANGEUID or FIRSTUID and LASTUID" + exit 1 + fi +} #}}} +case $1 in + start) + start + ;; + stop) + stop + ;; + restart) + restart + ;; + status) + status + ;; + *) + echo "Usage: $0 start|stop|restart|status" +esac diff --git a/spawn-fcgi-php.service b/spawn-fcgi-php.service new file mode 100644 index 000000000000..fa80d5a24365 --- /dev/null +++ b/spawn-fcgi-php.service @@ -0,0 +1,14 @@ +# vi: ft=systemd +[Unit] +Description=Script to provide simple cgi-php support for nginx http daemon +After=syslog.target network.target + +[Service] +Type=forking +EnvironmentFile=/etc/conf.d/spawn-fcgi-php.conf +ExecStart=/usr/lib/systemd/scripts/spawn-fcgi-php start +ExecStop=/usr/lib/systemd/scripts/spawn-fcgi-php stop +ExecReload=/usr/lib/systemd/scripts/spawn-fcgi-php restart + +[Install] +WantedBy=multi-user.target |