Nginx Installation, Configuration, and Basic Usage Guide
This guide walks through downloading, compiling, and installing Nginx from source on Linux, configuring dependencies, creating a systemd service, managing start/stop commands, understanding the directory layout, and setting up basic nginx.conf directives—including worker processes, connections, MIME types, and virtual host server_name patterns.
Nginx is a lightweight web server, reverse proxy, and mail proxy that excels in low memory usage and high concurrency. It is widely used for load balancing and serving static content.
Installation steps :
1. Download the source package from http://nginx.org/en/download.html and upload it to a Linux host.
2. Extract the archive: tar -zxvf nginx-1.21.6.tar.gz 3. Enter the source directory and run the configure script. If dependencies are missing, install them and re‑run the script:
yum install -y gcc yum install -y pcre pcre-devel yum install -y zlib zlib-devel4. Compile and install:
make make installThe installation path can be changed with ./configure --prefix=/usr/local/nginx. After successful installation, the nginx binary resides in /usr/local/nginx/sbin.
Starting and stopping :
Start the server: /usr/local/nginx/sbin/nginx Stop the server (graceful): nginx -s quit Force stop: nginx -s stop Check status with ps -ef | grep nginx or systemctl status nginx.
Creating a systemd service :
Create /usr/lib/systemd/system/nginx.service with the following content (adjust the paths if necessary):
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.targetReload systemd and start the service:
systemctl daemon-reload systemctl start nginx.serviceVerify with systemctl status nginx or ps -ef | grep nginx.
Directory structure :
The main directories under the installation prefix are conf (configuration files), html (static files), logs (access and error logs), and sbin (the nginx executable).
Basic configuration (nginx.conf) :
Key directives include: worker_processes 1; – number of worker processes. worker_connections 1024; – maximum connections per worker. include mime.types; – MIME type mappings. default_type application/octet-stream; – fallback MIME type. sendfile on; – enable zero‑copy file transmission. keepalive_timeout 65; – idle connection timeout.
A simple server block listening on port 80 serves files from the html directory and defines an error page for 5xx responses.
server_name usage :
Multiple virtual hosts can be defined by different server_name values. Wildcards (e.g., *.example.com) and regular expressions (e.g., ~^[0-9]+\.example\.com$) are supported to match a range of domain names.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
