Config Server Firewall

Install Apache PHP and MySQL on FreeBSD

In this tutorial you will learn how to setup a web server on FreeBSD using Apache Web Server, PHP programming language and MySQL/MariaDB database server. In addition to the LAMP stack we will also install phpMyAdmin for the database access and VSFTPD server for the FTP access.

This tutorial is compatible with the Latest FreeBSD version, freebsd 11.

Install and configure Apache Web Server on FreeBSD

Apache is the best server implementation for FreeBSD. The latest version of the Apache HTTP server for FreeBSD is provided by the www/apache24 package which you can install using the pkg command.

pkg install www/apache24

After the installation process is finished, open the /etc/rc.conf file and the following line (This will start HTTP server from startup).

apache24_enable="yes"

Then, run the following command to start Apache Web server:

service apache24 start

From a remote computer, Open a web browser and type the IP address of your FreeBSD server, You should get the response, It works!

Virtual Hosting

Apache Virtual Hosting is a mechanism that allows to host more than one website on a Single server. Following is the sample Apache Virtual Host configuration for FreeBSD (The virtual host configuration should be added to the httpd.conf file).

<VirtualHost *>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /usr/local/www/example.com

    <Directory /usr/local/www/example.com>
        Require all granted
    </Directory>
</VirtualHost>

In this example, the domain name of the website is www.example.com, The DocumentRoot is /usr/local/www/example.com folder.

After you made changes to the httpd.conf file, you need to restart the Apache httpd web server:

service apache24 restart

Install PHP on FreeBSD

PHP: Hypertext Preprocessor (PHP) is a scripting language that is especially used to create dynamic web pages. For FreeBSD, several versions of PHP available.

You can search:

pkg search -o mod_php

At the time of writing, PHP 7.2 is the latest available version and is provided by the www/mod_php72 package.

To install PHP 7, Run the following command:

pkg install www/mod_php72 php72-extensions

After the installation is finished, add the following to your Apache httpd.conf file:

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

Also make sure that index.php is part of your DirectoryIndex as shown below.

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

Run following command to create php.ini file:

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

Then, Restart the Apache HTTP Server:

service apache24 restart

To check the installation create an index.php file in the DocumentRoot and add the phpinfo() function. From the web browser, type your server IP-Address/index.php.

Install MariaDB Database Server

We are going to use MariaDB as our MySQL server implementation, At the time of writing MariaDB Server 10.2 is the latest version. You can run pkg search command to find the latest version available on your FreeBSD system.

To Install MariaDB server, Run:

pkg install databases/mariadb102-server

Then, add following to the /etc/rc.conf file:

mysql_enable="yes"

Start the MySQL server for the first time:

service mysql-server start

By default MySQL root user does not have a password, to set up a new mysql root password run the mysql_secure_installation command.

mysql_secure_installation

Install phpMyAdmin on FreeBSD

The phpMyAdmin package that's available from FreeBSD repository, not compatible with PHP version 7. For PHP 7 you need to download the latest version from the https://www.phpmyadmin.net/downloads/ page. The following section describes how to do it from the command line.

First install the mysqli and other PHP extensions:

pkg install php72-mysqli php72-mbstring-7.2.5 php72-extensions

Next, get the latest download link from the phpMyAdmin download page and download the package using wget command:

cd /usr/share/
wget https://files.phpmyadmin.net/phpMyAdmin/4.7.9/phpMyAdmin-4.7.9-all-languages.tar.gz

Extract archive file:

tar -zxvf phpMyAdmin-4.7.9-all-languages.tar.gz

Rename the folder:

mv phpMyAdmin-4.7.9-all-languages phpmyadmin

Create a file called phpmyadmin.conf inside the /usr/local/etc/apache24/Includes/ directory and add following configuration:

Alias /phpMyAdmin /usr/share/phpmyadmin
Alias /phpmyadmin /usr/share/phpmyadmin

<Directory /usr/share/phpmyadmin/>
    AddDefaultCharset UTF-8

    <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
      Require all granted
      Require ip 127.0.0.1
      Require ip ::1
     </RequireAny>
    </IfModule>
</Directory>

<Directory /usr/share/phpmyadmin/setup/>
    <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
      Require ip 127.0.0.1
      Require ip ::1
     </RequireAny>
    </IfModule>
</Directory>

# These directories do not require access over HTTP - taken from the original
# phpmyadmin upstream tarball
#
<Directory /usr/share/phpmyadmin/libraries/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>

<Directory /usr/share/phpmyadmin/setup/lib/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>

<Directory /usr/share/phpmyadmin/setup/frames/>
    Order Deny,Allow
    Deny from All
    Allow from None
</Directory>

Save the file and restart the Apache Web Server:

service apache24 restart

To access phpMyAdmin type your server-IP/phpmyadmin in the web browser.

Configure FTP Server with vsftpd

File transfer protocol provides a simple and easy way to transfer files between the server and client computers. There are several FTP server softwares available for FreeBSD, the one  we are going to use is vsftpd. The Following steps describe, how to install and configure VSFTPD on FreeBSD 11.1.

First install the vsftpd package:

pkg install ftp/vsftpd

Open the /usr/local/etc/vsftpd.conf and make sure that following configuration options are as it is:

anonymous_enable=NO
local_enable=YES
write_enable=YES
listen=YES
background=YES

Open the /etc/rc.conf and add following:

vsftpd_enable="yes"

And we are ready to start the vsftpd server on FreeBSD:

service vsftpd restart

Example: create an FTP user for the DocumentRoot

Following example demonstrate, how to create FTP user who have write access to the DocumentRoot of the example.com website.

Add new user with custom home directory:

pw useradd -n ftp_user1 -d /usr/local/www/example.com

Set Password for the user:

passwd ftp_user1

Set File permissions:

chown -R ftp_user1 /usr/local/www/example.com
chmod u+rw /usr/local/www/example.com

Now the FTP user can login to the server via  FTP client and access the files of the website.

And VSFTPD completes our Web Server, Now you have complete Web Server up and running on your FreeBSD system.