How to Install Apache, MariaDB, and PHP (FAMP) stack on FreeBSD 11

In this tutorial we will show you how to install Apache, MariaDB, and PHP on FreeBSD 11. If you don’t know FAMP is the acronym of FreeBSD, Apache, MySQL/MariaDB, PHP. FreeBSD 11 is the latest version right now when this article was made.

Prerequisites

Before you begin with this guide, you should at least have basic knowledge of linux or unix, know basic shell command for linux or unix, have login as root user and of course you need FreeBSD 11 installed on your PC or server.

Step 1 – Update your FreeBSD

Before we continue to installing any software , we need to update first our FreeBSD 11 operating system.

Run the following commandson your FreeBSD server.

$ su
# freebsd-update fetch
# freebsd-update install

If you have been installed lates update you should get output like these

# freebsd-update fetch
src component not installed, skipped
Looking up update.FreeBSD.org mirrors... 4 mirrors found.
Fetching public key from update5.freebsd.org... done.
Fetching metadata signature for 11.0-RELEASE from update5.freebsd.org... done.
Fetching metadata index... done.
Fetching 1 metadata files... done.
Inspecting system... done.
Preparing to download files... done.

No updates needed to update system to 11.0-RELEASE-p0.
# freebsd-update install
src component not installed, skipped
No updates are available to install.
Run '/usr/sbin/freebsd-update fetch' first.

Step 2 – Install Apache Webserver

We will install apache 2.4 using pkg command.

# pkg install apache24

If you first time using pkg command in your operating system you should get notification yo install package management tool, just choose y

# pkg install apache24
The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]: y

Next, we enable apache to run at boot time using these command:

# sysrc apache24_enable=yes

Start the apache service using these command:

# service apache24 start

Test apache web service:

Before we test apache, we need to configure apache configuration. Edit this file using nano /usr/local/etc/apache24/httpd.conf.

# nano /usr/local/etc/apache24/httpd.conf

Find and edit this variable like these based on your ip address or hostname, in this example we using ip address 192.168.14.126:

ServerName 192.168.14.126:80

Exit and save the file. Then restart apache service using below command:

# service apache24 restart

Now, open your web browser and navigate to: http://IP-address/ or http://localhost/. You should see the Apache test page.

Step 3 – Install MariaDB

MariaDB is drop-in replacement of MySQL.It has same process name, same syntax and configuration. To install run these following pkg command:

# pkg install mariadb100-server

Copy MariaDB example configuration from directory ‘/usr/local/share/mysql/‘ to ‘/usr/local/etc/‘:

# cp /usr/local/share/mysql/my-medium.cnf /usr/local/etc/my.cnf

Enable and start MariaDB service:

# sysrc mysql_enable=yes
# service mysql-server start

In default MariaDB installation root password was empty. For security reason we will create root password using this command:

# mysql_secure_installation

When prompt “Enter current password for root”, just press ENTER key and set the password twice. Then simply press Y to accept the default values.

Sample output:

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Step 4 – Install PHP

We need php to server dinamic content, to install PHP run these following command:

# pkg install mod_php56 php56-mysql php56-mysqli

After installation success, we need to copy php.ini sample configuration:

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

Update changes with this command:

# rehash

We need configure PHP with apache webserver, so we need edit apache configuration file using nano:

# nano /usr/local/etc/apache24/httpd.conf

Find the DirectoryIndex section and add index.php in front of the existing index.html as shown below.

[...]

<IfModule dir_module>
 DirectoryIndex index.php index.html
</IfModule>

[...]

And then, add the following lines at the bottom of Apache configuration file

<FilesMatch "\.php$">
 SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
 SetHandler application/x-httpd-php-source
</FilesMatch>

Save and close the file. We need restart Apache web server to take change configuration:

# service apache24 restart

Step 5 – Test PHP

To test PHP we will create sample php script using nano:

# nano /usr/local/www/apache24/data/test.php

Add the following line:

<?php phpinfo(); ?>

Save and close the file. Open web browser and navigate to your server address http://IP-Address/test.php.

Step 6 – Install PHP Modules (Extensions)

In order to get full functioning your PHP script sometimes we need to install some additional PHP modules (extensions), you can skip this step if you don’t need to install any extensions. To view the list of available modules, just run:

# pkg search php56

Sample output :

# pkg search php56
mod_php56-5.6.26               PHP Scripting Language
php56-5.6.26                   PHP Scripting Language
php56-bcmath-5.6.26            The bcmath shared extension for php
php56-bz2-5.6.26               The bz2 shared extension for php
php56-calendar-5.6.26          The calendar shared extension for php
php56-ctype-5.6.26             The ctype shared extension for php
php56-curl-5.6.26              The curl shared extension for php
php56-dba-5.6.26               The dba shared extension for php
php56-dom-5.6.26               The dom shared extension for php
php56-exif-5.6.26              The exif shared extension for php
php56-extensions-1.0           "meta-port" to install PHP extensions
php56-fileinfo-5.6.26          The fileinfo shared extension for php
php56-filter-5.6.26            The filter shared extension for php
php56-ftp-5.6.26               The ftp shared extension for php
php56-gd-5.6.26                The gd shared extension for php
php56-gettext-5.6.26           The gettext shared extension for php
php56-gmp-5.6.26               The gmp shared extension for php
php56-hash-5.6.26              The hash shared extension for php
php56-iconv-5.6.26             The iconv shared extension for php
php56-imap-5.6.26              The imap shared extension for php
php56-interbase-5.6.26         The interbase shared extension for php
php56-json-5.6.26              The json shared extension for php
php56-ldap-5.6.26              The ldap shared extension for php
php56-mbstring-5.6.26          The mbstring shared extension for php
php56-mcrypt-5.6.26            The mcrypt shared extension for php
php56-mssql-5.6.26             The mssql shared extension for php

You can verify what each module does from the comment section in the above output, or just run the following command:

# pkg search -f php56-curl

Sample output:

# pkg search -f php56-curl
php56-curl-5.6.26
Name           : php56-curl
Version        : 5.6.26
Origin         : ftp/php56-curl
Architecture   : freebsd:11:x86:64
Prefix         : /usr/local
Repository     : FreeBSD [pkg+http://pkg.FreeBSD.org/FreeBSD:11:amd64/quarterly]
Categories     : ftp
Licenses       : PHP301
Maintainer     : ale@FreeBSD.org
WWW            : http://www.php.net/
Comment        : The curl shared extension for php
Shared Libs required:
 libcurl.so.4
Annotations    :
 cpe            : cpe:2.3:a:php:php:5.6.26:::::freebsd11:x64
Flat size      : 90.1KiB
Pkg size       : 26.3KiB
Description    :
PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open
Source general-purpose scripting language that is especially suited for
Web development and can be embedded into HTML.  Its syntax draws upon C,
Java, and Perl, and is easy to learn.  The main goal of the language is to
allow web developers to write dynamically generated webpages quickly, but
you can do much more with PHP.

WWW: http://www.php.net/

To install php extension, for example we will install php56-curl, run with this following command:

# pkg install php56-curl

To take change after installation, apache web service need to be restarted:

# service apache24 restart

Congratulations, you have successfully install Apache, MariaDB, and PHP (FAMP) stack on FreeBSD 11, now you’re ready to host your websites or any web based applications.

Related Post