1. apapche user authentication: you need to enter a user name and password when you visit the website, and you can access it only after the authentication is passed, which increases the security: for example, manage the access to the background:
1. First, make authentication for the root directory: that is to say, when you visit the website, you will be prompted with the user name and password: you can access:
1: You need to edit the virtual host configuration file: / usr/local/apapche2.4/conf/extra/httpd-vhosts.conf: verify the first virtual host as follows:
[root@localhost_002 111.com]# vim /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com #Define administrator mailbox: DocumentRoot "/data/wwwroot/111.com" #The root directory of the website should be the same as the directory of the following validation: ServerName www.111.com #Website domain name: only one: ServerAlias www.example.com #Website alias: there can be multiple: #The certification is as follows: <Directory /data/wwwroot/111.com> #Specify the directory for certification: AllowOverride AuthConfig #Equivalent to opening the certified switch: AuthName "111.com user auth" #The user-defined authentication name is not very useful. It will be displayed on the authentication page: AuthType Basic #Certification type, generally Basic: AuthUserFile /data/.htpasswd #Specify the location of the password file, which needs to be generated manually with the command later: require valid-user #Specify the users to be authenticated as all users defined in auth: </Directory> #End of certification: ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost>
2: Create the password file defined in the above file (httpd-vhosts.conf): as follows:
[root@localhost_002 111.com]# /usr/local/apapche2.4/bin/htpasswd -m /data/.htpasswd lisi New password: Re-type new password: [root@localhost_002 111.com]# /usr/local/apapche2.4/bin/htpasswd -m /data/.htpasswd zhansan New password: Re-type new password: Adding password for user zhansan [root@localhost_002 111.com]# cat /data/.htpasswd lisi:$apr1$Ju.ZWThz$3aMSzII6cDp6xXCNm.a2A/ zhansan:$apr1$r8DvZQBQ$qm208cGSaTYQfP2uaW1Wa0
Two parameters are used: - c: create, create: - m: md5 encryption:
As shown in the above figure, we have created two users, Zhan and lisi, and the second user does not need to add the - c parameter:
3: Test and reload configuration file after configuration: - t graceful
[root@localhost_002 111.com]# /usr/local/apapche2.4/bin/apachectl -t Syntax OK [root@localhost_002 111.com]# /usr/local/apapche2.4/bin/apachectl graceful
4: Test: crul-x192.168.149.130:80-I
[root@localhost_002 111.com]# curl -x192.168.149.130:80 www.111.com -I HTTP/1.1 401 Unauthorized Date: Sun, 26 Aug 2018 16:29:26 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1
It can be seen from the above figure that the user status code 401 indicates that the currently accessed content page needs to be verified:
Access with user and password authentication: crul - x192.168.149.130:80 - uzhansan:nihao123! www.111.com -I
[root@localhost_002 111.com]# curl -x192.168.149.130:80 -uzhansan:nihao123! www.111.com -I HTTP/1.1 200 OK Date: Sun, 26 Aug 2018 16:36:03 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Content-Type: text/html; charset=UTF-8
As shown in the above figure, the status code is 200 OK, and the website can be accessed normally:
Of course, it can also be accessed by browser, but the hosts file bound to the physical host: C:\Windows\System32\drivers\etc\hosts: IP address domain name