Apache and PHP-FPM

Jun 11, 2015

Length: 12:29

Learn to hook Apache up to PHP-FPM using Apache's proxy modules.

Command Rundown

Install Repositories:

sudo apt-get update
sudo apt-get install -y vim tmux curl wget unzip software-properties-common

sudo add-apt-repository -y ppa:ondrej/apache2
sudo add-apt-repository -y ppa:ondrej/php5-5.6
sudo apt-get update

sudo apt-cache show apache2
sudo apt-cache show php5-fpm

Install Apache:

sudo apt-get install -y apache2

cd /etc/apache2/mods-enabled
ll | grep mpm
# mpm_event is used!

Install PHP-FPM:

sudo apt-get install -y php5-fpm \
     php5-mcrypt php5-mysql php5-gd php5-curl

php -v

sudo service php5-fpm status

Edit www.conf to lsiten on network listen = 127.0.0.1:9000:

sudo service php5-fpm restart

Configure Apache:

# See what's available
ll /etc/apache2/mods-available | grep proxy

# Enable proxy modules
sudo a2enmod proxy proxy_fcgi

sudo service apache2 restart

Virtualhost:

sudo vim /etc/apache2/sites-available/example.conf

Vhost:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAlias example.*.xip.io

    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
        # SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/"

        # Else we can just use a tcp socket:
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

</VirtualHost>

Create webroot and index.php file:

sudo mkdir -p /var/www/example.com/public
sudo vim /var/www/example.com/public/index.php

Make php file:

<?php

phpinfo();
?>

Modules and Reloading Apache

sudo a2dissite 000-default
sudo a2ensite example
sudo service apache2 reload

sudo a2enmod rewrite
sudo vim /var/www/example.com/public/.htaccess
# Add the below, then:
sudo service apache2 restart
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L].htaccess
sudo service apache2 restart