Blog

03/06/2019

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.

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

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

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 sxi.io as follows:
# vi /etc/nginx/conf.d/sxi.io.conf

server {
     # server ip #
     listen      10.114.13.11:80;
 
     # virtual server name i.e. domain name #
     server_name sxi.io;
 
     # document root #
     root        /home/www;
 
     # log files
     access_log  /var/log/nginx/sxi.io_access.log;
     error_log   /var/log/nginx/sxi.io_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;
     }
}

server { # server ip # listen 10.114.13.11:80; # virtual server name i.e. domain name # server_name sxi.io; # document root # root /home/www; # log files access_log /var/log/nginx/sxi.io_access.log; error_log /var/log/nginx/sxi.io_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/sxi.io_access.log
# grep 'error' /var/log/nginx/sxi.io_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

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

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.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...