FAMP Stack - jonatello/lab-musing GitHub Wiki

The following guide is for building a FreeBSD / Apache / MySQL / PHP Stack (FAMP) in order to host a website utilizing the Wordpress Content Management System. This also includes caching via Varnish.

Configuration:

Configure Apache

Update the jail

pkg update && pkg upgrade

Install the Apache package

pkg install apache24

Enable Apache via rc.conf

sysrc apache24_enable=yes

Configure the Apache config to load the rewrite_module and AllowOverride All within the "/usr/local/etc/apache24/data" Directory block. I suppose the awk command could be accomplished with sed if I used a start and end of the actual block, but learning an awk method was fun. In this case, it is taking the first instance of "AllowOverride None" and replacing with "AllowOverride All" (there are 2 instances).

cat /usr/local/etc/apache24/httpd.conf | awk '/AllowOverride None/{if (M==""){sub("AllowOverride None","AllowOverride All");M=1}}{print}' > /usr/local/etc/apache24/httpd.confnew

cp /usr/local/etc/apache24/httpd.confnew /usr/local/etc/apache24/httpd.conf

sed -i '' 's/#LoadModule rewrite_module libexec\/apache24\/mod_rewrite.so/LoadModule rewrite_module libexec\/apache24\/mod_rewrite.so/g' /usr/local/etc/apache24/httpd.conf

Configure gzip support within httpd.conf by setting the deflate_module to be loaded and appending the following lines. Additional configuration will be set within .htaccess

sed -i '' 's/#LoadModule deflate_module libexec\/apache24\/mod_deflate.so/LoadModule deflate_module libexec\/apache24\/mod_deflate.so/g' /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE text/plain" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE text/html" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE text/xml" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE text/css" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE application/xml" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE application/xhtml+xml" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE application/rss+xml" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE application/javascript" >> /usr/local/etc/apache24/httpd.conf

echo "AddOutputFilterByType DEFLATE application/x-javascript" >> /usr/local/etc/apache24/httpd.conf

Configure directories for managing multiple sites without having to modify httpd.conf, then configure Apache to include configurations from the sites-enabled directory

mkdir /usr/local/etc/apache24/sites-available

mkdir /usr/local/etc/apache24/sites-enabled

echo "IncludeOptional /usr/local/etc/apache24/sites-enabled/*" >> /usr/local/etc/apache24/httpd.conf

Set the ServerName appropriately

sed -i '' 's/#ServerName www.example.com:80/ServerName example.com/g' /usr/local/etc/apache24/httpd.conf

Start Apache

service apache24 start

Configure MySQL

Install the MySQL package

pkg install mysql56-server

Enable MySQL via rc.conf

sysrc mysql_enable=yes

Start MySQL

service mysql-server start

Run the Secure Installation (fill out prompts appropriately)

mysql_secure_installation

Log into MySQL via root

mysql -u root -p

Create the "example.com" database and configure the user with privileges, "jonatello" in this example

CREATE DATABASE example.com;

CREATE USER 'jonatello'@'localhost' IDENTIFIED BY '%Password%';

GRANT ALL PRIVILEGES ON example.com.* to 'jonatello'@'localhost';

FLUSH PRIVILEGES;

EXIT;

Configure PHP

Install PHP 5.6 and the necessary extension packages

pkg install mod_php56 php56-mysql php56-mysqli php56-xml php56-hash php56-gd php56-curl php56-tokenizer php56-zlib php56-zip php56-filter

Alternatively 7.2 seems to work well

pkg install mod_php72 php72-mysqli php72-xml php72-hash php72-gd php72-curl php72-tokenizer php72-zlib php72-zip php72-filter php72-json

Copy the default production config to php.ini and set the timezone appropriately

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

sed -i '' 's/;date.timezone =/date.timezone = "%My%\/%Timezone%"/g' /usr/local/etc/php.ini

Create php.conf using "famp.php.conf"

vi /usr/local/etc/apache24/Includes/php.conf

Restart Apache

service apache24 restart

Configure Wordpress

Download the latest Wordpress package

fetch http://wordpress.org/latest.tar.gz

Uncompress and then remove the archive

tar xvf latest.tar.gz

rm latest.tar.gz

Change directories into "wordpress"

cd ./wordpress

Copy the sample PHP to "wp-config.php"

cp wp-config-sample.php wp-config.php

Update the wp-config.php file, replacing "DB_Name", "DB_USER", and "DB_PASSWORD" appropriately

sed -i '' 's/database_name_here/example.com/g' ./wp-config.php

sed -i '' 's/username_here/%Username%/g' ./wp-config.php

sed -i '' 's/password_here/%Password%/g' ./wp-config.php

To add the ability for direct filesystem access through PHP, which seems to normally fix issues regarding Wordpress updates, you can also add the following line:

define('FS_METHOD', 'direct');

If you'd like to remove the "^M" characters as well (DOS/Windows line endings), use vi to edit the config, then use the following command to remove every occurrence of the pattern (use CTRL + V for the "^" and CTRL + M for the "M")

:%s/^M//

Change up one level from the now prepared Wordpress directory

cd ..

Copy the Wordpress directory recursively and without following symbolic links to the Apache data directory

cp -rp ./wordpress/* /usr/local/www/apache24/data/

Change ownership to the Apache user recursively for the data directory

chown -R www:www /usr/local/www/apache24/data/

chmod -R 755 /usr/local/www/apache24/data/

Remove the default Apache index

rm /usr/local/www/apache24/data/index.html

Create the .htaccess file within the Apache data directory using "apache.wordpress.htaccess", then change ownership of the file to Apache

vi /usr/local/www/apache24/data/.htaccess

chown www:www /usr/local/www/apache24/data/.htaccess

Configure Varnish

Install the Varnish package

pkg install varnish6

Reconfigure Apache to listen on port 8080

sed -i '' 's/Listen 80/Listen 8080/g' /usr/local/etc/apache24/httpd.conf

Enable Varnish, configure it to listen on port 80 and proxy to 8080 and allocate 512MB of memory all via rc.conf

sysrc varnishd_enable=YES

sysrc varnishd_listen=":80"

sysrc varnishd_backend="localhost:8080"

sysrc varnishd_storage="malloc,512M"

Restart Apache

service apache24 restart

Start Varnish

service varnishd start

Troubleshooting

Test Apache connectivity and get PHP information by creating an info.php page. First create the file

touch /usr/local/www/apache24/data/info.php

Then add the PHP content

echo '<?php phpinfo(); ?>' > /usr/local/www/apache24/data/info.php

Now browse to your website, adding the path /info.php

Remove the info.php page after testing to not reveal unecessary information

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

⚠️ **GitHub.com Fallback** ⚠️