Table of contents
1. Set the document root directory and home page file
4. Close the firewall and restart service access
2. Virtual host based on IP address
1. Create two main directories of ar and lql and the default home page file
2. Add /etc/httpd/conf.d/vhost.conf file
3. Modify the main configuration file
4. Close the firewall and restart service access
3. Domain name-based virtual hosting
1. Create two main directories of ar and lql and the default home page file
2. Modify the httpd.conf file. Add directory permissions
3. Modify the /etc/httpd/conf.d/vhost.conf file
5. Close the firewall and restart service access
4. Virtual host based on port number
1. Create two main directories of ar and lql and the default home page file
2. Modify the httpd.conf file. Add directory permissions
3. Modify the /etc/httpd/conf.d/vhost.conf file
4. Add a port (if the port reports an error)
1. Conventional Apache server
1. Set the document root directory and home page file
# mkdir /data/ar //创建文档根目录
# echo "welcome to my web" > /data/ar/lql.html //创建首页文件
# vim /etc/httpd/conf/httpd.conf //修改主配置文件
行 119 DocumentRoot "/data/ar"
124 <Directory "/data/ar">
164 Directory index.html lql.html
# firewall-cmd --permanent --add-service=http //防火墙允许http通过
# firewall-cmd --reload //立即生效
# setenforce 0 //关闭selinux
# systemctl restart httpd //重启服务
访问:浏览器输入 http://192.168.10.1
2. User's personal homepage
# useradd lql //新建用户lql
# passwd lql //设置密码
# chown 705 /data/lql /修改用户家目录权限
# mkdir /data/lql/public_html //创建存放用户个人主页空间的目录
# echo "welcome to my web" > /data/lql/public_html/index.html //创建个人空间首页文件
# vim /etc/httpd/conf.d/userdir.conf
17 # UserDir disabled
24 UserDir public_html
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# setenforce 0
# systemctl restart httpd //重启服务
访问:浏览器输入 http://192.168.10.1/~lql
3. Virtual directory
# mkdir -p /virdir/ //创建物理目录
# echo "welcome to my web" > /virdir/index.html //创建虚拟目录中的默认首页文件
# chmod 705 /virdir/index.html //修改文件默认权限,使其他用户具有读取和执行权限
# vim /etc/httpd/conf/httpd.conf
添加:
Alias /lql "/virdir"
<Directory "Virdir">
AllowOverride None
Require all granted
</Directory>
4. Close the firewall and restart service access
# setenforce 0
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# systemctl restart httpd //重启服务
访问:http://192.168.10.1/lql
2. Virtual host based on IP address
1. Create two main directories of ar and lql and the default home page file
# mkdir /var/www/ar /var/www/lql
# echo "192.168.10.1's web" > /var/www/ar/index.html
# echo "192.168.10.2's web" > /var/www/lql/index.html
2. Add /etc/httpd/conf.d/vhost.conf file
# vim /etc/httpd/conf.d/vhost.conf
添加基于ip地址为192.168.10.1的虚拟主机
<VirtualHost 192.168.10.1>
DocumentRoot /var/www/ar
</VirtualHost>
添加基于ip地址为192.168.10.2的虚拟主机
<VirtualHost 192.168.10.2>
DocumentRoot /var/www/lql
</VirtualHost>
3. Modify the main configuration file
# vim /etc/httpd/conf/httpd.conf
添加ar与lql目录权限,防止权限不足:
<Directory "/var/www/ar">
AllowOverride None
Require all granted
</Directory>
<Directory "/var/www/lql">
AllowOverride None
Require all granted
</Directory>
4. Close the firewall and restart service access
# setenforce 0
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# systemctl restart httpd
访问: http://192.168.10.1 http://192.168.10.2
3. Domain name-based virtual hosting
1. Create two main directories of ar and lql and the default home page file
# mkdir /var/www/ar /var/www/lql
# echo "www1.lql.com's web" > /var/www/ar/index.html
# echo "www2.lql.com's web" > /var/www/lql/index.html
2. Modify the httpd.conf file. Add directory permissions
# vim /etc/httpd/conf/httpd.conf
添加
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
3. Modify the /etc/httpd/conf.d/vhost.conf file
# vim /etc/httpd/conf.d/vhost.conf
添加:
<VirtualHost 192.168.10.1>
DocumentRoot /var/www/ar
ServerName www1.lql.com
</VirtualHost>
<VirtualHost 192.168.10.1>
DocumentRoot /var/www/lql
ServerName www2.lql.com
</VirtualHost>
4. Do DNS resolution
# vim /var/named/lql.com.zome //做正向解析
@ IN SOA dns.lql.com. mail.lql.com. (
........
@ IN NS dns.lql.com.
@ IN MX 10 mail.lql.com.
dns IN A 192.168.10.1
www1 IN A 192.168.10.1
www2 IN A 192.168.10.1
5. Close the firewall and restart service access
# setenforce 0
# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload
# systemctl restart httpd
访问: www1.lql.com www2.lql.com
4. Virtual host based on port number
1. Create two main directories of ar and lql and the default home page file
# mkdir /var/www/ar /var/www/lql
# echo "8088 port's web" > /var/www/ar/index.html
# echo "8089 port's web" > /var/www/lql/index.html
2. Modify the httpd.conf file. Add directory permissions
# vim /etc/httpd/conf/httpd.conf
添加
Listen 8088
Listen 8089
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
3. Modify the /etc/httpd/conf.d/vhost.conf file
# vim /etc/httpd/conf.d/vhost.conf
添加:
<VirtualHost 192.168.10.1:8088>
DocumentRoot /var/www/ar
</VirtualHost>
<VirtualHost 192.168.10.1:8089>
DocumentRoot /var/www/lql
</VirtualHost>
4. Add a port (if the port reports an error)
# firewall-cmd --zone=public --add-port=8088/tcp //添加端口到public区域
# firewall-cmd --permanent --zone=public --add-port=8088/tcp
# firewall-cmd --permanent --zone=public --add-port=8089/tcp
# firewall-cmd --reload //立即生效
5. Restart access
# systemctl restart httpd
访问: 192.168.10.1:8088 192.168.10.1:8089