First, the directory alias experiment
When apache accept requests by default DocumentRoot files in the directory will be sent to the client, not if you want to share a DocumentRoot directory files to the site, and want to stay in their original position without the need for mobile, then we can handle this situation by way of URL alias will point to a specific directory.
1, the main configuration file modified
In about 468 lines uncommented
[root@centos ~]# vim /usr/local/apache2/etc/httpd.conf
Include etc/extra/httpd-autoindex.conf
2, modify the child profile
[root@centos ~]# vim /usr/local/apache2/etc/extra/httpd-autoindex.conf
Alias /test/ "/tmp/test/"
<Directory "/tmp/test/">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
3, create a directory, and create files
[root@centos ~]# mkdir /tmp/test/
[root@centos test]# echo 123456 > 1.txt
4, browser access
Two, Apache user authentication
Sometimes, we need to give some special access settings for a user authentication mechanism, increase safety. For example, our personal website, usually have a management background, although the password management background in itself, but we have to be more secure, you can then set a layer of user authentication.
1, the main configuration file modified
[root@centos apache2]# vim /usr/local/apache2/etc/httpd.conf
<Directory "/usr/local/apache2/htdocs/admin"> #声明被保护目录
Options Indexes FollowSymLinks
AllowOverride All #开启权限认证文件.htaccess
Require all granted #允许访问
</Directory>
2, permission to create files in the specified directory
[root@centos apache2]# mkdir /usr/local/apache2/htdocs/admin
[root@centos admin]# vim .htaccess
AuthName "Welcome to admin manager" #提示信息
AuthType basic #加密类型
AuthUserFile /usr/local/apache2/htdocs/admin/admin.passwd #密码文件,文件名自定义。
require valid-user #允许密码文件中所有用户访问
3, create a password file in the specified directory, add the user allows access. (This has nothing to do user and system users)
[root@centos admin]# /usr/local/apache2/bin/htpasswd -c /usr/local/apache2/htdocs/admin/admin.passwd test1 #第一个用户使用 -c参数
New password:
Re-type new password:
Adding password for user test1
[root@centos admin]# /usr/local/apache2/bin/htpasswd -m /usr/local/apache2/htdocs/admin/admin.passwd test2 #再添加用户则使用 -m参数
New password:
Re-type new password:
Adding password for user test2
4, restart apache
[root@centos admin]# /usr/local/apache2/bin/apachectl -t
[root@centos admin]# /usr/local/apache2/bin/apachectl restart
5, visit
Third, Hosting experiment
Web hosting, also known as "web space" is the one running on the Internet physical servers into multiple "virtual" servers. Hosting technology greatly promote the application and popularization of network technology. At the same time the virtual host hire service economy has become a new form of the Internet age.
Categories Virtual Host:
based virtual hosts IP-: One server, multiple ip, set up multiple Web sites
based on the virtual host ports: a server, a ip, set up multiple sites, each using a different port network access
based on domain name Web host: a server, a ip, set up multiple sites, each using a different domain names (most common)
1, to prepare two domain names
www.qq.com
www.bb.com
2, modify the main configuration file
[root@centos admin]# vim /usr/local/apache2/etc/httpd.conf
Include etc/extra/httpd-vhosts.conf(大约在485行,取消注释)
3, modify the child profile
[root@centos admin]# vim /usr/local/apache2/etc/extra/httpd-vhosts.conf
<Directory "/usr/local/apache2/htdocs/qq"> #目录权限
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<VirtualHost 20.20.20.21:80> #虚拟主机配置
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs/qq"
ServerName www.qq.com
ErrorLog "logs/qq-error_log"
CustomLog "logs/qq-access_log" common
</VirtualHost>
<Directory "/usr/local/apache2/htdocs/bb">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<VirtualHost 20.20.20.21:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs/bb"
ServerName www.bb.com
ErrorLog "logs/bb-error_log"
CustomLog "logs/bb-access_log" common
</VirtualHost>
4, create a directory, and written content.
[root@centos admin]# mkdir /usr/local/apache2/htdocs/{qq,bb}
[root@centos admin]# echo thie is qq >/usr/local/apache2/htdocs/qq/index.html
[root@centos admin]# echo thie is bb >/usr/local/apache2/htdocs/bb/index.html
5, modify windows hosts file access test
20.20.20.21 www.qq.com
20.20.20.21 www.bb.com
6, visit
Fourth, the domain name to jump experiment
一个站点难免会有多个域名,而多个域名总得有一个主次,比如我的网站可以用两个域名访问: www.qq.com 和 www.bb.com 但大家发现不管我用哪个域名访问,最终都会跳转到 www.qq.com 上 来。这个行为就叫做域名跳转。
1、编辑主配置文件
[root@centos admin]# vim /usr/local/apache2/etc/httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so(大学约在153行,取消注释)
2、修改虚拟主机配置文件
root@centos admin]# vim /usr/local/apache2/etc/extra/httpd-vhosts.conf
<Directory "/usr/local/apache2/htdocs/bb">
Options Indexes FollowSymLinks
AllowOverride All #修改此处,在认证文件中,进行跳转。
Require all granted
</Directory>
<VirtualHost 20.20.20.21:80>
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/htdocs/bb"
ServerName www.bb.com
ErrorLog "logs/bb-error_log"
CustomLog "logs/bb-access_log" common
</VirtualHost>
3、创建规则匹配文件
[root@centos admin]# vim /usr/local/apache2/htdocs/bb/.htaccess
RewriteEngine on # 开启rewrite功能
RewriteCond %{HTTP_HOST} ^www.bb.com # 把以www.bb.com 开头的内容赋值给HTTP_HOST变量
RewriteRule ^(.*)$ http://www.qq.com/$1 [R=permanent,L]
# ^(.*)$ 指代客户端要访问的资源
# $1 把 .* 所指代的内容赋值到$1变量中
# R=permanent 永久重定向 = 301
# L 指定该规则为最后一条生效的规则,以后的不再生效
4、重启,访问
[root@centos ~]# /usr/local/apache2/bin/apachectl restart