Install Nginx from source code

Install Nginx from source code

Nginx is a web server that works very well as a reverse proxy server and is also very effective in load balancing, media streaming, serving static files and caching. Nginx also solves the C10k problem as it can serve 10k concurrent connections.

Package installation vs Source Code installation

Nginx can be installed through the package manager in ubuntu sudo apt install nginx . But it will install all the modules available in Nginx. Most of them might not be required for you. So, to avoid unnecessary module installation and have more control over your Nginx service, you can install it from the source.

Please visit the official Nginx-doc for more details on modules,

Downloading the source code

Let's get started with the installation.

If you already have Nginx installed, then please remove it before instantiating the installation.

sudo apt purge nginx nginx-common nginx-core

We need to download the source file in the system. Go to the directory where you want to keep the Nginx source files. Here you can find the updated Nginx version file link to download .

mkdir nginx
cd nginx

Now let's extract the file from the zip file and delete the .gz file.

tar -zxvf nginx-1.23.3.tar.gz 
rm -rf nginx-1.23.3.tar.gz

Installing Nginx

We need to install C compiler for the installation. So we need to install the build-essential in ubuntu.

sudo apt install build-essential

Now you can run the following command


You will get the following error output.

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

To resolve these errors, we need to install some packages in our system.

sudo apt install libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev

libpcer3 is to support regular expressions whose syntax and semantics are as close as possible to those of the Perl 5 language.

zlib1g library is used for implementing compression methods for creating gzip files.

libssl is used for the implementation of the SSL and TLS cryptographic protocols.

./configure \
> --sbin-path=/usr/bin/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --with-pcre \
> --pid-path=/var/run/
> --with-http_ssl_module

The main benefit of installing Nginx through source code is that you only need to install the modules you need, unlike the package installation where you will install all the modules.

For a detailed understanding of the options available for configuration in nginx, please visit this awesome clean documentation provided on the official site

Let's go through each of these parameters mentioned in the configuration briefly. I took the details from the official doc.

  • --sbin-path=path sets the name of an Nginx executable file. This name is used only during installation.

  • --conf-path=path sets the name of an nginx.conf configuration file. If needs be, nginx can always be started with a different configuration file, by specifying it in the command-line parameter -c file.

  • --error-log-path=path sets the name of the primary error, warnings, and diagnostic file.

  • --http-log-path=path sets the name of the primary request log file of the HTTP server.

  • --pid-path=path sets the name of an file that will store the process ID of the main process.

  • --with-pcre forces the usage of the PCRE library. (PCRE stands for Perl Compatible Regular Expressions which implements a regular expression engine.)

  • --with-http_ssl_module enables building a module that adds the HTTPS transfer protocol to an HTTP server.

You should be able to see your nginx configure as per the parameters you mentioned. In your terminal you should be able to see

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/bin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

Let's install nginx in our system by running the following commands


This will create the binary files for installation.

sudo make install

This will install the file in your system.

Now that you've installed Nginx on your machine, let's start it.

sudo nginx

If you visit from your browser, you will be able to see nginx's default page there.

sudo nginx -s stop

Add Nginx as a service

You should add Nginx as a service in your system.

To add Nginx as a systemd service, you need to add a service called nginx.service in the directory /lib/systemd/system/ .

sudo nano /lib/systemd/system/nginx.service

Add the following content to your service file and save it.

Description=The NGINX HTTP and reverse proxy server

ExecStartPre=/usr/bin/nginx -t
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID


Now, please restart your system.

Then run this command in the terminal

sudo systemctl start nginx

Now if you visit from your browser, you will be able to see Nginx's default page there.

To start nginx by default on reboot you need to run this command

sudo systemctl enable nginx


We succeeded in finally installing Nginx from the source file and running Nginx as a service.

Happy Learning !!