diff --git a/roles/php/files/php-fpm-stop.service b/roles/php/files/php-fpm-stop.service new file mode 100644 index 0000000..a5aa49a --- /dev/null +++ b/roles/php/files/php-fpm-stop.service @@ -0,0 +1,5 @@ +[Unit] +Description="Stop php-fpm" + +[Service] +ExecStart=/usr/local/bin/stop-php-fpm diff --git a/roles/php/files/php-fpm-stop.timer b/roles/php/files/php-fpm-stop.timer new file mode 100644 index 0000000..726fb2d --- /dev/null +++ b/roles/php/files/php-fpm-stop.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Stop php-fpm + +[Timer] +OnBootSec=2min +OnUnitActiveSec=2min +Persistent=true + +[Install] +WantedBy=multi-user.target diff --git a/roles/php/files/php-fpm.service b/roles/php/files/php-fpm.service new file mode 100644 index 0000000..ffce902 --- /dev/null +++ b/roles/php/files/php-fpm.service @@ -0,0 +1,11 @@ +[Unit] +Description=The PHP FastCGI Process Manager +After=syslog.target network.target + +[Service] +Type=notify +PIDFile=/run/php-fpm/php-fpm.pid +PrivateTmp=true +Environment="FPM_SOCKETS=/var/run/php-fpm.socket=3" +ExecStart=/usr/bin/php-fpm --nodaemonize --pid /run/php-fpm/php-fpm.pid +ExecReload=/bin/kill -USR2 $MAINPID diff --git a/roles/php/files/php-fpm.socket b/roles/php/files/php-fpm.socket new file mode 100644 index 0000000..d9fc386 --- /dev/null +++ b/roles/php/files/php-fpm.socket @@ -0,0 +1,5 @@ +[Socket] +ListenStream=9000 + +[Install] +WantedBy=sockets.target diff --git a/roles/php/files/stop-php-fpm b/roles/php/files/stop-php-fpm new file mode 100644 index 0000000..178e81c --- /dev/null +++ b/roles/php/files/stop-php-fpm @@ -0,0 +1,8 @@ +#!/bin/bash + +PID=$(cat /run/php-fpm/php-fpm.pid) +CHILDREN=$(ps -o pid --no-headers --ppid "$PID" | wc -l) + +if [ $CHILDREN == "0" ]; then + kill "$PID" +fi diff --git a/roles/php/handlers/main.yml b/roles/php/handlers/main.yml index 1723a03..8f9f88b 100644 --- a/roles/php/handlers/main.yml +++ b/roles/php/handlers/main.yml @@ -1,2 +1,4 @@ -- name: restart php-fpm +- name: stop php-fpm service: name=php-fpm state=restarted +- name: Reload systemd + action: command systemctl daemon-reload diff --git a/roles/php/tasks/main.yml b/roles/php/tasks/main.yml index 2d4fee3..90c12c2 100644 --- a/roles/php/tasks/main.yml +++ b/roles/php/tasks/main.yml @@ -1,10 +1,27 @@ - name: install php-fpm pacman: name=php-fpm state=present -- name: Enable php-fpm - service: name=php-fpm state=started enabled=yes - name: php-fpm.ini template: src=php-fpm.conf.j2 dest=/etc/php/php-fpm.conf mode=0644 backup=yes - notify: restart php-fpm + notify: stop php-fpm - name: php.ini template: src=php.ini.j2 dest=/etc/php/php.ini mode=0644 backup=yes - notify: restart php-fpm + notify: stop php-fpm +- copy: src={{ item }} dest=/etc/systemd/system/{{ item }} mode=0644 + with_items: + - php-fpm.service + - php-fpm.socket + - php-fpm-stop.timer + - php-fpm-stop.service + notify: Reload systemd +- copy: src=stop-php-fpm dest=/usr/local/bin/stop-php-fpm mode=0755 +- shell: /usr/bin/systemctl --quiet is-active php-fpm.socket + register: fpm_socket_started + ignore_errors: True +- name: Stop php-fpm + service: name=php-fpm.service state=stopped + when: fpm_socket_started.rc != 0 +- name: Start php-fpm socket activation + service: name={{item}} state=started enabled=yes + with_items: + - php-fpm.socket + - php-fpm-stop.timer diff --git a/roles/php/templates/php-fpm.conf.j2 b/roles/php/templates/php-fpm.conf.j2 index 8e0d3b0..21d2f20 100644 --- a/roles/php/templates/php-fpm.conf.j2 +++ b/roles/php/templates/php-fpm.conf.j2 @@ -158,7 +158,7 @@ group = http ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. -listen = 0.0.0.0:9000 +listen = /var/run/php-fpm.socket ; pseudo socket, will no be used really ; Set listen(2) backlog. ; Default Value: 65535 (-1 on FreeBSD and OpenBSD)