在测试机上配置Apache服务器下的第一个虚拟主机

By 大伟哥 | 2017-09-16,周六, 22:40

在安装好了测试服务器的WEB服务器套件LAMP并且运行起来之后,我们又给测试服务器设置了一个静态的IP地址192.168.1.8,现在已经可以通过在浏览器地址栏输入IP地址(如前面例子里的http://192.168.1.8)来访问默认的网站服务器了。如果你只打算做一个网站的话,并且只有IP在址访问的话,就可以把自己的网站程序直接上传到/var/www/html/里面,开始安装网站系统了。

但是,如果你想在服务器上同行开发/测试/运行多个不同的网站,并且还要绑定各自不同的域名,就像我们真的在访问一个个网络上的网络的话,我们还需要配置Apache的虚拟主机, 以便让一台服务器可以分别运行多个网站。

前两天我们讲过了Ubuntu Server 16.04下的Apache服务器默认配置,我们知道所有网站的配置文件都要放在/etc/apache2/sites-available/文件夹里面,今天我们就来实际操作一下,为测试服务器配置第一个真正意义上的网站。我们把第一个网站的域名设置为drupal8.dev。

1. 首先进入Apache 2.4的可用网站配置文件目录/etc/apache2/sites-available,并复制一个默认的配置文件,重命名为drupal8.dev.conf:

daweibro@Web-Ubuntu-Server-1604:~$ cd /etc/apache2/sites-available/
daweibro@Web-Ubuntu-Server-1604:/etc/apache2/sites-available$ sudo mv 000-default.conf drupal8.dev.conf

2. 然后用自己喜欢的编辑器打开drupal8.dev.conf,在合适的位置加入ServerName和ServerAlias参数,并修改网站的根目录,修改后的文件内容如下例:

<virtualhost>
# 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

    ServerName drupal8.dev
    ServerAlias www.drupal8.dev
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/drupal8.dev
    
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all  
    

# 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>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

检查无误后保存文件并退出。

3. 在/var/www/html/文件夹下创建一个新的目录,命名为drupal8.dev,用于存放网站的程序文件:

sudo mkdir /var/www/html/durpal8.dev

4. 启用新网站,并重启Apache 2.4服务,有史以来我们自己的测试服务器上第一个网站的虚拟主机环境就算搭建完成了,它的名字就叫drupal8.dev。

daweibro@Web-Ubuntu-Server-1604:~$ sudo a2ensite drupal8.dev.conf
Enabling site drupal8.dev.
To activate the new configuration, you need to run:
  service apache2 reload
daweibro@Web-Ubuntu-Server-1604:~$ sudo service apache2 reload

注意,如果启用新网站不成功,可能是配置文件有问题,需要仔细检查核对才能解决问题,可以参考这一篇:Apache启用网站报错:ERROR: Site drupal8.dev does not exist!

添加新评论

此字段内容将保密,不会被其他人看见。
  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
发布评论前,请通过下面的验证,以免被当作垃圾评论机器人屏蔽。
2 + 6 =
计算出这道简单的算术题并键入答案。例如、1+3,就输入 4。