Apache · December 17, 2023

Apache for Newbie: Set up Apache with mod_authz_user

Apache for Newbie: Set up Apache with mod_authz_user

Apache is one of the most popular web servers in the world, known for its flexibility and robustness. If you are new to Apache and want to learn how to set it up with mod_authz_user, this article is for you. Mod_authz_user is an Apache module that allows you to control access to your web server based on user authentication. It provides a simple and effective way to secure your website and protect sensitive information.

Step 1: Install Apache

The first step is to install Apache on your server. If you are using a Hong Kong VPS hosting service like Server.HK, you can easily install Apache using the control panel provided by your hosting provider. Alternatively, you can install Apache manually by following the instructions on the Apache website.

Once Apache is installed, you can start and enable it by running the following commands:

sudo systemctl start apache2
sudo systemctl enable apache2

Step 2: Enable mod_authz_user

To enable mod_authz_user, you need to edit the Apache configuration file. The location of this file may vary depending on your operating system, but it is usually located at /etc/apache2/apache2.conf or /etc/httpd/conf/httpd.conf.

Open the configuration file in a text editor and look for the line that starts with #LoadModule authz_user_module. Remove the # at the beginning of the line to uncomment it. Save the file and exit the text editor.

Step 3: Configure Access Control

Now that mod_authz_user is enabled, you can configure access control for your web server. Open the Apache configuration file again and add the following lines:

<Directory /var/www/html>
    AuthType Basic
    AuthName "Restricted Content"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

Let's break down what each line does:

  • <Directory /var/www/html>: This specifies the directory for which the access control rules apply. Change it to the appropriate directory for your website.
  • AuthType Basic: This sets the authentication type to Basic, which means that users will be prompted for a username and password.
  • AuthName "Restricted Content": This sets the authentication realm, which is the name that will be displayed in the authentication dialog.
  • AuthUserFile /etc/apache2/.htpasswd: This specifies the location of the password file. You need to create this file and add user credentials to it.
  • Require valid-user: This requires a valid username and password to access the specified directory.

Save the configuration file and exit the text editor.

Step 4: Create User Credentials

Now it's time to create the password file and add user credentials to it. Run the following command to create the password file:

sudo htpasswd -c /etc/apache2/.htpasswd username

Replace username with the desired username. You will be prompted to enter and confirm a password for the user.

If you want to add more users, you can run the same command without the -c option:

sudo htpasswd /etc/apache2/.htpasswd another_username

Step 5: Restart Apache

Finally, restart Apache to apply the changes:

sudo systemctl restart apache2

Your Apache web server is now set up with mod_authz_user. Users will be prompted for a username and password when trying to access the restricted directory specified in the configuration file. Only users with valid credentials will be granted access.

Summary

In this article, we have learned how to set up Apache with mod_authz_user. We started by installing Apache and enabling the mod_authz_user module. Then, we configured access control by adding the necessary lines to the Apache configuration file. We also created user credentials and restarted Apache to apply the changes.

By following these steps, you can secure your website and protect sensitive information from unauthorized access. Apache with mod_authz_user is a powerful combination that allows you to control access to your web server with ease.

If you are looking for reliable VPS hosting in the Hong Kong, consider Server.HK. They offer high-performance Hong Kong VPS hosting solutions that are perfect for hosting your Apache web server. Visit their website at https://server.hk to learn more about their hosting services.