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

Posted on in Categories , , , , last updated January 11, 2015

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:

  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 portsnap.FreeBSD.org mirrors... 7 mirrors found.
Fetching public key from your-org.portsnap.freebsd.org... done.
Fetching snapshot tag from your-org.portsnap.freebsd.org... 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 your-org.portsnap.freebsd.org... 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
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
Fig.02: Starting/Stopping Apache

Note: If you are getting this error “Could not reliably determine the server's fully qualified domain name, using 127.0.0.1.” 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
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
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
Bye

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
Bye

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/mysql-server.sh 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:

-Sg|--skip-grant-tables

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/mysql-server.sh:
/usr/local/bin/safe_mysqld --user=mysql --skip-grant-tables > /dev/null & && echo -n ' mysqld'

Start mysqld:
# /usr/local/etc/rc.d/mysql-server.sh 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/mysql-server.sh stop

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

=> Restart mysqld with: /usr/local/etc/rc.d/mysql-server.sh 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
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
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
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:

ServerAdmin webmaster@cyberciti.biz
ServerName www.cyberciti.biz:80
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>
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>

Now restart Apache server:
# /usr/local/etc/rc.d/apache24 restart
OR
# 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:

<?php
    phpinfo();
?>

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

http://your-domain-name/test.php
http://your-ip-address/test.php

Sample outputs:

Fig.08: Apache + PHP server ready on FreeBSD
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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 10 comments/add one below):

10 comment

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

    Thanks
    Kiran.

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

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

    Have a question? Post it on our forum!