Search This Blog

Pages

Header Ads Widget

Responsive Advertisement

Sunday, 4 October 2020

How to Install NGINX(Compiled From Source Code) in Ubuntu

 Todays post is about how to install by direct compiling NGINX in ubuntu.

There are two ways two install nginx:-

1. Install pre-built version of nginx. For this you can check nginx docs there are some very simple steps for this step. 

2. Install nginx by download it source and then compile it by yourself.


Why we need to install nginx by  2 step??

In a pre-built version of nginx there are some default modules(you can say some default functionality), but if you need some extra functionality of nginx that is not in pre-built version, then you have to compile it yourself. Because nginx does not support adding module after installation, so every time if you need some new module you have to compile it again. BTW later version of nginx supports adding third party modules after installation. But here we require extra module of nginx not third party thats why we are installing it again. So lets start.

Before installing if you already have nginx install in your system them remove it and also take snapshot of a system because we are going to change lots of config.

sudo apt-get purge nginx nginx-common

STEP - 1;

Here are some basic commands before starting

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential

STEP - 2;

Now install some support libraries that is essential for nginx to run like pcre(for rewrite functionality), openssl(for https), zlib(for compression).

 1. PCRE

sudo apt-get install libpcre3
sudo apt-get install libpcre3-dev

2. OPENSSL

sudo apt-get install openssl

3. ZLIB

sudo apt-get install zlib1g zlib1g-dev

STEP - 3;

Now download source code of nginx current stable version is 1.18 but you can change according to time.

wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar xzf nginx-1.18.0.tar.gz
cd nginx-1.18.0.tar.gz/

STEP - 4;

After downloading the source code now comes the fun part we have to configure it and tell nginx which modules we are going to need.

Example script that i copy paste from nginx docs. Dont worry if you did not understand it actually i also not understand it completely just joking. So as you can see you have to provide all custom setting in /configure command so that nginx can build according to our taste. If you have to understand details of this command you can comment i will write a post about it.

./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-pcre=../pcre-8.44
--with-zlib=../zlib-1.2.11
--with-http_ssl_module
--with-stream
--with-mail=dynamic
--add-module=/usr/build/nginx-rtmp-module
--add-dynamic-module=/usr/build/3party_module 

Now as you can see to add module you can use --with-{module_name} and to remove any default module you can use --without-{module_name}. For all module name refer to nginx docs. Here is a command that i use because i want secure_link module, secure link module is use to secure your link like expire time, or bound with ip basicaaly used when you serve protected content to user.

Remember all this command is one line 

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_realip_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_degradation_module

Now you have to run some last commands.

make
sudo make install

 

Done nginx is installed in your system.

Now simply run:-

 sudo nginx
 to start the nginx


NOTE:-

I know while you perform all these some of you got stuck at any step so don't panic comment me the error trace and i will help you out. Because i cannot cover all the error cases in this post.

 


No comments:

Post a Comment