mac下重启、打开、关闭Apache

重启apache:sudo /usr/sbin/apachectl restart

关闭apache:sudo /usr/sbin/apachectl stop

开启apache:sudo /usr/sbin/apachectl start

### Mac 上安装和配置 Apache 服务器 #### 启动 ApachemacOS 中,Apache 已经预装好了。可以通过命令行工具来启动、停止或重启 Apache 服务。以下是具体操作: - **启动 Apache**: 使用 `sudo apachectl start` 命令可以启动 Apache[^4]。 - **重启 Apache**: 如果修改了配置文件或其他设置,需要通过 `sudo apachectl restart` 来刷新更改并应用新配置。 - **停止 Apache**: 当不再需要运行 Apache 时,可执行 `sudo apachectl stop` 关闭服务。 默认情况下,Apache 的根目录位于 `/Library/WebServer/Documents/`,访问地址为 `http://localhost/` 或者本机 IP 地址[^1]。 --- #### 修改 Apache 配置文件 Apache 的核心配置文件名为 `httpd.conf`,通常存储于路径 `/etc/apache2/httpd.conf` 下。为了自定义网站功能或者调整性能参数,可能需要编辑此文件: ```bash sudo nano /etc/apache2/httpd.conf ``` 一些常见的配置项包括但不限于: - 更改监听端口:默认是 80 (HTTP),如果想更改为其他端口号,则找到 `Listen 80` 并将其替换为目标端口号[^3]。 - 开启模块支持:例如启用 PHP 支持,取消注释加载 PHP 模块的相关行 `LoadModule php_module libexec/apache2/libphp.so`。 完成编辑之后记得保存退出,并重新启动 Apache 让改动生效。 --- #### 测试页面展示 确认一切正常工作的一个简单办法就是创建测试 HTML 文件放置到站点根目录里试试看效果怎样。比如新建 index.html 如下所示: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test Page</title> </head> <body> <h1>Hello from my local server!</h1> </body> </html> ``` 把这段代码存入上述提到过的文档夹内 (`/Library/WebServer/Documents/index.html`) ,接着打开浏览器输入网址 http://localhost 即可见效验证成功与否[^2]。 --- #### 虚拟主机设定 对于开发多项目环境来说非常实用的功能之一便是虚拟主机的支持。这允许在同一台机器上托管多个独立域名对应的网页内容而互不影响。实现方式同样是通过对 main configuration file 添加 VirtualHost entries 完成的[^5]。 假设我们要新增一个叫 example.local 的 vhost 设置如下步骤即可达成目标: 1. 编辑 hosts 文件增加映射关系: ```bash sudo echo "127.0.0.1 example.local" >> /private/etc/hosts ``` 2. 创建新的 site directory and test page: ```bash mkdir -p ~/Sites/example/ echo "<!doctype html><html><body><h1>Welcome to Example Local Site!</h1></body></html>" > ~/Sites/example/index.html ``` 3. 更新 httpd-vhosts.conf with new entry: ```bash sudo nano /etc/apache2/extra/httpd-vhosts.conf ``` 加入下面的内容片段进去: ```apacheconf <VirtualHost *:80> ServerName example.local DocumentRoot "/Users/<your_username>/Sites/example" ErrorLog "/private/var/log/apache2/example-error_log" CustomLog "/private/var/log/apache2/example-access_log" common <Directory "/Users/<your_username>/Sites/example"> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory> </VirtualHost> ``` 4. Restart the service again after making these changes. ```bash sudo apachectl restart ``` 现在应该能够通过访问 URL https://example.local 查看到刚才建立的那个简易欢迎界面啦! --- ### 注意事项 确保防火墙不会阻挡外部请求到达指定端口;另外某些安全更新可能会重写原始配置所以建议定期备份重要资料以防丢失。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值