Foundation

My VOD Platform, a development journey

Your project’s foundation

From the original idea, how do you get started ? You might want to do some research first, mainly to see if anyone else had the same idea. Or maybe to find some technical solutions that you did not know about. In my case, the research had been done before hand, and this it what gave me the idea.

Once your mind is ready to start working, once you have enough visibility on which way to go, you need to start with the foundation: what architecture, database, language, tools…

 

The idea

My idea is to build a platform where users can handle their own video library, live stream and photo gallery. I know this is a project I am able to build, I haven’t find a solution that allows me to do all of this at once, and I also know that I will be using it to share my photos and videos with my family. These are good reasons for me to get started: I have a need, an audience and a capable team.

I know that I want to use Angular for the front end since I master it, PHP because it is easy enough for a project like this, Laravel seems a good option to build an application faster and I know it well. On top of that, I will need Nginx, RTMP and a database, probably MySQL.

Let’s get started!

I would usually use a pre-built solution like MAMP to focus only on the code, and deal with the server side when the project is done, but that is a bit of an outdated thinking. Nowadays, you would use Docker to set everything up and run everything with one command. The obvious advantage of that is that I only need to pull my code once to be able to run the application. Oh, it might not be optimised for performance yet, but it will eventually come.

Docker-compose file

Defining and running multi-container Docker applications

Docker compose file

Docker Compose works with docker-compose.yml files. Create the file at the root of your project.
This file will be used to define the different containers we want to use (aka services), which image to use, what ports to open…

Here is a minimal version of a docker-compose file that includes php :

To set everything up, run docker-compose up in your project’s root folder. This will build everything required by the php :7-fpm machine, then run it. The building part will be done only once, then will be cached for the next time.

Expect a few minutes to install the image.

Adding Nginx

What we have defined here is a list of services, when the image is the name of the docker image to use to build the container. If we want to add Nginx to this services, we would create another block using image : nginx :

Directly under services, write your container ID. You can name it as required, not necessarily php or nginx. You can override this name by using the directive « container_name ». It can come handy when you need to execute any command in your container since you need to type his name.

That would be enough to have nginx and php side by side, but far from what we want to achieve. We do not want to use a default nginx machine, but we want a specific github repository. And we need nginx and php to be able to communicate together to be able to display php pages.

Adding the NGINX RTMP module

The directive build allow you to use a specific github URL to build an image.
Edit your docker compose file to replace the image with the build (git) url :

 

Stop Docker if it is running by running docker-composer down and re-run docker-compose up
The application is now pulling the GIT repository and running the instructions that are in the docker file (building nginx with rtmp, hls, ffmpeg…).

When this has fully ran, congratulations: you now have PHP7.0 and NGINX RTMP running side by side !

Opening the ports:

Yes… But if you try to reach your localhost, nothing happens because the port aren’t open. You can use the ports directive to map your local port to your container’s port:

1935 is the default port for RTMP, 80 is the default for HTTP.

Try to reach http://127.0.0.1 now – it works !
If you know how to send a live stream, you can follow the instructions here https://github.com/brocaar/nginx-rtmp-dockerfile to test this out: it should work as well!

Nginx Configuration

What if I want to I override the nginx config so it does not use the default file included in the github url? Well, you will use the volumes directive.

Create a nginx.conf file in your project folder. I inserted mine in conf/nginx.conf, copied the content from the github URL, and edited the ports to my convenience. Once done, edit the docker-compose file as follow:

After building the image, after running the container, your local copy of the nginx.conf is placed in /config/nginx.conf
You can use volumes with folders, which we will require later.

The /config/nginx.conf url is not a standard path but this is how the NGINX from the github is being compiled so we will use this.

Re-run docker-compose up and browse to 127.0.0.1:80. You should see now the default nginx message.

Displaying an php index page

Create a folder in your project’s root called my_app and create an index.php file in it. You can insert this content:

Which will be enough to prove that PHP is working.
The first thing we need to do is ensure that nginx has the ability to understand PHP files. Classic PHP-FPM configuration.
Empty the content of your nginx.conf file and paste the following:

That is a very basic server configuration, matching any PHP file to php fpm. I will not detail how to set this up here, you will find plenty of tutorials online for this.

What is interesting though:

  • Nginx runs in its own container where php-fpm is not set up
  • /my_app does not exist on the NGINX container or on the PHP container

We need to set up docker to allow all this. We will use the volumes directive and the links directive:

Re-run docker-compose up and browse to localhost: voilà! You have displayed your own index page using PHP and NGINX with docker. Congrats!

As I mentioned in the previous post, I have found a nginx rtmp docker image that I can use to run what I need for Live Stream. But I also need PHP7 to run on the side. Let’s use docker-compose to do this.