Add Basic Auth to Local site

I need to setup basic authentication in order to test something (to match a client site setup). Is it possible to do this with Local? If so, how?

Hi @briandichiara,

As long as your site is using the Custom environment, you can easily customize the web server config for your Local site and add Basic Auth.

I just tested the example in this article by nginx and it worked: https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/ … Just note that some of the quotes in the config examples are actual quotation marks () and not double quotes (") which will cause syntax errors in the nginx config.

Here’s what I did to test this:

  1. Right-clicked on the Local site in the sidebar and clicked on “Open Site SSH”
  2. Ran apt-get update && apt-get install -y apache2-utils
  3. Ran mkdir -p /etc/apache2/
  4. Ran htpasswd -c /etc/apache2/.htpasswd EXAMPLE_USER and entered a password
  5. Closed the Terminal window
  6. Browsed to the Local site’s conf/nginx folder and added the location /status block from the example to site.conf
  7. Restarted the Local site
2 Likes

Hello @clay,

Does this method still apply to Local 5.* and above?

Hey @Mamaduka,

The example above will only work on Linux. Here’s how you can configure Basic Auth on macOS with Local Lightning.

  1. Use htpasswd to add a user to an .htpasswd file in the site’s app directory. Example command (be sure to adjust the path to the site and change EXAMPLE_USER):

    htpasswd -c ~/Local\ Lightning\ Sites/basic-auth/app/.htpasswd EXAMPLE_USER
    
  2. Add the following to the site’s conf/nginx/includes/wordpress-single.conf.hbs right before the try_files directive (edit wordpress-multi.conf.hbs if the site is multisite)

    auth_basic           "Please log in";
    auth_basic_user_file /PATH/TO/SITE/app/.htpasswd; 
    

    So, for me, conf/nginx/includes/wordpress-single.conf.hbs becomes…

     # WordPress single blog rules.
     # Designed to be included in any server {} block.
    
     # This order might seem weird - this is attempted to match last if rules below fail.
     # http://wiki.nginx.org/HttpCoreModule
     location / {
     	auth_basic           "Please log in";
     	auth_basic_user_file "/Users/claygriffiths/Local Lightning Sites/basic-auth/app/.htpasswd"; 
    
     	try_files $uri $uri/ /index.php$is_args$args;
     }
    
     # Add trailing slash to */wp-admin requests.
     rewrite /wp-admin$ $resolved_scheme://$host$uri/ permanent;
    
    
  3. Restart the site so the config takes effect

1 Like

Thanks for the update Clay.

1 Like