# Set up Password Authentication with NGINX

Learn how to set up a basic HTTP authentication system. This will allow you to password protect particular directories on your website, so that they can only be accessed after logging in with a username and password.

This form of password protection is a fast and easy way to provide a basic level of security via access authorization to a website.

## Requirements

- A server running Linux (Ubuntu 16.04 or CentOS 7)
- NGINX installed and running.

## Create the Password File with OpenSSH

Your server will most likely already have OpenSSH installed. If not, you can install it with the commands:

**CentOS 7:**

```none
sudo yum install openssh openssh-server openssh-clients openssl-libs
```

**Ubuntu 16.04:**

```none
sudo apt-get install openssh-server
```

Use the following command to create a file named .htpasswd in the /etc/nginx directory:

```none
sudo sh -c "echo -n '[username]:' >> /etc/nginx/.htpasswd"
```

For example, to add the user **jdoe** the command is:

```none
sudo sh -c "echo -n 'jdoe:' >> /etc/nginx/.htpasswd"
```

Then add a password for this user with the command:

```none
sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"
```

You will be prompted to enter the password twice to confirm it.

## Set Up Password Authentication in NGINX

The next step is to add the password authentication directives to the NGINX configuration file for the domain on which you are installing Joomla.

In most cases, the domain's configuration file will be located in **/etc/nginx/conf.d** on CentoS 7, and **/etc/nginx/sites-available** on Ubuntu 16.04.

Edit this file:

**CentOS 7:**

```none
sudo nano /etc/nginx/conf.d/example.com.conf
```

**Ubuntu 16.04:**

```none
sudo nano /etc/nginx/sites-available/example.com.conf
```

You will add the following lines to this file:

```none
auth_basic "Password Required";
    auth_basic_user_file /etc/nginx/.htpasswd;
```

---

### Note

These two directives will provide a basic password restriction to the specified directory. NGINX offers many ways you can set up password protection for various files and directories, including the ability to filter by file type. If you want to set up a more complicated system for password-protection, consult the [official NGINX documentation](http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html "NGINX") for more details.

---

To protect the entire site, put the directives inside the existing location / command block. If no command block exists, add one:

```none
location / {
    auth_basic "Password Required";
    auth_basic_user_file /etc/nginx/.htpasswd;
}
```

If you wish to password-protect a sub-directory, specify that directory instead:

```none
location /admin {
    auth_basic "Password Required";
    auth_basic_user_file /etc/nginx/.htpasswd;
}
```

Be sure to add this location block **inside** the server block. For example, if your configuration file looks like this:

```none
server {
    listen       80;
    server_name  example.com;
    root /usr/share/nginx/example.com/html/;
    index index.php index.html index.htm;
}
```

After adding the new section, it will look like this:

```none
server {
        listen             80;
        server_name    example.com;
        root /usr/share/nginx/example.com/html/;
        index index.php index.html index.htm;
location / {
        auth_basic "Password Required";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}
```

Save and exit the file, then restart NGINX:

```none
sudo nginx -s reload
```

To test the configuration, visit the URL in a browser. If the configuration is set up correctly, you will see a pop-up message asking you to enter the username and password.


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/nginx-password-authentication/](https://www.ionos.com/digitalguide/server/configuration/nginx-password-authentication/) for AI/LLM consumption.