一、需求描述
一台电脑上有时之家是一个Web服务器,而服务器上有时可能需要搭建自己的博客、开发环境和一些框架的示例程序,这就需要把它们分开,用不同域名在浏览器上进行访问。本文采用Apache架设web服务器。
二、解决方案
1、映射几个域名
打开文件:sudo vim /etc/hosts
添加映射:
127.0.0.1 www.boss.com
127.0.0.1 www.applee.com
127.0.0.1 www.help.com
127.0.0.1 www.laravel.com
127.0.0.1 www.vue.com
127.0.0.1 www.work.com
2、配置Apache:
打开文件:vim /etc/apache2/sites-available/000-default.conf
添加配置信息:
<VirtualHost *:80>
ServerAdmin admin@applee.com
DocumentRoot /var/www/html/www.applee.com
ServerName www.applee.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@help.com
DocumentRoot /var/www/html/www.help.com
ServerName www.help.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@work.com
DocumentRoot /var/www/html/www.work.com
ServerName www.work.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@laravel.com
DocumentRoot /var/www/html/www.laravel.com/blog/public
ServerName www.laravel.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@vue.com
DocumentRoot /var/www/html/www.vue.com/dist
ServerName www.vue.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@boss.com
DocumentRoot /var/www/html/boss-admin/dist
ServerName www.boss.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3、如果根目录不在默认目录内考虑添加权限设置:
打开文件:sudo vim /etc/apache2/apache2.conf
添加目录权限设置:<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
4、重启服务:
sudo service apache2 restart
三、版本信息
操作系统:Ubuntu 18.04.4 LTS
apache:Apache/2.4.29 (Ubuntu)
php:7.2.24-0ubuntu0.18.04.4