11.18 Apache user authentication
- Background: When some content on the site does not want others to access to, can do user authentication. This way, when a user accesses the site, a dialog box pops up asking for a user and password to continue accessing the site.
- To implement the user authentication step:
vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf #把123. com The virtual host is edited into the following content
<virtualhost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<Directory/data/wwwroot/www.123.com> #指定认证的目录
AllowOverride authconfig #这个相当于打开认证的开关
AuthName "123.com user auth" #自定义认证的名字, not very useful
AuthType Basic//type of authentication #一般为Basic
AUTHUSERFILE/DATA/.HTPASSWD #指定密码文件所在位置
Require Valid-user #指定需要认证的用户为全部可用用户
</Directory>
</VirtualHost>
- /usr/local/apache2.4/bin/htpasswd-c-m/data/.htpasswd aming #设置aming用户的认证密码
- /usr/local/apache2.4/bin/apachectl-t #测试语法是否正确
- /usr/local/apache2.4/bin/apachectl Graceful #重新加载配置文件
- Verify that the setting is successful:
- Method 1 Test---The local Computer browser
Modify the C:\Windows\System32\drivers\etc\hosts file on the local computer, add a line 192.168.206.135 www.abc.com to bind the hosts (where IP is the IP of the Linux virtual machine), Then open the browser input www.abc.com test---Normal will pop up a dialog box, asking you to enter the account password, after the page is displayed.
- Method 2---Test directly in the Linux virtual machine:
curl-x127.0.0.1:80 www.123.com #此时应显示错误状态码为401
Curl-x127.0.0.1:80-uaming:123 www.123.com #此时应正确显示index. The contents of the HTML. 123 is the password that was previously set. The correct status code is 200 (not shown here)
- You can also authenticate against a single file
<virtualhost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<filesmatch admin.php>
AllowOverride authconfig
AuthName "123.com User auth"
AuthType Basic
authuserfile/data/.htpasswd
Require Valid-user
</FilesMatch>
</VirtualHost>
- Verify that the setting is successful:
curl-x127.0.0.1:80 www.123.com/admin.php #此时应显示错误状态码为401
curl-x127.0.0.1:80-uaming:123 www.123.com/admin.php #此时应正确显示admin. PHP content
11.19/11.20 Domain Jump
- Background: Sometimes our site is enabled a new domain name, the original domain name is no longer used, but in order to allow old users to access the old domain name can automatically go to the new site, then we need to do domain jump.
- Example: Jump the www.example.com domain name to 123.com
The configuration for the domain jump is as follows:
<virtualhost *:80>
DocumentRoot "/data/wwwroot/123.com"
ServerName 123.com
Serveralias www.example.com 2111.com
<ifmodule mod_rewrite.c> #需要mod_rewrite模块支持
Rewriteengine on//Open rewrite function
Rewritecond%{http_host}!^123.com$ #定义rewrite的条件, host name (domain name) is not 123.com satisfies the condition
Rewriterule ^/(.) $ http://123.com/$1 [r=301,l]
#定义rewrite规则, this rule does not execute until the above conditions are met. 301 means a permanent jump, 302 is a temporary jump, L is the last meaning, means only jump once.
</IfModule>
</VirtualHost>
- /usr/local/apache2.4/bin/apachectl-m|grep-i rewrite #检测是否加载rewrite_module (shared) module
- If there is no module, you need to edit the configuration file httpd.conf, delete the # in front of Rewrite_module (shared) to have Apache load the module
- Verify the Jump Status (status code is 301)
Curl-x127.0.0.1:80-i www.example.com/
11.21 Apache Access Log
- The access log records every request from the user, and the Apache log directory is/usr/local/apache2.4/logs.
- To view system pre-defined log formats:
vim/usr/local/apache2.4/conf/httpd.conf//Search Logformat
Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined
Logformat "%h%l%u%t \"%r\ "%>s%b" common
Change the current log format (changed from common to combined, and change the virtual host configuration file to the following:
<virtualhost *:80>
DocumentRoot "/data/wwwroot/123.com"
ServerName 123.com
Serveralias www.example.com 2111.com
Customlog "Logs/123.com-access_log" combined
</VirtualHost>
Reload the configuration file after the change:
/usr/local/apache2.4/bin/apachectl-t
/usr/local/apache2.4/bin/apachectl Graceful
- Access through curl to generate logging:
Curl-x127.0.0.1:80-i 123.com #
- To view the resulting log records:
Tail/usr/local/apache2.4/logs/123.com-access_log
Extended Learning:
Apache Virtual host opens PHP's short tag http://ask.apelearn.com/question/5370
2018-3-2 Linux Learning Notes