**Project Overview**

As we continue to advance our hosting capabilities, certain projects may emerge that require tailored
solutions. In this case, we are deploying a video sharing platform for a client who requires a robust and
scalable solution for their lecture content. The videos in question can be quite large, ranging from
several GBs in size, necessitating a custom approach.

**Project Requirements**

The project specifications include the creation of a YouTube-like clone, with no user authentication
required to access the content. Additionally, the client has opted to integrate advertising into the
platform. To streamline the process, it is assumed that the videos have already been encoded and
processed, eliminating the need for FFmpeg-based encoding and thumbnail generation.

**System Requirements**

To meet the project’s demands, we will be deploying a high-performance computing environment on Linode.
Specifically, we will be using a Nano instance running Ubuntu 22. This setup provides an efficient
foundation for hosting and serving large video files. Prior to installing any software, we will perform
initial configuration tasks to ensure optimal performance and resource utilization.

** This writeup assumes you have basic linux system skills, such as basic SSH, terminal commands, and your favorite editor of choice function. Example Nano, VI, and such. **


The first things I do once I SSH into the box.

## Change that password
passwd
## Enable that firewall.
## Since where only running a webserver and ssh server to the world, we only need three ports.
## 22, 80, and 443
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable
## Now we update/upgrade.
apt-get update && apt-get upgrade -y
## Reboot just to make sure.
reboot

Great now the vps is at its core updated, and the firewall is installed. Now lets get our Webserver, PHP, and MySQL installed on this vps.

## Install the transport agent
apt install -y debian-keyring debian-archive-keyring apt-transport-https curl -y
## Grab the keyring
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
## Add the source list
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
## Update that source list
apt-get update
## Install Caddy
apt-get install caddy -y

Great Caddy is now installed, not configured or running, but installed.
Now lets install PHP and MySQL

## Install PHP and MySQL
apt-get install php-fpm php-mysql php-curl php-gd php-mbstring php-common php-xml php-xmlrpc php-bcmath php-imagick php-intl php-json php-zip mysql-server mysql-client -y

