常见的 Apache Ubuntu 和 Debian 上的命令

Apache 是最流行的网络服务器开发的 Apache 1995 年成立。 Apache 许可证 2.0。 它是一个跨平台的应用程序,可用于大多数较旧的操作系统,如 Linux、Windows 和 macOS 系统。 拥有45%的市场份额, Apache 几乎为互联网上的每一个网站提供服务。 这说明它在用户之间的受欢迎程度。

在本教程中,我们将讨论一些常用的管理命令 Apache Ubuntu 和基于 Debian 的系统上的服务器。 这包括如何启用/禁用虚拟主机、模块或配置文件 Apache 服务器。

1.检查 Apache 版本

-v 检查正在运行的命令行选项 Apache Ubuntu 和其他基于 Debina 的系统上的版本。

apache2 -v   Server version: Apache/2.4.41 (Ubuntu) Server built:   2021-06-17T18:27:53 

要查看详细信息,请使用大写字母 -V 使用 apache2 命令。

apache2 -V  

2. 测试 Apache 配置

在进行任何更改后 Apache 配置文件,您必须检查语法。 使用以下命令检查所有 Apache2 配置文件的语法。

sudo apachectl -t  

如果所有配置文件都正确,您将看到 “语法确定” 在结果中。

Output Syntax OK 

3.启用和禁用虚拟主机

网站配置文件保存有关 Apache 虚拟主机。 在基于 Debian 的系统的情况下,实际文件是在 /etc/apache2/sites-available 目录。

然后,我们使用 a2ensite,它只是创建该文件到 /etc/apache2/sites-enabled 的符号链接。 Apache 将此目录中的所有文件加载到环境中。

例如,如果您创建了一个名为 example.com.conf. 然后使用以下命令。

sudo a2ensite example.com  

类似用途 a2dissite 命令来取消已经被禁用的站点。

sudo a2dissite example.com  

4. 启用和禁用配置

原始配置文件存储在 /etc/apache2/sites-available/ 目录。 Apache 从中读取配置文件 /etc/apache2/sites-enabled/ 目录,因此您需要创建文件的符号链接到 site-enabeld 目录。

配置文件 命令为配置文件创建一个符号链接,并 a2disconf 删除符号链接。 例如为配置文件创建符号链接 phpmyadmin.conf 跑:

sudo a2enconf phpmyadmin  

要停用配置,只需通过 a2disconf 命令禁用它,例如:

sudo a2disconf phpmyadmin  

5. 启用和禁用模块

一切 Apache 模块文件存储在 /etc/apache2/mods-available/ 目录。 所有活动模块都符号链接到 /etc/apache2/mods-enabled/ 目录。

a2enmod 启用模块的命令 Apache 服务器和 a2dismod 禁用模块。 例如要启用重写模块,请键入:

sudo a2enmod rewrite  

要禁用重写模块,请运行:

sudo a2dismod rewrite  

6.管理Apache2服务

最新的操作系统选择了该系统来管理服务。 您可以使用 系统控制 用于管理的命令行实用程序 Apache 在您的系统上提供服务。

以下命令将停止、启动和重新启动系统上的 apache2 服务。

sudo systemctl stop apache2   sudo systemctl start apache2  sudo systemctl restart apache2  

配置文件的变化可以在运行环境中重新加载 重新加载 选项。 这不会重新启动 Apache 服务。

sudo systemctl reload apache2  

使用以下命令启用或禁用 apcahe2 服务。

sudo systemctl enable apache2  sudo systemctl disable apache2  

7. 展示 Apache 命令帮助

如需更多帮助,请使用 -h 命令行选项。

sudo apache2 -h  
Output Usage: apache2 [-D name] [-d directory] [-f file]                [-C "directive"] [-c "directive"]                [-k start|restart|graceful|graceful-stop|stop]                [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X] Options:   -D name            : define a name for use in  directives   -d directory       : specify an alternate initial ServerRoot   -f file            : specify an alternate ServerConfigFile   -C "directive"     : process directive before reading config files   -c "directive"     : process directive after reading config files   -e level           : show startup errors of level (see LogLevel)   -E file            : log startup errors to file   -v                 : show version number   -V                 : show compile settings   -h                 : list available command line options (this page)   -l                 : list compiled in modules   -L                 : list available configuration directives   -t -D DUMP_VHOSTS  : show parsed vhost settings   -t -D DUMP_RUN_CFG : show parsed run settings   -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG   -t -D DUMP_MODULES : show all loaded modules   -M                 : a synonym for -t -D DUMP_MODULES   -t -D DUMP_INCLUDES: show all included configuration files   -t                 : run syntax check for config files   -T                 : start without DocumentRoot(s) check   -X                 : debug mode (only one worker, do not detach) 

结论

在本初学者指南中,您已经了解了管理的基本命令 Apache 服务器通过命令行。