Root-Server Tutorial

ROOT-SERVER TUTORIAL

How to Install Dockerized nginx with MariaDB

This tutorial explains how to install docker and nginx with a mariaDB database in just a few minutes.

Author:

Peter Mesiha

Last updated:

19/04/2023

Introduction

In this "how to" tutorial you are going to set up your own nginx instance with a mariaDB. Nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, and as an open source project is free to use.

First we are going to uninstall any old docker engine and install the latest (can be skipped), then we'll create/edit the docker-compose file, and check all the containers and the setup.

Requirements

  • Docker
  • Docker Compose or docker-compose
  • Linux server

Step 1 - Uninstall old Docker Engine, CLI, containerd, and Docker Compose packages

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

Step 2 - Curl the script for installation from docker

curl -fsSL https://get.docker.com -o get-docker.sh

Step 3 - Dry run the script to check for complications

sudo sh ./get-docker.sh --dry-run

Step 4 - Run the script

sudo sh ./get-docker.sh

Step 5 - Verify the installation

docker --version

Step 6 - Create a folder to save the docker-compose file and cd into that folder

mkdir nginx-with-postgres
cd nginx-with-postgres

Step 7 - Copy the docker-compose file to your system

vim docker-compose.yml

Paste the content inside this file.

---
version: "3.8"
    
services:
   app:
      hostname: 'nginx'
      image: 'jc21/nginx-proxy-manager:latest'
      container_name: 'nginx-app'
      restart: 'unless-stopped'
      ports:
         ## These ports are in format <host-port>:<container-port>
         - '80:80' # Public HTTP Port
         - '443:443' # Public HTTPS Port
         - '81:81' # Admin Web Port
         ## Add any other Stream port you want to expose
         ## - '21:21' # FTP
      environment:
         DB_MYSQL_HOST: "db"
         DB_MYSQL_PORT: 3306
         DB_MYSQL_USER: "nginx-user"
         DB_MYSQL_PASSWORD: "changeMe.aHighlyComplexPasswordToKeepYouSecure"
         DB_MYSQL_NAME: "npm"
         ## Uncomment this if IPv6 is not enabled on your host
         ## DISABLE_IPV6: 'true'
      networks:
         - 'nginx-network'
      volumes:
         - 'nginx-app-data-vol:/data'
         - 'nginx-app-letsencrypt-vol:/etc/letsencrypt'
      depends_on:
         - db
   db:
      image: 'jc21/mariadb-aria:latest'
      container_name: 'nginx-db'
      restart: 'unless-stopped'
      environment:
         MYSQL_DATABASE: 'npm'
         MYSQL_USER: 'nginx-user'
         MYSQL_PASSWORD: 'changeMe.aHighlyComplexPasswordToKeepYouSecure'
         MYSQL_ROOT_PASSWORD: 'changeMe.aHighlyComplexPasswordToKeepYouSecure'
      networks:
         - 'nginx-network'
      volumes:
         - 'nginx-db-data-vol:/var/lib/mysql'

volumes:
   nginx-app-data-vol:
      name: 'nginx-app-data-vol'
   nginx-app-letsencrypt-vol:
      name: 'nginx-app-letsencrypt-vol'
   nginx-db-data-vol:
      name: 'nginx-db-data-vol'

networks:
   nginx-network:
      name: 'nginx-network'

Step 8 - Edit the docker-compose file

Edit the parts with 'changeMe.' and of course any other parts you want to modify, such as the ports exposed on the host machine.

Step 9 - Check the configuration

Make sure you have changed all the 'changeMe.' parts. Now you are good to go.

Step 10 - Create the containers

With docker-compose, it's fairly easy to set everything up.

Type:

docker compose up -d

Step 11 - Check the containers

Deployment should take a maximum of 2 minutes to be up and running as the app waits for the db and a successful health check. Type:

docker ps -a

You should wait till the nginx-db container is up and healthy and the nginx-app is running.

Step 12 - Check the URL

Now you can check your port 80 (or another port if you have changed the left side) with your browser at localhost:80, or serverIP:80 if you run it on a server in a different network or your provided URL inside the docker-compose file.

If you cannot reach the URL after a few minutes (3 to 4), you should check your ufw/ufw-docker to expose the ports through the firewall or check your proxy manager, if you use one.

Congratulations, you have created your own nginx server with its own db in just a few minutes.

You can now access the server on the 81 port to open the admin panel and start configuring the proxy.

Conclusion

As you have seen, setting up nginx with its own db instance is quite simple and straightforward.

License

MIT

Copyright (c) 2021 netcup

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Contributor's Certificate of Origin

By making a contribution to this project, I certify that:

  1. The contribution was created in whole or in part by me and I have the right to submit it under the license indicated in the file; or

  2. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same license (unless I am permitted to submit under a different license), as indicated in the file; or

  3. The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

  4. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the license(s) involved.

Published 19/04/2023 by Peter Mesiha

Submit your tutorial

Get 60€ netcup vouchers for every published tutorial.