DEV Community

Cover image for Moving away from Ubuntu: peeking at FreeBSD (Part 2)
Juan Miguel Medina Prieto
Juan Miguel Medina Prieto

Posted on

Moving away from Ubuntu: peeking at FreeBSD (Part 2)

Previously, I had installed FreeBSD in a VirtualBox VM and had started tinkering with it: I simply created another user and set it up so that I could log in as root from it via SSH. Besides, I attempted to set bash as root's shell, even though I failed. While doing so, I had started using pkg, FreeBSD's software management tool, and this time, I'm going to use it even further, as I'll be installing a LAMP stack (that is, Apache or a web server, PHP and MySQL) and then I'll be trying to set a desktop environment up.

The disk structure

Before that, though, I want to try something: attaching another virtual HDD to the VM, to check the way FreeBSD lays it out. Let's recall the following image:

Disk setup

This was the result the final partitioning screen showed during the installation, display only one disk with two partitions: the main one, and the swap partition. It's quite a bit different and more complex than what's usual in Linux (the disk itself would have been called "sda" and their partitions, "sda1" and "sda2", respectively). And out of curiosity, I want to explore this naming convention a bit further by experimenting a little bit.

As such, I add two more disks in the storage section while the VM is turned off, and the setup comes out like this.

Three disks in the VM

Next, I turn it on and log in via SSH. Then I try to run the command "lsblk", but it's not found, so I check the manual page for "fdisk". It says it's obsolete and advises to use "gpart" instead.

gpart's manual page

That's probably the command I need. I try it and... It seems like it just lists the main disk, probably because the others are blank. And this is when I check the VM's configuration and notice the changes I had made before were missing. I'll have to do them again. Unfortunately, after adding the disks, the command's output was still the same.

There's only one disk listed here

Still, when checking the contents of /dev, two new devices appear just after the ones representing the main HDD and its partitions. Even if I can't get them to appear on any tools that come to my mind, I still want to try to initialize, so I'll log in as root and try "fdisk /dev/ada1", as it's the only console-based tool I really have experience with.

FreeBSD's fdisk

Seems like I'm out of luck here, too, as this behaves in a different way to Debian's "fdisk". So I check the manual, and I eventually try "gpart create -s mbr /dev/ada1", wanting to initialize the second disk's partition table.

Trying gpart for the first time

It does seem to have liked that, even though I can't really see any difference when running "gpart list". Still, it does appear if I add the "-a" option, so I think I'm in the right direction. Now, I'll try creating two partitions.

Adding partitions to the second disk

It seems to work, but will it have processed the partition sizes as I wanted? I was trying to make the first take 10 gigabytes, and then the rest for the second one.

The disk's partitions

Yes, it does! Now I guess I can initialize the third disk without any problems. That one will have a single "linux-data" partition. Now I'm not sure of whether "gpart" just makes the layout or does the full formatting process. Once I'm done, I'll commit the changes for both disks, since this tool seems to work in a similar way to version-control and transaction-based systems... Or maybe I don't need to do so, as I didn't specify any flags at all, and those were just meant to do exactly what I was talking about before. Just to be sure, I reboot the machine, and sure enough, there they are. Whoops!

/dev after doing all that stuff with gpart

Anyway, before moving on, I want to check whether the partitions are formatted or not. I just noticed the naming style is a bit different in the main disk compared to the other, probably because of the swap thing. So I'll try to mount... "ada1s1", for example.

mount fails

It doesn't seem to work, so I'll try to format that partition, then try again. Unluckily, there's no "mkfs" command, so I'll have to look around. This online manual page shows the command "newfs", but it doesn't suit what I need here, since that partition is meant to have a FAT32 filesystem. This one, however, shows the "newfs_msdos" command, which is the precise one I need.

Formatting and trying to mount

Still, I don't get any different results than before. I search online how to mount a FAT32 device, land here, and it turns out I wasn't specifying the device type correctly.

It finally worked

Now it makes a lot more sense to me. And... I have finally determined what "gpart" does: it just makes the partitions, it doesn't format then. Oh well, I'll move on.

Installing a web server

After messing with the disk partitioning and formatting tools, I think it's time to try to do one of the main attractions: the LAMP stack. Just as a reminder, the LAMP stack consists of:

  • Apache, a web server

  • PHP, a programming language which is mainly used alongside Apache

  • MySQL, a database management system

Obviously, these three components would normally be running on a Linux machine, but I'm going to try to make them run together under FreeBSD in order to get some experience with pkg and recall how these had to be configured.

I'll start by Apache. The first thing I should obviously try is searching for it in the repositories. It should be done with "pkg search apache".

What results of "pkg search apache"

It does seem to return a lot of results, but I quickly find the one that I need: "apache24-2.4.41". I won't be picky about the version number since I just want to see it running.

Installing Apache

After waiting for a while, the installation ends and I get this message:

Post-installation message

Once I do the "rc.conf" modification, which is a weird way to add a service to start after booting, especially coming from Ubuntu, I try the VM's IP address in my browser and... it doesn't work. Probably I need to start it manually. Luckily, the "service" command does drive me somewhere.

The service command

