Installing nginx with nginx-rtmp-module on Ubuntu from a Custom .deb

Here at WOTS, we stream to our YouTube channel via an nginx relay. Out of the box, nginx doesn’t support RTMP, though. There’s a module you can download – but it’s source only. Since nobody is currently making a PPA for nginx with RTMP support built in, I build mine from source. It’s not as hard as it sounds – anybody can figure it out! In this article, I’ll archive my scripts for building nginx with the RTMP module the Ubuntu way.

Normally, to install software on Ubuntu, we’d use something like apt-get, aptitude, or synaptic – a package manager that installs binary software someone else built for us. In order to add a module to nginx, we’ve got to compile it against the nginx code for the release of nginx we want to use.

Step one is to grab the latest version of nginx source from the Ubuntu repositories. This ensures that the nginx we build will be compatible with the other Ubuntu packages we have installed already.

# Always a good idea before you start doing apt commands:
sudo apt-get update && sudo apt-get upgrade
# Grab any build dependencies, this will install all required packages automatically:
sudo apt-get build-dep nginx
# We'll need git to grab the RTMP module later, and fakeroot for the .deb build
sudo apt-get install git fakeroot
# Make a directory for our build of nginx:
mkdir nginx && cd nginx
# Get the source to the nginx package
apt-get source nginx

If the apt-get source nginx command fails, you may have to edit your /etc/apt/sources.list and uncomment the lines that start with deb-src. Save the file, then re-run sudo apt-get update and try the apt-get source nginx command again.

Now, we need to add the source for nginx-rtmp-module to the nginx source code:

# Enter the modules directory for this build of nginx.  Since the build number will vary, use the wildcard:
cd nginx*/debian/modules/
# Clone the git repo for the rtmp module, this copies its source into the Ubuntu nginx sources:
git clone https://github.com/arut/nginx-rtmp-module.git
# Go back to the top of the nginx source code
cd ../../

From here, we need to tell the package build utility that it needs to build nginx-rtmp-module. For this, you’ll need to edit the file debian/rules. There are many ways to add this to the build – as of 14.04, Ubuntu builds several packages for nginx. I tend to want to install all of the nginx extras, so I add rtmp to the “extras” build.

Look for a section like extras_configure_flags in the file. Add the line --add-module=$(MODULESDIR)/nginx-rtmp-module to the end of this list. Note that the \ at the end of each line means “continue on to the next line.” You’ll need to add a \ to the previous line in order to add a new line.

Save this file, and then exit your editor.

At this point, if you want to change the version number (ie, to prevent the “stock” nginx from installing on top of your custom RTMP enabled version) you can. You’ll need to edit debian/changelog, and add a new section at the top. Follow the same pattern as the other sections in the file, and change the version number to whatever you want. I suggest just adding your initials or similar to the end of the existing version number.

Now we build. From the same nginx-<version> directory as above:

dpkg-buildpackage -b

This may take a while, depending on your hardware. When it is finished, you’ll have a set of .deb files in the directory above nginx-<version>:

sudo dpkg -i nginx-common_*.deb
sudo dpkg -i nginx-extras_*.deb

