How To Install Apache, MySQL, PHP stack on FreeBSD Unix Server

I‘m a new FreeBSD Unix system users. How can I setup and install Apache, MySQL, PHP stack on a FreeBSD 10 based Unix server?

FAMP stack is nothing but group of source software to run php based apps. Our sample setup includes:

    Tutorial details
    Difficulty Easy (rss)
    Root privileges Yes
    Requirements FreeBSD v10+
    Time 20m
  1. FreeBSD 10.1-RELEASE amd64
  2. Apache v2.4
  3. PHP v5.6
  4. MySQL v5.6

This tutorial explains how to install and configure FAMP stack.

Update your ports

Like always make sure everything is up to date before starting. I like to do:
# portsnap fetch update && portupgrade -a
Sample outputs:

Looking up mirrors... 7 mirrors found.
Fetching public key from done.
Fetching snapshot tag from done.
Fetching snapshot metadata... done.
Fetching snapshot generated at Fri Jan  2 00:09:16 UTC 2015:
4955cbbaee76bcad21123666d1c7eee0d55bc059e2ea04100% of   70 MB 5735 kBps 00m13s
Extracting snapshot... done.
Verifying snapshot integrity... done.
Fetching snapshot tag from done.
Fetching snapshot metadata... done.
Updating from Fri Jan  2 00:09:16 UTC 2015 to Fri Jan  2 11:42:26 UTC 2015.
Fetching 4 metadata patches... done.
Applying metadata patches... done.
Fetching 0 metadata files... done.
Fetching 8 patches. 

See FreeBSD Update All Installed Ports / Applications tutorial for more information.

Install Apache server

To install the port:
# cd /usr/ports/www/apache24/ && make install clean
Or, to add the package:
# pkg install www/apache24
Sample outputs:

Fig. 01: Install apache

Starting up Apache service on boot

Add following to the end of “/etc/rc.conf” file to launch Apache at start up:

echo 'apache24_enable="YES"' >> /etc/rc.conf

Starting / stopping / restarting Apache server

To start Apache to make sure it works:
# /usr/local/etc/rc.d/apache24 start
To restart Apache server:
# /usr/local/etc/rc.d/apache24 restart
To stop Apache server:
# /usr/local/etc/rc.d/apache24 stop
You can also use the service command for starting/stoping/restarting Apache server on FreeBSD:

## service command to control Apache server ##
service apache24 start
service apache24 restart
service apache24 stop
service apache24 status

Sample outputs:

Fig.02: Starting/Stopping Apache

Note: If you are getting this error “Could not reliably determine the server's fully qualified domain name, using” Set the ‘ServerName’ directive globally to suppress this message:
Add next line to /usr/local/ect/apache24/httpd.conf file:
ServerName localhost

Replace localhost with the server’s domain name which can be obtained with:
# hostname -f

Setting up MySQL server

The package or port to be installed will depend on available MySQL version. As of this writing, the maximum available version is 5.6.

Install MySQL server

To install the port:
# cd /usr/ports/databases/mysql56-server/ && make install clean
or, to add the package:
# pkg install databases/mysql56-server

Fig.03: Installing mysql database server

Install MySQL client

To install the port:
# cd /usr/ports/databases/mysql56-client/ && make install clean
or, to add the package:
# pkg install databases/mysql56-client

Starting up Mysql server service on boot

Finally, /etc/rc.conf must contain the following line to allow the MySQL server to start:

echo 'mysql_enable="YES"' >> /etc/rc.conf

Starting / stopping / restarting Mysql server

To start the Mysql server type:
# /usr/local/etc/rc.d/mysql-server start
To restart the Mysql server type:
# /usr/local/etc/rc.d/mysql-server restart
To stop the Mysql server type:
# /usr/local/etc/rc.d/mysql-server stop
You can also use the service command for starting/stoping/restarting mysql server on FreeBSD:

## command to start/stop mysql servers on a FreeBSD 10 ##
service mysql-server start
service mysql-server restart
service mysql-server stop
service mysql-server status

Sample outputs:

Fig.04: Starting/Stopping mysql server

Setting up Mysql server passwords

The default is set to allow anyone to have full access. It’s very important that you set up passwords. To set a password on the anonymous accounts use:
# mysql -u root
Run the following sql queries at mysql> prompt (replace host_name with actual system host name which can be obtained with hostname -f command:

    mysql> SET PASSWORD FOR ''@'localhost' = PASSWORD('newpwd-here');
    mysql> SET PASSWORD FOR ''@'host_name' = PASSWORD('newpwd-here');
    mysql> quit

To set a password for the root account use:
# mysql -u root
Run the following sql queries at mysql> prompt:

    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd-here');
    mysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpwd-here');
    mysql>  quit

NOTE: An alternative method to set a password for the MySQL root user is to run the following command:
/usr/local/bin/mysqladmin -u root password 'PASSWORD-HERE'
See “MySQL Change root Password” tutorial for more information.

After setting the password, here is how you shutdown mysqld server:
# mysqladmin -u root -p shutdown
Sample outputs:
Enter password:

Tip: If you “forget” the root password, here is how you can reset it

Stop mysqld with this command:
# /usr/local/etc/rc.d/ stop
Modify the start command to add this option to the command line. Do not leave this option on for long. Only for when you need it. It bypasses all usual mysql security:


This option causes the server not to use the privilege system at all. This gives everyone full access to all databases! (You can tell a running server to start using the grant tables again by executing mysqladmin flush-privileges or mysqladmin reload.)

Modify it in /usr/local/etc/rc.d/
/usr/local/bin/safe_mysqld --user=mysql --skip-grant-tables > /dev/null & && echo -n ' mysqld'

Start mysqld:
# /usr/local/etc/rc.d/ start
Connect to mysql:
# mysql
Look out because this next step will reset the passwords for all root users. Make sure that’s what you want to do. You may wish to restrict this SQL by including an “and host = ‘something'” clause. Inspect existing users with “SELECT host, user from user;“. Select the mysql database and reset the password:

    mysql> use mysql
    mysql> update user set password = PASSWORD('secret') where user = 'root';
    mysql> quit

=> Do not forget to undo that mysql bypass i.e. Stop mysqld with: /usr/local/etc/rc.d/ stop

=> Remove the option from the vi /usr/local/etc/rc.d/ and remove --skip-grant-tables options.

=> Restart mysqld with: /usr/local/etc/rc.d/ start

=> See “Recover MySQL root Password” tutorial for more information.

Install PHP

When you build PHP, you need to add the configuration option so that PHP build includes support for the Apache server. Type the following commands:
# cd /usr/ports/lang/php56
# make config

When the menu comes up to select/deselect various build options. You should select:

Fig.05: Build PHP using ports collection

Now you will do the make clean command. I normally do these commands all in one but to get the configuration menu, I do them apart.
# make install clean

Install mod_php for Apache

Type the following commands to build mod_php for Apache:
# cd /usr/ports/www/mod_php56
# make install clean

Sample outputs:

Fig.06: Install mod_php for Apache

Install php extensions

If you aren’t sure if it’s or you didn’t check it with your MySQL install, you will do it the commands the same way so you get the menus to configure to add support for both MySQL and MySQLi to communicate with the MySQL server.
# cd /usr/ports/lang/php56-extensions/
# make config

Fig.07: Install popular extensions for Apache

You may also select other extensions as per your PHP apps requirements. Then finish up with:
# make install clean

Configure mod_php

To configure it you will type the following command that just makes a copy of a file:
# cp /usr/local/etc/php.ini-development /usr/local/etc/php.ini

To configure Apache and open the file:
# ee /usr/local/etc/apache24/httpd.conf
I use ee (feel free to use vi or Emacs) text editor for simple edits, look for this line:

    DirectoryIndex index.html

And change it so it reads as follows:

    DirectoryIndex index.html index.htm index.php

Find and set the following values as per your domain, IP, and port number:

Listen :80

Save and close the file. Create a file called /usr/local/etc/apache24/modules.d/001_mod_php.conf as follows:

# cat /usr/local/etc/apache24/modules.d/001_mod_php.conf
<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source

Now restart Apache server:
# /usr/local/etc/rc.d/apache24 restart
# service apache24 restart

Test your setup

Create a file called /usr/local/www/apache24/data/test.php:
# vi /usr/local/www/apache24/data/test.php
Append the following code:


Save and close the file. Fire a web-browser and type the url:


Sample outputs:

Fig.08: Apache + PHP server ready on FreeBSD

How do I secure PHP?

See our 25 PHP Security Best Practices For Sys Admins for more info.

Recommended readings

This quick tutorial was contributed by Wendy Michele. Editing by admin. You can too contribute to nixCraft.

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Kiran Jan 11, 2015 @ 17:49

    Hi Vivek,

    Please send me the link which explains clearly on how to compile and install apache2.x manually with Mysql, PHP on linux(centos) using source file. and also how to install it with various modules like (rewrite etc). Also please send me links on how to install tomcat with apache.


  • Riccardo Feb 2, 2015 @ 11:51

    Hi, I would like to see a similar tutorial for Ruby.

  • luis Feb 18, 2015 @ 23:07

    Very good information, a guide for desktop Xfce in system FreeBSD? What difference between server and desktop? what difference between apache and firefox browser, change the for spanish language of the whole system FreeBSD and Xorg, and its variables locale

  • qtsidekick Apr 2, 2015 @ 15:23

    By any chance you have the instruction for Adminer on FreeBSD 10.1?

  • qtsidekick Apr 6, 2015 @ 20:55

    I got Adminer installed. Thanks :)

  • Marcio Cruz Jun 14, 2015 @ 4:19

    It’s wokrs !

    Thanks !!!

  • J Oct 14, 2015 @ 10:50

    What about a more high performance option: php-fpm instead of mod_php?

  • DaTTaBaNShiK Dec 18, 2015 @ 15:44

    Great post! Well structured and detailed. Just following through the steps made it work. Thanks a lot!

  • Nobody Mar 13, 2016 @ 10:23

    I followed these steps, however the info.php file shows up as plain txt when I call it.

    • bob Feb 11, 2017 @ 19:54

      I have the same problem. did you ever solve it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @