How To Install Nginx web server on Alpine Linux

last updated in Categories , , ,

I am a new user of Alpine Linux. How do I install nginx web server on Alpine Linux?

Nginx is a free and open source web server. You need nginx to display static or dynamic web pages. Nginx can also act as a reverse proxy and load balancer. This tutorial shows how to install nginx on Alpine Linux.

Step 1: Install the Nginx web server

First update your repo, run apk command as follows:
# apk update
Install the nginx server, run:
# apk add nginx
Sample outputs:

Fig.01: Installing the Nginx web server
Fig.01: Installing the Nginx web server

Step 2: Create the user and Nginx directory

I am going to store files in /home/www/ directory and going to create a user named wwwcbz for the nginx. Run the following command:
# adduser -g 'Nginx www user' -h /home/www/ wwwcbz
You will be prompted for the password as follows:

Changing password for wwwcbz
New password: 
Retype password: 
passwd: password for wwwcbz changed by root


  1. -g 'Nginx www user' : Set general information about the account wwwcbz
  2. -h /home/www/ : The account home directory
  3. wwwcbz : The account name

Step 3: Nginx configuration

You need to edit the /etc/nginx/nginx.conf file:
# vi /etc/nginx/nginx.conf
Your virtual hosts configs are located in /etc/nginx/conf.d/ directory:
# ls -l /etc/nginx/conf.d/
Sample outputs:

-rw-r--r--  1 root  root    342 May  9 17:48 default.conf

Find out your serer IP address, run ifconfig command or ip command:
# ip a
# ifconfig -a

Fig.02: Finding out your IP address on Alpine Linux
Fig.02: Finding out your IP address on Alpine Linux

Note down the IP address I recommend that you create your virtual host entry in /etc/nginx/conf.d/ directory. For example, I am going to create a virtual entry for domain called as follows:
# vi /etc/nginx/conf.d/

server {
     # server ip #
     # virtual server name i.e. domain name #
     # document root #
     root        /home/www;
     # log files
     access_log  /var/log/nginx/www.cyberciti.biz_access.log;
     error_log   /var/log/nginx/www.cyberciti.biz_error.log;
     # cache files on browser level #
     # Directives to send expires headers and turn off 404 error logging. #
     location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;

I suggest you read the Nginx wiki for all other config options.

Step 4: Start the Nginx server

Make sure the nginx starts when system reboots:
# rc-update add nginx default
Sample outputs:

 * service nginx added to runlevel default

Type the following command to start the nginx server:
# /etc/init.d/nginx start
# rc-service nginx start
Sample outputs:

 * Caching service dependencies ...                              [ ok ]
 * /run/nginx: creating directory
 * /run/nginx: correcting owner                                  [ ok ]
 * Starting nginx ...          

Command to restart nginx server

# rc-service nginx restart

Command to stop the nginx server

# rc-service nginx stop

Command to see status of the nginx server

# rc-service nginx status

Step 5: Viewing log files

The default log files can be views with the help of grep command/egrep command/more command/tail command:
# less /var/log/nginx/error.log
# less /var/log/nginx/access.log
# tail -f /var/log/nginx/www.cyberciti.biz_access.log
# grep 'error' /var/log/nginx/www.cyberciti.biz_error.log

Finally, you must configure logrotate in Alpine Linux to rotate Nginx log files.

Verifying that Nginx is running

Type the following pgrep command:
# pgrep nginx
OR use the ps command along with grep command
# ps aux | grep "[n|N]ginx"
Sample outputs:

27876 root       0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
27877 nginx      0:00 nginx: worker process
27878 nginx      0:00 nginx: worker process
27879 nginx      0:00 nginx: worker process
27880 nginx      0:00 nginx: worker process
27882 nginx      0:00 nginx: worker process
27883 nginx      0:00 nginx: worker process
27884 nginx      0:00 nginx: worker process
27885 nginx      0:00 nginx: worker process

Verifying that Nginx port is open

Use the netstat command:
# netstat -tulpn | grep :80
Sample outputs:

tcp   0  0*     LISTEN      27876/nginx.conf
tcp   0  0 :::80        :::*          LISTEN      27876/nginx.conf
This entry is 1 of 4 in the Installing Linux, Nginx, MySQL/MariaDB, PHP (LEMP stack) in Alpine Linux series. Keep reading the rest of the series:
  1. Install Nginx On Alpine Linux
  2. Install PHP7-fpm On Alpine Linux
  3. How to install and configure logrotate
  4. How to install Letsencrypt free SSL/TLS for Nginx certificate on Alpine Linux

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.