Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@duan-li
Last active August 21, 2022 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save duan-li/21e42ae393177f1fbf52 to your computer and use it in GitHub Desktop.
Save duan-li/21e42ae393177f1fbf52 to your computer and use it in GitHub Desktop.
Ubuntu 14.04 LTS apache php-fpm setup
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
CURRENT_USER=$(id -u -n)
CURRENT_GROUP=$(id -g -n)
CURRENT_USER_HOME=$(eval echo ~${SUDO_USER})
if [ "${CURRENT_USER}" == "root" ]; then
echo "Type a new username:"
read CURRENT_USER
CURRENT_GROUP=$CURRENT_USER
adduser --disabled-login --gecos "${CURRENT_USER}" ${CURRENT_USER}
CURRENT_USER_HOME="/home/${CURRENT_USER}"
fi
sudo -u ${CURRENT_USER} mkdir -p ${CURRENT_USER_HOME}/htdocs/default
sudo -u ${CURRENT_USER} cat >> ${CURRENT_USER_HOME}/htdocs/default/index.php <<EOF
<?php
phpinfo();
EOF
sudo -u ${CURRENT_USER} cat >> ${CURRENT_USER_HOME}/htdocs/default/index.html <<EOF
<h1>heading</h1>
EOF
apt-get update -y;
apt-get upgrade -y;
apt-get clean -y;
apt-get autoclean -y;
apt-get autoremove -y;
apt-get install -y unzip vim git-core curl wget build-essential python-software-properties
apt-get install -y apache2 apache2-mpm-worker apache2-utils
a2dismod mpm_event mpm_prefork
a2enmod mpm_worker actions fastcgi alias rewrite deflate expires
cat >> /etc/apt/sources.list <<EOF
deb http://us.archive.ubuntu.com/ubuntu/ trusty multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty multiverse
deb http://us.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-updates multiverse
EOF
apt-get update -y;
apt-get install -y libapache2-mod-fastcgi php5-fpm php5 php5-cli php5-curl php5-gd php5-imagick php-apc php5-mysql php5-mcrypt php5-xmlrpc php5-json
# use 'apachectl -V | grep -i mpm' check mpm_worker is working
cat > /etc/apache2/conf-available/php5-fpm.conf <<EOF
#<IfModule mod_fastcgi.c>
# Alias /usr/sbin/php-fpm.fcgi /usr/sbin/php-fpm
# AddHandler php-fastcgi .php
# Action php-fastcgi /usr/sbin/php-fpm.fcgi
# FastCGIExternalServer /usr/sbin/php-fpm -host 127.0.0.1:9000 -pass-header Authorization -idle-timeout 600
# <Directory /usr/sbin>
# Options ExecCGI FollowSymLinks
# SetHandler fastcgi-script
# Order allow,deny
# Allow from all
# </Directory>
#</IfModule>
<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>
</IfModule>
EOF
cat > /etc/php5/fpm/pool.d/www.conf <<EOF
[www]
user = $CURRENT_USER
group = $CURRENT_GROUP
chdir = /
#listen = 127.0.0.1:9000
listen = /var/run/php5-fpm.sock
pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 2000
EOF
cat > /etc/apache2/sites-available/001-default.conf <<EOF
<VirtualHost *:80>
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot $CURRENT_USER_HOME/htdocs/default
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
EOF
a2dissite 000-default
a2ensite 001-default
a2enconf php5-fpm
service php5-fpm restart
service apache2 restart
chown ${CURRENT_USER}:${CURRENT_GROUP} /var/run/php5-fpm.sock
##References
#http://2bits.com/articles/high-performance-drupal-with-apache-mpm-worker-threaded-server-and-php-fpm.html
#https://www.garron.me/en/blog/ubuntu-lamp-apache2-mpm-worker-and-php-fpm.html
## php5-fpm.conf file
#https://www.vultr.com/docs/use-php5-fpm-with-apache-2-on-ubuntu-14-04
##fix permission problem
#http://stackoverflow.com/questions/23804659/fpm-with-apache2-not-working-permission-denied
##how to check issue
#http://serverfault.com/questions/646245/php-fpm-not-working-on-apache2-failed-to-connect-to-fastcgi-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment