How to install NGINX on Alpine Linux?

Nginx is a lightweight and high performance web server/reverse/email(IMAP/POP3) proxy.

Basic features

  • Static file server, indexes and auto-indexing.
  • Reverse proxy with caching options.
  • Load balancing.
  • Fault tolerance.
  • Support of HTTP and HTTP2 over SSL.
  • Support for FastCGI with caching options.
  • Virtual hosts based on name and/or IP address.
  • Support for authentication.
  • IPv6 compatible
  • gzip compression.

You can see all NGINX features at:

Readings interest:

Install

# apk add nginx
fetch https://ams.edge.kernel.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
(1/4) Installing pcre (8.44-r0)
(2/4) Installing nginx (1.20.1-r3)
Executing nginx-1.20.1-r3.pre-install
Executing nginx-1.20.1-r3.post-install
(3/4) Installing nginx-openrc (1.20.1-r3)
(4/4) Installing nginx-vim (1.20.1-r3)
Executing busybox-1.33.1-r3.trigger
OK: 264 MiB in 72 packages

Check available modules

# nginx -V
nginx version: nginx/1.20.1
built with OpenSSL 1.1.1k  25 Mar 2021
TLS SNI support enabled
...

Configuration files

Where the configuration files are located.

# tree /etc/nginx/
/etc/nginx/
├── fastcgi.conf
├── fastcgi_params
├── http.d
│   └── default.conf
├── mime.types
├── modules
├── nginx.conf
├── scgi_params
└── uwsgi_params

2 directories, 7 files

Web dir

Where our websites will be hosted, as you can see we only have the files for localhost.

# tree /var/www/
/var/www/
└── localhost
    └── htdocs

2 directories, 0 files

Manage the service

In this part we will learn how to start, stop or restart the nginx service, for this we will use the OpenRC initialization system. OpenRC is the default boot system in distributions like Gentoo and Alpine Linux.

Check the status

# rc-service nginx status
 * status: stopped

Start

# rc-service nginx start
 * Caching service dependencies ...   [ ok ]
 * Starting nginx ...                 [ ok ]

Check the status again.

# rc-service nginx status
 * status: started

Start with the Operating System

Start automatically after a system reboot by exec:

# rc-update add nginx default
 * service nginx added to runlevel default

Reload the configuration

Verify any configuration file change with:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

then reload the changes:

# rc-service nginx reload
 * Reloading nginx configuration ... [ ok ]

Restart

# rc-service nginx restart
 * Stopping nginx ...  [ ok ]
 * Starting nginx ...  [ ok ]

Stop

# rc-service nginx stop
 * Stopping nginx ... [ ok ]

Virtual Hosts

Virtual hosting allows multiple domains to be hosted on the same server, sharing server resources such as memory and processor cycles.

Create the configuration file

Replace librebyte by your project name

# touch  /etc/nginx/http.d/librebyte.conf

Generic proxy

This configuration is suitable for generic proxy.

server {
    listen 80;

    # Put here you domain   
    #
    server_name midominio.com;

    # Max file size useful for file uploading 
    # 
    client_max_body_size 8M;

    location / {    
        # NGINX acting as reverse proxy
        #
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        #
        # Set the backend url and port, port is optional for standard services
        # when there is a variable as a part of proxy_pass URL a resolver is needed.
        #
        proxy_pass http://miurl.internal:port;
    }
}

Set the listen, server_name and proxy_pass directives according to your needs, see: How to install and configure NGINX as reverse proxy

FastCGI Proxy

This configuration is suitable for FastCGI proxy, example: PHP-FPM.

server {
    listen 80;
    server_name midominio.com;
    # Set the document root
    root /var/www/librebyte/htdocs;
    index index.php;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        # This is cool because no php is touched for static content.
        # include the "?$args" part so non-default permalinks doesn't break when
        # using query string
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

Set the listen, server_name, root and fastcgi_pass directives according to your needs. See: How to install NGINX on NetBSD? y How to install PHP 7.4 in Ubuntu 20.04? for more information.

Creating DIR project:

# mkdir -p /etc/nginx/http.d/librebyte/htdocs

In the htdocs DIR we put the public files (css, javascripts, html) and outside of htdocs the PHP files.

References

YouTube video

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.