That should do it! You’ve now installed a custom build of nginx on Ubuntu that includes the RTMP module!

  • Samual Shepard

    can i ask why this is different from the step in this guide https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.50/…….I tried this guide and got a nginx service not found error. Im using ubuntu 16.04 on a VM hosted on my physical computer not a cloud format. Im a total noob at setting up servers/linux so your guide is a little hard to understand when it gets to the part about apt-get source nginx…is that were this goes? wget http://nginx.org/download/nginx-1.9.15.tar.gz

    • agent86ix

      The guide on the OBS website does some things differently than mine. I’d suggest following theirs or mine completely, and not mixing the two.

      At what point did you get a “service not found” error?

      One of the differences is that they download the source code from the nginx website, and I download the source from Ubuntu’s repositories. That’s why I do apt-get source and they do a wget. Doing it my way ensures you get nginx code that is tested and works with the version of Ubuntu you have, but doing it their way works on more flavors of Linux.

      My apt-get build-dep command automatically picks the correct packages to install. If nginx ends up depending on other things to build in the future, my command doesn’t have to change. Theirs might, so I prefer my way.

      • Samual Shepard-Freudenburg

        1st thanks so much for the help, i’ve been trying all week with this, im good on computers but totally new to linux and servers…. Ok so here’s what I did…I followed you steps–

        Always a good idea before you start doing apt commands:

        sudo apt-get update && sudo apt-get upgrade

        Grab any build dependencies, this will install all required packages automatically:

        sudo apt-get build-dep nginx….(*I also added “sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev”)

        We’ll need git to grab the RTMP module later:

        sudo apt-get install git …. After this point I was confused…I think i type “apt get source nginx” and nothing happened, but also I thought that the repositories built into nginx were not modifiable…digitalocean talks about using nginx open source if you want to add modules…so i thought the only way to do this was to use the the files that end in .gz so i used wget http://nginx.org/download/nginx-1.10.2.tar.gz

        Enter the modules directory for this build of nginx. Since the build number will vary, use the wildcard:

        cd nginx*/debian/modules/…..(I was confused what this is asking me to do)

        Ok so after I did “sudo apt-get install git” it looks like you want me to make a directory and install nginx into that? But since I was confused how you wanted me to get nginx and what version I then followed OBS guide which i thinks is having me download nginx and extract the files into a directory? I went as follows… wget http://nginx.org/download/nginx-1.10.2.tar.gz wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

        (***when i used the “git clone https://github.com/arut/nginx-rtmp-module.git” command i got and error later when I gave the ./configure command.)

        tar -zxvf nginx-1.10.2.tar.gz unzip master.zip cd nginx-1.10.2 $ ./configure –with-httpsslmodule –add-module=../nginx-rtmp-module-master $ make $ sudo make install $ sudo /usr/local/nginx/sbin/nginx Everything seemd ok to this point with no errors, but when I typed my ip adress into the search bar it just took me to my router login page not a nginx splash page,…. also to answer you question about service not found…after i couldn’t find nginx splash page I ran: “$ sudo ufw app list” it just says CUPS…..I also ran “$ systemctl status nginx” and got nothing…..these instructions are from https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04

        So at this point when I give the ($ sudo /usr/local/nginx/sbin/nginx) command it seems to work, but I can’t find the nginx page and when i type systemctl status nginx i get “unit nginx service could not be found.”

        • Samual Shepard-Freudenburg

          This shouldn’t be that hard right?! I know you probably thinking follow my instructions to the T, but I don’t really understand them since I am new to this and not sure how to fill in the gaps

          • agent86ix

            You didn’t so much follow my steps as you followed part of my guide and part of at least 2 other guides :) It’s a bit like trying to bake a cake by buying 3 cake mix boxes, mixing the various powders together, and then following the steps on different boxes at different steps. Mostly what you’re going to make is a mess ;)

            If you just set up this VM, you might just wipe it and start fresh.

            I just did this today on a fresh Ubuntu 16.04 VM for a project I’m working on, so I edited the steps slightly to add a few additional notes for a “fresh” box.

            I would suggest starting with a fresh VM, follow my steps (and ONLY my steps). If you run into an error, don’t check someone else’s directions – go looking for help with the error you run into. You can google a lot of error messages and get some helpful advice, or you can try posting it here and see if I’m around and able to help.

            For Ubuntu/Linux help, you might check the various StackExchange sites, like AskUbuntu.com. There are more people helping there than here, where it’s just me. They’d probably get back to you faster. Explain clearly what you’re trying to do and the steps you took, and they can usually help.

          • agent86ix

            Some specific bits about where you went wrong:

            If apt-get source nginx fails, what is the error? It might be that you need to uncomment the deb-src lines in your /etc/apt/sources.list. By default, Ubuntu only checks the binary (precompiled) packages repositories. Uncommenting the deb-src lines in your sources.list will make sure it can get source code if you request it.

            When the apt-get source nginx command completes, it creates a directory that is named nginx, a dash, and then a number representing the current version for your version of Ubuntu. For instance, today when I did this, it made a directory called “nginx- 1.10-0”. Since this number changes often, it wouldn’t make sense for me to put the exact name in the guide. Thus, I say, “cd nginx-*” which will work for any version.

            apt-get source nginx will do something similar to the wget & tar command you list. (I think I mentioned this in my previous comment). apt-get source nginx pulls the code down from Ubuntu’s repositories, which means it’s designed to be built and installed on your version of Ubuntu. The wget method downloads the generic Linux code for nginx, which isn’t as easy to build and install alongside other Ubuntu packages. That’s probably also the reason why your build didn’t work with systemctl and ufw.

  • Samual Shepard

    also whats the difference between sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev and

    sudo apt-get build-dep nginx