Hey, everyone.
And welcome back.
Now, in the earlier video, we had a great introduction about modular architecture.
We also looked into how Nginx is built with the modular architecture and how that helps in enhancing
the functionality of Nginx by itself.
Now, till now we have been installing Nginx with a package manager like Yum or App.get.
So those are ready made things that are being fed to us.
However, in order to understand about the modular architecture, we need to learn on how we can compile
Nginx from source.
So for example, if I do a nginx hyphen V over here, so currently you see this Nginx has a lot of modules
which are available over here.
So many times we might need certain modules, we might not need certain modules, we might want to change
the path associated with the binary.
We might want to change the path associated with modules.
So we want a lot of customization.
So when you do so, like when you install Nginx via Yum or App.get, then your ability to customize
each and everything is limited there.
So in order to have a full capability of customization, you will have to compile Nginx from source.
And this is what we will be looking in today's video.
So for our demo purposes, I have a brand new server that I have created.
So currently this does not have Nginx or any other package so that we can start from absolute scratch.
So this is a CentOS seven based server now while compilation.
So whenever you are compiling Nginx from source, there are certain packages that you will need as a
prerequisite.
So basically these are some of the important packages that we'll be using throughout our demo.
So I have just created a document I'll be sharing the entire document after this video so that you can
try it out yourself.
So let me copy this up and I'll paste it within my terminal.
Great.
So our prerequisite is completed.
Now the next thing that we need to do is we have to download the Nginx.
So basically there are mainline versions.
You have stable version, you have legacy version, legacy.
Are the older ones, stable is the current stable.
So in my case the stable is 1.16.
In your case, if you are watching a video at a later stage, this version might change, but the overall
procedure will remain the same.
So I'll right click here, I'll click on copy link, address and within the terminal let's do a wget
and I'll paste the link.
So this will go ahead and download the tar file.
So when I do an LS, this is the tar file.
So let's go ahead and decompress it with tar xvf nginx.
So it has decompressed it.
Now if you do an ls, then you will be able to see the directory of nginx.
So let's go inside the nginx directory here.
And let's do an ls.
So these are the files which are present.
The one that is most important here is the configure file.
So when I do a configure hyphen hyphen help you see there are various options which are available.
So let's go a bit up.
So basically here we while configuring, we can enable various modules like you have Http SSL module,
you have Http version two module and various others.
So there are a lot of modules which are available.
So since we are doing things from scratch, all of these things are something that we will be able to
modify according to the things that we need.
All right.
So the way things work are let me actually show you.
Let me copy these things.
All right.
I'll copy this up.
And within the atom editor, I'll paste it.
All right.
So now the way things would work here is you do a configure and various options which are specified
over here.
You have to mention the appropriate values for that.
So for prefix, let's say you specify user share nginx.
This is just an example.
Then for sbin path, you specify this, you say user bin or user is bin and for modules port and so
on.
So this is how things would work.
So generally in order to make things easy, we'll use the readymade values which are associated with
these path.
So when you look into the engine which was installed through Yum, there are already the values which
are predefined.
So we'll use the same values here.
So what are the things that we need?
We need prefix.
We'll need SBN path.
We'll need a module path.
We'll also need the conf path.
The error log path.
The http log path.
We'll also need the client body path here.
All right.
So these are the minimum things.
So I'll just copy it up here.
And along with that it is also recommended to make use of user specify the group.
And while we are doing that, we also have to specify the PID path and the log path.
So let's go back here.
So here you have the PID path.
You have the log path, you have the user as enginex and the group as Enginex.
So I'll copy this up
and within my atom editor I'll just paste them.
All right.
So this is how the configure command would look like.
Now, apart from this, if you want to add a module here, you can definitely do that.
So for our example, let's add one module.
So I'll say with http mp4 module.
So this is just an example that we are using here.
So I'll specify with http, underscore MP4 module.
So once you have this built, go ahead and configure this.
So let me paste everything here.
So this is the entire command that is being formed.
Now, before we run this, do remember that there are certain things that you will have to create because
currently here you see you have user is equal to nginx group is equal to Nginx.
However, when you do an ID, you see the Nginx, no such user is there.
So the user thinks are some of the parts that you will have to create.
So let's go ahead and do it.
I'll say user add Nginx.
All right, so now we have the Nginx user.
We'll also have to specify this specific directory path.
So I'll copy this up and I'll say mkdir hyphen P I'll specify this path and I'll change the ownership
so that the user nginx will be able to access this path.
So I'll change the ownership to nginx.
All right.
Now, various other things can be automatically created by Nginx.
So that is something that we don't have to worry much about.
So with this said, let's go ahead and run the configure command.
All right.
So once this is done, you can go ahead and run, make command.
And the last thing that we'll do is the make install command.
Perfect.
So once you have done that, you should be able to see the nginx directory and this Nginx directory
has all the necessary configuration files.
Now the next important thing is to make sure whether the enginex service is there or not.
So when you do a systemctl status enginex you see that it is not able to find the Nginx service.
So basically you will have to download this specific file.
Now the official documentation already has this specific file and its contents, so these are the contents
that we are interested in.
So let's copy the path here and let's do a nano.
And within here, we'll just paste the contents.
Let's paste the content.
All right.
Now, we don't really have to edit here.
Now, in case in case if you are not using the default values here, then you might have to edit the
values associated within the service definition here.
Now, since we are using the default path, we don't really have to do that and that is the advantage
of using the default path.
You don't have to worry much about the later stages, so I'll go ahead and I'll save it.
So once it is done, let's verify whether the Nginx service is running.
So it says that the Nginx service is inactive.
All right, let's go ahead and start the Nginx service.
And now let's do a status on Nginx.
And you see the nginx is up and running.
Now if you do a netstat on hyphen p you should see that the nginx is listening on port 80.
So in order to quickly verify you can do a curl on 127001.
You should be able to see the default nginx page.
So if you want to also quickly verify, let's get the public IP address of this server.
And when you paste it within the browser, you should be able to see the default.
Welcome to Nginx page.
So that's the high level overview about how you can compile Nginx from source.
Now once you know on how you can compile nginx from source, then it will also be easier for you to
understand the static as well as dynamic modules aspect.
Now one last thing because when we do a nginx v hyphen here you see the output of this is much more
less when you compare with the default Yum base installation.
All right.
So it is much more lesser.
But one thing that you have to make sure that when you're running this in production environment, make
sure that the functionality that you typically need within production, you have the appropriate modules
for that.
Otherwise, at a later stage, you will realize that you do not really have a module for a specific
functionality that you are looking.
So generally what many of the people they do, they directly just copy this entire thing and they remove
certain modules which are not required.
So for example, if you do not really require MP four, they'll just remove this.
Otherwise most of the things people tend to keep it same as the one that comes from the package repository.
So that's the high level overview about how you can compile Nginx from source and configure things according
to your needs.
So with this we'll conclude this video.
I hope this video has been informative for you and I look forward to see you in the next video.