在Ubuntu Server 16.04上安装和配置Apache2
在本教程中,我们将学习如何在Ubuntu 16.04上安装和配置Apache Web服务器。
我们还将了解如何在Ubuntu Apache Web服务器上配置虚拟主机。
安装Ubuntu Apache2 Web服务器
Ubuntu Server 16.04的Apache服务器由Apache2包提供。
我们可以使用apt-get install命令在Ubuntu 16.04上安装Apache2.
sudo apt-get update sudo apt-get install apache2
这将在Ubuntu Server 16.04上安装Apache HTTP Web服务器。
要检查Apache安装,请尝试使用wget命令访问localhost。
wget 127.0.0.1
运行wget命令后,应将index.html文件下载到当前的工作目录。
我们还可以尝试从远程计算机访问Apache Web服务器。
从远程计算机,打开Web浏览器并在地址列中键入Ubuntu服务器的IP地址。
我们应该将Ubuntu Apache默认网页作为响应。
ubuntu 16.04中Apache Web服务器的默认文档root是/var/www/html目录。
我们可以通过将的内容置于/var/www/html文件夹来托管静态。
在Ubuntu Server 16.04上配置Apache虚拟主机
使用Apache虚拟主机我们可以在Ubuntu Web服务器上托管多个。
对于本教程,我将为域www.example.com创建一个新的虚拟主机。
首先,使用mkdir命令创建文档根源(文档root是我们将HTML文件的位置)。
sudo mkdir /var/www/example.com sudo chgrp www-data /var/www/example.com sudo chmod 750 /var/www/example.com
在/var/www/example.com文件夹中创建示例index.html文件。
echo 'This is example.com' > /var/www/example.com/index.html
接下来,我们需要为example.com创建Apache虚拟主机配置文件。
配置文件应在/etc/apache2/sites可用/目录中创建.conf扩展名。
touch /etc/apache2/sites-available/example.conf
然后打开example.conf文件并添加以下配置。
<VirtualHost *:80> ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined </VirtualHost>
最后,使用2ensite命令启用站点并重新启动Ubuntu Apache2 Web服务器。
a2ensite example.conf systemctl restart apache2.service
这样,我们可以在Ubuntu服务器上托管无限数量的域。
要通过公共互联网使用域名访问,我们需要将域名指向Ubuntu Server的IP地址(我们可以通过域名注册器执行此操作)。