Skip to main content

Setting Up an Web Server with Nginx

··2 mins·
Linux Nginx
Makoto Morinaga
Author
Makoto Morinaga
A personal notebook for tech notes, coding, and system experiments.
Table of Contents

A web server is required to provide access to various services over a network. This post describes the process of setting up an Nginx web server, making it accessible within a local network.

Installing Nginx
#

To install Nginx, execute the following command:

Terminal
# Arch Linux
sudo pacman -S nginx-mainline

Configuring iptables
#

To allow access to TCP ports 80 and 443, configure iptables as follows:

Terminal
# Allow access to TCP port 80 from LAN (192.168.10.0/24)
iptables -A INPUT -p tcp -s 192.168.10.0/24 --dport 80 -j ACCEPT
# Allow access to TCP port 443 from LAN (192.168.10.0/24)
iptables -A INPUT -p tcp -s 192.168.10.0/24 --dport 443 -j ACCEPT

For further details, refer to: iptables Configuration.

Starting the Nginx Service
#

Enable and start the Nginx service:

Terminal
sudo systemctl enable nginx
sudo systemctl start nginx

Accessing http://[server-ip] should display the default Nginx page.

Configuring nginx.conf
#

Modify /etc/nginx/nginx.conf as follows to disable direct IP access and enable virtual host configurations:

/etc/nginx/nginx.conf
#user html;
worker_processes auto;
worker_cpu_affinity auto;
worker_priority 0;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
listen 80  default_server;
server_name _;
return 444;
}

include sites-enabled/*;

Setting Up Virtual Hosts
#

Creating Directories for Virtual Host Configuration
#

The following directories should be created for managing virtual host configurations:

Terminal
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled

Placing Configuration Files in sites-available
#

An example configuration file for www.mkt3.dev:

/etc/nginx/sites-available/www.conf
server {
    listen 80;
    server_name www.mkt3.dev;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

Enabling a Virtual Host
#

To enable the configuration:

Terminal
cd /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/www.conf .

Configuring DNS
#

To enforce domain-based access, DNS configuration is required. Modify the zone file with the following entry:

zone file
www         IN A     192.168.10.xxx

For further dns details, refer to: Setting Up an Internal DNS Server Using NSD and Unbound.

Verifying Virtual Host Configuration
#

Restart the Nginx service:

Terminal
sudo systemctl restart nginx

Accessing http://[configured-domain] should display the default Nginx page.

Related

Setting Up a File Sharing Server Using Samba
··3 mins
Linux Samba
Setting Up an Internal DNS Server Using NSD and Unbound
··2 mins
Unbound Nsd Linux
iptables Configuration
··4 mins
Linux Firewall Iptables