WillSquire
12/17/2015 - 11:23 AM

FAMP (FreeBSD, Apache, MySQL & PHP stack) setup

FAMP (FreeBSD, Apache, MySQL & PHP stack) setup

FAMP (FreeBSD, Apache, MySQL & PHP stack) setup

Prerequisites

This assumes the FreeBSD OS has been installed and setup ready for Apache, MySQL and PHP installation and configuration: https://gist.github.com/WillSquire/f10bfff0655a15b3ffc6

Apache

Download and install Apache: https://gist.github.com/WillSquire/88befcafca8d09acec12399c422b5987

####Compression This optional Apache module mod_deflate compresses content served to reduce size of data transmitted over the network. This does not need to be installed but is recommended to increase the performance of the server: https://gist.github.com/WillSquire/035314eb81b318457403

####Caching This optional Apache module mod_expires adds expirey time periods to the headers of served content, signaling to web browsers to save the item in their cache for the amount of time given as not to send additional request later. This does not need to be installed but is recommended to increase the performance of the server: https://gist.github.com/WillSquire/fc711fa2dfa54f07aa4b

MySQL

Download and install MySQL: https://gist.github.com/WillSquire/2d9cfc9eb97c3dd40f375dbf5e90f75b

PHP

Note: Future edit to PHP install will include php-fpm. See "Optimisation setup" and "php-fpm via a socket" reference in the footer for more.

First navigate the ports collection, then install the ports (PHP, PHP MySQL module and PHP MySQLi module) at the given locations and clean (note the PHP version here is 5.6 and may change in future):

cd /usr/ports/lang/php56 && sudo make config-recursive install distclean
cd /usr/ports/databases/php56-mysql && sudo make config-recursive install distclean
cd /usr/ports/databases/php56-mysqli && sudo make config-recursive install distclean

Important note: If mpm_prefork_module was selected as the primary MPM (instead of mpm_event_module), then the PHP Apache module mod_php will need to be installed also:

cd /usr/ports/www/mod_php56 && sudo make config-recursive install distclean

Alternative way to install PHP extensions

Use FreeBSD's PHP extensions ports to select and install all the common PHP modules (using the mouseclick to choose what gets installed) with:

cd /usr/ports/lang/php56-extensions && sudo make config-recursive install distclean

Restart Apache to load the new extensions:

sudo apachectl graceful

Rehash the system to find binaries paths (note that rehash can be used in place of this):

hash -r

Copy PHP's sample configuration file (for production) to be used as the current config:

sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Open the new php.ini configuration file:

sudo ee /usr/local/etc/php.ini

Find disable_functions = and list functions (using comma seperation) that are a potential security risk to the system (note: make sure you application is not going to call any of these functions first):

disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

mpm_prefork_module only

Create/open a php.conf file for apache:

sudo ee /usr/local/etc/apache24/Includes/php.conf

Insert the following for Apache to process PHP files, then save and exit:

<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    </FilesMatch>
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source
    </FilesMatch>
</IfModule>

mpm_event_module only

MPM Event needs FastCGI to connect to PHP-FPM. To download the FastCGI Apache mod:

cd /usr/ports/www/mod_fastcgi && sudo make config-recursive install distclean

Open httpd.conf:

sudo ee /usr/local/etc/apache24/httpd.conf

Once installed, ensure the following lines are present and are uncommented (i.e. are not prefixed with #), then save and exit httpd.conf:

LoadModule fastcgi_module libexec/apache24/mod_fastcgi.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

Create and open php-fpm.conf:

sudo ee /usr/local/etc/apache24/Includes/php-fpm.conf

Add the following, save and exit (note /var/run/php-fpm.sock is the path to the unix socket binary file and /usr/local/www/apache24/data/ is the same path as Apache's DocumentRoot, but the latter depends on where you want this to serve from):

<IfModule mod_fastcgi.c>
        ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/var/run/php-fpm.sock|fcgi://127.0.0.1:9000/usr/local/www/apache24/data/
        DirectoryIndex index.php
</IfModule>

Open php-fpm.conf:

sudo ee /usr/local/etc/php-fpm.conf

Change listen = 127.0.0.1:9000 to listen on a BSD socket instead of a TCP socket by replacing 127.0.0.1:9000 for /var/run/php-fpm.sock:

listen = /var/run/php-fpm.sock

Uncomment listen.owner, listen.group and listen.mode:

listen.owner = www
listen.group = www
listen.mode = 0660

Open php.ini:

sudo ee /usr/local/etc/php.ini

Uncomment cgi.fix_pathinfo by removing the ; infront and change the value to 0. This is to prevent malicious behaviour:

cgi.fix_pathinfo=0

PHP-FPM can also be added to startup (as PHP will operate as it's own seperate process):

sudo ee /etc/rc.conf

Add the follow to rc.conf, then save and exit:

# PHP
php_fpm_enable="YES"

MPMs can be fine tuned by altering the httpd-mpm.conf file (note: fine tuning this will be added at a later date):

sudo ee /usr/local/etc/apache24/extra/httpd-mpm.conf

The service will then need to be started with:

sudo service php-fpm start

Restart Apache for the changes to take effect:

sudo service apache24 restart

Note if required PHP extensions are not be installed when the PHP application is running, check Apache's httpd-error.log for undefined call errors: sudo ee /var/log/httpd-error.log

####Caching This optional PHP extension OpCache comes with PHP 5.5+ and is a PHP accelerator. This does not need to be installed but is recommended to increase the performance of the server: https://gist.github.com/WillSquire/d55327342d921413b3d2ed1d6dc033d7

Testing

The FAMP stack is now setup, but it is important to make sure it is working correctly by testing it. Create a file in Apache's default DocumentRoot directory /usr/local/www/apache24/data containing <?php phpinfo(); ?> with:

sudo sh -c "echo '<?php phpinfo(); ?>' > /usr/local/www/apache24/data/info.php"

Go to the IP address of the server with /info.php. If it is working, then remove the info script with:

sudo rm /usr/local/www/apache24/data/info.php

PHP optional modules

According to the PHP documentation some PHP modules should be included as apart of PHP by default, one of these being Filter (found here: http://php.net/manual/en/filter.installation.php). However, this is not included as part of the php56 port. To check the list of PHP modules that have been installed, enter:

php -m

If any modules that need to be installed, these can be installed seperatly when needed like so (using php5-filter as an example):

cd /usr/ports/security/php5-filter && sudo make config-recursive install distclean

Restart Apache for the changes to take effect:

apachectl graceful

References