Skip to content

Instantly share code, notes, and snippets.

@facsiaginsa
Last active April 11, 2020 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facsiaginsa/ba5a48f676fb5c3d9fa958479f758649 to your computer and use it in GitHub Desktop.
Save facsiaginsa/ba5a48f676fb5c3d9fa958479f758649 to your computer and use it in GitHub Desktop.
This tutorial is for making a simple authentication on your site via nginx using mysql.

How to Create MySQL Authentication on NGINX

This tutorial is for making a simple authentication on your site via nginx using mysql.

This tutorial is assuming that you already have:

  1. nginx installed
  2. have a virtual host already setup on nginx config
  3. mysql installed

Get Sudo Privilege

sudo su

Install Required Package

apt-get install nginx-extras libpam-mysql

Add Configuration on NGINX

open your virtual host config file

nano /etc/nginx/site-available/your-site

add this statement on your server configuration

auth_pam "Restricted Zone";
auth_pam_service_name "your-service-name";

Add Configuraion on PAM

create file that match your-service-name

nano /etc/pam.d/your-service-name

add this statement

auth required pam_mysql.so user=db_user passwd=db_pass host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=3
account sufficient pam_mysql.so user=db_user passwd=db_pass host=127.0.0.1 db=mail table=users usercolumn=email passwdcolumn=password crypt=3

note that you must adjust this statement based on your mysql host, user, password, table, and coloumn you want to use. This statement works with md5 password encryption.

Restart NGINX

service nginx restart

all done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment