How To Install Nginx web server on Alpine Linux

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 created using PHP/Python. Nginx can also act as a reverse proxy and load balancer. This tutorial shows how to install nginx on Alpine Linux.

ADVERTISEMENTS

Install Nginx web server on Alpine Linux

Let us see all steps and commands in details to install Nginx, create users and set up your first web site 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

Where,

  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
OR
# 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 10.114.13.11. 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 www.cyberciti.biz as follows:
# vi /etc/nginx/conf.d/www.cyberciti.biz.conf

server {
     # server ip #
     listen      10.114.13.11:80;
 
     # virtual server name i.e. domain name #
     server_name www.cyberciti.biz;
 
     # 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
OR
# rc-service nginx start
OR
# 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 0.0.0.0:80   0.0.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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • Ramesh Apr 2, 2018 @ 7:25

    This was useful. How do I add PHP 7 for my Nginx server?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.