Moving away from Ubuntu: peeking at FreeBSD (Part 2)
Tags: unix, experimenting, freebsd, shell
Published: 2019-12-27 15:11:32

[Previously,](https://dev.to/jmmedina00/moving-away-from-ubuntu-peeking-at-freebsd-part-1-2jne) 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](https://thepracticaldev.s3.amazonaws.com/i/761dfse4fc8dvkdg6rfg.png)

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](https://thepracticaldev.s3.amazonaws.com/i/6nrlhz0etvfx7uuri5gs.png)

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](https://thepracticaldev.s3.amazonaws.com/i/z8wjkzzeagm0u2hvsqvl.png)

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](https://thepracticaldev.s3.amazonaws.com/i/w1194esvit5ebeqat8bh.png)

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](https://thepracticaldev.s3.amazonaws.com/i/rf2g3gxxz7thu1hz7r3m.png)

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](https://thepracticaldev.s3.amazonaws.com/i/wa1qxg7tpgkjw9vrf4tm.png)

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](https://thepracticaldev.s3.amazonaws.com/i/0hc9f2hglu3z7yavm8ef.png)

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](https://thepracticaldev.s3.amazonaws.com/i/0p2hdj00gefqbmoarngr.png)

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](https://thepracticaldev.s3.amazonaws.com/i/hxjy7aqukh7sec15ycwc.png)

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](https://thepracticaldev.s3.amazonaws.com/i/a6ssqfsh3x2zv5uoxu66.png)

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](https://www.freebsd.org/doc/handbook/disks-adding.html) 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](http://www.codenicer.com/content/formatting-usb-drive-fat32-using-freebsd), however, shows the "newfs_msdos" command, which is the precise one I need.

![Formatting and trying to mount](https://thepracticaldev.s3.amazonaws.com/i/b04h5ucq9ej9l67d1ebw.png)

Still, I don't get any different results than before. I search online how to mount a FAT32 device, [land here,](https://forums.freebsd.org/threads/mount-usb-stick-with-fat32-file-system.56675/) and it turns out I wasn't specifying the device type correctly.

![It finally worked](https://thepracticaldev.s3.amazonaws.com/i/pdmnlld71umz4lmqfemh.png)

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"](https://thepracticaldev.s3.amazonaws.com/i/nsdx8gyv7hjbrjqyzvyn.png)

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](https://thepracticaldev.s3.amazonaws.com/i/6emxdn8n1niiny9zvk4e.png)

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

![Post-installation message](https://thepracticaldev.s3.amazonaws.com/i/ke0lovw78isv7d1eelga.png)

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](https://thepracticaldev.s3.amazonaws.com/i/tf3bww20x3pcykn6dw22.png)

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!](https://thepracticaldev.s3.amazonaws.com/i/0g0gjvhfofklxfskvmxb.png)

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](https://thepracticaldev.s3.amazonaws.com/i/dsdhl5v0ggxl4a6ul632.png)

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](https://thepracticaldev.s3.amazonaws.com/i/wv2hoi9amyqh9i7f9z4s.png)

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](https://thepracticaldev.s3.amazonaws.com/i/vysf02iivrcjyxurguep.png)

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](https://thepracticaldev.s3.amazonaws.com/i/6y9mraxehcrei88y6but.png)

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"](https://thepracticaldev.s3.amazonaws.com/i/nneze6qawy1zu9gsrzfe.png)

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"](https://thepracticaldev.s3.amazonaws.com/i/bbm8tj8yb3s4s6hszaps.png)

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](https://thepracticaldev.s3.amazonaws.com/i/07eidj8cz07z7kor5qlw.png)

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](https://www.freebsd.org/doc/handbook/network-apache.html), and after finding "mod_php" while explaining the modules, I have an idea: searching *that* in the repositories.

![pkg search mod_php](https://thepracticaldev.s3.amazonaws.com/i/g9j1xae5se9k4w4sz8qv.png)

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](https://thepracticaldev.s3.amazonaws.com/i/nohgq3pv2glfdcoa4uf9.png)

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](https://thepracticaldev.s3.amazonaws.com/i/smmnixytoh0r0dubme2w.png)

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"](https://thepracticaldev.s3.amazonaws.com/i/tv4y0tpckibmvxr93sfa.png)

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](https://thepracticaldev.s3.amazonaws.com/i/b145j7f7hnee2p7oh3ok.png)

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

![MySQL has been installed](https://thepracticaldev.s3.amazonaws.com/i/whmv9vhenvl04au4w089.png)

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](https://thepracticaldev.s3.amazonaws.com/i/44of4ih8qnplg4g2cqju.png)

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

![Altering root's password](https://thepracticaldev.s3.amazonaws.com/i/giouz827uf5fvl4vtmmm.png)

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](https://thepracticaldev.s3.amazonaws.com/i/a58c1b2hdhfatf5fj5g9.png)

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](https://thepracticaldev.s3.amazonaws.com/i/3b4tuvo89v4b0e01172y.png)

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](https://thepracticaldev.s3.amazonaws.com/i/d20cohv91sgo30y0h391.png)

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](https://thepracticaldev.s3.amazonaws.com/i/9394cyowrvj290tfnlo9.png)

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](https://thepracticaldev.s3.amazonaws.com/i/9ljggqy7jmxa8149fbc8.png)

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

![How it's seen on the browser](https://thepracticaldev.s3.amazonaws.com/i/iptw277asqgrwieiiili.png)

----

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.