介绍
虚拟客人
虚拟主机是从单个 IP 地址/服务器中托管多于一个域的方法。这可能有帮助,例如,对于那些希望从单个 dropplet 托管多于一个网站的人来说,网站访问者将根据他们访问的域显示正确的信息,而如果虚拟主机没有正确设置,所有域都将显示相同的信息。
前提条件
为了运行本教程中的命令,用户必须具有 root 权限. 如果您使用 root 用户帐户登录您的 droplet,您不应该担心这一点. 如果您没有,您可以在文章中看到如何设置 初始服务器设置 。
此外,您需要在云服务器上安装和运行Apache,如果您还没有,您可以使用以下命令安装它:
sudo apt-get install apache2
如果你要托管基于PHP或MySQL的网站(例如Wordpress),设置LAMP(Linux,Apache,MySQL,PHP)堆栈的最简单方法是运行以下命令:
sudo tasksel install lamp-server
红色 是什么意思
用户需要输入或自定义的行将在 red tro这个教程中!
其余的都是可复制的。
步骤一:创建新文件夹/目录
第一步是创建一个目录,我们将为你的新域存储文件(和文件夹)。通常,这个新目录的名称应该与你试图设置的域名相匹配,但这不是一个规则。
sudo mkdir -p /var/www/example.com
-p 旗确保这个目录的所有父母都存在,如果没有,它会生成它们。
「example.com」是位址 - 用正确的域名取代它。
第二步:授予许可
首先,我们需要将目录的所有权授予用户Apache作为运行。
sudo chown -R www-data:www-data /var/www/example.com
接下来,我们需要为目录设置正确的权限,以便所有人都能访问文件。
sudo chmod -R 755 /var/www
这就是为这个步骤所做的。
步骤三:创建页面
我们现在将创建一个样本 index.html 文件,以便我们可以测试我们的虚拟主机是否正常工作。
对于这个步骤,你会想确保你有纳米文本编辑器安装。
sudo apt-get install nano
然后创建 index.html 文件。
sudo nano /var/www/example.com/index.html
您可以复制并粘贴下面的代码到新创建的 index.html 文件。
<html>
<head>
<title>www.example.com</title>
</head>
<body>
<h1>Success: You Have Set Up a Virtual Host</h1>
</body>
</html>
使用 Ctrl + O 保存和退出,然后输入,然后 Ctrl + X。
步骤四:创建一个新的虚拟主机配置文件
现在我们将设置虚拟主机配置文件. 幸运的是,对于我们来说,Ubuntu为此配置文件提供了模板。
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
请注意,从Ubuntu 13.10开始,必须添加.conf,这与以前的版本有所不同。
步骤五:修改配置文件
接下来,我们需要修改虚拟主机配置文件以匹配我们对域的设置。
sudo nano /etc/apache2/sites-available/example.com.conf
当你打开此文件时,你应该得到一个类似的消息。
< VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf < /VirtualHost>
修改此文件以匹配我们的域名配置很容易。 首先,从ServerName前面删除#
符号,然后在它前面添加您的域名。
ServerName example.com
如果您希望您的网站可以从多个名称访问,例如名称中的www
,则您需要在 ServerName 行后添加一个 ServerAlias 行。
ServerAlias www.example.com
在遵循上述步骤后,您还需要修改 DocumentRoot 行,以匹配您为域名创建的目录。
DocumentRoot /var/www/example.com
一旦您正确遵循所有这些步骤,您的文件应该看起来类似于此。
ServerAdmin [email protected] ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com
这些都是你需要对这个文件进行的所有更改. 现在保存和退出。
要激活主机,请使用此命令。
sudo a2ensite example.com
现在重新启动Apache,让您的更改生效。
sudo service apache2 restart