Apache常见配置
Posted tanshouke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache常见配置相关的知识,希望对你有一定的参考价值。
一、yum安装与配置
1.1安装:
[root@apache ~]# yum install http* -y
[root@apache ~]# echo "test01" >>/var/www/html/index.html
[root@apache ~]# firewall-cmd --permanent --add-service=http
[root@apache ~]# firewall-cmd --reload
[root@apache ~]# systemctl enable httpd.service
[root@apache ~]# systemctl restart httpd.service
1.2测试验证:
[root@apache ~]# curl localhost:80
test01
[root@apache ~]#
二、配置虚拟主机
2.1创建内容:
[root@apache ~]# mkdir /srv/{default,example}/www -p
[root@apache ~]# echo "test02" > /srv/default/www/index.html
[root@apache ~]# echo "test03" > /srv/excample/www/index.html
2.2如果开了SELinux,需配置安全上下文:
[root@apache ~]# semanage fcontext -a -t ‘httpd_sys_content_t‘ ‘/srv(/.*)?‘
[root@apache ~]# restorecon -Rv /srv/
2.3添加配置文件
[root@apache ~]# vim /etc/httpd/conf.d/my.conf
输入:
<VirtualHost *:80>
DocumentRoot "/srv/default/www"
</VirtualHost>
<Directory "/srv/default/www">
Require all granted
</Directory>
2.4验证:
[root@apache ~]# systemctl restart httpd.service
[root@apache ~]# curl localhost:80
test02
[root@apache ~]#
2.5添加配置:
[root@apache ~]# vim /etc/httpd/conf.d/my.conf
<VirtualHost *:80>
DocumentRoot "/srv/default/www"
</VirtualHost>
<Directory "/srv/default/www">
Require all granted
</Directory>
<VirtualHost *:80>
ServerName www.test03.com 域名
ServerAlias test03 别名
DocumentRoot "/srv/example/www" 访问主页(默认进入index.html)
</VirtualHost>
<Directory "/srv/example/www">
Require all granted
</Directory>
2.6修改hosts:
[root@apache ~]# cat /etc/hosts
192.168.247.17 www.test03.com
192.168.247.17 test03
2.7测试验证:
[root@apache ~]# curl localhost:80
test02
[root@apache ~]#
[root@apache ~]# curl www.test03.com:80
test03
[root@apache ~]# curl test03:80
test03
[root@apache ~]#
三、控制访问
3.1添加配置:
[root@apache ~]# cat /etc/httpd/conf.d/my.conf
#<VirtualHost *:80>
#DocumentRoot "/srv/default/www"
#</VirtualHost>
#<Directory "/srv/default/www">
# Require all granted
#</Directory>
<VirtualHost *:80>
ServerName www.test03.com
ServerAlias test03
DocumentRoot "/srv/example/www"
</VirtualHost>
<Directory "/srv/example/www">
Require all granted
AllowOverride AuthConfig
</Directory>
3.2设置秘钥访问:
[root@apache ~]# cd /srv/example/www
[root@apache www]# vim .htaccess
输入:
AuthName testACL
Authtype Basic
AuthUserfile /srv/example/www/.htpasswd
require user tansk
3.3创建密码:
[root@apache www]# htpasswd -cm .htpasswd tansk
New password: (123456)
Re-type new password:(123456)
四、开启个人主页
(已关闭SELinux,否则需设置:setsebool -P httpd_enable_homedirs=on )
4.1修改配置:
[root@apache ~]# vim /etc/httpd/conf.d/userdir.conf
设置:
<IfModule mod_userdir.c>
UserDir public_html
</IfModule>
4.2创建主页内容
[root@apache ~]# chmod -R 755 /home/tansk/
[root@WWW ~]# su tansk
[tansk@WWW root]$ cd
[tansk@WWW ~]$ mkdir public_html
[tansk@WWW ~]$ echo "this is tansk home" > public_html/index.html
[tansk@WWW ~]$ exit
4.3测试验证:
[root@apache ~]# systemctl restart httpd.service
[root@apache ~]# curl http://192.168.247.17/~tansk/
this is tansk home
[root@apache ~]#
4.4给个人用户主页设置密码
使用htpasswd命令生成密码数据库,并将tansk用户加进去
[root@apache ~]# htpasswd -c /etc/httpd/passwd.txt tansk
New password: (123456)
Re-type new password: (123456)
Adding password for user tansk
[root@apache ~]#
4.5编辑httpd子配置文件
vim /etc/httpd/conf.d/userdir.conf
修改为:
<Directory "/home/*/public_html">
#AllowOverride FileInfo AuthConfig Limit Indexes
#Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
#Require method GET POST OPTIONS
AllowOverride all
authuserfile "/etc/httpd/passwd.txt"
#提示信息
authname "tansk’s home"
authtype basic
#需要认证的用户
require user tansk
</Directory>
4.6登录验证
以上是关于Apache常见配置的主要内容,如果未能解决你的问题,请参考以下文章