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:
- Right-clicked on the Local site in the sidebar and clicked on “Open Site SSH”
- Ran
apt-get update && apt-get install -y apache2-utils
- Ran
mkdir -p /etc/apache2/
- Ran
htpasswd -c /etc/apache2/.htpasswd EXAMPLE_USER
and entered a password - Closed the Terminal window
- Browsed to the Local site’s
conf/nginx
folder and added thelocation /status
block from the example tosite.conf
- Restarted the Local site
Hey @Mamaduka,
The example above will only work on Linux. Here’s how you can configure Basic Auth on macOS with Local Lightning.
-
Use
htpasswd
to add a user to an.htpasswd
file in the site’sapp
directory. Example command (be sure to adjust the path to the site and changeEXAMPLE_USER
):htpasswd -c ~/Local\ Lightning\ Sites/basic-auth/app/.htpasswd EXAMPLE_USER
-
Add the following to the site’s
conf/nginx/includes/wordpress-single.conf.hbs
right before thetry_files
directive (editwordpress-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;
-
Restart the site so the config takes effect
Thanks for the update Clay.