Skip to content

Commit

Permalink
πŸ”„ Update "Install a FEMP Stack on FreeBSD" (#925)
Browse files Browse the repository at this point in the history
* Update tutorial
* Rename path
  • Loading branch information
svenja11 committed Aug 28, 2024
1 parent 6276806 commit ea2e929
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 95 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
SPDX-License-Identifier: MIT
path: "/tutorials/install-a-femp-stack-on-freebsd-12"
slug: "install-a-femp-stack-on-freebsd-12"
date: "2019-05-28"
title: "Install a FEMP Stack on FreeBSD 12"
short_description: "This tutorial will show how to install a FEMP stack (FreeBSD, Nginx, MySQL, PHP) on FreeBSD 12"
path: "/tutorials/install-a-femp-stack-on-freebsd"
slug: "install-a-femp-stack-on-freebsd"
date: "2024-08-28"
title: "Install a FEMP Stack on FreeBSD"
short_description: "This tutorial will show how to install a FEMP stack (FreeBSD, Nginx, MySQL, PHP) on FreeBSD 14"
tags: ["FreeBSD", "FEMP", "Nginx", "PHP", "MySQL"]
author: "youiopmop"
author_link: ""
Expand All @@ -20,13 +20,12 @@ cta: "dedicated"

A FEMP Stack (FreeBSD, Nginx, MySQL, PHP) is a combination of FOSS software that can be used together in order to host dynamic websites and Content Management Systems such as Wordpress or Joomla.

This guide will help with the installation and configuration of a FEMP stack on FreeBSD 12.

* It is assumed that you are running as the `root` user during this guide. Use `su` to change to `root` if you are not running as `root` already.
This guide will help with the installation and configuration of a FEMP stack on FreeBSD 14.

**Prerequisites**

* A FreeBSD 12 server with root access.
* A server with FreeBSD
* Access to the root user or a user with sudo permissions

## Step 1 - Updating FreeBSD

Expand All @@ -35,7 +34,7 @@ This guide will help with the installation and configuration of a FEMP stack on
It is best to update the system beforehand to ensure you are getting the latest packages when installing the FEMP stack. You can do this by running the following command:

```bash
pkg update ; pkg upgrade
sudo pkg update ; sudo pkg upgrade
```

This will update the repositories of `pkg`, and then upgrade any installed packages that you may have.
Expand All @@ -45,22 +44,24 @@ This will update the repositories of `pkg`, and then upgrade any installed packa
To install Nginx, the web server we are using for this tutorial, use the following command:

```bash
pkg install nginx
sudo pkg install nginx
nginx -v
```

Before we can start nginx to test that it is working, we need to enable it to start on boot.

You can do this by appending `nginx_enable="YES"` to the end of `/etc/rc.conf`.

```
echo 'nginx_enable="YES"' >> /etc/rc.conf
service nginx start
```bash
echo 'nginx_enable="YES"' | sudo tee -a /etc/rc.conf
sudo service nginx start
sudo service nginx status
```

Test that it is working by going to your IP address in a web browser. You can check your server's IP address by running `fetch -qo- http://ifconfig.co`, or by checking your control panel.
Test that it is working by going to your IP address in a web browser. You can check your server's IP address by running `curl -4 https://ip.hetzner.com`, or by checking your control panel.

```
http://10.0.0.1/
```http
http://203.0.113.1/
```

You should see something like this:
Expand All @@ -71,21 +72,31 @@ You should see something like this:

Next, we need to install and configure PHP, which we'll be using to process PHP requests from nginx.

```bash
pkg install php74 php74-mysqli
```
* Check which versions are available:

You can use either `awk` or `grep`.
```bash
sudo pkg search ^php[0-9] | awk -F'-' 'NF==2 {print $0}'
sudo pkg search ^php[0-9] | grep -E '^php[0-9]+-[0-9]+\.[0-9]+\.[0-9]+'
```

Like before, we need to enable it to start on boot before we can start the service.
* Install the latest version and some modules:
```bash
sudo pkg install php83 php83-mysqli php83-curl php83-gd php83-intl php83-mbstring php83-xml php83-zip
php -v
```

```bash
echo 'php_fpm_enable="YES"' >> /etc/rc.conf
```
* Like before, we need to enable it to start on boot before we can start the service.
```bash
echo 'php_fpm_enable="YES"' | sudo tee -a /etc/rc.conf
```

However, before we can start it, we need to make a few changes to the configuration file `/usr/local/etc/php-fpm.d/www.conf`
Before we can start it, we need to make a few changes to the configuration file `/usr/local/etc/php-fpm.d/www.conf`

```bash
cd /usr/local/etc/
vi php-fpm/www.conf
#vi php-fpm/www.conf
sudo vi php-fpm.d/www.conf
```

Look for the line:
Expand All @@ -100,15 +111,15 @@ Replace it with the following, so that PHP listens under an unix socket:
listen = /var/run/php-fpm.sock
```

Later in the file, look for these lines:
Further down in the file, look for these lines:

```ini
;listen.owner = www
;listen.group = www
;listen.mode = 0660
```

Uncomment them so it looks like this, to change the unix socket owner to be the same as the nginx user:
Uncomment them to change the unix socket owner to be the same as the nginx user:

```ini
listen.owner = www
Expand All @@ -118,19 +129,20 @@ listen.mode = 0660

After you have completed that, save and close the file.

A `php.ini` file needs to be created. The example file `php.ini-production` can be used with a slight modification for security:
You need to create a `php.ini` file. You can use the example file `php.ini-production` with a slight modification for security:

* Assuming you are still in `/usr/local/etc/`
Assuming you are still in `/usr/local/etc/`:

```bash
cp php.ini-production php.ini
sed -i -E 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' php.ini
sudo cp php.ini-production php.ini
sudo sed -i -E 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' php.ini
```

Then finally, start the PHP service.

```bash
service php-fpm start
sudo service php-fpm start
sudo service php-fpm status
```

### Step 3.1 - Configuring Nginx to use PHP
Expand All @@ -140,24 +152,37 @@ Nginx and PHP are both enabled now, however we need to configure Nginx to be awa
Open the file `/usr/local/etc/nginx/nginx.conf` in a text editor:

```bash
vi /usr/local/etc/nginx/nginx.conf
sudo vi /usr/local/etc/nginx/nginx.conf
```

Find the following in the file (commented out parts are removed):

```nginx
server {
listen 80;
server_name localhost;
worker_processes 1;
location / {
root /usr/local/www/nginx;
index index.html index.htm;
}
events {
worker_connections 1024;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/www/nginx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
}
}
```
Expand All @@ -173,47 +198,46 @@ The following changes need to be made to the `nginx.conf` file:
Your `nginx.conf` file should look something like this:
```nginx
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $uri $document_root$fastcgi_script_name =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $uri $document_root$fastcgi_script_name =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
* If using IPv6 on your server, add the following line under `listen 80;`

```nginx
listen [::]:80;
```
> If your server uses IPv6, add the following line under `listen 80;`
> ```nginx
> listen [::]:80;
> ```
Then finally, create the nginx root directory, symlink the default nginx `index.html` page, and restart nginx.
```bash
mkdir -p /var/www/html/
ln -s /usr/local/www/nginx-dist/index.html /var/www/html/
service nginx restart
sudo mkdir -p /var/www/html/
sudo ln -s /usr/local/www/nginx-dist/index.html /var/www/html/
sudo nginx -t
sudo service nginx restart
sudo service nginx status
```
*If nginx fails to restart, run `nginx -t` to check any configuration errors in your config file and resolve them, then try restarting nginx again.*
Going to your server's domain or IP address, should still return the `Welcome to nginx!` page from before.

Going to your server's domain or IP address should still return the `Welcome to nginx!` page from before.

To test that PHP is working, create a file under `/var/www/html/phpinfo.php` with the following content:
To test if PHP is working, create a file under `/var/www/html/phpinfo.php` with the following content:

```php
<?php phpinfo(); ?>
Expand All @@ -227,56 +251,74 @@ http://example.com/phpinfo.php
It should return a page like this:
![PHP Info](images/02-phpinfo.png)
![PHP Info](images/02-phpinfo.8.3.8.png)
It is best to delete the file afterwards for security, as it can expose information about your PHP install to anyone.
```bash
rm /var/www/html/phpinfo.php
sudo rm /var/www/html/phpinfo.php
```
## Step 4 - Installing MySQL (MariaDB)
Now that we have Nginx and PHP setup and working together, the only thing left that we need to install and configure is MySQL.
However, we'll be using a community maintained drop-in replacement to MySQL, MariaDB; which is fully compatible with applications targeting MySQL.

```bash
pkg install mariadb104-server mariadb104-client
```

Like earlier, we need to enable the service to start on boot before we can start it using service.

```bash
echo 'mysql_enable="YES"' >> /etc/rc.conf
service mysql-server start
```
* Check which versions are available:
> We'll be using a community maintained drop-in replacement to MySQL, MariaDB; which is fully compatible with applications targeting MySQL.
```bash
sudo pkg search '^mariadb[0-9]+-server' | grep -E '^mariadb[0-9]+-server-[0-9]+\.[0-9]+\.[0-9]+'
sudo pkg search '^mariadb[0-9]+-client' | grep -E '^mariadb[0-9]+-client-[0-9]+\.[0-9]+\.[0-9]+'
```

* Install the latest version:
```bash
sudo pkg install mariadb114-server mariadb114-client
sudo pkg info -x mariadb114-server
sudo pkg info -x mariadb114-client
```

* Like before, we need to enable the service to start on boot before we can start the service.
```bash
echo 'mysql_enable="YES"' | sudo tee -a /etc/rc.conf
```

* Add a configuration file
```bash
sudo cp /usr/local/etc/mysql/my.cnf.sample /usr/local/etc/mysql/my.cnf
```

* Start the service
```
sudo service mysql-server start
sudo service mysql-server status
```

Next, we need to secure the MariaDB installation.

```bash
mysql_secure_installation
sudo mysql_secure_installation
```

```
```shellsession
Enter current password for root (enter for none):
```

Since we are setting up MariaDB for the first time, the root account does not have a password, so just press the "ENTER" key here.

```
Switch to unix_socket authentication [Y/n]
```shellsession
Enable unix_socket authentication? [Y/n]
```

Like before, press the "ENTER" key to answer yes to use unix_socket authentication.

```
```shellsession
Set root password? [Y/n]
```

Press the "ENTER" key here to answer yes, and then enter a secure password for your MariaDB root user.

```
```shellsession
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ea2e929

Please sign in to comment.