Skip to content

Instantly share code, notes, and snippets.

@samir-plusb
Forked from magnetikonline/README.md
Last active March 16, 2017 21:32
Show Gist options
  • Save samir-plusb/792578d9d872fab3b3461ff2c607c651 to your computer and use it in GitHub Desktop.
Save samir-plusb/792578d9d872fab3b3461ff2c607c651 to your computer and use it in GitHub Desktop.
Apache and Nginx & PHP-FPM systemd service config templates

Apache and Nginx & PHP-FPM systemd service config templates

A set of really basic systemd config templates for starting Nginx or Apache together with PHP-FPM on system boot. It can be used with sed to create systemd config files for multiple php-fpm instances:

  • Will ensure Nginx/Apache web server has started before kicking off PHP-FPM process.
  • PHP-FPM pid file placed in /run/php7
  • PHP7 PHP-FPM config at /etc/php7.
  • Based on usage with Centos 7 together with ISPConfig.

Both scripts placed in /lib/systemd/system and enabled by the following:

$ sudo systemctl enable nginx.service
$ sudo systemctl enable php-fpm.service

Fin.

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target

Install php for nextCloud

Install php 5.6

Required:

yum install php56-php-bcmath php56-php-cli php56-php-common php56-php-fpm php56-php-gd php56-php-intl php56-php-mbstring php56-php-mcrypt php56-php-mysqlnd php56-php-opcache php56-php-pdo php56-php-pear php56-php-pecl-uploadprogress php56-php-soap php56-php-xml php56-php-xmlrpc php56-php-ctype php56-php-dom php56-php-gd php56-php-iconv php56-php-json php56-php-libxml php56-php-mbstring php56-php-posix php56-php-simplexml php56-php-xmlwriter php56-php-zip php56-php-zlib

Database connectors

yum install php56-php-sqlite php56-php-pgsql 

Required for specific apps

yum install php56-php-curl php56-php-fileinfo php56-php-bz2 php56-php-intl php56-php-mcrypt php56-php-openssl php56-php-ldap php56-php-smbclient php56-php-ftp php56-php-imap

Recommended for specific apps

yum install php56-php-exif php56-php-gmp

For enhanced server performance

yum install php56-php-apcu php56-php-memcached php56-php-redis

Imagick

yum install php56-php-imagick

For command line processing

yum install php56-php-pcntl

Install php 7

Required:

yum install php70-php-bcmath php70-php-cli php70-php-common php70-php-fpm php70-php-gd php70-php-intl php70-php-mbstring php70-php-mcrypt php70-php-mysqlnd php70-php-opcache php70-php-pdo php70-php-pear php70-php-pecl-uploadprogress php70-php-soap php70-php-xml php70-php-xmlrpc php70-php-ctype php70-php-dom php70-php-gd php70-php-iconv php70-php-json php70-php-libxml php70-php-mbstring php70-php-posix php70-php-simplexml php70-php-xmlwriter php70-php-zip php70-php-zlib

Database connectors

yum install php70-php-sqlite php70-php-pgsql 

Required for specific apps

yum install php70-php-curl php70-php-fileinfo php70-php-bz2 php70-php-intl php70-php-mcrypt php70-php-openssl php70-php-ldap php70-php-smbclient php70-php-ftp php70-php-imap

Recommended for specific apps

yum install php70-php-exif php70-php-gmp

For enhanced server performance

yum install php70-php-apcu php70-php-memcached php70-php-redis

Imagick

yum install php70-php-imagick

For command line processing

