Installing OpenBSD's httpd server, MariaDB, PHP 5.6 on OpenBSD 5.9

This tutorial will guide you through installing OpenBSD's httpd server, MariaDB, and PHP 5.6 on a base system running OpenBSD 5.9. Please start with a freshly-built system to avoid bugs and conflicts.

Since version 5.6, OpenBSD ships with a built-in http server called "httpd". This tutorial covers installing and configuring OpenBSD's httpd instead of Apache or Nginx.

1. Preliminary steps

The first thing to do is choose a software mirror for installing PHP and MariaDB; a full listing may be found on OpenBSD's download page. You'll want to choose a mirror closest to you that has complete packages (located in /5.9/packages/thearchyouinstalled/) for 5.9. Most do, but it's worth checking just to make sure.

Once you've chosen a mirror, add it to the .profile file in root with the following line:

export PKG_PATH="http://full.url.of/the/mirror/you/chose"

Replacing, of course, the contents between the quotes with the full URL of your chosen packages mirror.

Adding the mirror to .profile will make it available each time you log in.

2. Install and configure httpd

The OpenBSD man pages on httpd and httpd.conf will be helpful here. No actual installation is necessary; httpd comes with OpenBSD and all you need to do is enable and configure it.

Create and edit /etc/httpd.conf if it doesn't already exist and add the following code:

server "default" {
        listen on egress port 80
}

types {
        text/css                css
        text/html               html htm
        text/txt                txt
        image/gif               gif
        image/jpeg              jpeg jpg
        image/png               png
        application/javascript  js
        application/xml         xml
}

Start httpd with:

/etc/rc.d/httpd -f start

Create a file in /var/www/htdocs called "index.html" containg the text "Hello world, I'm HTML!" Navigate to your server's IP address in your browser and you should see the "Hello world, I'm HTML!" text.

3. Install and configure MariaDB

MySQL is no longer available for OpenBSD but MariaDB is the comparable (almost 100% exactly the same) open-source drop-in replacement.

Use pkg_add to install MariaDB. The command to do that is:

pkg_add mariadb-server

Now run the following command to finalize the MariaDB installation:

mysql_install_db

Start MariaDB by running:

/etc/rc.d/mysqld start

At this point you will need to make some security modifications to your MariaDB installation, particularly setting a root password and removing test and anonymous data. Do that with the following command:

mysql_secure_installation
  1. Press "Enter" at Enter current password for root (enter for none)
  2. Enter "y" at Set root password? [Y/n]
  3. Now enter a new secure password for the root MySQL user
  4. At Remove anonymous users? [Y/n], enter "y"
  5. Enter "y" at Disallow root login remotely? [Y/n]
  6. Remove the test database by entering "y" at Remove test database and access to it? [Y/n]
  7. Reload the privilege tables so your changes take effect by entering "y" at Reload privilege tables now? [Y/n]

MariaDB should now be successfully installed, time to move on to installing PHP 5.6!

4. Install and configure PHP 5.6

First, install PHP 5.6 by using the following command:

pkg_add php php-gd php-mysql php-mysqli

When prompted to select the php version, choose the php*-5.6.X options. For the primary PHP package, do not choose the *-ap2 one.

Our choices (yours may differ depending on version):

  1. 5 for "Ambiguous: choose package for php"
  2. 3 for "Ambiguous: choose package for php-gd"
  3. 3 for "Ambiguous: choose package for php-mysql"
  4. 3 for "Ambiguous: choose package for php-mysqli"

Set index.php as the default index page by adding this under the above code, ensuring it is still inside the "default" server block:

directory {
    index "index.php"
}

Create a file in /var/www/htdocs called "index.php" and paste in the following code:

<?php
echo "Hello world, I'm PHP!";
?>

Restart httpd with:

/etc/rc.d/httpd -f restart

Navigate to your server's IP address in your browser again and you should see the text "Hello world, I'm PHP!"

5. Start the http, PHP, and MariaDB services on reboot

Edit / create /etc/rc.conf.local and add the following lines:

httpd_flags=
pkg_scripts="mysqld php56_fpm"

Now reboot the server to enure httpd, php_fpm, and mysqld start on reboot.

That concludes our tutorial on installing httpd, MariaDB, and PHP 5.6 on OpenBSD 5.9.

Instantly deploy a BSD cloud VPS with RootBSD

deploy instantly

  • Fast SSD hosting
  • Full root access
  • IPv6 and IPv4 enabled hosting
  • Over 25 data center locations worldwide
  • Friendly and knowledgeable support
  • Build with current FreeBSD, OpenBSD, NetBSD, Linux releases