如何在Ubuntu 18.10上设置Apache虚拟主机
Apache HTTP服务器,以前称为Apache Web Server是由Apache软件基础开发和维护的免费和开源软件。
它是一个强大的网络服务器之一全世界。
在本教程中,我们将指导我们在Ubuntu 18.10上设置Apache虚拟主机.generally,虚拟主机用于托管同一Web服务器上的许多。
如果我们有兴趣设置Apache HTTP Server,请在Ubuntu上设置Apache HTTP Web服务器的步骤教程。
我们可以在Apache上配置两种类型的虚拟主机。
- 单个Apache服务器,一个IP和多个=>基于名称的虚拟主机
- 单个Apache服务器和每个的唯一IP =>基于IP的虚拟托管
基于IP的虚拟主机我们只能在一个IP地址上配置一个。
如果我们需要托管多个,则应有多个Web服务器的IP。
基于名称的虚拟主机通常用于在单个服务器上托管多个。
在本教程中,我们将讨论如何创建基于名称的虚拟主机。
配置基于名称的Apache虚拟主机
文档root是一个用于存储域名的文件的目录,用于响应请求的用于服务。
1.设置目录结构
我们将需要在/var/www目录下创建两个目录。
启动终端,让我们开始为theitroadexample1.com创建目录
sudo mkdir -p /var/www/html/theitroadexample1.com/public_html
为theitroadexample2.com创建目录
sudo mkdir -p /var/www/html/theitroadexample2.com/public_html
将目录所有权更改为theapache用户(www-data)
sudo chown -R www-data: /var/www/html/theitroadexample1.com/public_html sudo chown -R www-data: /var/www/html/theitroadexample2.com/public_html
2.设置文件夹权限
sudo chmod -R 755 /var/www/html/theitroadexample1.com/public_html sudo chmod -R 755 /var/www/html/theitroadexample2.com/public_html
3.创建网页
现在,让我们为每个主机创建网页。
在我的示例中,我将为theitroadexample1.com创建一个网页
sudo vim /var/www/html/theitroadexample1.com/public_html/index.html
将以下内容添加到index.html文件。
这基本上是HTML内容。
<html> <head> <title>theitroadexample1.com Test Page</title> </head> <body> <h1>Hello, This is a test page for theitroadexample1.com website</h1> </body> </html>
时间保存和关闭文件。
同样,我们将为theitroadexample2.com创建一个网页。
sudo vim /var/www/html/theitroadexample2.com/public_html/index.html
将以下内容添加到文件中。
<html> <head> <title>theitroadexample2.com Test Page</title> </head> <body> <h1>Hello, This is a test page for theitroadexample2.com website</h1> </body> </html>
保存并关闭文件。
4.创建虚拟主机
通常,Apache虚拟主机配置文件存储在/etc/apache2/sites可用的目录和/etc/apache2 /启用站点的目录中。
从创建虚拟主机fortheitroadexample1.com开始。
sudo vim /etc/apache2/sites-available/theitroadexample1.com.conf
将以下内容添加到文件中。
<VirtualHost *:80> ServerName theitroadexample1.com ServerAlias www.theitroadexample1.com ServerAdmin [email protected] DocumentRoot /var/www/html/theitroadexample1.com/public_html <Directory /var/www/html/theitroadexample1.com/public_html> Options -Indexes +FollowSymLinks AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/theitroadexample1.com-error.log CustomLog ${APACHE_LOG_DIR}/theitroadexample1.com-access.log combined </VirtualHost>
保存并退出文件。
在类似的行中,创建虚拟主机fortheitroadexample2.com
sudo vim /etc/apache2/sites-available/theitroadexample2.com.conf
将以下内容添加到第二个。
<VirtualHost *:80> ServerName theitroadexample2.com ServerAlias www.theitroadexample2.com ServerAdmin [email protected] DocumentRoot /var/www/html/theitroadexample2.com/public_html <Directory /var/www/html/theitroadexample2.com/public_html> Options -Indexes +FollowSymLinks AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/theitroadexample2.com-error.log CustomLog ${APACHE_LOG_DIR}/theitroadexample2.com-access.log combined </VirtualHost>
保存并退出文件。
5.启用虚拟主机配置文件
有不同的方法可以启用虚拟主机。
最简单的方法是启用虚拟主机是使用A2Sensite命令。
或者,我们可以通过创建与/etc/apache2 /站点的目录创建符号链接来启用它。
现在我们应该将000-default.conf和启用新创建的虚拟主机文件
使用此命令行禁用默认虚拟主机。
sudo a2dissite 000-default.conf
以下命令将启用新的虚拟主机。
sudo a2ensite theitroadexample1.com.conf sudo a2ensite theitroadexample2.com.conf
启用虚拟主机创建符号链接。
如果启用了虚拟主机,请使用a2ensite命令无需运行以下命令:
sudo ln -s /etc/apache2/sites-available/theitroadexample1.com.conf /etc/apache2/sites-enabled/ sudo ln -s /etc/apache2/sites-available/theitroadexample2.com.conf /etc/apache2/sites-enabled/
检查配置filessyntax。
sudo apachectl configtest
重新启动Apache以激活新配置。
sudo systemctl restart apache2
6.测试虚拟主机
在测试Web页面之前,我们需要修改/etc/hosts文件以配置服务器IP。
sudo vim /etc/hosts
将以下行添加到主机文件中。
请记住使用服务器IP替换下面的命令行中的10.94.12.217.
完成后,保存并关闭文件。
10.94.12.217 theitroadexample1.com 10.94.12.217 theitroadexample2.com
最后,启动我们喜欢的Web浏览器并开始浏览!
theitroadexample1.com.
从Apache虚拟主机浏览
theitroadexample2.com.