一、Apache默认登录用户是daemon,如何修改登录用户?

新建一个用户Zhangsan和一个组httpd,修改httpd主配置文件。我是存放在/usr/local/apache2/conf/http.conf的。

将User daemon改为User ZhangSan
Group daemon改为Group httpd

重启httpd,查看进程用户

二、加密
为网站设置加密,需提供账号和密码才能访问
修改httpd主配置文件。
vim /usr/local/apache2/conf/http.conf
将AllowOverride none改为AllowOverride all
将Require all denied改为Require all granted


然后在htdocs目录下新建隐藏配置文件.htaccess
cd /usr/local/apache2/htdocs
vim .htaccess //内容如下

重启httpd,然后用htpasswd(可以先用whereis htpasswd查看一下命令所在目录,直接用绝对路径,这样不会出现命令找不到的问题)命令生成上图中的/usr/local/apache2/bin/a.psd文件
/usr/local/apache2/bin/apachectl restart //重启httpd
/usr/local/apache2/bin/htpasswd -c /usr/local/apache2/bin/a.psd web //生成web用户,并为其设置密码
进浏览器访问,发现是需要账号密码登录的了
如果想要去掉加密功能,可以直接将配置文件里的AllowOverride设为none
三、本机开启httpd服务后,局域网内其他用户不能访问网站
经查是防火墙的原因
重新设置防火墙


重启防火墙

成功
四、限制访问网站的客户机地址
修改主配置文件
将Require all granted修改为
<requireall>
Require all granted
require ip 192.168.252.128 //设置此网站只能被192.168.252.128访问,如果设置这个地址不能访问,则在前面加not
</requireall>
按照上面的设置后,我再用本机访问网站地址,被拒绝了,用搭建这个网站的虚拟机来访问是可以的。


五、首页文件缺失
首页文件在主配置文件的设置模块为
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
如果首页文件index.html被删除的话,访问网站会直接显示出网站上的其他文件,方便但是可能不太安全。

如果想要关闭这个功能,在首页文件缺失的时候不暴露内容,
用参数Options Index FollowSynlinks来控制,修改主配置文件。去掉Indexes参数

重新进网站,已经不会显示其他文件了。

六、vhosts实现虚拟主机访问
vhosts功能开关在主配置文件中,所以先修改主配置文件,使得httpd-vhosts.conf文件生效,在httpd-vhosts.conf文件中配置虚拟机信息,然后配置主机的hosts,重启httpd,使配置生效。

vim /usr/local/apache2/conf/httpd.conf //修改主配置文件

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //配置虚拟主机信息

vim /etc/hosts //修改hosts

为两个虚拟主机添加首页文件。

重启httpd服务后,成功


七、本机可以访问虚拟主机域名,局域网内其他用户不可以
分析:
设定虚拟主机后,发现只能在本机输入域名访问才有效,在局域网内的其他用户都是无效的,仍然只能通过访问ip来进行。这个问题困扰了我好久,通过修改防火墙和vhosts配置问价多次仍不得要领。所以仔细把ip和域名的关系缕了一遍,终于给我找到了问题。因为我刚刚在本机上的hosts文件添加了ip和域名的对应关系,所以本机输入域名是可以找到ip,并进行解析的。但是在局域网内的其他主机上并没有这个对应关系,所以我在windows系统的hosts文件中增加了对应关系。windows系统的hosts文件存在这里:


成功!
八、设置网页文件存放目录
Apache的网页文件存放目录默认为htdocs,在主配置文件中修改目录权限。
[root@www htdocs]# vim /usr/local/apache2/conf/httpd.conf
DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
Options Indexes FollowSymLinks //Options指令通知Apache禁止使用目录索引,FollowSymLinks表示不允许使用符号链接
AllowOverride None //禁止用户对目录配置文件(.htaccess)进行重载 (修改)
Order deny,allow //更改访问权限的优先顺序,禁止默认访问的存在
Allow from all
</Directory>

网友评论