password protect the website running on nginx php fpm

Given:
On ubuntu system

nginx config file location

/etc/nginx/sites-available/default

Web site root location

/usr/share/nginx/www/mysite.com/

htpasswd is installed, if not, do

sudo apt-get install apache2-utils

1. Create users and passwords

sudo htpasswd -c /usr/share/nginx/www/mysite.com/.htpasswd admin_user
sudo htpasswd  /usr/share/nginx/www/mysite.com/.htpasswd admin_user2

2. Add these to the nginx config file

auth_basic "Restricted Area”;
auth_basic_user_file /usr/share/nginx/www/mysite.com/.htpasswd;

For example, if the config file is /etc/nginx/sites-available/default, open it

vim /etc/nginx/sites-available/default

Find the “location /“ and add these two lines like this

  location / {
      #some other configurations…
      auth_basic "Restricted Area”;                               
      auth_basic_user_file /usr/share/nginx/www/mysite.com/.htpasswd;
  }

3. Reload nginx configuration and restart nginx

sudo /etc/init.d/nginx reload
sudo service php5-fpm restart

Search within Codexpedia

Custom Search

Search the entire web

Custom Search