## Verify PHP Version
## php -v
root@localhost:~# php -v
PHP 8.1.2-1ubuntu2.18 (cli) (built: Jun 14 2024 15:52:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.18, Copyright (c), by Zend Technologies
root@localhost:~# 

## Verify MySQL
root@localhost:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 8.0.39-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
root@localhost:/#   

So now we have the core system setup, now lets add our domain and start uploading!

## Create your directory for your domain
mkdir -p /home/caddy-demo.theserveradmin.com

Now we need to tweak PHP to run as the Caddy user.

nano /etc/php/8.1/fpm/pool.d/www.conf

## Locate and Change the following
user = caddy
group = caddy
listen.owner = caddy
listen.group = caddy

## Save the file and exit
## Restart PHP
systemctl restart php8.1-fpm

Now we setup the domain in the Caddy file and get it ready to host.

## Edit the Caddy File.
nano /etc/caddy/Caddyfile

## Add your domain(s) and setup the PHP

caddy-demo.theserveradmin.com:443 {
        root * /home/caddy-demo.theserveradmin.com
        encode gzip zstd
        file_server browse
        php_fastcgi unix//run/php/php8.1-fpm.sock
}
## Save and Exit
## Restart Caddy
systemctl restart caddy

Now if you already have DNS pointed over to the new IP then in a matter of minutes your site will show up with a new happy SSL installed.

Now lets head over and create a phpinfo page here real quick to make sure its all connected.

cd /home/caddy-demo-theserveradmin.com
nano info.php

## phpinfo
<?php
phpinfo();
?>
## Save the file
## Open it in your web browser

Great! You have a working website! So whats next? What is that PHP Melody your talking about? Well I am glad you asked. Check out the working demo over at https://videos.theserveradmin.com/ and for more information check out the official site over at https://www.phpsugar.com/phpmelody.html

Now we have a few more steps to finish up the install and hand it over to our client to start the uploading and configuring of the site.

First item, how do I get my files onto the server? Pretty easy actually, SFTP, just like FTP but without any extra software.

Launch your client of choice, my personal is FileZilla. Create the new account, but instead of ftp, your going to want sftp, and the user name is root, and the root password.

Now go ahead and navigate to your home directory ( or wherever you setup your www directory )

Now we can start uploading!
So a few assumptions here.
1. The PHPMelody is the software your going to use.
2. You already purchased it, licensed it, and downloaded the zip file.

So now we have to create that MySQL user/password/database so we can install that PHP Melody software.

## Connect to the MySQL Engine.
mysql
## Create the Database
CREATE DATABASE phpmelody DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
## Create the User/Pass
CREATE USER 'phpmelody_user'@'localhost' IDENTIFIED BY 'your_super_password';
## Grant Access
GRANT ALL ON phpmelody.* TO 'phpmelody_user'@'localhost';
## Exit
exit

Next thing we do is edit our config.php to reflect our database settings, and then you start uploading!

Fix the requests, and then your off to the races! Almost, have a unique change we have to make,

chown caddy:caddy -R /home/caddy-demo.theserveradmin.com/*

Now we are all setup with a vps that can run a PHP Melody Video Platform, and it runs on a larger potatoe.

However now we need to make another tweak to our php.ini file so we can upload large video files.

nano /etc/php/8.1/fpm/php.ini
The following are configurations that you will want to play with.
memory_limit
max_execution_time
upload_max_filesize
post_max_size
max_input_time

Now for me, this is what I find works the best.

memory_limit = 512M
max_execution_time = 900
upload_max_filesize = 4096M
post_max_size = 4096M
max_input_time = 900

save and exit.

## Restart PHP
systemctl restart php8.1-fpm

Now that we have our core site setup, we can now add a little bit more. Logging and Stats.

To do this, we need to enable logging in Caddy as by default it does not log, and secondly we need to install some sort of log analizing software, and third automate the report creation.

Great! Lets begin. 1st thing were going to need to do is enable logging, so fire up that SSH and login to your account, now were going to edit that /etc/caddy/Caddyfile and add the log options.

caddy-demo.theserveradmin.com:443 {
        root * /home/caddy-demo.theserveradmin.com
        encode gzip zstd
        file_server browse
        php_fastcgi unix//run/php/php8.1-fpm.sock
log {
        output file /var/log/caddy/caddy-demo.log {
                roll_size 1gb
                roll_keep 5
                roll_keep_for 720h
                }
    }

}

Now save the file, and restart Caddy.

systemctl restart caddy

What is happening here is the following.
1. output file -> This is where the log will be stored.
2. roll_size -> Size of the log file.
3. roll_keep -> The number of logs to keep.
4. roll_keep_for -> How long to keep the files.

Now if you access your site a few times, you will see that the log file was created.

Now lets install a really cool log analizer software, GoAccess. This is pretty straight forward.

$ wget -O - https://deb.goaccess.io/gnugpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/goaccess.gpg >/dev/null
$ echo "deb [signed-by=/usr/share/keyrings/goaccess.gpg arch=$(dpkg --print-architecture)] https://deb.goaccess.io/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/goaccess.list
$ sudo apt-get update
$ sudo apt-get install goaccess

Yep thats pretty much it, you now have GoAccess installed, now lets create our first report.

goaccess /var/log/caddy/caddy-demo.log --log-format=CADDY -a -o /home/caddy-demo.theserveradmin.com/report.html

Now you have to match up your log location, name, and output location, but you get the idea. After your report was created, lets head over to that URL and look at it.

Now for that database, we need to back that up right?
Backing up from the command line is pretty easy now, and we can automate this with a script * at the end we will put this all together.

The first thing we need to do is create a directory to store our database backups.

mkdir -p /home/db_backups

Now since were already the root user, we can run as root to do the db dump.
** If your following along with the above example, but please change the fields to reflect your db credentials.

mysqldump phpmelody > "$(date +%F).phpmelody".sql

Now lets run this and make sure it works.

root@localhost:/# cd /home/db_backups/
root@localhost:/home/db_backups# mysqldump phpmelody > "$(date +%F).phpmelody".sql
root@localhost:/home/db_backups# ls -lah
total 116K
drwxr-xr-x 2 root root 4.0K Sep  8 12:00 .
drwxr-xr-x 4 root root 4.0K Sep  8 11:55 ..
-rw-r--r-- 1 root root 105K Sep  8 12:00 2024-09-08.phpmelody.sql
root@localhost:/home/db_backups# 

Great! So we can now we have a database dump command!

Next is now Backups, yes this is super important, I mean yes you have your videos on your computer, but hey things happen. So we need to set this up. I have a nice write up over here, so I can not going to dive super deep into this again, but. https://theserveradmin.com/s3-buckets-mounting-as-user/

So quick and dirty because I already did this in the above post.

mkdir -p /backups
## Install S3FS
apt install s3fs
## Setup your keys
nano /etc/passwd-s3fs
## Set permissions.
chmod 600 /etc/passwd-s3fs
## Edit the fuse file.
nano /etc/fuse.conf
## Automount on reboots
nano /etc/fstab
## Mount the drive.
mount -a

So lets do a quick recap.
We now have a VPS/Dedicated Server setup with Caddy Webserver, PHP 8.1, MySQL 8, PHP Melody, Backups, and Stats, for $5/Month.

So how do we automate the backups and such, as I don’t want to manually run this everytime I remember to do a backup thats cool and all, but we don’t want to have to manually run this report, so a cronjob it is!

So we need to do a few things.
1. Create the database dump.
2. Create the stats report.
3. Rsync the days data offsite.
4. Fully Automated.

So lets create a super simple script to do this, test it to make sure, then we will setup a cron to run every X hours to do this.

nano /root/backups.sh
#!/bin/bash

##############################################################

## Lets Create your stats report.
goaccess /var/log/caddy/caddy-demo.log --log-format=CADDY -a -o /home/caddy-demo.theserveradmin.com/report.html

## Change into your database backup directory, then dump the database backup.
cd /home/db_backups && mysqldump phpmelody > "$(date +%F).phpmelody".sql

## Now to rsync your /home folders to the remote offsite backup storage.
rsync -arvz /home /backups/

## Remove old database dumps, they are already transfered offsite. 
cd /home/db_backups && rm -Rf *.sql

Now save the file and make it executable.

chmod +x /root/backups.sh

Now lets run this script to make sure.

cd /root
sh backups.sh

If all is good, you will see things start to move, your stats will generate, your database will be dumped, and your files will start to sync over to iDrive.com

Now lets automate this, as we still don’t want to run this manually.

nano /etc/crontab

## Add to the end of the file, this will run the script every 12 hours.
0 */12 * * *  root /root/backups.sh 2>&1

## Save the file and exit.

Now restart your cron service.

systemctl restart cron

And that is it! Your off to the races now with a fully configured server, stats, backups, uploads, streaming, the works.

So after all that, with everything we did, the end result is the following.

So now that we are setup, with the OS installed, updates, tweaks, and software install, plus the site demo.

Yes your now limited to the 19GB’s of space, and the 1TB of bandwidth, but again for the demo, this is personally pretty darn cool. Now one last thing I do, just to 100% all works and starts as it should.

reboot

Good luck with your video travels!!

OK so I could not help myself, what can this little VPS do? So again, remember we set this up on a $5 VPS designed for a small group of people *Students for a teachers lectures in this example. So lets say that there are 25 students and they all jump on at 2PM after their professor uploaded todays lecture.

ab -n 1500 -c 25 https://caddy-demo.theserveradmin.com/

So that is 25 students at the same time, beating 1500 requests on the server, which we know won’t happen but lets test this.

So this little box just welcomed a crap ton of people at the same time, and was able to push out 84/Reqs/per/second at 25 concurrent connections.

With this, if you have the need for a video site, no matter for teaching, for a band, for commercials, for the mature audience, then PHP Melody will be your platform of choice, and you need it, can be hosted and start you off for $5/Month

By Anthony