在开始尝试 WordPrss 时, 访问博客文章会出现错误:
The requested URL /hello-world-.html was not found on this server
查找各种答案, 发现是由 Apache2 未开启重写模式 导致的.
关于重写模式, 很多资源都是介绍修改 Apache2 httpd.conf, 但我找了很久都未找到 httpd.conf 文件.
Ubuntu 终端查找命令:
find / -name httpd.conf
实际上, Apache2 并没有 httpd.conf 配置文件, 真实的Apache2配置文件是 /etc/apache2/apache2.conf. Apache2 启动时默认自动读取该文件内的配置信息, 而其它的配置信息则是通过 Include 包含进来的.
如:
# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/*.conf
# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf
# Include list of ports to listen on
Include ports.conf
1. Apache2 配置文件
/etc/apache2/ 的目录结构:
├── apache2.conf #配置文件
├── ports.conf
├── conf-available
└── *.conf
├── conf-enabled
└── *.conf
├── mods-available
├── *.load
└── *.conf
├── mods-enabled
├── *.load
└── *.conf
├── sites-available
├── 000-default.conf
└── default-ssl.conf
├── sites-enabled
└── 000-default.conf
available 和 enabled 文件夹的区别和作用:
- enable/ 文件夹内的文件, 是被包含在 apache2.conf 内的配置, 即生效的配置文件.
- available/ 文件夹内的文件, 是未用到所有可能用到的配置文件, 即未生效的配置文件. 如果需要特定设置, 即从 available 文件夹内复制对应的配置文件到 enabled 文件夹内, 即可.
2. Apache2 开启重写模式
_LoadModule rewrite_module_
[1] - 复制 rewrite.load 文件到 mods-enabled 文件夹:
sudo cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
#或
sudo a2enmod rewrite
[2] - 更改 mods-enabled/rewrite.load 文件权限:``
chmod -R 775 rewrite.load
[3] - 编辑 apache2.conf 配置参数:
sudo vim apache2.conf
更改如下部分的参数:
更改前: AllowOverride None
更改后:AllowOverride All
更改前:
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
更改后:
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride All
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
[4] - 重启 Apache
sudo systemctl restart apache2.service