1、将域名A记录解析到服务器的公网 IP 地址,把www也添加A记录到公网ip
2、ubuntu系统修改hosts文件,hosts文件目录为/etc/hosts,可以用vim编辑 sudo vim /etc/hosts
添加内容:
127.0.0.1 a.com www.a.com
127.0.0.1 b.com www.b.com
编辑后,你需要重新启动一下你的网络。命令:/etc/init.d/networking restart
3、创建站点目录,比如/data/www/a 和/data/www/b ;分别在a文件夹下和b文件夹下新建index.html文件,内容自己随便写。
4、进入目录 /etc/apache2/sites-available/ ;可以看到有一个默认文件000-default.conf,修改内容如下:
<VirtualHost *:80>
ServerName a.com
ServerAlias www.a.com
<Directory /data/www/a>
#AllowOverride伪静态
AllowOverride All
Require all granted
</Directory>
ServerAdmin webmaster@localhost
DocumentRoot /data/www/a
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
复制000-default.conf文件并命名为001-default.conf作为站点b的配置文件,相应修改域名和网站根目录
5、启动站点
sudo a2ensite 000-default.conf
sudo a2ensite 001-default.conf如果提示需要运行apache load,你可以按照提示运行命令。
6、重启Apache service
sudo service apache2 restart
7、成功
中途遇到的问题:apache无法启动
1、提示:Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
解决方法:命令:apache2ctl configtest;可以查看出现错误文件行号,主要是000-default.conf出现错误,注意空格。
2、提示:Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}
解决方法:hosts文件配置错误,查看是否添加了域名指向127.0.0.1