如何为您的网站设置登录保护区

您已经学会了如何建立网站。下一步是什么?为您的网站创建一个受密码保护的区域对于确保您的内容安全至关重要。幸运的是,它比您想象的要容易!

入门

有很多方法可以对网站进行密码保护。您可以设置一个在线数据库。

今天,我将向您展示如何使用Web服务器对网站进行密码保护。

您将需要一个Apache Web服务器来进行跟踪沿。许多其他Web服务器也提供类似的功能,但是所需的配置步骤可能有所不同。

Apache是​​地球上最受欢迎的Web服务器之一,顾名思义,它可以提供Web页面。您可能会听到的一个常见短语是 LAMP ,它代表Linux,Apache,MySQL和PHP / Python / Perl。您现在不需要任何编程语言或数据库,也不需要Linux-Windows变体称为 WAMP 堆栈。

现在有很多变体,尽管许多网站都使用PostgreSQL之类的数据库引擎或NGINX之类的Web服务器,但是LAMP比LEMP,LAPP,LNMP或LNPP容易说得多,因此它可以被看作是通俗易懂的词组。

如果您使用虚拟主机。

什么是.htaccess文件?

Htaccess, or hypertext access is a configuration file. It’s used to configure Apache to your needs. It works on a per-directory basis, so you can have one set of rules and configurations for your media hosting, and another totally different set for your blog. Htaccess files are called so after their name. An htaccess file is a text file called .htaccess. Notice how it does not have a name, rather the extension is called htaccess. This may be hidden by default in Windows, so take a look at how to view hidden files and folders in Windows if you’re having trouble.

这是htaccess可以做的一些很酷的事情:

  • 即时压缩页面。
  • 防止图像热链接。
  • 提供自定义错误页面。
  • 您可能已经看到默认错误页面。他们完成了工作,但是看起来很基础:

    使用Apache,您可以使这些页面变得更加漂亮,并提供其他功能!

    Configure htaccess

    现在您知道了这是什么,让我们设置htaccess。首先,您需要启用它,方法是编辑 httpd.conf 文件,该文件是另一个Apache配置文件。找到以下行:

    <Directory "/var/www/htdocs">

    目录路径( / var / www / htdocs )是网站根目录的位置。如果您进行了更改,则可能会有所不同。从下面更改以下行:归档到您喜欢的任何位置将其放在顶层将使用密码保护您的整个网站,或者您可以将其放置在特定目录中,而仅用它来保护该文件夹。

    此操作有两个关键步骤。 AuthName 会显示给尝试登录的所有用户。在这种情况下,它设置为“ MUO Secret Area",但是您可以将其更改为所需的任何内容。 AuthUserFile 需要指向有效的 .htpasswd 文件。该文件需要包含以下格式的用户名和密码列表:

    user:password

    就是这样!您的网站或目录现已受密码保护。这是当提示您输入凭据时的样子:底部。最重要的是:

    Allow from 127.0.0.1

    这告诉Apache允许免费访问IP地址 127.0.0.1 。来自不同IP地址的任何人都需要提供有效的用户名和密码。确保将Pi或您希望提供访问权限的任何其他设备的IP地址更改为127.0.0.1。

    如果您未正确登录或没有登录,则为默认错误页面正确的IP地址

    性能提示

    Htaccess provides a brilliant way to configure a variety of things on your web server, particularly if you don’t have much control over the rest of the server. It’s easy to get carried away however, and reduce the performance of your website significantly. If you’re able to, consider using PHP or any other language to perform complex tasks, and save htaccess for when your requirements are smaller, or you don’t have full control of the server.

    您今天学到了什么新东西吗?您最喜欢的htaccess技巧是什么?在下面的评论中让我们知道!

    标签: 密码 Web开发