How to Set up An Apache Website in FreeBSD
In this tutorial, I will show you how to create a website on a FreeBSD server. We will be setting up a BAMP stack (BSD, Apache, MySQL, PHP), which is basically a LAMP stack but for FreeBSD. This tutorial will not tell you how to actually code your website. You will have to do that yourself with HTML/CSS/JavaScript/PHP or install a premade website editor like WordPress. If you haven’t done so already, please go through our tutorial on basic FreeBSD setup: First Things to Do After Installing FreeBSD.
Also see: How to Make Your Own Website For Free (Except Domain Names)
Step 1: Installing Apache in FreeBSD
In this tutorial, we will be using Apache 2.4. If you’re reading from a later date, please adjust to the latest release of Apache in FreeBSD so that you can get security updates.
Without further ado, enter the following command to install Apache:
sudo pkg install apache24
Now that you’ve installed apache in FreeBSD, you will still need to enable it with rc and start apache with the following commands:
sudo sysrc apache24_enable="YES"
sudo service apache24 start
Step 2: Installing MySQL
The same rules apply here for apache as did MySQL. If you’re reading this article from a later date, and the current version of MySQL is later than MySQL 8.0, then install the current package.
sudo pkg install mysql80-server
Now, you have to start mysql and enable it to start on reboot:
sudo sysrc mysql_enable="YES"
sudo service mysql-server start
Now that you’re done with that, you’re going to want to lock up the mysql server and install everything:
sudo mysql_secure_installation
Go through the wizard. The questions are pretty straightforward, so I’ll let you choose what’s right for your server.
If you don’t know how to write SQL statements, consider learning from here: https://www.tutorialspoint.com/mysql/index.htm.
Step 3: Installing PHP to Apache in FreeBSD
Now you need to install PHP. As per usual, if you’re reading the article from a later date, you’ll want to install the latest version of PHP. The current version is PHP 8.0, so we’ll be using that. I will also walk you through installing the needed modules. Enter the following command to install PHP and its modules:
sudo pkg install php83 mod_php83 php83-bcmath php83-curl php83-dom php83-exif php83-fileinfo php83-filter php83-pecl-imagick php83-mysqli php83-sodium php83-xml php83-zip
Note: The optional modules above are for a standard WordPress installation; they may change depending on what you want to do with your website.
Now you’ll want to secure and install your php installation.
sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Now you need to allow apache to know that .php files are a thing and they need to be handled by the PHP module. Open up “/usr/local/etc/apache24/Includes/php.conf” in vim or nano and enter in the following code:
<IfModule dir_module>
DirectoryIndex index.php index.html
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>
Now you’re going to want to restart apache in FreeBSD:
sudo apachectl restart
Now let’s test your PHP installation. Open up “/usr/local/www/apache24/data/info.php” in your preferred editor. Enter in the following code:
<?php phpinfo(); ?>
Now go into your web browser. We’ll use localhost, but you just need to replace the name of the host with whatever the IP address of your server is. Enter in the following URL:
http://localhost/info.php
You should see a table listing all sorts of variables and installations having to do with PHP. If you see that, you’re successful in your installation.
NOTE: This Section Edited 05/11/2022 since latest PHP version is now 8.0 and modules have changed
Conclusion
Well, I will be doing more tutorials on FreeBSD later, so stay tuned! I will post more in this conclusion section as it comes out. That was how to install Apache in FreeBSD.