Vincent's Weblog

Installing Apache, PHP and MySQL on FreeBSD

In a previous post I went over installing FreeBSD, in this post I will install a "FAMP" stack (FreeBSD, Apache, MySQL and PHP).

Installing Apache

First we will install Apache:

pkg install apache24
sysrc apache24_enable="YES"
service apache24 start
service apache24 status

When we browse to our webpage, we will see a large "It works!".

Installing PHP

Now Apache is installed, we can install PHP. As of writing, the latest php version is 8.2 (packages are php82), so I will be installing this

pkg install php82 php82-mysqli mod_php82
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

next, create a new file /usr/local/etc/apache24/modules.d/001_mod-php.conf and add the following content:

<IfModule dir_module>
    DirectoryIndex index.php index.html
    <FilesMatch "\.php$">
        SetHandler application/x-httpd-php
    <FilesMatch "\.phps$">
        SetHandler application/x-httpd-php-source

now we can restart apache

apachectl restart

Now PHP is installed and added to apache, we can test it by removing the default index document, and replacing it with a php file printing the php information.

Remove /usr/local/www/apache24/data/index.html and create a new file /usr/local/www/apache24/data/index.php with the following content:

<?php phpinfo(); ?>

If you visit the server's IP address again, you should see information about the PHP version that is installed.

Installing MySQL

First we need to install the mysql server:

pkg install mysql80-server
sysrc mysql_enable="YES"
service mysql-server start

Next, we have to run mysql_secure_installation to do the initial configuration of mysql.

  • Set a password (a secure one!)
  • Remove anonymous users: yes
  • Disallow root login remotely: yes
  • Remove test database and access to it: yes
  • reload privilege tablees: yes

Now we have all the basics installed

HTTPS and virtualhosts

Because, even when testing, it is a good idea to set up HTTPS, we will add this first. Open /usr/local/etc/apache24/httpd.conf and uncomment the line that loads

LoadModule ssl_module libexec/apache24/

Next, uncomment the line that includes the httpd-ssl.conf file

Include etc/apache24/extra/httpd-ssl.conf

Next, we need to add a self signed certificate

openssl req -x509 -nodes -newkey rsa:2048 -keyout /usr/local/etc/apache24/server.key -out /usr/local/etc/apache24/server.crt -days 365

tip: If you are deploying a production website, you should use Let's Encrypt.

Testing our setup by installing Wordpress

cd /usr/local/www/apache24/data
rm index.html
tar xzf latest.tar.gz
mv wordpress/* .
chown -R www:www *

Next, create a mysql database (be sure to change the password to something else):

mysql -u root -p
create database wordpress;
create user 'wordpress'@'localhost' identified by 'password';
grant all privileges on wordpress.* to 'wordpress'@'localhost';
flush privileges;

Next visit the website and follow the steps of the installer to set up wordpress!