在昨天的帖子中介绍了如何在Ubuntu 18.04下安装Apache2、PHP7.2、MYSQL,完成安全后,我们略作设置并绑定域名就可以跑起网站啦。
(图源 :pexels.com)
默认配置
但是默认情况下,网站的目录配置为:
DocumentRoot /var/www/html
而Apache2的用户和用户组为
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
也就是说,如果我们想运行多个站点,站点的用户和组都是www-data
,这样有个风险就是,一旦一个站点被黑,那么上载恶意脚本后很容易黑掉其它站点。
mpm-itk 模块
那么有没有办法已不同的用户运行虚拟主机呢?比如说a用户的虚拟主机程序用户和用户组都是a,而b用户的虚拟主机程序用户和用户组都是b,答案是使用mpm-itk 模块。
老一点的版本可能会使用以下指令安装模块:
sudo apt-get install apache2-mpm-itk
在Ubuntu 18.04下,上述指令无法安装mpm-itk,正确的指令为:
sudo apt-get install libapache2-mpm-itk
安装完成时会自动使能这个模块。
Setting up libapache2-mpm-itk (2.4.7-04-1) ...
apache2_invoke: Enable module mpm_itk
如果需要手动使能相关模块,可以使用如下指令:
sudo a2enmod mpm_itk
提示信息如下:
Considering dependency mpm_prefork for mpm_itk:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Module mpm_itk already enabled
创建站点
安装好这个mpm_itk模块后,我们创建个新站点(可以从默认站点配置文件来修改)
cd /etc/apache2/sites-available
sudo cp 000-default.conf mysite.conf
sudo vi mysite.conf
然后主要修改如下:
ServerName mysite.com
<ifmodule mpm_itk_module>
AssignUserID mysite mysite
</ifmodule>
DocumentRoot /home/mysite/www
ErrorLog /home/mysite/logs/error.log
CustomLog /home/mysite/logs/access.log combined
然后使用adduser添加用户:
sudo adduser mysite
登陆上述用户,创建对应目录和站点文件。
然后执行如下指令启动站点:
sudo a2ensite mysite.conf
sudo systemctl reload apache2
权限错误
按上述操作配置后,访问站点会提示 403 Forbidden 错误:
查看错误日志发现类似如下错误:
[Tue Jul 17 00:46:08.773031 2018] [authz_core:error] [pid 12821] [client xxxxx:54251] AH01630: client denied by server configuration: /home/mysite/www/
[Tue Jul 17 00:46:11.360338 2018] [authz_core:error] [pid 12821] [client xxxx:54251] AH01630: client denied by server configuration: /home/mysite/www/
我一直以为是我libapache2-mpm-itk模块没有配置好,经过一整天的反复测试和调查后,才发现在apche2.conf中设置了如下访问限制。
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
也就是说除了/var/www/
以及/usr/share
以外的目录都是禁止访问的。知道了这点就很好解决啦,在我们的mysite.conf中运行访问我们的目录就可以啦。
<Directory /home/mysite/www>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
再来测试一下,耶,一切正常。
总结
现在我们就可以已独立的用户运行虚拟主机啦,这样做安全性更高,也更便于管理。是不是很简单?
最好弄个一键安装的!这个还是需要些代码!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
很简单是没有啦,还是需要些基础的。
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
請收下我崇拜的眼光,每次看到O哥的技術文,只能默默的鼓掌,然後默默的飄走
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
飘得不错呀:)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
提到虚拟主机,又让我这个技术小白想起上半年为自己搭梯子,买虚拟主机什么的,熬了几夜 花了好长时间都没搞好的惨痛经历
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@oflyhigh, 要不是我膝盖上中了一箭,我的膝盖就送给你了!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
wow,,what a post..hope that everyone learn from here...
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@oflyhigh, 写得好好哇~~~
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@oflyhigh you were flagged by a worthless gang of trolls, so, I gave you an upvote to counteract it! Enjoy!!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
没觉得简单~~能不弄就不弄,看着就懒癌发作~
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit