Apache, PHP-FPM, MariaDB and FreeBSD
Written by Guillermo Garron
Date: 2015-02-01 00:00:00 00:00
Introduction
I always liked the bsd-like ports that Slackware Linux uses and now that Digital Ocean offers FreeBSD I am having a great time working with it.
Today I want to show you how to setup an FAMP (Apache 2.4, MySQL and PHP on FreeBSD 10), we will be installing Apache using Event MPM, and PHP-FPM.
We will also be using both methods of installation in FreeBSD, first the ports way then, the pkg binary way.
Apache, MySQL and PHP on FreeBSD from ports
Apache 2.4 Event MPM
Let's first start with Apache, as I said we will be installing the Event MPM which is a threaded MPM and will serve files a lot faster, actually normal files as fast as Nginx
cd /usr/ports/www/apache24/
make install clean
Select these options.
| [x] ACCESS_COMPAT mod_access compatibility
| [x] ACTIONS Action triggering on requests
| [x] ALIAS Mapping of requests to different filesystem parts
| [x] AUTHZ_CORE Core authorization provider vector module
| [x] AUTHZ_HOST Host-based authorization control
| [x] DIR Directory request handling
|+[x] EXPIRES Expires header control
|+[x] HEADERS HTTP header control
|+[x] REWRITE Rule based URL manipulation
|+[x] VERSION Determining httpd version in config files
|+[x] VHOST_ALIAS Mass virtual hosting
|+(*) MPM_EVENT MPM worker variant with the goal of consuming
| [x] MIME Mapp file-ext. to MIME (recommended)
| [x] PROXY Build enabled PROXY modules
| [x] PROXY_FCGI FastCGI support module for mod_proxy
I like to have Virtual sites as separate files and not as part of the monolitic httpd.conf file, so let's configure Apache that way.
mkdir /usr/local/etc/apache24/conf.d
Add this line at the end of the file /usr/local/etc/apache24/httpd.conf
Include etc/apache24/conf.d/*.conf
PHP 5.6
Now it is the turn of PHP, we will build it with PHP-FPM option turned on.
cd /usr/ports/lang/php56
make install
Configure this way.
|+[x] CLI Build CLI version
|+[x] CGI Build CGI version
|+[x] FPM Build FPM version
|+[ ] EMBED Build embedded library
|+[ ] PHPDBG
|+[ ] DEBUG Enable debug
|+[ ] DTRACE Enable DTrace support
|+[x] IPV6 Enable ipv6 support
|+[ ] MAILHEAD Enable mail header patch
|+[x] LINKTHR Link thread lib (for threaded extensions)
|+[ ] ZTS Force Zend Thread Safety (ZTS) build
Finally clean the installation.
make clean
We now have PHP 5.6 installed, but we need the modules, so:
cd ../php56-extensions/
make install clean
Be sure to mark, GD, CURL and MySQL as well as MySQLi
|+[x] CURL CURL support
|+[x] GD GD library support
|+[x] MYSQL MySQL database support
|+[x] MYSQLI MySQLi database support
Those among the default ones.
MariaDB 10.0
We are going to install MariaDB instead of MySQL, if you do not know already, MariaDB is a fork of MySQL, where new developments are taking place, but complete compatibility is being manteined
We are going to install MariaDB 10.0 which according to the oficial site is:
MariaDB 10.0 is the current stable version of MariaDB. It is built on the MariaDB 5.5 series with backported features from MySQL 5.6 and entirely new features not found anywhere else.
To install it, run:
cd /usr/ports/databases/mariadb100-server
make install clean
And keep all defaults. To secure your intallation run:
mysql_secure_installation
Set a password, and then accept all defaults.
Finishing up installation
Preparing services to run
We need to configure all servers to autostart when the computer starts, to do that you need to edit the vim /etc/rc.conf
and add this lines.
apache24_enable="YES"
php_fpm_enable="YES"
mysql_enable="YES"
We need to take one more step for Apache. Edit the file /usr/local/etc/httpd.conf
and add this line just before the Include…. line.
ServerName localhost
We can now start all services:
service apache24 start
service mysql-server start
service php-fpm start
Make sure you have these modules enabled
LoadModule authn_core_module libexec/apache24/mod_authn_core.so
LoadModule authz_core_module libexec/apache24/mod_authz_core.so
LoadModule log_config_module libexec/apache24/mod_log_config.so
LoadModule expires_module libexec/apache24/mod_expires.so
LoadModule headers_module libexec/apache24/mod_headers.so
LoadModule version_module libexec/apache24/mod_version.so
LoadModule unixd_module libexec/apache24/mod_unixd.so
LoadModule vhost_alias_module libexec/apache24/mod_vhost_alias.so
LoadModule dir_module libexec/apache24/mod_dir.so
LoadModule rewrite_module libexec/apache24/mod_rewrite.so
LoadModule mime_module libexec/apache24/mod_mime.so
LoadModule actions_module libexec/apache24/mod_actions.so
LoadModule alias_module libexec/apache24/mod_alias.so
LoadModule authz_host_module libexec/apache24/mod_authz_host.so
LoadModule access_compat_module libexec/apache24/mod_access_compat.so
LoadModule proxy_module libexec/apache24/mod_proxy.so
LoadModule proxy_fcgi_module libexec/apache24/mod_proxy_fcgi.so
Restart Apache
service apache24 restart
Test installation
We will create a testing site in order to test everything, and be sure it is all setup and ready to work.
Create a folder for your site
mkdir -p /usr/local/www/site1/{public_html,logs}
chown -R www:www /usr/local/www/site1/
Configure a virtual host
Create a file in /usr/local/etc/apache24/conf.d/site1.conf
, you can name it any other way you want, and add this content inside:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
DocumentRoot "/usr/local/www/site1/public_html"
ServerName freebsd.garron.me
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/www/site1/public_html/$1
ErrorLog "/usr/local/www/site1/logs/error.log"
CustomLog "/usr/local/www/site1/logs/access.log" common
ScriptAlias /cgi-bin/ "/usr/local/www/site1/cgi-bin/"
<Directory "/usr/local/www/site1/public_html">
Options +Indexes +FollowSymLinks +ExecCGI
AllowOverride AuthConfig FileInfo
Require all granted
Allow from all
</Directory>
</VirtualHost>
Test the configuration
I will need to create a file named phpinfo.php
with this contents:
<?php
phpinfo();
?>
Once saved, I can go to http://freebsd.garron.me/phpinfo.php, and the info of PHP should appear. This is the important line:
Server API FPM/FastCGI
That means, you are running PHP-FPM, and this is also a command you want to run to verify the MPM
httpd -V | grep MPM
The reply should be:
Server MPM: event