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

            I made it to this point without a hitch… $ apt-get source nginx Reading package lists… Done NOTICE: ‘nginx’ packaging is maintained in the ‘Git’ version control system at: git://anonscm.debian.org/collab-maint/nginx.git Please use: git clone git://anonscm.debian.org/collab-maint/nginx.git to retrieve the latest (possibly unreleased) updates to the package. Need to get 1,418 kB of source archives. WARNING: The following packages cannot be authenticated! nginx E: Some packages could not be authenticated

            but sorry im still confused by cd nginx*/debian/modules/…instead of * should i put the version number in? But since i just said apt-get source nginx how do I know what version I have?

            $ cd nginx/debian/modules/ bash: cd: nginx/debian/modules/: No such file or directory

            should this be?? $ cd nginx- 1.10.0/debian/modules/

          • Samual Shepard
          • Samual Shepard-Freudenburg

            also I tried to look in the root folder to find nginx directory but even with gksudo nautalis it wont let me in….just shows me a desktop folder inside of root

          • Samual Shepard

            perhaps this is hopeless smh…if i click files, home, there is a nginx and nginx rtmp module folders……….but they don’t say anything about the version numbers as far as i can tell

          • Samual Shepard

            I found 1.10.2-3 in the debian change long file so should this part in your instructions ($ cd nginx*/debian/modules/) change to $ cd nginx-1.10.2-3/debian/modules/ ???

          • agent86ix

            Okay, so I created a new user, went through the steps in my guide up to the apt-get source nginx line, and captured what happens on the terminal:

            
            [email protected]:~$ mkdir nginx

            [email protected]:~$ cd nginx

            [email protected]:~/nginx$ apt-get source nginx Reading package lists... Done NOTICE: 'nginx' packaging is maintained in the 'Git' version control system at: git://anonscm.debian.org/collab-maint/nginx.git Please use: git clone git://anonscm.debian.org/collab-maint/nginx.git to retrieve the latest (possibly unreleased) updates to the package. Need to get 1,562 kB of source archives. Get:1 http://mirrors.linode.com/ubuntu xenial-updates/main nginx 1.10.0-0ubuntu0.16.04 .4 (dsc) [3,158 B] Get:2 http://mirrors.linode.com/ubuntu xenial-updates/main nginx 1.10.0-0ubuntu0.16.04 .4 (tar) [909 kB] Get:3 http://mirrors.linode.com/ubuntu xenial-updates/main nginx 1.10.0-0ubuntu0.16.04 .4 (diff) [650 kB] Fetched 1,562 kB in 0s (30.7 MB/s) gpgv: Signature made Thu 27 Oct 2016 02:59:57 PM UTC using RSA key ID A744BE93 gpgv: Can't check signature: public key not found dpkg-source: warning: failed to verify signature on ./nginx1.10.0-0ubuntu0.16.04.4.ds c dpkg-source: info: extracting nginx in nginx-1.10.0 dpkg-source: info: unpacking nginx1.10.0.orig.tar.gz dpkg-source: info: unpacking nginx_1.10.0-0ubuntu0.16.04.4.debian.tar.xz dpkg-source: info: applying perl-use-dpkg-buildflags.patch dpkg-source: info: applying ubuntu-branding.patch dpkg-source: info: applying cve-2016-4450.patch

            [email protected]:~/nginx$ ls nginx-1.10.0 nginx1.10.0-0ubuntu0.16.04.4.dsc nginx1.10.0-0ubuntu0.16.04.4.debian.tar.xz nginx_1.10.0.orig.tar.gz

            [email protected]:~/nginx$ cd nginx*/debian/modules [email protected]:~/nginx/nginx-1.10.0/debian/modules$

            As you can see, I create a new directory. Then I run apt-get source nginx in that directory. It creates a new directory and a few new files. The directory is named nginx-1.10.0. I can use the command "cd nginx*/debian/modules" to change into the nginx-1.10.0/debian/modules directory.

            All of this is precisely as explained in the guide.

            From here, I could start up in the guide again starting at the "git" command.

          • Samual Shepard

            HI! Thanks for the reply! Yeah a few hours ago I actually managed to make it all the way though you guide without a hitch! My only concern is

            $ sudo /usr/local/nginx/sbin/nginx does not seem to work, but service start nignx does idk if thats normal?

            ALSO $sudo ufw status returns “inactive” idk what that means below are some commands i ran after getting to the end of your guide. HOWEVER my big problem is that I dont know how to check if nginx is working? It says put your IP in the address bar and you should be taken to a page that says welcome to nginx…but when i do that I just get taken to the webpage for my router…My set up is this: Host machine WIN10 with a VM running through virtual box on Ubuntu 16.0.4…….How do I find my server page?? Sorry this probably wans’t the easiest project to take on the first time i ever touched a linux OS or a VM.

            [email protected]:~$ sudo ufw app list [sudo] password for samual: Available applications: CUPS Nginx Full Nginx HTTP Nginx HTTPS OpenSSH

            [email protected]:~$ sudo /usr/local/nginx/sbin/nginx sudo: /usr/local/nginx/sbin/nginx: command not found

            [email protected]:~$ service nginx start

            [email protected]:~$ sudo ufw status Status: inactive [email protected]:~$ systemctl status nginx ● nginx.service – A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en Active: active (running) since Wed 2017-01-25 22:31:12 CST; 5min ago Process: 831 ExecStart=/usr/sbin/nginx -g daemon on; masterprocess on; (code= Process: 789 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; masterprocess o Main PID: 850 (nginx) CGroup: /system.slice/nginx.service ├─850 nginx: master process /usr/sbin/nginx -g daemon on; master_proc └─851 nginx: worker process

            Jan 25 22:31:11 Cotillion-Server systemd[1]: Starting A high performance web ser Jan 25 22:31:12 Cotillion-Server systemd[1]: Started A high performance web serv Jan 25 22:35:34 Cotillion-Server systemd[1]: Started A high performance web serv

          • agent86ix

            sudo /usr/local/nginx/sbin/nginx

            This fails because that’s not where Ubuntu installed nginx to. This command probably comes from some other guide. Try using the command “which nginx” to see where nginx is installed. On my system, it’s /usr/sbin/nginx.

            There’s no need to run nginx from the command line directly; Using “service nginx start” is a good way to start it once. If you want to start it automatically the next time (and every time) you boot, use “systemctl enable nginx”.

            sudo ufw status

            This command is for ufw, the Ubuntu FireWall (thus “ufw”). That’s a totally different topic, and you haven’t turned it on. I don’t use it much, so I’m probably not going to be much help there.

            Can’t see the nginx page

            If nginx starts successfully, and you’re seeing the router page instead of the nginx default home page, you’re using the wrong IP. If you’re trying to get there from a browser inside the VM, use the IP given by the command “ifconfig”. You may have a loopback interface (127.0.0.1) and an ethernet interface (varies, but possibly something that starts with 10.)

            Trying to access a VM from outside the VM is a complex topic. There are a lot of VM settings that can interfere. I think by default it’s not even possible to access a web server running inside a VM from outside it. This is another totally different topic that I’m probably not going to be able to help with much.

          • Samual Shepard

            HI thanks so much for all you thoughtful advice!! I finally made it to the nginx page the other day!! Now i just have to figure out how to use it with obs lol,,…I set this up rtmp { server { listen 1935; chunk_size 4096;

                        application live {
                                live on;
                                record off;
                        }
                }
            

            }

            But anyway one las question…down below i get this: Jan 27 20:27:40 Gaming-Server systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Inv do you get this error when you run the custom version?

            nginx.service – A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2017-01-27 20:27:40 CST; 51min ago Main PID: 2572 (nginx) CGroup: /system.slice/nginx.service ├─2572 nginx: master process /usr/sbin/nginx -g daemon on; master_process on └─2573 nginx: worker process

            Jan 27 20:27:40 Gaming-Server systemd[1]: Starting A high performance web server and a reverse proxy serv Jan 27 20:27:40 Gaming-Server systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Inv Jan 27 20:27:40 Gaming-Server systemd[1]: Started A high performance web server and a reverse proxy serve Jan 27 21:16:25 Gaming-Server systemd[1]: Started A high performance web server and a reverse proxy serve

          • Samual Shepard

            use the wildcard in this CD since the nginx version will vary

            $ cd nginx*/debian/modules/ git clone https://github.com/arut/nginx-rtmp-module.git

            please could you explain what it means to use wildcard….am I supposed to copy that line of code exactly? is that line of code the wildcard code?? or am I supposed to replace the (*) symbol with the version of nginx that “apt-get source nginx” downloaded…in Debian changlog it says nginx 10.2.2-3…should my line of code then be “cd nginx-10.2.2-3/debian/modules/” ???

            You say “use the wildcard in this CD since the nginx version will vary” does this mean your are just using a *wildcard symbol and I am supposed to fill in the blanks with my own version of nginx? Are you meaning to say “I used the wildcard in this CD instruction since the nginx version will vary and in real life when you enter this CD command you should replace the wildcard symbol with your version of nginx which can be found xxx (?? I found V# in changelog file is that correct?). OR Do you want me to use that exact line of code since we don’t know what version of nginx we have after doing the “apt-get source nginx” commmand?? OR Should i remove the * and just enter the command “cd nginx/debian/modules/

            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. ?? So “$ cd nginx/debian/modules/” that exact line of code should be used because it will work for any version of nginx?? OR Are you saying “I inserted (*) which is a wild card symbol…because it will work for people using the guides with different nginx versions….but you should NOT actually type or “use” a wildcard symbol in your line of code…you should instead substitute you build version of nginx….i.e. if you have nginx 10.0.2 you should enter “$ cd nginx-10.0.2/debian/modules”

            use the wildcard in this CD since the nginx version will vary

            $ cd nginx*/debian/modules/ git clone https://github.com/arut/nginx-rtmp-module.git

  • 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

  • Samual Shepard

    Well I made it this far without problems!….but know I dont know what this means :-/

    dpkg-buildpackage -b

    I am guessing the b means build number of nginx THis what I added at top of change log nginx (1.10.0-0rtmp-ubuntu0.16.04.4) xenial-security; urgency=medium

    nginx (1.10.0-0ubuntu0.16.04.4) xenial-security; urgency=medium

    • SECURITY REGRESSION: config upgrade failure (LP: #1637058)

      • debian/nginx-common.config: fix return code so script doesn’t exit.

      — Marc Deslauriers marc.deslauriers@ubuntu.com Thu, 27 Oct 2016 10:42:14 -0400

    nginx (1.10.0-0ubuntu0.16.04.3) xenial-security; urgency=medium

    Soooo then if i go over to the terminal and use: dpkg-buildpackage -nginx-1.10.0-0rtmp-ubuntu0.16.04.4dpkg-buildpackage: unknown option or argument -nginx-1.10.0-0rtmp-ubuntu0.16.04.4

    Use –help for program usage information. [email protected]:~$ dpkg-buildpackage -nginx-1.10.0-0rtmp dpkg-buildpackage: unknown option or argument -nginx-1.10.0-0rtmp

    Use –help for program usage information. [email protected]:~$ sudo dpkg-buildpackage -nginx-1.10.0-0rtmp-ubuntu0.16.04.4 [sudo] password for samual: dpkg-buildpackage: unknown option or argument -nginx-1.10.0-0rtmp-ubuntu0.16.04.4

    Use –help for program usage information. [email protected]:~$ sudo dpkg-buildpackage -nginx-1.10.0-0rtmp dpkg-buildpackage: unknown option or argument -nginx-1.10.0-0rtmp

    Use –help for program usage information. [email protected]:~$ sudo dpkg-buildpackage -nginx-1.10.0 dpkg-buildpackage: unknown option or argument -nginx-1.10.0

    Use –help for program usage information.

    NO luck with anything I try :-/

    • Samual Shepard

      Do i need to CD into nginx or something first?

  • Samual Shepard-Freudenburg

    So I made it through everything fine…nginx splash page says the server is running fine……but it doesn’t appear the RTMP module actually works :-( have you actually been able to use OBS to connect to nginx,,much less send or receive any RTMP streams?