Now I suspect FreeBSD is running services by using System V or something similar... Oh well, at least listing the scripts shows "apache24", so I start it by typing "service apache24 start", it complains a bit and, once I try it in my web browser...

It works!

Yeah, that's what I was going to say... Nevermind, I should create a quick PHP script so that the module can be tested as soon as it's installed. But it seems like I have to find where the main folder is, since it's not /var/www/html. I think Apache is the best guy to ask. However, /etc doesn't contain any "apache" folders. After having a little fight with "find", since this command is a lot pickier than in Linux (or than I recalled), I manage to get Apache's folders.

Apache's folders

Checking the server's folder, /usr/local/etc/apache24, I notice its file structure is very different from what I expected, so I'll probably have to look further in the files in order to find where that HTML file lives. I'll start by the main one, httpd.conf, and right there I find it.

httpd.conf contents

That's the DocumentRoot, the directive you're looking for when you want to find to figure out where the server's files are located. Since there are no virtual hosts, apparently, there's no room for error, I'll check that one directory.

There's the index

Bingo! Now I'll write a quick script to test PHP. It will be useful once we start messing with it.

A quick PHP script

Oddly enough, when I try it without having installed PHP, it shows me the PHP code instead of directly offering it as a download. No one wants either of those scenarios, not even me, so let's tackle that. First, I'll search in the repository:

Searching just "php"

The result of running "pkg search php" scrolled on my face for a few seconds, so I should try to reduce the results a little bit. "apache php" and vice-versa don't return any results at all, and it doesn't even understand it if I don't quote it. Oh well, I guess "grep" will come to my rescue once again.

Searching "php" and filtering by "Apache"

I don't really like the results I'm seeing, but at least I know PHP 7 is available. Searching "php7" or even "php7." should be helpful, right? Well, just barely, and I still can't see the main package, which is what I probably need, so I'll just run "pkg install php"... which doesn't work. However, specifying a version (7.3) without dots seems to do the trick.

Installing PHP 7.3

The installation worked. Now, will it directly link to Apache? Well, nope, it still displays the same result when I try running the script I had made before. It doesn't surprise me at all, even though now I have to figure out how to make it work. No hints from the manual, nor the configuration files, nor the packages info, so I'll have to search online. Not wanting to "cheat", as there are several tutorials on how to make a FAMP (which is the same I'm trying to achieve), I find FreeBSD's manual page on the server, and after finding "mod_php" while explaining the modules, I have an idea: searching that in the repositories.

pkg search mod_php

And there it is, I think. I haven't figured out a way of looking at a package's info without installing it, so running "pkg install mod_php73-7.3.12" is going to be a shot in the dark. It doesn't need anything else before installing itself, and sure enough, look at this message:

mod_php's message

After adding those lines to Apache's configuration file, right after the root directory's directives, I restart Apache's service with "service apache24 restart". It seems to complain about the same about not having a domain, and...

PHP is working under Apache

Tada! PHP scripts are now working under Apache. MySQL is the only thing that remains to prepare. I'm not sure about installing it, but linking it with PHP should be pretty easy. Anyway, I'll do another search in the repository:

The result of running "pkg search mysql"

Okay, we're good for the moment. I'll just pick "mysql57-server-5.7.27" and "php73-pdo_mysql-7.3.12". The last one isn't PHP's traditional MySQL API, but a newer one called PDO, aimed at trying to provide an almost universal interface when dealing with SQL databases. Regardless, this is a good time to test whether I can select several packages to install.

Selecting several packages

Looks like I can, so I'll perform the installation.

MySQL has been installed

Interesting, an automatically generated password. Normally, when working on Ubuntu, MySQL had a root user without a password who could just access it locally. That will be interesting to implement in the MySQL script. Right now, I have to start the service manually, so upon listing the available services, I run "service mysql-server start". Then I have to change something up in the rc.conf, and then I run the command again. And the secret password file appears. I won't be showing it, but I can tell copying and pasting the password works even in the terminal.

MySQL wants me to change my password

Yeah, it makes sense. Let's see if I remember how the "ALTER USER" statement worked.

Altering root's password

I got it right first try. I quickly exit and try to enter again to check it changed correctly, and sure enough, it worked perfectly. In order to make things a little bit easier for me, I create another user for remote access with all privileges. Before trying it out in a graphical program, I test it in a local terminal.

Can't connect, the port isn't even open

I guess that would have been way too easy. No problem, I think I have to touch some configuration in order for the MySQL to be exposed externally. I'll try checking the main configuration file.

MySQL's main config file

It must be that directive. If I change the IP address to the one assigned by my DHCP, I should be able to connect, right? Granted, I'd have to restart the service in the first place.

Checking the VM's ports and connecting to MySQL

That's a lot better, now I can open my GUI client and insert some data. I'll just make a quick table so that the script isn't all that difficult.

Some test data inserted into a table

And here goes the script, which doesn't use HTML5 nor AJAX since this is just a test:

Another PHP script, this time testing MySQL

And here's the result. It seems to work perfectly.

How it's seen on the browser

Well, this is getting long enough, so I'll split this further into another part, where I'll finally try to install a desktop and make some conclusions about how easy FreeBSD is to deal with.

Top comments (0)