Apache服务器配置(Ubuntu)
1. 准备
- Apache服务器
- HTML基础
- 文本编辑工具
- 终端
2. 添加自己的网页
Apache服务器的默认网页位于/var/www/html/index.html
,我们可以在/var/www
下新建一个自己的文件夹用于存放自己的html文件。
- 创建文件夹。
$ sudo mkdir /var/www/xxx.com/
- 设置文件夹权限。
$ sudo chown user:user /var/www/xxx.com -R
$ sudo chmod 755 /var/www/xxx.com -R
- 编辑自己的网页1。
$ vi /var/www/ourtest.com/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Hello world</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
3. 配置网页路径
此处需要配置Apache服务器的工作参数,包括指定网页路径为刚才创建的index.html
文件。
我们有两种方法更改网页路径。第一种是在默认配置中修改,第二种是新增配置并通过命令激活新增的配置。
3.1 方法一:更改默认配置
- 在路径
/etc/apache2/sites-available
下修改配置文件000-default.conf
$ sudo vi /etc/apache2/sites-available/000-default.conf
将网页的访问路径改为自己添加的网页路径
ServerAdmin webmaster@localhost
--- DocumentRoot /var/www/html
+++ DocumentRoot /var/www/xxx.com
- 在路径
/etc/apache2/mods-available
下修改配置文件dir.conf
$ sudo vi /etc/apache2/sites-available/dir.conf
也是添加自己的网页路径
<IfModule mod_dir.c>
--- DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
+++ DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm /xxx.com
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
3.2 方法二:添加新配置
- 在
/etc/apache2
下添加新的配置文件。
$ sudo vi /etc/apache2/sites-available/xxx.com.conf
<VirtualHost *:80>
ServerAdmin admin@xxx.com
ServerName xxx.com
ServerAlias www.xxx.com
DocumentRoot /var/www/xxx.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- 启用自己的网页。
$ sudo a2ensite xxx.com.conf
Enabling site xxx.com.
To activate the new configuration, you need to run:
service apache2 reload
- 禁用默认网页2。
$ sudo a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:
service apache2 reload
4. 重新加载配置
- 重启Apache以使刚才的配置生效。
$ service apache2 reload
5. 验证
在局域网的主机浏览器中输入Apache服务器主机的地址,若成功显示相应的页面,则配置成功。
6. 错误
- 通过命令
apache2ctl configtest
查看配置是否正确时可能出现如下提示。
$ apache2ctl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
可通过修改配置文件/etc/apache2/apache2.conf
解决。
$ sudo vi /etc/apache2/sites-available/apache2.conf
在文件中添加如下配置
# Global configuration
#
+++ ServerName 127.0.0.1
上一步:Apache服务器搭建(Ubuntu)
下一步:安装PHP环境(Ubuntu)