Set Up Password Authentication with Apache/Nginx

Updated 10 March 2023

In this article, we will learn how to protect your website using a password authentication feature, so that only authorized users can access the website.

Prerequisites

To complete this tutorial, you will need:

  • Apache/Nginx
  • Ubuntu OS
  • sudo access

CASE 1: If you are using an Apache webserver

Step 1 – Install the Apache server and the required packages.

Step 2. Creating the Password File

The htpasswd command will allow us to create a password file that Apache can use to authenticate users.

We will create a hidden file for this purpose called .htpasswd within our /etc/apache2 configuration directory.

The first time we use this utility, we need to add the -c option to create the specified passwdfile.

We specify a username (webkul in this example) at the end of the command to create a new entry within the file:

it will ask you to set the New Password and just press enter and retype your Password

Step 3. Add another user

Leave out the -c argument for any additional users you wish to add so you don’t overwrite the file:

Now, We can see the username and encrypted password for each record:

Using below command

Step 4. Configuring Apache Password Authentication

Edit the Apache configuration and add password protection to the virtual host file

Copy the below lines and paste your vhost file

Just Like this

Save and close the file using Press “Esc” and :wq

Before restarting the web server, you can check the configuration with the following command:

If everything checks out and you get Syntax OK as output, you can restart the server

Now, Go to the browser and hit your domain, your page will be protected with a username and password.

CASE 2: If you are using Nginx

You need to follow Steps 2 and Step 3 as mentioned above and then follow the below-mentioned steps –

Step 3 – Just copy the below content and paste it into your nginx vhost file

Step 4 – Open your nginx vhost file using the command –

Save and close the file using Press “Esc” and :wq

Before restarting the web server, you can check the configuration with the following command:

If everything checks out and you get Syntax OK as output, you can restart the server

Now, Go to the browser and hit your domain

Conclusion

In this way, you can either project your site or you can provide limited access to the site when you are in the development phase.

Need Support?

Thank You for reading this Blog!

For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at https://webkul.uvdesk.com/en/.

You may also visit our CS-Cart development services and quality CS-Cart Extensions.

For further help or query, please contact us or raise a ticket.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home