This post will guide you how to install and configure Nginx Web Server On a Alpine Linux. How do I setup a Nginx Web Server on your Alpine Linux Docker VM.
- What is Nginx?
- Step1: Installing Nginx Package
- Step2: Creating User And Home Directory For Nginx Server
- Step3: Configuring Nginx Server
- Step4: Starting Nginx Server
What is Nginx?
Nginx is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and first publicly released in 2004.
Step1: Installing Nginx Package
You need to install Nginx package with the following apk commands firstly, type:
# apk update # apk add nginx
outputs:
bash-5.0# apk update fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz v3.10.2-46-gbb3e7ac09b [http://dl-cdn.alpinelinux.org/alpine/v3.10/main] v3.10.2-42-g95d37f7648 [http://dl-cdn.alpinelinux.org/alpine/v3.10/community] OK: 10334 distinct packages available bash-5.0# apk add nginx (1/2) Installing pcre (8.43-r0) (2/2) Installing nginx (1.16.1-r0) Executing nginx-1.16.1-r0.pre-install Executing busybox-1.30.1-r2.trigger OK: 21 MiB in 23 packages
Step2: Creating User And Home Directory For Nginx Server
You also can create a user(mynginx) and home directory(/home/mynginx/) with the following command:
$ adduser -g 'Nginx www user' -h /home/mynginx/
outputs:
bash-5.0# adduser -g 'Nginx www user' -h /home/mynginx mynginx
Changing password for mynginx
New password:
Bad password: too weak
Retype password:
passwd: password for mynginx changed by root
Note: the home directory can be used to store files for your Nginx web server. it will be used when you configuring Nginx server.
Step3: Configuring Nginx Server
Once Nginx package is installed on your Alpine Linux and User account and Home directory are also created. you can create a virtual hosts configuration file of Nginx server called www.mytest.com.conf under /etc/nginx/conf.d/ directory with your vi or vim text editor. type:
# vi /etc/nginx/conf.d/www.mytest.com.conf
then add the following line into the file:
server { listen 80;; root /home/mynginx; index index.html; server_name www.mytest.com; }
Save and close the file.
Step4: Starting Nginx Server
Now you need to start the Nginx server with the following command:
/ # /etc/init.d/nginx start
or
/ # service nginx start
Outputs:
/ # service nginx start
* WARNING: nginx is already starting
If you got the following error message: “/bin/sh: ./etc/init.d/nginx: not found“, then you need to install another package called openrc, type:
# apk add openrc --no-cache
Outputs:
/ # apk add openrc --no-cache
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/2) Installing openrc (0.41.2-r1)
Executing openrc-0.41.2-r1.post-install
(2/2) Installing openrc-bash-completion (0.41.2-r1)
Executing busybox-1.30.1-r2.trigger
OK: 23 MiB in 25 packages
If you want to check the status of Nginx server, and you can use the following command:
# /etc/init.d/nginx status