In this article we will demonstrate how to setup a Freebsd 12 kvm server from Servercheap for web hosting by installing apache, mysql and php.
Step 1 — Installing Apache
The Apache web server is currently the most popular web server in the world, which makes it a great choice for hosting a website.
We can install it on FreeBSD using the pkg command:
$ sudo pkg install apache24
Enter y
at the confirmation prompt to install Apache and its dependencies.
To enable Apache as a service, add apache24_enable="YES"
to the /etc/rc.conf
file and start apache. You’ll use the sysrc
command to do just that:
$ sudo sysrc apache24_enable="YES"
$ sudo service apache24 start
To check that Apache has started you can run the following command:
$ sudo service apache24 status
apache24 is running as pid 20815.
Let’s do another check to verify that everything went as planned by visiting your server’s public IP address in your web browser.
http://your_server_IP_address/
You will see the default FreeBSD Apache web page, which is there for testing purposes. You’ll see: It Works!, which indicates that your web server is correctly installed.
Step 2 — Installing MySQL
To install MySQL 8.0 using pkg
, use this command:
$ sudo pkg install mysql80-server
Enter y
at the confirmation prompt to install the MySQL server and client packages.
To enable and start MySQL server as a service, add mysql_enable="YES"
to the /etc/rc.conf
file. You can us the sysrc
command to do just that:
$ sudo sysrc mysql_enable="YES"
$ sudo service mysql-server start
You can verify the service is up and running:
$ sudo service mysql-server status
mysql is running as pid 21587.
Now that your MySQL database is running, you will want to run a simple security script that will remove some dangerous defaults and slightly restrict access to your database system. Start the interactive script by running this command:
$ sudo mysql_secure_installation
The prompt will ask you if you want to set a password. Since you just installed MySQL, you most likely won’t have one, so type Y
and follow the instructions:
Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.
New password: password
Re-enter new password: password
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
Step 3 — Installing PHP
To install PHP 7.3 with pkg
, run this command:
sudo pkg install php73 php73-mysqli mod_php73
Enter y
at the confirmation prompt. This installs the php73
, mod_php73
, and php73-mysqli
packages.
Now copy the sample PHP configuration file into place with this command:
sudo cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
Now run the rehash
command to regenerate the system’s cached information about your installed executable files:
rehash
Before using PHP, you must configure it to work with Apache.
Installing PHP Modules (Optional)
To enhance the functionality of PHP, you can optionally install some additional modules.
To see the available options for PHP 7.3 modules and libraries, you can type this:
pkg search php73
The results will be mostly PHP 7.3 modules that you can install:
Outputphp73-7.3.5 PHP Scripting Language
php73-aphpbreakdown-2.2.2 Code-Analyzer for PHP for Compatibility Check-UP
php73-aphpunit-1.8 Testing framework for unit tests
php73-bcmath-7.3.5 The bcmath shared extension for php
php73-brotli-0.6.2 Brotli extension for PHP
php73-bsdconv-11.5.0 PHP wrapper for bsdconv
php73-bz2-7.3.5 The bz2 shared extension for php
php73-calendar-7.3.5 The calendar shared extension for php
php73-composer-1.8.4 Dependency Manager for PHP
php73-ctype-7.3.5 The ctype shared extension for php
php73-curl-7.3.5 The curl shared extension for php
php73-dba-7.3.5 The dba shared extension for php
php73-deployer-6.4.3 Deployment tool for PHP
php73-dom-7.3.5 The dom shared extension for php
...
To get more information about what each module does, you can either search the internet or you can look at the long description of the package by typing:
pkg search -f package_name
There will be a lot of output, with one field called Comment which will have an explanation of the functionality that the module provides.
For example, to find out what the php73-calendar
package does, you could type this:
pkg search -f php73-calendar
Along with a large amount of other information, you’ll find something that looks like this:
Outputphp73-calendar-7.3.5
Name : php73-calendar
Version : 7.3.5
...
Comment : The calendar shared extension for php
...
If, after researching, you decide that you would like to install a package, you can do so by using the pkg install
command.
For example, if you decide that php73-calendar
is something that you need, you could type:
sudo pkg install php73-calendar
If you want to install more than one module at a time, you can do that by listing each one, separated by a space, following the pkg install
command, like this:
sudo pkg install package1 package2 ...
Step 4 — Configuring Apache to Use PHP Module
Apache HTTP has a dedicated directory to write configuration files into it for specific modules. You will write one of those configuration files for Apache HTTP to “speak” PHP.
sudo vi /usr/local/etc/apache24/modules.d/001_mod-php.conf
Add the following lines to that file:/usr/local/etc/apache24/modules.d/001_mod-php.conf
<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 check Apache’s HTTP configuration is in good condition:
sudo apachectl configtest
You’ll see the following output:
OutputPerforming sanity check on apache24 configuration:
Syntax OK
Because you’ve made configuration changes in Apache you have to restart the service for those to be applied. Otherwise Apache will still work with the prior configuration.
sudo apachectl restart
Now you have a FAMP stack installed on FreeBSD 12.
Enjoy!