In this article, we will explain how to build a vod service with nginx + RTMP module.
Mirror download, domain name resolution, time synchronization please click alibaba open source mirror site author: Tang Qingsong
First, operating environment
In order to deploy this service to the production environment in the later stage, we will adopt docker method to build this service. In addition, we usually use Linux system as the server in the production environment. Therefore, we will take Ubuntu system as an example to explain how to build other systems.
1. The docker container
The default port for the RTMP service is 1935. In addition, after installing nginx, you need to open an HTTP port. In order to prevent conflicts with the host, port 8081 is used. We need to copy the video file to the container, so we also need to mount a directory, docker container run command as follows:
Docker run - name video - v/Users/tangqingsong/files: / root/videos - d - I - p, 8081:8081-1935: p 1935 ubuntu: 18.04 && docker psCopy the code
After the command is executed, docker returns the following result:
As you can see in the image above, we already have a container running. Then we need to go into the container and install the nginx and RTMP modules.
docker exec -it video bash
Copy the code
After the command is executed, the following information is displayed:
In the figure above, you can see that the root account name is displayed to the left of the cursor position, indicating that the container has been successfully entered.
2. Domestic accelerators
We replaced the apt software source with the address of Ali Yunyuan and executed the following command:
echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
' > /etc/apt/sources.list && cat /etc/apt/sources.list
Copy the code
After the command is executed, the following information is returned:
It can be seen from the above figure that the implementation has been successful and the default software source has been replaced by the software source of Ali Cloud.
3. Update the software source list
Next we update the local software source information by executing the command shown below
apt update
Copy the code
After the command is executed, the following information is displayed
It can be seen from the above figure that we have updated the software source information from Ali Cloud and the update speed is very fast. So far, we have completed the basic preparation of the running environment.
Nginx installation
1. Install Nginx dependencies
After we update apt software source, we can install Nginx and other software dependent environment, execute the command as follows:
apt-get install -y libpcre3 libpcre3-dev libssl-dev zlib1g-dev gcc wget unzip vim make curl
Copy the code
After the command is executed, the following information is returned:
Nginx can not be installed with APT, it needs source code compilation to install nginx, because we need to compile a module into it.
2. Download source code
Nginx-rtmp-module, nginx-http-flV-module, nginx-rtmp-module, nginx-http-flV-module, nginx-rtmp-module First download it and unzip it, execute the following command:
wget https://github.com/winshining/nginx-http-flv-module/archive/master.zip ; unzip master.zip
Copy the code
After the command is executed, the following information is returned:
Download the nginx source code and unzip the nginx source code as follows:
Wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar - ZXVF nginx - 1.17.6. Tar. GzCopy the code
After the command is executed, the following information is returned:
Download and decompress complete, to this we need two source code are ready to complete
3. Compile and install
Nginx: nginx: nginx: nginx: nginx
CD nginx - 1.17.6 && lsCopy the code
After the command is executed, the following information is returned:
/configure: nginx-http-flv-module: nginx-http-flv-module: nginx-http-flv-module
./configure --add-module=.. /nginx-http-flv-module-masterCopy the code
After the command is executed, the following information is returned:
As you can see in the figure above, preparation is complete, but there may be some unexpected factors during the compilation process. Nginx is very strict by default, and the compilation will be interrupted if there are some unexpected things. Therefore, we set some non-fatal accidents to warning mode, and execute the following command:
vim objs/Makefile
Copy the code
There is an objs/Makefile file in the current folder, we delete -werror in it, open the file with vim, execute the command as follows, and return the information as shown in the picture below
Once deleted, save and exit, Nginx is ready to compile, which takes a bit longer, using the following command
make
Copy the code
After the command is executed, the following information is displayed
In the figure above, you can see some Nginx log path information. When we execute the installation command, we will write the corresponding information to these files
make install
Copy the code
After the installation command is executed, the following information is displayed
In the figure above, you can see which commands are roughly executed during the installation process. After the installation is complete, some simple configuration is required to use them.
3. Configure the RTMP service
1. Add the RTMP service
We directly use the vim command to edit the Nginx configuration file, as shown below
vim /usr/local/nginx/conf/nginx.conf
Copy the code
After the vim command is executed, the following edit window opens
Let’s copy and paste the following configuration information into the configuration file information above the HTTP configuration
RTMP {#RTMP server {listen 1935; #// Service port chunk_size 4096; Application vod {play /opt/video/vod; #// Video file location. }}}Copy the code
2. Verify the configuration
After pasting and saving, run the nginx -t command on the terminal to test whether the configuration file is abnormal, as shown below
/usr/local/nginx/sbin/nginx -t
Copy the code
After the command is executed, the following information is displayed
$Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx = $Nginx
/ usr/local/nginx/sbin/nginx && curl http://127.0.0.1Copy the code
After the command is executed, the following information is displayed
From the information returned by Nginx, we can see that the Nginx service has been started successfully.
Iv. Video playback
1. Add a video file
Then we create a folder to store the video, and set the permission setting to 777, in case the video cannot be played due to the permission problem, execute the command as follows
mkdir -p /opt/video/vod && chmod -R 777 /opt/video/vod
Copy the code
After the command is executed, the following information is displayed
As can be seen from the above figure, the commands to create folders and set permissions have been executed. Then we need to copy our prepared video files to the directory specified in our previous configuration, and execute the commands as follows
cp /root/videos/out.mp4 /opt/video/vod && ls /opt/video/vod
Copy the code
After the command is executed, the following information is displayed
As you can see in the image above, the video file out.mp4 has been copied to this directory, and we can test the playback; We’ll need to install a video player before we can test it, because the browser doesn’t support RTMP.
2. Install the VLC player
Generally used for debugging streaming media, we are used to using VLC player, let’s go to the official website to download it, the official website address is as follows
https://www.videolan.org/
Copy the code
After using the browser to open the official website, we can see the download button on the home page. It can also be seen that various platforms are supported, and the current system version is downloaded by default, as shown in the figure below
After downloading and installing the VLC player, we need to go to File->Open Network in the menu, as shown below
After clicking the Open Network option, a new window will Open. In this window, we can enter our playing address. The video playing address I copied in the past is shown below
rtmp://localhost/vod/out.mp4
Copy the code
In the figure above, you can see that there is an input box in the window. After copying the playing address into it, click the Open button in the lower right to start playing. The successful playing effect is shown in the figure below
At this point, the vod service has been set up through the Nginx+ RTMP module.
This article is adapted from: Using Nginx+ RTMP module to build streaming media video on demand service in Ubuntu – Ali Cloud developer community