yum install php70-php-pcntl
#!/bin/sh
#
# This is a bash script, which can be used to compile multiple versions of php for ispconfig.
#
# See here for more instructions:
# https://www.howtoforge.com/how-to-use-multiple-php-versions-php-fpm-and-fastcgi-with-ispconfig-3-ubuntu-12.10
#
# Unfortunately this script is not tested very well, because it exists a much more simpler way to do this:
# http://www.hexblot.com/blog/centos7-ispconfig3-and-multiple-php-versions
#
#
#
TARGET="/opt"
BUILD="/usr/local/src/php"
PHP_TYPE="fpm" # fpm or cgi
PHP_VERSION="7.0.5"
PHP_NAME="php-$PHP_VERSION"
case "$PHP_TYPE" in
"fpm")
ADDITIONAL_PARAMETERS="--enable-fpm --with-fpm-user=apache --with-fpm-group=apache" #for 'fpm'
;;
"cgi")
ADDITIONAL_PARAMETERS="--enable-cgi" #for 'fcgi'
;;
esac
PHP_TARGET_DIR="$TARGET/php/$PHP_VERSION/$PHP_TYPE" #php will be in this dir later on
PHP_DOWNLOAD_DIR="$BUILD/php-$PHP_VERSION" #The dir in which we will download and compile php
PHP_BUILD_DIR="$PHP_DOWNLOAD_DIR/$PHP_NAME"
PHP_PACKAGE_FILE="php-$PHP_VERSION.tar.bz2";
mkdir -p $PHP_TARGET_DIR
mkdir -p $PHP_BUILD_DIR
mkdir -p $PHP_DOWNLOAD_DIR
cd $PHP_DOWNLOAD_DIR;
wget "http://de.php.net/get/php-$PHP_VERSION.tar.bz2/from/this/mirror" -O $PHP_PACKAGE_FILE
tar xfv $PHP_PACKAGE_FILE;
cd $PHP_BUILD_DIR;
./configure \
--prefix=$PHP_TARGET_DIR \
--enable-sockets \
--enable-exif \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-intl \
--enable-fileinfo \
--enable-posix \
--enable-mbregex \
--enable-mbstring \
--enable-soap \
--enable-calendar \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-ftp \
--enable-ctype \
--enable-dom \
--enable-libxml \
--enable-simplexml \
--enable-xmlwriter \
--enable-zip \
--with-gd \
--with-gmp \
--with-pdo-pgsql \
--with-ldap \
--with-ldap-sasl \
--with-zlib-dir \
--with-freetype-dir \
--with-libxml-dir=/usr \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--with-pgsql \
--with-bz2 \
--with-mhash \
--with-pcre-regex \
--with-pdo-mysql \
--with-mysqli \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-openssl \
--with-libdir=lib64 \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--disable-rpath \
$ADDITIONAL_PARAMETERS
make
make install
cp $PHP_BUILD_DIR/php.ini-production $PHP_TARGET_DIR/lib/php.ini
if [ "$PHP_TYPE" = 'fpm' ]
then
cp $PHP_TARGET_DIR/etc/php-fpm.conf.default $PHP_TARGET_DIR/etc/php-fpm.conf
mkdir -p $PHP_TARGET_DIR/etc/pool.d
#PHPFPM_INITD_SCRIPT_TEMPLATE="https://gist.githubusercontent.com/samir-plusb/42cf102a52f369dd404a177f9c5c5e59/raw/cdfa04de0d9102914dad824353808379e2f46c6b/php-5.3.22-fpm.sh"
PHPFPM_INITD_SCRIPT_TEMPLATE="https://gist.githubusercontent.com/samir-plusb/792578d9d872fab3b3461ff2c607c651/raw/964ed8f2e4505fce242d423afa2bea065a3fa8cc/php-fpm.service";
SYSTEMD_FPM_SERVICE_CONFIG="/lib/systemd/system/php-$PHP_VERSION-$PHP_TYPE";
TMP=~/.tmp_$(date +%s);
curl $PHPFPM_INITD_SCRIPT_TEMPLATE -o TMP;
# The CentOS version you are using might need an initd script instead of systemd
# Then you might need something like this:
#
# vi /etc/init.d/php-$PHP_VERSION-fpm
#
# chmod 755 /etc/init.d/php-$PHP_VERSION-fpm
# chkconfig --levels 235 php-$PHP_VERSION-fpm on
#
# /etc/init.d/php-$PHP_VERSION-fpm start
sed -i -- "s=###PHP_TARGET_DIR###=$PHP_TARGET_DIR=g" $TMP;
sed -i -- "s/###PHP_VERSION###/$PHP_VERSION/g" $TMP;
sed -i -- "s/###PHP_TYPE###/$PHP_TYPE/g" $TMP;
cp $TMP SYSTEMD_FPM_SERVICE_CONFIG
mkdir $PHP_TARGET_DIR/etc/pool.d
chmod 755 /etc/init.d/php-$PHP_VERSION-fpm
chkconfig --levels 235 php-$PHP_VERSION-fpm on
/etc/init.d/php-$PHP_VERSION-fpm start
fi
# pecl install smbclient
# pecl install APCu-beta
# pecl install imagick
# echo $PHP_BUILD_DIR/php.ini-production
# echo $PHP_TARGET_DIR/lib/php.ini
# echo $PHP_TARGET_DIR/etc/php-fpm.conf.default
# echo $PHP_TARGET_DIR/etc/php-fpm.conf
cat << EOF
---------------------------------------------------------------------------
---------------------------------------------------------------------------
---------------------------------------------------------------------------
Execute the following:
Open $PHP_TARGET_DIR/etc/php-fpm.conf and adjust the following settings
- in the listen line you must use an unused port
e.g. 8999; port 9000 might be in use by the default CentOS PHP-FPM already
- you must add the line include=$PHP_TARGET_DIR/etc/pool.d/*.conf at the end
- If you want to use this PHP-FPM version with Apache, please use
user = apache;
group = apache;
- if you want to use this PHP-FPM version with nginx, please use
user = nginx
group = nginx
- Example:
[...]
pid = run/php-fpm.pid
[...]
user = apache
group = apache
[...]
listen = 127.0.0.1:8999
[...]
include=$PHP_TARGET_DIR/etc/pool.d/*.conf
EOF
[Unit]
Description=Nginx web server
After=local-fs.target network.target
[Service]
ExecStart=/usr/local/nginx/sbin/nginx
Type=forking
[Install]
WantedBy=multi-user.target
[Unit]
Description=PHP FastCGI process manager
After=local-fs.target network.target httpd.service
[Service]
ExecStartPre=/bin/mkdir -p ###PHP_TARGET_DIR###/var/run/
PIDFile=###PHP_TARGET_DIR###/var/run/php-fpm.pid
ExecStart=###PHP_TARGET_DIR###/sbin/php-fpm --fpm-config ###PHP_TARGET_DIR###/etc/php-fpm.conf
Type=forking
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment