Category Archives: FreeBSD

FAMP Install Guide – FreeBSD, Apache, Mysql, and PHP Howto

Here is a quick guide of how to properly do a FAMP install on FreeBSD 11.

I will be using the pkg system for this install, but first lets understand a little bit about pkg. For years, FreeBSD used the ports repository – a file tree that contained Makefiles and all the necessary info needed to build any software package from source. In recent years, FreeBSD has put more emphasize on precompiled packages, the pkg utility does just this, it downloads the latest version of the precompiled package and installs it. The syntax is simple:

# pkg install package_name

You can search by using the command:

# pkg search name

For example, “pkg search apache”, will return this list:

apache-ant-1.10.3 Java- and XML-based build tool, conceptually similar to make
apache-forrest-0.9 Tool for rapid development of small sites
apache-mode.el-2.0_1 Emacs major mode for editing Apache configuration files
apache-openoffice-4.1.5_6 Integrated wordprocessor/dbase/spreadsheet/drawing/chart/browser
apache-openoffice-devel-4.2.1833124,4 Integrated wordprocessor/dbase/spreadsheet/drawing/chart/browser (developer version)
apache-poi-3.15 Java API To Access Microsoft Format Files
apache-rat-0.12 Release audit tool
apache-solr-7.1.0 High performance search server built using Lucene Java
apache-solr3-3.6.2 High performance search server built using Lucene Java
apache-spark-2.1.1_1 Fast big data processing engine
apache-xml-security-c-1.7.3_1 Apache XML security libraries - C++ version
apache24-2.4.34 Version 2.4.x of Apache web server
apachetop-0.12.6_5 Apache realtime log stats
p5-Apache-ASP-2.63 Active Server Pages for Apache
p5-Apache-Admin-Config-0.95_1 Perl module to manipulate Apache configuration files
p5-Apache-AuthCookie-3.27 Perl module to provide custom forms for reauthentication

and so on…

When doing a pkg install, you only need the name portion not the full name with version number. So lets begin with our FAMP tutorial.

Step 1 – Install Apache

# pkg install apache24

Once this is completed, you need to add apache24_enable=”YES” to the /etc/rc.conf file. This can be done manually, or with the command:

# sysrc apache24_enable="YES"

Lastly, start apache with either “/usr/local/etc/rc.d/apache24 start” or “service apache24 start”

Step 2 – Install PHP

Which version? You’ll notice that “pkg search php” returns a few different options. For core PHP, your options are:

php56-5.6.36_1
php70-7.0.31
php71-7.1.20
php72-7.2.8

Lets assume you want the latest 7.2 stable, here is the install command, note in addition to PHP core I am going to install a few other popular PHP add-ons. These add-ons were in the output of “pkg search php72”.

# pkg install php72 php72-gd mod_php72 php72-mysqli php72-zlib

NOTE: If you wanted an older version, say 5.6, you would have used the command:

# pkg install php56 php56-gd mod_php56 php56-mysqli php56-zlib

Once PHP is installed, add the following to /usr/local/etc/apache/Includes/php.conf:

<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>

Its also important to install the proper php.ini file. The distribution comes with a sample production and development .ini file, choose the one thats appropriate and move it into position as follows:

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

Now lets test everything before we continue:

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

Visit your server via http://IP-ADDRESS/phpinfo.php and you should see something like this:

Step 3 – Install Mysql

Just as we have done before, we do a “pkg search mysql” and find several versions:

mysql55-client-5.5.60
mysql55-server-5.5.60
mysql56-client-5.6.40
mysql56-server-5.6.40
mysql57-client-5.7.22_1
mysql57-server-5.7.22_2
mysql80-client-8.0.11_1
mysql80-server-8.0.11_1

Lets go with version 5.7, so to install we do:

# pkg install mysql57-client mysql57-server

Next, we add the startup syntax to /etc/rc.conf and startup Mysql:

# sysrc mysql_enable="YES"
# service mysql-server start

Now that MySQL us running, we execute the following script:

# mysql_secure_installation

This script allows you to setup the initial root user password for access to mysql via the command line.

How is FreeBSD different then Linux

This article gives a basic overview of the major differences between FreeBSD and Linux with regards to IP networking, application installs, and starting/stopping services. The assumption is the reader has a good basic understanding of Linux to start of with. We will cover three areas: 1) IP networking, 2) package installation, and 3) starting/stopping services both manually and automated.
IP Networking
I have covered IP settings in previous articles pertaining to Centos and Debian. Debian uses the /etc/system/interfaces file, and Centos uses the /etc/sysconfig/networking-scripts/if-eth0 file.
FreeBSD is different. Everything is in /etc/rc.conf – not just IP settings but everything pertaining to the entire system is in /etc/rc.conf. Thats sounds great right? Basically, FreeBSD has /etc/defaults/rc.conf which contains everything for default settings, but whatever it sees in /etc/rc.conf at bootup overrides the default. Here is the sample rc.conf syntax for basic IP settings:
ifconfig_em0="208.50.100.5 netmask 255.255.255.0 up"
defaultrouter="208.50.100.1"

And for IPv6 add the additional lines:
ipv6_enable="YES"
ipv6_network_interfaces="lo0 em0"
ipv6_ifconfig_em0="2610:bf00:50:100::5/64"
ipv6_defaultrouter="2610:bf00:50:100::1"

In this case our network interface is em0 – we know that from bootup or by looking at the output of dmesg command. Name resolution is still controlled by /etc/resolv.conf as it is in Linux. If you want to add IPv4 aliases, the rc.conf syntax is:
ifconfig_em0_alias0=”208.50.100.6 netmask 255.255.255.255″
ifconfig_em0_alias1=”208.50.128.7 netmask 255.255.255.255″
ifconfig_em0_alias2=”208.50.100.8 netmask 255.255.255.255″
Software Package Installation
In Linux, apt-get and yum are the common tools for adding packages. FreeBSD has a completely different way of doing this. There are two main ways to add software packages: 1) the ports repository, or 2) the sysinstall utility.
The ports repository is a large directly/file tree located in /usr/ports – under that directory there are categories, like /usr/ports/databases, and within the category directory you will find the individual packages, like /usr/ports/databases/mysql51-server. The install the package you go into the packages directory (i.e. /usr/ports/databases/mysql51-server) and run “make && make install”. This will download the most recent tarball, applying system specific patches, compile, and then install the package. If the package has configurable options, a TEXT GUI will pop-up asking you to make those optional selections.
Obviously, the ports to work you need to have the most recent /usr/ports or even have it at all. If you did not install it when creating the system, you can always add it later using the sysinstall utility – /usr/sbin/sysinstall.
sysinstall
sysinstall is a TEXT GUI system that allows you to do many things (too many for me to describe), one of those things is adding distributions. If you dont have /usr/ports installed, go into sysinstall, select “Configuration”, then select “Distributions”, then scroll down and select “Ports”. When you continue from there, it will ask where to install from and you can select the FreeBSD FTP servers to install over the network.
While inside the sysinstall utility you may notice that the utility itself can be used to install software packages. From the main screen, select “Configure”, then select “Packages”. You can now browse through the same category tree and select, for example, Databases -> Mysql 5.1 Server. When you continue through the process, you will again be asked from where to install and you can select the FreeBSD FTP servers.
A sysinstall package add is different then a ports add because the package is not compiled, rather, a pre-compiled package is downloaded and installed.
System Services – Starting, Stopping, Automation
Lastly, how do you start and stop services and set services for automatic start at bootup? Again, this is vastly different then Linux methods, but again, its all covered in /etc/rc.conf. First, the actual scripts for starting and stopping services that have been been added via ports or the sysinstall package system will always be located in /usr/local/etc/rc.d – the exception being core OS services which are located in /etc/rc.d – things like nfsserver or sendmail (on FreeBSD sendmail is a core base service).
So if you have installed apache22, starting and stopping is done by:
/usr/local/etc/rc.d/apache22 stop
or
/usr/local/etc/rc.d/apache22 start
What about automation? That is done in rc.conf as follows:
apache22_enable="YES"
99% of the time the name of the script located in /usr/local/etc/rc.d is the syntax for the above _enable statement, but there are cases where it differs. To be sure, open up the init script located in /usr/local/etc/rc.d and look for its “name” directive which is located near the top. The name is the service name. Oddly enough, mysql is a culprit of this. The mysql service name is mysql, but the init script in /usr/local/etc/rc.d is named mysql-server, so if you added mysql-server_enable=”YES” to /etc/rc.conf it would not work, it has to be mysql_enable=”YES”.