Operations 3 min read

Step-by-Step Guide to Building and Deploying Nginx from Source on Linux

This tutorial walks you through downloading the Nginx source package, extracting it, compiling and installing the binary, setting up the required directories, creating a systemd service file, and finally enabling and starting the Nginx service on a Linux system.

Raymond Ops
Raymond Ops
Raymond Ops
Step-by-Step Guide to Building and Deploying Nginx from Source on Linux

First, change to the

/opt

directory and download the Nginx 1.18.0 source archive:

<code>cd /opt
wget http://nginx.org/download/nginx-1.18.0.tar.gz</code>

Extract the archive:

<code>tar xf nginx-1.18.0.tar.gz</code>

Create the installation directory and compile Nginx:

<code>cd nginx-1.18.0
./configure --prefix=/apps/nginx
make && make install</code>

After compilation, create a symbolic link to the binary if needed and copy any additional configuration files.

Next, create a systemd service unit file (e.g.,

/etc/systemd/system/nginx.service

) with the following content:

<code>[Unit]
Description=The nginx HTTP and reverse proxy server

[Service]
PIDFile=/apps/nginx/logs/nginx.pid
ExecStart=/apps/nginx/sbin/nginx
ExecStop=/usr/bin/kill -s TERM $MAINPID
ExecReload=/apps/nginx/sbin/nginx -s reload

[Install]
WantedBy=multi-user.target</code>

Reload systemd to recognize the new unit, then start and enable Nginx:

<code>systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
systemctl status nginx</code>

These commands verify that Nginx is running correctly and will start automatically on boot.

operationsLinuxNginxWeb ServersystemdSource Compilation
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.