Posts

Ngnix Docker setup Guide from scratch: Web 3 development Part 1

avatar of @expire0
25
@expire0
·
0 views
·
2 min read

Background: It’s easy to pull down an existing docker image from https://hub.docker.com/ that has NGINX pre-configured. This article is for those who wish to build their own Nginx docker image. This is the best way to avoid security flaws or running an outdated version of Nginx and the OS. A prime example of this is this docker image that I almost downloaded https://hub.docker.com/r/richarvey/nginx-php-fpm . It hasn’t been updated since 2020.

NGINX is a lightweight web server that can be used for web 3 applications and other applications or websites. So learning how to install and configure Nginx is a great start for anyone new to web 3 development. This will not go into detail about how to install Docker or how to write up a Dockerfile. This will be a two part series which includes installing Nginx, serving some basic content ,installing PHP , and if I can find the time. I will touch on some JavaScript configuration.

Prerequisites: Some basic knowledge of Linux ,vim and Docker containers

OS USED: CentOS Linux release 8.4.2105
Author : Expire0 for https://Expire0.dev

                         Build out the base image 
           

1 Pull down the latest Centos image from the docker. Run the below commands

  • docker pull centos:latest
  • docker run -d -it –name nginx docker.io/centos (This command will run the centos image in a container)

2 Notice from step one, we named the container “nginx”, This will make it easier to find. Next we’re going to drop into the container and install our base Nginx instance.

  • docker exec -it nginx bash (2a). Install any available updates for Centos
  • dnf update

3 Installing a Prebuilt CentOS/RHEL Package from the Official NGINX Repository.

Defined yum variables

  • echo “centos” > /etc/yum/vars/osname

  • echo “8” > /etc/yum/vars/release You may need to change this based on your centos version

  • vi /etc/yum.repos.d/nginx.repo

Add the following lines to nginx.repo.

[nginx] 
name=nginx repo 
baseurl=https://nginx.org/packages/mainline/$osname/$release/$basearch/ 
gpgcheck=0 
enabled=1 

run dnf to sync up the new repo

  • dnf update
  • dnf install nginx

4 Start nginx

  • nginx
    Verify nginx is running
curl -I 127.0.0.1 
Output  
HTTP/1.1 200 OK 
Server: nginx/1.14.1 

5 Nginx has been installed and ready to serve some content .Exit out of the container

  • exit

6 Save the running container as an image.

  • docker commit nginx nginx:latest

If you run “docker images”, you will now see your newly created docker image with Nginx installed. The next part We’ll focus on configuring Nginx ,installing PHP and JS. Thanks for reading and stay tune for the next part.