總網頁檢視次數

星期四, 7月 23, 2020

Password protecting your site with an .htaccess file

Overview

This article explains how to password protect your directory via SSH by creating an .htaccess and .htpasswd file. The following steps are covered in this article.
Using the panel to password protect your site
The easiest way to password protect your site is to use the tool in the DreamHost panel. Navigate to the Htaccess/WebDAV page. You can then set up password protection there.
No access to your .htaccess and .htpasswd files
However, please note that if you use the panel option, the .htaccess and .htpasswd files will be owned by the server. This means you will not be able to manually edit either of these files if you need to. Additionally, these instructions will overwrite any existing .htaccess file. Make sure to backup your existing .htaccess file before beginning these steps.
If you only need to password protect your site and will need access to your .htaccess and .htpasswd file at any time in the future, you should use the instructions in this article instead to manually create those files.

Creating the .htpasswd file

  1. Log into your server via SSH.
  2. Create an .htpasswd file in the directory you wish to password protect using the the htpasswd utility. For the first user, say user1, run the following:
    [server]$ htpasswd -c /home/username/example.com/.htpasswd user1
  3. Enter the password for the user. This creates a password for a user named 'user1'. The code in your .htpasswd file will show the encrypted password like this:
    user1:$apr1$bkS4zPQl$SyGLA9oP75L5uM5GHpe9A2
  4. Run it again (without the -c option) for any other users you wish to allow access to your directory.
  5. Set the permissions on this file to 644.
    [server]$ chmod 644 .htpasswd
View the following page for further information:

Creating the .htaccess file

Next, create an .htaccess file using the 'nano' editor:
Make sure to add this .htaccess file in the directory you wish to password protect. For example, if you are password protecting your entire site, it would go in your site's main directory:
  • example.com
If you're password protecting a subdirectory, it would go into that subdirectory.
  • example.com/members
[server]$ nano .htaccess

Code examples to add to the .htaccess file

Protect an entire directory

This example password protects an entire website directory. Make sure to change the lines in bold to your actual file path while changing to your username and domain name.
#Protect Directory
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user

Protect a single file

This example password protects a single file:
#Protect single file

AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user

Protect multiple files

This example protects multiple files:
#Protect multiple files

AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/.htpasswd
Require valid-user

Code to protect a WordPress subdirectory

Due to how WordPress routes all page requests, attempting to access a password protected subdirectory will throw a 404 Not Found error. To resolve this, you must an extra line to the .htaccess file to reference ErrorDocument.
This example protects a subdirectory named 'members'.
ErrorDocument 401 default
    
#Protect Directory
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/username/example.com/members/.htpasswd
Require valid-user

Force SSL (HTTPS) on the login prompt

By default, the login prompt you see is not encrypted. This means your password will be sent as plain text over http. In order to encrypt this login, you must add an SSL certificate to your domain. Once added, add the code below to force SSL when logging in.
This method prevents submission of an .htaccess password prompt on an unencrypted connection. If you wish to ensure that your server is only serving documents over an encrypted SSL channel, then you must use the SSLRequireSSL directive with the +StrictRequire Option enabled:

Step 1 — Adding code to your .htaccess file

Make sure the URL you enter next to SSLRequire is your site's base URL. Do not include 'www' in front of the URL if you're forcing 'www' to be removed in your panel.
If you're securing a subdirectory such as 'example.com/blog', this URL would still be 'example.com'.
Additionally, you can use any file you like for your 403 document. Below it is shown as 'error_redirect.php'. Change this to your chosen file.
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "example.com"

ErrorDocument 403 /error_redirect.php

error_redirect.php
> AuthType none

If you're only protecting a subdirectory

If you only want to protect a single subdirectory and not the whole site, specify the subdirectory in your .htaccess file as shown in the following code:
#Protect Directory
AuthName "Dialog prompt"
AuthType Basic
AuthUserFile /home/example_username/example.com/blog/.htpasswd
Require valid-user

SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire %{HTTP_HOST} eq "www.example.com"

ErrorDocument 403 /blog/error_redirect.php


  AuthType none

If your site is on a server running Ubuntu 14 (Trusty), make sure to change the ErrorDocument line to the full URL path. For example:
ErrorDocument 403 https://example.com/blog/error_redirect.php

Step 2 — Add code to your error_redirect.php file

Now that your .htaccess will redirect to your error page, you must put some code into this error page to correctly redirect back to your secure login. Add the following PHP code.

If you now try to log in, you''ll see both the URL and login prompt change to https://example.com.

Issue with renewing a 'Let's Encrypt' certificate

The code may cause a 'Let's Encrypt' certificate to not renew properly. If you have added a 'Let's Encrypt' certificate to your domain, make sure to disable the code below in your .htaccess file when your certificate is about to renew. Once renewed, you can re-enable the code below.

Recover crash ec2 instance by attaching it to new instance

1. attach the old volume to new instance in console
2. in new instance, mount the the old volume to a folder ..

If you can not mount your XFS partition with classical wrong fs type, bad superblock etc. error and you see a message in kernel logs (dmesg) like that:
XFS: Filesystem sdb7 has duplicate UUID - can't mount
you can still mount the filesystem with nouuid options as below:
mount -o nouuid /dev/sdb7 disk-7
But every mount, you have to provide nouuid option. So, for exact solution you have to generate a new UUID for this partition with xfs_admin utility:
xfs_admin -U generate /dev/sdb7
Clearing log and setting UUID
writing all SBs
new UUID = 01fbb5f2-1ee0-4cce-94fc-024efb3cd3a4
after that, you can mount this XFS partition regularly.

星期三, 7月 15, 2020

How to send meesage to slack channel

curl -X POST 
-H 'Content-type: application/json' 
--data '{"text":"Allow me to reintroduce myself!"}' YOUR_WEBHOOK_URL
 
 
 
curl -X POST 
-H 
--slient
--data-urlencode 
   "payload={\"text\": \" Hello $(sudo -u username /home/hello/tryme.sh)\"}"
YOUR_WEBHOOK_URL